cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
quic_flow_control.c File Reference
Include dependency graph for quic_flow_control.c:

Go to the source code of this file.

Macros

#define QUIC_MAX_VARINT   ((1ULL << 62) - 1)
 
#define QUIC_FRAME_MAX_DATA   0x10
 
#define QUIC_FRAME_MAX_STREAM_DATA   0x11
 

Functions

void quic_flow_control_init (quic_conn_fc_t *fc, uint64_t initial_conn_window, uint64_t initial_stream_window, uint64_t max_conn_window, uint64_t max_stream_window)
 Initialize the connection-level flow control state.
 
void quic_stream_fc_init (quic_stream_fc_t *sfc, uint64_t stream_id, uint64_t initial_stream_window)
 Initialize the flow control state for a single stream.
 
void quic_flow_control_update_rtt (quic_conn_fc_t *fc, uint64_t rtt_sample_us)
 Feed an RTT sample into the smoothed RTT estimator and re-evaluate quality.
 
quic_net_quality_t quic_flow_control_evaluate_quality (quic_conn_fc_t *fc)
 Recompute the network quality class from the smoothed RTT.
 
void quic_flow_control_adjust_windows (quic_conn_fc_t *fc, quic_stream_fc_t *sfc)
 Grow flow control windows when the network quality is degraded.
 
void quic_flow_control_consume_conn (quic_conn_fc_t *fc, uint64_t bytes)
 Account for connection-level flow control bytes consumed by the peer.
 
void quic_flow_control_consume_stream (quic_conn_fc_t *fc, quic_stream_fc_t *sfc, uint64_t bytes)
 Account for stream-level flow control bytes consumed by the peer.
 
int quic_flow_control_maybe_send_max_data (quic_conn_fc_t *fc, uint8_t *buf, size_t buf_len, size_t *written, bool force)
 Write a MAX_DATA frame if the connection limit should be raised.
 
int quic_flow_control_maybe_send_max_stream_data (quic_conn_fc_t *fc, quic_stream_fc_t *sfc, uint8_t *buf, size_t buf_len, size_t *written, bool force)
 Write a MAX_STREAM_DATA frame if the stream limit should be raised.
 

Macro Definition Documentation

◆ QUIC_FRAME_MAX_DATA

#define QUIC_FRAME_MAX_DATA   0x10

Definition at line 4 of file quic_flow_control.c.

◆ QUIC_FRAME_MAX_STREAM_DATA

#define QUIC_FRAME_MAX_STREAM_DATA   0x11

Definition at line 5 of file quic_flow_control.c.

◆ QUIC_MAX_VARINT

#define QUIC_MAX_VARINT   ((1ULL << 62) - 1)

Definition at line 3 of file quic_flow_control.c.

Function Documentation

◆ quic_flow_control_adjust_windows()

void quic_flow_control_adjust_windows ( quic_conn_fc_t *  fc,
quic_stream_fc_t *  sfc 
)

Grow flow control windows when the network quality is degraded.

Only acts when the connection quality is at least QUIC_NET_QUALITY_HIGH_RTT. Each window that is below its configured maximum is doubled, capped at that maximum. No-op if fc is NULL or quality is better than QUIC_NET_QUALITY_HIGH_RTT; sfc may be NULL, in which case only the connection window is adjusted.

Parameters
fcConnection flow control state.
sfcStream flow control state to adjust, or NULL.

Definition at line 206 of file quic_flow_control.c.

References cap, quic_conn_fc::conn_max_window, quic_conn_fc::conn_window_size, quic_conn_fc::quality, QUIC_NET_QUALITY_HIGH_RTT, and quic_conn_fc::stream_max_window.

◆ quic_flow_control_consume_conn()

void quic_flow_control_consume_conn ( quic_conn_fc_t *  fc,
uint64_t  bytes 
)

Account for connection-level flow control bytes consumed by the peer.

The consumed counter saturates at the QUIC varint maximum instead of wrapping. No-op if fc is NULL.

Parameters
fcConnection flow control state.
bytesNumber of bytes consumed.

Definition at line 230 of file quic_flow_control.c.

References quic_conn_fc::conn_consumed.

◆ quic_flow_control_consume_stream()

void quic_flow_control_consume_stream ( quic_conn_fc_t *  fc,
quic_stream_fc_t *  sfc,
uint64_t  bytes 
)

Account for stream-level flow control bytes consumed by the peer.

The consumed counter saturates at the QUIC varint maximum instead of wrapping. The connection state fc is not touched; sfc may be NULL, in which case the call is a no-op.

Parameters
fcConnection flow control state (currently unused).
sfcStream flow control state.
bytesNumber of bytes consumed.

Definition at line 245 of file quic_flow_control.c.

References cap.

◆ quic_flow_control_evaluate_quality()

quic_net_quality_t quic_flow_control_evaluate_quality ( quic_conn_fc_t *  fc)

Recompute the network quality class from the smoothed RTT.

Classifies by srtt thresholds: <= 50 ms excellent, <= 150 ms normal, <= 500 ms high RTT, above that poor. The result is stored in fc->quality and returned. Returns QUIC_NET_QUALITY_NORMAL if fc is NULL or no RTT sample has been recorded yet.

Parameters
fcConnection flow control state.
Returns
The (updated) quality classification.

Definition at line 179 of file quic_flow_control.c.

References quic_conn_fc::quality, QUIC_NET_QUALITY_EXCELLENT, QUIC_NET_QUALITY_HIGH_RTT, QUIC_NET_QUALITY_NORMAL, QUIC_NET_QUALITY_POOR, quic_conn_fc::rtt_initialized, and quic_conn_fc::srtt_us.

Referenced by quic_flow_control_update_rtt().

◆ quic_flow_control_init()

void quic_flow_control_init ( quic_conn_fc_t *  fc,
uint64_t  initial_conn_window,
uint64_t  initial_stream_window,
uint64_t  max_conn_window,
uint64_t  max_stream_window 
)

Initialize the connection-level flow control state.

Initial and maximum windows are clamped to QUIC_FLOW_CONTROL_MIN_INITIAL_WINDOW, and each maximum is raised to at least the corresponding initial value. No-op if fc is NULL.

Parameters
fcFlow control state to initialize.
initial_conn_windowInitial connection-level window in bytes.
initial_stream_windowInitial per-stream window in bytes.
max_conn_windowUpper bound for the connection window in bytes.
max_stream_windowUpper bound for the per-stream window in bytes.

Definition at line 90 of file quic_flow_control.c.

References cap, quic_conn_fc::quality, and QUIC_NET_QUALITY_NORMAL.

◆ quic_flow_control_maybe_send_max_data()

int quic_flow_control_maybe_send_max_data ( quic_conn_fc_t *  fc,
uint8_t *  buf,
size_t  buf_len,
size_t *  written,
bool  force 
)

Write a MAX_DATA frame if the connection limit should be raised.

Unless force is set, a frame is produced only when at least half of the connection window has been consumed (see should_update()). The new maximum is consumed + window, never lower than the current maximum, and is stored back in fc->max_data on success. On entry *written is cleared to 0 when written is non-NULL.

Parameters
fcConnection flow control state.
bufOutput buffer for the encoded frame.
buf_lenCapacity of buf in bytes.
writtenReceives the encoded frame length in bytes.
forceEmit the frame even if the update threshold is not met.
Return values
1Frame written; *written holds its length.
0No update needed; nothing written.
-1Invalid argument or buf too small; nothing written.

Definition at line 268 of file quic_flow_control.c.

References cap, quic_conn_fc::conn_consumed, quic_conn_fc::conn_window_size, quic_conn_fc::max_data, and QUIC_FRAME_MAX_DATA.

◆ quic_flow_control_maybe_send_max_stream_data()

int quic_flow_control_maybe_send_max_stream_data ( quic_conn_fc_t *  fc,
quic_stream_fc_t *  sfc,
uint8_t *  buf,
size_t  buf_len,
size_t *  written,
bool  force 
)

Write a MAX_STREAM_DATA frame if the stream limit should be raised.

Unless force is set, a frame is produced only when at least half of the stream window has been consumed (see should_update()). The new maximum is consumed + window, never lower than the current maximum, and is stored back in sfc->max_stream_data on success. On entry *written is cleared to 0 when written is non-NULL.

Parameters
fcConnection flow control state.
sfcStream flow control state.
bufOutput buffer for the encoded frame.
buf_lenCapacity of buf in bytes.
writtenReceives the encoded frame length in bytes.
forceEmit the frame even if the update threshold is not met.
Return values
1Frame written; *written holds its length.
0No update needed; nothing written.
-1Invalid argument or buf too small; nothing written.

Definition at line 308 of file quic_flow_control.c.

References cap, and QUIC_FRAME_MAX_STREAM_DATA.

◆ quic_flow_control_update_rtt()

void quic_flow_control_update_rtt ( quic_conn_fc_t *  fc,
uint64_t  rtt_sample_us 
)

Feed an RTT sample into the smoothed RTT estimator and re-evaluate quality.

Uses RFC 6298-style smoothing: rttvar is updated with weight 1/4 and srtt with weight 1/8; the first sample initializes both. Tracked state is confined to fc and is not thread-safe across concurrent calls on the same connection. No-op if fc is NULL or rtt_sample_us is zero.

Parameters
fcConnection flow control state.
rtt_sample_usLatest RTT sample in microseconds.

Definition at line 147 of file quic_flow_control.c.

References cap, quic_conn_fc::min_rtt_us, quic_flow_control_evaluate_quality(), quic_conn_fc::rtt_initialized, quic_conn_fc::rttvar_us, and quic_conn_fc::srtt_us.

◆ quic_stream_fc_init()

void quic_stream_fc_init ( quic_stream_fc_t *  sfc,
uint64_t  stream_id,
uint64_t  initial_stream_window 
)

Initialize the flow control state for a single stream.

The initial window is clamped to QUIC_FLOW_CONTROL_MIN_INITIAL_WINDOW. No-op if sfc is NULL.

Parameters
sfcStream flow control state to initialize.
stream_idID of the stream this state belongs to.
initial_stream_windowInitial per-stream window in bytes.

Definition at line 124 of file quic_flow_control.c.

References cap.