The Transport Layer is the backbone of process-to-process communication in the TCP/IP protocol suite. Operating directly between the application layer and the network layer, transport protocols convert the unreliable, connectionless packet-delivery service of IP into a reliable, ordered stream-delivery service for client-server processes.
While the User Datagram Protocol (UDP) offers minimal overhead by sacrificing reliability, the Transmission Control Protocol (TCP) implements complex flow, error, and congestion control algorithms to guarantee robust data transfer over unpredictable networks.
To understand how TCP provides reliability and flow regulation, we must first study the generic sliding window models that form the mathematical basis of transport layer protocols.
In a simple Stop-and-Wait protocol, the sender transmits a single packet and must wait for an acknowledgment (ACK) before sending the next. If we analyze this using a network link with transmission rate (bps), propagation delay (seconds), and packet size (bits), the packet transmission time is defined as:
The round-trip time () for the link is:
The total time required to send a packet and receive its acknowledgment is . The link utilization () is the fraction of time the link is actively transmitting data:
For high-bandwidth or high-delay links (known as networks with a large bandwidth-delay product ), the utilization of a Stop-and-Wait protocol is extremely low.
Stop-and-Wait Timeline:
Sender Receiver
| -- [ Packet (L bits) ] -------------> |
|=========T_trans========= |
| | (Propagation Delay T_prop)
| |
| v
| [Packet Arrives]
| |
| <--------- [ ACK ] ---------------------- |
| |
|<------------------ RTT ------------------>|
v vTo solve the inefficiency of Stop-and-Wait, sliding window protocols allow the sender to transmit multiple packets before receiving an acknowledgment. The maximum number of outstanding packets is defined by the window size ().
Go-Back-N (GBN) sliding window at the sender:
Sent & ACKed Outstanding (Sent, Not ACKed) Can Send Immediately
[===============] [=============================] [=========================]
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
... | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10| 11| 12| 13| 14| 15| 16| 17| 18| ...
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
^ ^
|-- Sf (First outstanding) |-- Sn (Next to send)
|<--------- Window Size -------->|Go-Back-N (GBN):
TCP operates on a stream of bytes rather than discrete packets. It groups these bytes into logical units called segments for transmission.
The standard TCP segment consists of a header (typically 20 to 60 bytes) followed by the application payload.
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Port | Destination Port |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Sequence Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Acknowledgment Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Data | |U|A|P|R|S|F| |
| Offset| Reserved |R|C|S|S|Y|I| Window |
| | |G|K|H|T|N|N| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Checksum | Urgent Pointer |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options (0 to 40 bytes) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Data |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+URG: Urgent pointer field is valid.ACK: Acknowledgment field is valid (always set after connection establishment).PSH: Push function; requests that the receiver deliver data immediately to the application without waiting for buffers to fill.RST: Reset the connection.Before exchanging data, TCP uses a three-way handshake to establish a connection. This process synchronizes sequence numbers and negotiates connection parameters like the Maximum Segment Size (MSS).
Client (Active Open) Server (Passive Open)
| |
| ------ SYN (Seq=X, ACK=0) -----------------------> | (State: SYN-RCVD)
| |
| <----- SYN-ACK (Seq=Y, ACK=X+1) ------------------ |
| |
| ------ ACK (Seq=X+1, ACK=Y+1) -------------------> | (State: ESTABLISHED)
v vSYN flag set, containing its initial sequence number . This segment consumes 1 sequence number even though it contains no payload.SYN and ACK flags are set. It contains the server's initial sequence number , and acknowledges the client’s SYN by setting the acknowledgment number to . This segment also consumes exactly 1 sequence number.ACK set and an acknowledgment number of . This segment carries no data and does not consume a sequence number.Connection termination uses a four-step sequence (or a three-way handshake with a combined FIN-ACK) to close both directions of the full-duplex communication channel. The terminating peer enters a TIME-WAIT state for a duration of (Maximum Segment Lifetime) to ensure that any delayed segments still in transit are discarded and do not corrupt subsequent connections.
Flow control prevents a fast sender from overwhelming a slow receiver's buffer space. TCP implements flow control using a Sliding Window mechanism where the receiver dynamically regulates the sender's transmission rate via the advertised receive window ().
Receiver Buffer State:
+-------------------------[ Receiver Buffer ]-------------------------+
| Bytes Read | Bytes Received & Buffered | Free |
| by Process | (Waiting to be read) | Space |
+--------------+----------------------------------------+------------+
^ ^ ^
| | |
LastByteRead LastByteRcvd MaxBufSize
* Active Receive Window (rwnd) = MaxBufSize - (LastByteRcvd - LastByteRead)The sender maintains a send window whose effective size is determined by the minimum of the receive window () advertised by the receiver and the congestion window () calculated by the sender:
The boundaries of the sender's sliding window are tracked using three pointer variables:
Sender Sliding Window Pointers:
|<---------- S_size (rwnd) ---------->|
+---------+--------------------+----------------+---------+
| ACKed | Sent, Unacknowledged| Can Send Now | Cannot |
+---------+--------------------+----------------+---------+
^ ^
|-- Sf |-- Sn To ensure reliable operation, the receiver must not shrink its window. Window shrinking occurs if the right-hand boundary of the send window moves to the left. To prevent this, the receiver must satisfy the following mathematical inequality across consecutive advertisements:
If the receiver violates this rule, bytes that were previously inside the sender's window (and potentially already transmitted) could suddenly fall outside the window, leading to severe synchronization errors.
Silly Window Syndrome is a state where data is exchanged in very small segments (e.g., 1 byte of payload accompanied by 40 bytes of TCP/IP header, resulting in extremely low efficiency). SWS can be triggered by either the sender or the receiver.
If the application generates data slowly (e.g., keystrokes in a Telnet session), the sender might transmit numerous tiny segments. Nagle's Algorithm mitigates this by buffering small payloads until they can be sent as a larger segment:
Nagle's Algorithm Decision Tree:
+------------------------+
| Application writes |
| data to TCP |
+------------------------+
|
v
/------------------------------------\
/ Is outstanding data unACKed? \--- No ---> [Send Segment
\ / Immediately]
\------------------------------------/
| Yes
v
/------------------------------------\
/ Is accumulated data >= MSS? \--- Yes --> [Send Segment
\ / Immediately]
\------------------------------------/
| No
v
[Buffer data and wait
for ACK or more data]If the receiving application consumes data slowly, the receiver's buffer will fill up. When a single byte is read, the receiver might advertise a of 1 byte. The sender immediately transmits a 1-byte segment, keeping the buffer full and trapping the connection in a loop of tiny segments.
TCP provides reliable data delivery over unreliable channels using three main tools: checksums, cumulative/selective acknowledgments, and retransmission timers.
TCP triggers a retransmission under two conditions:
Fast Retransmit Scenario:
Sender Receiver
| -- [Segment 1] (Seq=1000) -------------> | (Receives Seg 1, ACK=2000)
| |
| -- [Segment 2] (Seq=2000) --- LOST ---> |
| |
| -- [Segment 3] (Seq=3000) -------------> | (Receives Seg 3, Out-of-Order! Sends ACK=2000)
| |
| -- [Segment 4] (Seq=4000) -------------> | (Receives Seg 4, Out-of-Order! Sends Duplicate ACK=2000)
| |
| -- [Segment 5] (Seq=5000) -------------> | (Receives Seg 5, Out-of-Order! Sends Duplicate ACK=2000)
| |
| <==== Receives 3 Duplicate ACKs! ======= |
| [Retransmits Segment 2 (Seq=2000)] |
v vBy default, TCP uses cumulative acknowledgments. If segments 1000-2000 and 3000-4000 are received but 2000-3000 is lost, the receiver can only advertise . The sender must then either wait for a timeout or retransmit everything starting from 2000.
The Selective Acknowledgment (SACK) option solves this inefficiency. In the TCP options header, the receiver includes SACK blocks that specify the boundaries of out-of-order blocks it has already buffered. SACK blocks use a half-open interval representing bytes from sequence number up to .
While flow control protects the receiver, congestion control protects the network from being overloaded by too many active senders. TCP dynamically adjusts its congestion window () based on network conditions.
The TCP congestion control state machine uses a threshold variable (, slow-start threshold) and consists of three main phases:
TCP Congestion Policy State Diagram:
+-------------------------------------------+
| STARTUP |
+-------------------------------------------+
|
v
+-------------------------+
| Slow Start (Exponential)| <-------------+
| (cwnd < ssthresh) | |
+-------------------------+ |
| | (Timeout)
| (cwnd >= ssthresh) |
v |
+-------------------------+ |
| Congestion Avoidance | |
| (Additive Increase) | --------------+
+-------------------------+
|
| (3 Duplicate ACKs)
v
+-------------------------+
| Fast Recovery |
| (TCP Reno) |
+-------------------------+At the start of a connection or after a timeout, the sender initializes its congestion window to a small value (typically ). To quickly probe the network's capacity, the sender increases exponentially:
This means that for every round-trip time () where all outstanding packets are acknowledged, the congestion window doubles:
Once reaches , the sender transitions to a more conservative linear increase phase to avoid causing network congestion. The congestion window increases by 1 MSS per RTT:
This results in a total increase of after a full window of segments has been successfully acknowledged:
TCP handles packet loss differently depending on whether it is detected by a timeout or by triple duplicate ACKs.
A timeout indicates severe network congestion. Both TCP Tahoe and TCP Reno react the same way:
Because duplicate ACKs indicate that subsequent segments are still getting through, the network is not completely congested.
TCP Congestion Window (cwnd) Evolution (Sawtooth Behavior):
cwnd (MSS)
^
32 | /\
24 | / \ (Timeout: ssthresh=12, cwnd=1)
16 | /\ / \
12 |-------- /--\-------/---- \------------ ssthresh (after duplicate ACKs)
8 | /\ / \ / \
4 | / \ / \---/ \
1 |/____\/_______________________\__________> Time (RTTs)
|<--->|<---------->|<--------->|
Slow Congestion Fast Slow Start
Start Avoidance RecoveryTo manage its operations, TCP uses several timers: the Persistence Timer (prevents deadlocks when the receive window is zero), the Keepalive Timer (detects dead connections), the TIME-WAIT Timer (ensures orderly connection teardown), and the Retransmission Timer (RTT).
Because network conditions vary, TCP dynamically calculates its Retransmission Timeout () using continuous measurements of the Sample Round-Trip Time ().
Measurement Timeline:
Sender Receiver
| -- [Segment X] (Record Transmit Time t1) -> |
| |
| <--------- [ ACK X ] (Arrives at t2) ------ |
v v
* Sample RTT Measurement (RTT_M) = t2 - t1To smooth out measurement noise, TCP calculates a weighted moving average called the Smoothed RTT ():
Typically, RFC 6298 recommends setting .
Next, TCP tracks network jitter by calculating the RTT Deviation ():
Typically, RFC 6298 recommends setting .
The Retransmission Timeout () is then calculated as:
Note: If the calculated is less than second, it is typically rounded up to second to avoid premature retransmissions.
When the first segment (the SYN) is sent, no RTT measurements exist yet. The initial values are set as follows:
SYN-ACK):
Let's walk through an example of these calculations over multiple transmission rounds.
We start with no prior measurements. The connection is initialized with:
The first SYN-ACK arrives, giving us a measured round-trip time of .
Using our initialization rules:
The sender transmits a data segment and receives its acknowledgment, giving a new measurement of . We update our estimates using and :
Calculate :
SYN SYN-ACK ACK) and terminated using a four-way handshake (FIN ACK ), with a state to prevent duplicate segments from corrupting subsequent connections.A dynamic flow control mechanism where the sender maintains a range of sequence numbers it is permitted to transmit without waiting for an acknowledgment.
A severe degradation of network performance occurring when either the sender generates data slowly or the receiver consumes data slowly, resulting in tiny segments with high protocol overhead.
An optimization that triggers the retransmission of a lost segment before its timer expires when the sender receives three duplicate acknowledgments.
A congestion control state machine where the congestion window increases linearly during normal operation but drops to half instantly upon packet loss detection.
A rule stating that Round-Trip Time (RTT) measurements must not be computed from retransmitted segments, preventing calculation skew caused by ambiguity.
Test your understanding with 5 questions
A TCP connection is established and the current estimated Round-Trip Time ($RTT_S$) is 2.0 seconds with a deviation ($RTT_D$) of 0.5 seconds. The sender transmits a segment, but a timeout occurs. According to Karn's algorithm and standard TCP RTO calculation rules, what is the new Retransmission Timeout (RTO) value applied to the retransmitted segment?
A link has a transmission rate of 10 Mbps and a round-trip propagation delay (RTT) of 40 milliseconds. If the data packet size is 1,250 bytes, what is the maximum link utilization percentage when using a sliding window protocol with a window size of $W = 10$?
To prevent the critical error of 'Window Shrinking' on the sender's side, which of the following mathematical inequalities must the receiver enforce when advertising its receive window ($rwnd$) and acknowledgment number ($ackNo$)?
During a TCP Reno connection, the congestion threshold ($ssthresh$) is set to 16 KB and the Maximum Segment Size (MSS) is 1 KB. While in the Congestion Avoidance phase at a congestion window ($cwnd$) size of 24 KB, a triple duplicate acknowledgment is received. What are the new values of $ssthresh$ and $cwnd$ respectively?
A TCP receiver utilizing the SACK option sends a segment containing 'ACK: 4001' and a SACK block specifying 'Left Edge: 5001, Right Edge: 6001'. Which of the following statements correctly interprets the receiver's state?
Selective-Repeat (SR):
SYN: Synchronize sequence numbers during connection establishment.FIN: Terminate the connection.Calculate :
Calculate the new RTO:
ACKTIME-WAIT6 Modules
6 Modules