TCP Sliding Window Mechanism
Description:
The TCP sliding window mechanism is one of the core technologies for implementing flow control and reliable transmission in TCP. By dynamically adjusting the size of the send window, it coordinates the data processing capabilities of the sender and receiver, preventing data loss or network congestion caused by mismatched rates. The sliding window mechanism not only ensures the orderly transmission of data but also improves channel utilization, allowing the sender to transmit multiple data segments continuously without receiving acknowledgments.
Detailed Explanation:
1. Basic Concepts
- Window Definition: The sliding window is a data range maintained separately by the sender and receiver, indicating the current allowable sequence number interval for sending or receiving data. The window size is determined by the receiver's remaining buffer capacity.
- Core Functions:
- Flow Control: The receiver limits the sender's data injection rate by advertising the window size (
rwnd). - Reliable Transmission: Combined with timeout retransmission and Selective Acknowledgment (SACK), it ensures data arrives in order.
- Flow Control: The receiver limits the sender's data injection rate by advertising the window size (
- Window Structure:
- The send window is divided into four parts: sent and acknowledged, sent but unacknowledged, unsent but sendable, and unsent and unsendable.
- The receive window is divided into three parts: acknowledged and delivered, not yet received but receivable, and not yet received and unreceivable.
2. Window Sliding Process
Assume the initial send window size is 4 (unit: data packets), with sequence numbers 0~3:
- Step 1: The sender continuously sends data packets with sequence numbers 0~3; all data within the window is "sent but unacknowledged."
- Step 2: The receiver correctly receives packet 0 and returns ACK=1 (acknowledging packet 0, expecting to receive packet 1), but the ACK may arrive delayed.
- Step 3: After the sender receives ACK=1, the window slides one position to the right. The new window range becomes 1~4, allowing packet 4 to be sent (if there is still space in the window).
- Key Point: Window sliding depends on the smallest unacknowledged sequence number among the sent data (i.e., the left boundary of the window), not on individual ACKs.
3. Flow Control Implementation
- The receiver advertises the current available buffer space (
rwnd) via the "Window Size" field in the TCP header each time it sends an ACK. - Zero Window Problem: If the receiver's buffer is full, it advertises
rwnd=0, and the sender pauses transmission. To avoid deadlock, the sender starts a persist timer to periodically probe whether the window has recovered. - Silly Window Syndrome: When the receiver only frees up a small amount of space each time, advertising a small window leads to inefficient transmission. Solutions:
- Receiver Strategy: Advertise the window only when the buffer space reaches a certain threshold (e.g., MSS or half the buffer).
- Sender Strategy (Nagle's Algorithm): Accumulate small data packets and send them when a full MSS is reached or an ACK for previous data is received.
4. Difference from Congestion Control
- Sliding Window: Based on receiver capacity (end-to-end flow control).
- Congestion Window: Based on network conditions (global regulation), dynamically adjusting the send rate through algorithms like slow start and congestion avoidance.
- The actual send window is the minimum of the two:
W = min(rwnd, cwnd).
5. Typical Scenario Example
Assume the initial window size rwnd=6, with data packet sequence numbers 1~10:
- The sender continuously sends packets 1~6, filling the window with unacknowledged data.
- The receiver acknowledges packets 1 and 2 while advertising a new window
rwnd=4(due to reduced buffer space). - After receiving the ACK, the sender slides the window to sequence number 3, but the actual amount of data it can send is limited by the new window, allowing only packets 7~8 to be sent (since the window size is now 4, and packets 3~6 are unacknowledged).
- If an ACK is lost, the cumulative acknowledgment mechanism (e.g., receiving ACK=5 indicates all packets before 5 are acknowledged) ensures the window slides normally.
Summary:
The sliding window mechanism dynamically adjusts the send rate to balance the processing capabilities of the sender and receiver. It also ensures reliability through sequence number ordering and retransmission mechanisms. Understanding the window sliding rules, flow control strategies, and their coordination with congestion control is key to mastering the core design of TCP.