3.5.1 Connection-oriented Transport TCP
TCP Reliable Data Transfer
TCP implements a reliable data transfer service on top of IP's unreliable best-effort service. It ensures that the byte stream is delivered without errors, loss, or duplication.
Core Mechanisms:
Checksums: Used to detect bit-level corruption in the segment (header and data).
Sequence Numbers: Allow the receiver to reorder segments and identify gaps in the byte stream.
Cumulative Acknowledgments (ACKs): Communicate to the sender which bytes have been successfully received.
Retransmission Timers: Used to resend segments that are assumed lost or corrupted.
Pipelining: Allows the sender to have multiple transmitted but unacknowledged segments in flight, increasing throughput.
TCP Characteristics
Point-to-Point: A single connection exists between exactly one sender and one receiver.
Reliable, In-order Byte Stream: No concept of "message boundaries"; the application reads data as a continuous stream of bytes.
Full Duplex Operations: Data can be sent and received simultaneously on the same connection. Each side maintains its own sequence numbers and flow control windows.
Connection-Oriented: Requires a multi-step handshake procedure to initialize state variables (sequence numbers, buffer sizes) before data exchange begins.
MSS (Maximum Segment Size): Limits the maximum amount of application-layer data in a segment. It is typically determined by the path's Maximum Transmission Unit (MTU), often set to 1460 bytes to fit within a 1500-byte Ethernet frame (after accounting for 40 bytes of TCP/IP headers).
Flow Control: Prevents the sender from overflowing the receiver's buffer by dynamically signaling available buffer space.
TCP Segment Structure
Source/Destination Ports: Facilitate multiplexing and demultiplexing to specific application processes.
Sequence Number (32 bits): The byte-stream number of the first data byte in the segment's payload.
Acknowledgment Number (32 bits): The sequence number of the next byte the receiver expects to receive. It is cumulative, meaning it acknowledges all bytes prior to that number.
Header Length (4 bits): Specifies the size of the TCP header in 32-bit words.
Flag Bits (6 bits):
URG: Indicates the urgent pointer field is valid (rarely used).
ACK: Indicates the acknowledgment field is valid.
PSH: Requests the receiver to push the data to the application immediately.
RST, SYN, FIN: Used for connection setup (SYN), teardown (FIN), and emergency reset (RST).
Receive Window (16 bits): Used for flow control; indicates the number of bytes the receiver is willing to accept.
Checksum (16 bits): Used for error detection of the segment.
Options: Variable length; used for MSS negotiation, window scaling, or selective acknowledgments (SACK).
Sequence and Acknowledgment Numbers
Because TCP views data as an ordered stream of bytes, sequence numbers represent the byte's position rather than the segment number.
Example: If a stream consists of 500,000 bytes and the MSS is 1,000 bytes, the first segment has sequence number 0, the second has 1,000, the third has 2,000, etc.
Cumulative ACK: If a receiver sends an ACK for 1,501, it implies it has received everything up to byte 1,500 and is waiting for 1,501.
TCP Timeout Mechanism
TCP uses an adaptive timeout based on continuous measurements of the Round Trip Time (RTT).
Estimated RTT: Calculated using an Exponentially Weighted Moving Average (EWMA) to smooth out fluctuations.
Recommended .
DevRTT: Estimates how much the Sample RTT deviates from the Estimated RTT.
Recommended .
Timeout Interval: The actual timer value used.
Flow Control Mechanism
The receiver maintains a receive buffer (). If the application is slow to read, the buffer fills up.
The receiver advertises the remaining space () in every segment sent back to the sender.
The sender ensures that the amount of unacknowledged data () is always less than or equal to the advertised .
Connection Management
Three-Way Handshake (Establishment):
Step 1: Client sends a SYN segment (Sequence = , SYN=1).
Step 2: Server replies with a SYN-ACK (Sequence = , ACK = , SYN=1, ACK=1).
Step 3: Client sends an ACK (Sequence = , ACK = , ACK=1).
Teardown: Uses a four-way handshake where each side independently closes its half of the connection using the FIN bit.
TCP Sender and Receiver Operations
Sender Transitions:
Receives data from app: Encapsulates in segment, starts timer if not already running.
Timeout: Retransmits the oldest unacknowledged segment and doubles the timeout interval (exponential backoff).
ACK Received: Updates ; if there are still outstanding segments, restarts the timer.
Fast Retransmit: If the sender receives three duplicate ACKs for the same data (triple duplicate ACKs), it assumes the segment after the acknowledged byte is lost and retransmits immediately without waiting for the timer to expire.
Retransmission Scenarios
Lost ACK: If an ACK is lost but the data was received, the sender's timeout will trigger a retransmission. The receiver discards the duplicate and re-ACKs the data.
Premature Timeout: If the timer is too short, the sender may retransmit data that is still in transit. Cumulative ACKs help recover by acknowledging the original and the redundant segments together.