Detailed Explanation of TCP's SACK (Selective Acknowledgment) Mechanism
Detailed Explanation of TCP's SACK (Selective Acknowledgment) Mechanism
1. Background of the SACK Mechanism
- Limitations of Traditional TCP Acknowledgment: Standard TCP uses a cumulative acknowledgment mechanism where the receiver can only acknowledge the highest sequence number of consecutive data packets received. For example, when packets 1, 2, 4, and 5 are received, only packet 2 can be acknowledged, causing the sender to retransmit packet 3 and all subsequent packets (i.e., packets 4 and 5 are also unnecessarily retransmitted).
- Network Efficiency Issues: In high packet loss environments, cumulative acknowledgment leads to a large number of unnecessary retransmissions, severely wasting bandwidth. The SACK mechanism allows the receiver to inform the sender of successfully received non-contiguous data blocks, enabling precise retransmission.
2. How SACK Works
- TCP Option Field Extension:
- Defines the SACK option (Kind=5) within the TCP header options section. The structure is
[Kind=5][Length][Left Edge1][Right Edge1]...[Left Edgen][Right Edgen]. - Each data block is represented by two 32-bit sequence numbers: the Left Edge (the start sequence number of the received data block) and the Right Edge (the end sequence number + 1).
- Limited by the TCP option length (maximum 40 bytes), it can practically carry information for no more than 4 data blocks.
- Defines the SACK option (Kind=5) within the TCP header options section. The structure is
- Receiver Behavior:
- When out-of-order packets are received, it includes the SACK option in the ACK packet. For example, if packets with sequence numbers 1000-1999 and 3000-3999 are received, but 2000-2999 is missing, the ACK packet's acknowledgment number remains 1000 (expecting the next packet), and the SACK option records the block 3000-4000.
- Duplicate ACKs (e.g., when new out-of-order packets are received) continue to send the latest SACK information, helping the sender update the network state.
- Sender Behavior:
- Maintains a "SACK Retransmission Queue" to record the non-contiguous data blocks acknowledged by the receiver.
- When packet loss is detected (e.g., upon receiving 3 duplicate ACKs), it retransmits only the missing data segments. For example, if 2000-2999 is missing, only that segment is retransmitted, avoiding retransmission of the already SACK-acknowledged segment 3000-3999.
3. Collaboration Between SACK and Fast Retransmit
- Trigger Condition: When the sender receives 3 duplicate ACKs and the SACK information indicates out-of-order data, it immediately retransmits the missing packet without waiting for a timeout.
- Example Process:
- Sender transmits packets 1-5; receiver receives packets 1, 2, 4, and 5.
- Receiver replies with ACK=3 (expecting packet 3) and includes a SACK block [4-6] (the range covering packets 4 and 5).
- Sender, upon receiving ACK=3 three times and seeing from SACK that packets 4 and 5 are received, determines packet 3 is lost and retransmits only packet 3.
4. Boundary Handling and Algorithm Optimization in SACK
- Data Block Merging: The receiver maintains a receive queue, merging adjacent data blocks (e.g., merging [1000-2000] and [2000-3000] into [1000-3000]) to reduce the space occupied by SACK options.
- Sender Retransmission Strategy:
- Prioritizes retransmitting the oldest missing segment (e.g., if packets 3 and 5 are missing, retransmit packet 3 first).
- Integrates with congestion control: The congestion window may be reduced during retransmission, but SACK avoids excessive retransmissions, utilizing the window more efficiently.
5. Practical Considerations for SACK
- Network Device Compatibility: Some older firewalls or NAT devices may mishandle TCP options; it's essential to ensure path devices support SACK.
- Collaboration with TSOPT (Timestamp Option): SACK is often used alongside the Timestamp option. Timestamps provide more accurate RTT calculations, avoiding retransmission ambiguity.
- Linux Kernel Parameter Tuning: For example,
net.ipv4.tcp_sackcan enable/disable SACK, andtcp_max_sack_bytescontrols the maximum bytes for a single retransmission.
6. Summary
The SACK mechanism improves retransmission precision from "retransmit everything" to "retransmit on-demand" by providing feedback on non-contiguous data blocks. It significantly enhances throughput, especially in high packet loss networks (e.g., wireless environments). It is a crucial optimization in the evolution of TCP reliability transmission, forming, along with fast retransmit and congestion control, the core capabilities of modern TCP.