Skip to content

Commit 9915da4

Browse files
hikejsclaude
andcommitted
docs(vsock): add detailed documentation for credit flow control
Add comprehensive documentation for credit-based flow control functions: 1. harvest_stream_reads(): - Explains credit checking before reading - Documents tx_cnt tracking - Describes burst reading behavior 2. available_tx_credit(): - Detailed formula explanation - Describes each variable's meaning - Explains the in-flight bytes concept This improves code maintainability by making the credit flow control algorithm easier to understand for future developers. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent b985444 commit 9915da4

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

src/devices/src/virtio/vsock_windows.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,17 @@ impl Vsock {
443443
}
444444

445445
/// Calculate available TX credit for a stream.
446-
/// Returns the number of bytes we can send to the peer.
446+
///
447+
/// Credit-based flow control ensures we don't overflow the peer's receive buffer.
448+
/// The formula is: available_credit = peer_buf_alloc - (tx_cnt - peer_fwd_cnt)
449+
///
450+
/// Where:
451+
/// - peer_buf_alloc: Total buffer space the peer has allocated
452+
/// - tx_cnt: Total bytes we've sent to the peer
453+
/// - peer_fwd_cnt: Total bytes the peer has consumed (forwarded to application)
454+
/// - in_flight: Bytes sent but not yet consumed by peer (tx_cnt - peer_fwd_cnt)
455+
///
456+
/// Returns the number of bytes we can safely send without overflowing peer's buffer.
447457
fn available_tx_credit(state: &StreamState) -> u32 {
448458
// Credit = peer_buf_alloc - (tx_cnt - peer_fwd_cnt)
449459
// This represents how much buffer space the peer has available
@@ -511,6 +521,15 @@ impl Vsock {
511521
.or_insert(1);
512522
}
513523

524+
/// Read data from all active streams and queue RX packets to the guest.
525+
///
526+
/// This function implements credit-based flow control:
527+
/// - Checks available TX credit before reading from each stream
528+
/// - Only reads up to the available credit amount
529+
/// - Updates tx_cnt to track bytes sent to the peer
530+
///
531+
/// For each stream, reads up to MAX_READ_BURST_PER_STREAM times or until
532+
/// no more data is available or credit is exhausted.
514533
fn harvest_stream_reads(&mut self) {
515534
let mut responses: Vec<([u8; 44], Vec<u8>)> = Vec::new();
516535
let mut closed_ports: Vec<u32> = Vec::new();

0 commit comments

Comments
 (0)