📡 Tutorial 7: TCP Reliable Data Transfer and Flow Control (Advanced)
University‑level treatment – COMP347 (TrustOpen University)
🎯 Learning Objectives
After completing this tutorial, you should be able to:
- Describe the sender and receiver operations for TCP reliable data transfer using finite‑state machines.
- Explain the role of timeouts, duplicate ACKs, and fast retransmit in TCP loss recovery.
- Analyze the flow control mechanism, including the advertised window (rwnd) and its computation.
- Evaluate the effects of zero‑window conditions and the persistent timer.
- Compare cumulative and selective ACK strategies and their impact on performance.
- Design solutions to avoid Silly Window Syndrome and understand Nagle’s algorithm and delayed ACKs in context.
- Quantify the impact of window size, RTT, and loss on TCP throughput.
🔍 Overview
TCP’s reliable data transfer is a sophisticated combination of sliding‑window flow control, cumulative acknowledgments, timeouts, and retransmissions. This tutorial dives into the operational details of the sender and receiver, the precise handling of timeouts and duplicate ACKs, and the fast retransmit/fast recovery mechanisms that improve performance. We also explore flow control in depth—how the receiver advertises its available buffer space, how zero‑window conditions are managed, and how both sender and receiver cooperate to avoid Silly Window Syndrome. These mechanisms are the backbone of TCP’s robustness and efficiency in the Internet.
📘 1. Overview of TCP Reliable Data Transfer
TCP provides reliable, ordered delivery of a byte stream. It achieves this by:
- Using sequence numbers to identify each byte in the stream.
- Using cumulative acknowledgments (ACKs) to indicate the next expected byte.
- Maintaining a sliding window to manage flow and congestion (though congestion control is covered later).
- Using retransmission timers to detect lost segments.
- Using duplicate ACKs to trigger fast retransmit without waiting for a timeout.
- Employing flow control via the advertised window to prevent receiver buffer overflow.
TCP’s reliability is a hybrid of Go‑Back‑N (cumulative ACKs) and Selective Repeat (fast retransmit for individual packets), with SACK providing even finer granularity.
📘 2. TCP Sender Finite‑State Machine
The TCP sender can be modeled as a finite‑state machine with the following states and transitions:
- State: Ready – waiting for data from application or ACKs from receiver.
- Events:
rdt_send(data) – application passes data to send.
rdt_rcv(ACK) – an ACK segment arrives.
timeout – retransmission timer expires.
- Actions:
- On rdt_send: if window not full, segment and send; if first segment, start timer.
- On ACK receipt: if cumulative ACK acknowledges new data, update send window; if duplicate ACK, increment duplicate ACK count.
- On timeout: retransmit oldest unacknowledged segment, restart timer.
The sender also maintains variables: SendBase (oldest unacknowledged byte), NextSeqNum (next byte to send), and window (min(cwnd, rwnd)).
2.1 Sender Pseudocode
Initialize: SendBase = 0, NextSeqNum = 0, dupACKcount = 0
rdt_send(data):
if NextSeqNum < SendBase + window:
create segment with seq = NextSeqNum, send it
if SendBase == NextSeqNum: start timer
NextSeqNum += length(data)
else:
buffer data (or block)
rdt_rcv(ACK):
if ACKnum > SendBase:
SendBase = ACKnum
if SendBase == NextSeqNum: stop timer
else: restart timer
dupACKcount = 0
else if ACKnum == SendBase:
dupACKcount += 1
if dupACKcount == 3:
retransmit segment with seq = SendBase
dupACKcount = 0
// (fast retransmit)
// ignore if ACKnum < SendBase
timeout:
retransmit segment with seq = SendBase
restart timer
// (exponential backoff may apply)
📘 3. TCP Receiver Behaviour and ACK Generation
The receiver’s actions are defined in RFC 5681:
- On receipt of an in‑order segment that fills a gap: send cumulative ACK for the next expected byte.
- On receipt of an out‑of‑order segment: buffer it and send a duplicate ACK (the ACK for the last in‑order byte).
- On receipt of a segment with data that overlaps already received data (duplicate): discard and send ACK for the next expected byte.
- Delayed ACK: may delay sending an ACK for up to 500 ms, hoping to piggyback on outgoing data.
The receiver maintains the variable ExpectedSeqNum (the next byte expected). When a segment arrives with sequence number exactly ExpectedSeqNum, it delivers the data and advances ExpectedSeqNum past any contiguous buffered data, then sends a cumulative ACK.
📘 4. Timer Management and Retransmission Strategies
TCP uses a single retransmission timer for the oldest unacknowledged segment. When that segment’s ACK arrives, the timer is restarted for the next oldest segment. This is simpler than per‑packet timers.
On timeout:
- Retransmit the oldest unacknowledged segment.
- Restart the timer (with exponential backoff: RTO is doubled to reduce network load).
- Set
SendBase to the sequence number of the retransmitted segment (no change if using cumulative ACKs).
Exponential backoff: After a timeout, the RTO is doubled (up to a maximum) to avoid repeated retransmissions when the network is congested.
📘 5. Fast Retransmit and Fast Recovery
Fast retransmit: When the sender receives 3 duplicate ACKs (i.e., 4 total ACKs for the same byte), it assumes the segment with sequence number SendBase was lost and retransmits it immediately, without waiting for the timeout. This reduces recovery time significantly.
Fast recovery (in TCP Reno): After fast retransmit, the sender sets ssthresh = cwnd/2 and cwnd = ssthresh + 3*MSS (to account for the segments already ACKed), then enters congestion avoidance. This avoids the slow start phase after a loss, improving throughput.
These mechanisms are critical for TCP performance in modern networks.
📘 6. Flow Control: The Advertised Window (rwnd)
Flow control is a separate mechanism from congestion control. The receiver advertises a receive window (rwnd) in each ACK, indicating the amount of free buffer space. The sender limits the amount of unacknowledged data to min(cwnd, rwnd).
The receiver’s buffer is divided into:
- Data already delivered to application.
- Data received but not yet delivered (in‑order).
- Data received out‑of‑order (buffered).
- Free space.
The advertised window is computed as:
rwnd = buffer size - (LastByteReceived - LastByteRead)
where LastByteReceived is the highest sequence number received, and LastByteRead is the highest sequence number read by the application.
📘 7. Zero‑Window Probing and Persistent Timer
If rwnd = 0, the sender stops sending data. However, the receiver may later free buffer space and send an ACK with a non‑zero window. If that ACK is lost, the sender would wait forever. To prevent deadlock, the sender uses a persistent timer. When the persistent timer expires, the sender sends a zero‑window probe (a single byte of data) to the receiver. The receiver responds with an ACK containing the current window. If the window is still zero, the persistent timer is reset with exponential backoff.
📘 8. Silly Window Syndrome (SWS) Avoidance
Silly Window Syndrome occurs when the receiver advertises very small windows (e.g., a few bytes), and the sender sends tiny segments, leading to poor network utilization. Both sides take action:
- Receiver‑side SWS avoidance: The receiver should not advertise a window that is smaller than either a full‑size segment (MSS) or a certain fraction of the buffer (e.g., half the buffer). It delays sending window updates until the window grows sufficiently.
- Sender‑side SWS avoidance: The sender should not send a segment smaller than the MSS unless it has a full buffer or it has been a certain time since the last send (Nagle’s algorithm also helps).
These mechanisms prevent the inefficient transmission of many small packets.
📘 9. TCP Buffering and Window Update Semantics
The send buffer holds data sent but not yet ACKed. The receive buffer holds data received but not yet read. When the application reads data, the receiver’s window opens. The receiver may send a window update (an ACK with a new rwnd) even without receiving new data to inform the sender of the increased space. This is called a window update.
📘 10. Advanced Topics: ACK Clocking, Cumulative vs. Selective ACKs
ACK clocking: In steady state, TCP’s transmission is paced by the rate of incoming ACKs. Each ACK allows the sender to transmit a new segment (if window permits). This self‑clocking helps maintain smooth traffic and is important for congestion control.
Cumulative ACKs are simple but can cause unnecessary retransmissions if multiple packets are lost. Selective ACKs (SACK) allow the receiver to inform the sender of non‑contiguous blocks of received data, enabling selective retransmission and improving performance under loss.
📝 Quiz
Test your understanding with these 35 questions. Answers are hidden below each.
- What event triggers a TCP timeout retransmission?
Answer
The retransmission timer expires for the oldest unacknowledged segment.
- What does TCP do when it receives a duplicate ACK?
Answer
It increments the duplicate ACK count; after 3 duplicate ACKs, it triggers fast retransmit.
- What is the purpose of fast retransmit?
Answer
To retransmit a lost segment before the timeout expires, reducing recovery time.
- How many duplicate ACKs are needed to trigger fast retransmit?
Answer
3 duplicate ACKs (i.e., total 4 ACKs for the same byte).
- What is the difference between fast retransmit and timeout retransmission?
Answer
Fast retransmit occurs after 3 duplicate ACKs without waiting for timeout; timeout retransmission occurs when the timer expires.
- What is the purpose of the advertised window (rwnd)?
Answer
To inform the sender of the receiver’s available buffer space for flow control.
- How is rwnd computed at the receiver?
Answer
rwnd = buffer size - (LastByteReceived - LastByteRead).
- What happens when rwnd becomes 0?
Answer
The sender stops sending data and starts a persistent timer to probe for window updates.
- What is a zero‑window probe?
Answer
A segment with 1 byte of data sent by the sender when rwnd=0 to check if the window has opened.
- What is the persistent timer used for?
Answer
To prevent deadlock when rwnd=0 and the window update ACK is lost; it triggers zero‑window probes.
- What is Silly Window Syndrome (SWS)?
Answer
A condition where small window advertisements cause the sender to send tiny segments, wasting bandwidth.
- How does the receiver avoid SWS?
Answer
By not advertising a window smaller than the MSS or a threshold, and delaying window updates until they are significant.
- How does the sender avoid SWS?
Answer
By using Nagle’s algorithm (buffering small data) and not sending segments smaller than the MSS unless it has a full buffer.
- What is Nagle’s algorithm?
Answer
An algorithm that reduces small packet transmissions by buffering small data until an ACK arrives or the segment reaches the MSS.
- What is a delayed ACK?
Answer
An ACK that is delayed up to 500 ms in the hope of piggybacking on outgoing data, reducing ACK overhead.
- How does the TCP sender’s window size affect throughput?
Answer
A larger window allows more data in flight, increasing throughput up to the bandwidth‑delay product.
- What is ACK clocking?
Answer
The pacing of sender transmissions by the arrival of ACKs, which helps smooth traffic.
- What is the advantage of SACK over cumulative ACKs?
Answer
SACK allows selective retransmission of only lost packets, improving performance under loss.
- When does TCP use exponential backoff for RTO?
Answer
After a timeout, the RTO is doubled to reduce network load.
- What is the purpose of the
SendBase variable?
Answer
It is the sequence number of the oldest unacknowledged byte (the left edge of the send window).
- What is
NextSeqNum?
Answer
The sequence number of the next byte to be sent (the right edge of the send window).
- How does the receiver handle an out‑of‑order segment?
Answer
It buffers the segment and sends a duplicate ACK for the next expected byte.
- How does the receiver handle a duplicate segment?
Answer
It discards the duplicate and sends an ACK for the next expected byte.
- What is a window update?
Answer
An ACK segment that updates the advertised window (rwnd) without containing new data.
- What is the effect of a very small rwnd on TCP performance?
Answer
It limits the sender’s throughput, potentially underutilizing the network.
- What is the relationship between rwnd and the receive buffer?
Answer
rwnd = buffer size - unread data; it represents the free space.
- What is the purpose of the
dupACKcount variable?
Answer
To count the number of duplicate ACKs received, used to trigger fast retransmit.
- What happens to
dupACKcount when a new cumulative ACK arrives?
Answer
It is reset to 0.
- Can TCP send data while rwnd=0?
Answer
It cannot send normal data, but it may send zero‑window probes (1 byte).
- What is the difference between flow control and congestion control?
Answer
Flow control prevents receiver overload; congestion control prevents network overload.
- How does fast recovery differ from slow start?
Answer
Fast recovery after fast retransmit sets cwnd to ssthresh (half) and grows linearly (congestion avoidance), avoiding slow start’s exponential growth.
- What is the role of the
ssthresh variable in TCP?
Answer
Slow start threshold; when cwnd reaches ssthresh, TCP switches from slow start to congestion avoidance.
- Why does TCP use a single timer for the oldest segment instead of per‑packet timers?
Answer
To reduce overhead; it works because cumulative ACKs acknowledge all earlier packets.
- What is a window scale option?
Answer
An option that allows the advertised window to be scaled, enabling windows larger than 65535 bytes.
- How does TCP handle a retransmitted segment if the original segment was already received?
Answer
The receiver discards the duplicate and sends an ACK for the next expected byte.
🛠️ Exercises
Apply your knowledge with these 20 exercises. Solutions are provided below each.
- Exercise 1: Sender State Update
A TCP sender has SendBase = 1000, NextSeqNum = 1200, window = 500. It receives an ACK with ACKnum = 1100. What are the new SendBase and NextSeqNum?
Solution
SendBase = 1100, NextSeqNum remains 1200 (since the ACK acknowledges up to 1099). The window allows sending up to 1100+500=1600, so NextSeqNum can advance if data available.
- Exercise 2: Duplicate ACK Count
A sender receives three duplicate ACKs in a row. What does it do?
Solution
It triggers fast retransmit: retransmits the segment with sequence number equal to SendBase.
- Exercise 3: Timeout Retransmission
After a timeout, the sender retransmits the oldest segment. What else happens to the timer?
Solution
The timer is restarted (with possible exponential backoff) for the same segment.
- Exercise 4: Advertised Window Calculation
Receiver buffer size = 8192 bytes. LastByteRead = 2000, LastByteReceived = 5000. What is rwnd?
Solution
rwnd = 8192 - (5000 - 2000) = 8192 - 3000 = 5192 bytes.
- Exercise 5: Zero‑Window Scenario
If rwnd = 0, the sender stops sending. How does it know when to resume?
Solution
It sends zero‑window probes periodically until it receives an ACK with a non‑zero rwnd.
- Exercise 6: SWS Avoidance on Receiver
A receiver has 100 bytes free. Should it advertise 100 bytes? Explain.
Solution
No, it should not advertise such a small window to avoid SWS. It should wait until the free space reaches the MSS or a threshold.
- Exercise 7: Nagle’s Algorithm Effect
An application sends 10 bytes every 10 ms. Nagle’s algorithm is enabled. How does this affect transmission?
Solution
The first byte may be sent immediately; subsequent bytes may be buffered until an ACK is received or enough data accumulates, increasing latency but reducing the number of small packets.
- Exercise 8: Delayed ACK Impact
A receiver delays ACKs for 200 ms. How might this affect the sender’s RTT estimation?
Solution
It increases the measured RTT, potentially causing the sender to set a larger RTO, which can reduce performance.
- Exercise 9: Fast Retransmit Trigger
How many duplicate ACKs are needed for fast retransmit? What if only 2 duplicates arrive?
Solution
3 duplicate ACKs are needed. With 2, the sender continues waiting; it may later timeout if no further ACKs arrive.
- Exercise 10: Persistent Timer
Why is the persistent timer needed in addition to the retransmission timer?
Solution
The persistent timer handles the case where rwnd=0 and the window update ACK is lost, preventing deadlock.
- Exercise 11: ACK Clocking Example
In steady state, an ACK arrives for a segment. How does this allow the sender to send a new segment?
Solution
The ACK slides the window, freeing up space; the sender can then transmit a new segment if data is available.
- Exercise 12: Cumulative vs. SACK
If two segments are lost (e.g., seq 100 and 300) but intervening segments are received, how does cumulative ACK behave compared to SACK?
Solution
Cumulative ACK only acknowledges up to the highest in‑order byte, so it only indicates the first loss. SACK can specify both gaps, allowing the sender to retransmit both without waiting.
- Exercise 13: RTO Doubling
After a timeout, the RTO is doubled. Why is this necessary?
Solution
To reduce the sending rate and alleviate potential network congestion.
- Exercise 14: Send Window Limitation
If cwnd = 4000, rwnd = 2000, what is the effective window size?
Solution
min(cwnd, rwnd) = 2000 bytes.
- Exercise 15: Receiver Buffer Full
If the receive buffer is full (rwnd=0) and the sender sends a zero‑window probe, what does the receiver respond with?
Solution
It responds with an ACK containing the current rwnd (which may still be 0) and the acknowledgment number.
- Exercise 16: Fast Recovery
After fast retransmit, what happens to cwnd and ssthresh in TCP Reno?
Solution
ssthresh = cwnd/2; cwnd = ssthresh + 3*MSS; then congestion avoidance.
- Exercise 17: Duplicate ACK Handling
A sender receives an ACK with ACKnum less than SendBase. How does it handle it?
Solution
It ignores it (it's a duplicate ACK for an already acknowledged segment).
- Exercise 18: Window Update without Data
Can a receiver send an ACK that only updates the window (no data)?
Solution
Yes, when the application reads data from the buffer, the receiver may send a window update to inform the sender.
- Exercise 19: SWS Avoidance Sender Side
How does Nagle’s algorithm help prevent SWS?
Solution
It buffers small amounts of data until either an ACK arrives or enough data to fill a maximum segment, preventing many tiny segments.
- Exercise 20: Throughput Calculation
If window = 64 KB and RTT = 100 ms, what is the maximum throughput?
Solution
Throughput = (64 * 1024 * 8) / 0.1 = 524,288 bits/sec ≈ 5.24 Mbps.
📚 Homework
These advanced problems require synthesis, research, and quantitative analysis. Sample answers are provided below.
- Problem 1: Sender FSM Formalization
Provide a formal FSM specification of the TCP sender including all states, events, and actions, covering both normal operation and fast retransmit.
Sample Answer
States: Ready, WaitingForACK (when at least one segment outstanding). Transitions: on rdt_send if window not full -> send segment, start timer; on ACK with acknum > SendBase -> slide window, reset dupACKcount, restart timer; on ACK with acknum == SendBase -> increment dupACKcount, if count==3 -> fast retransmit; on timeout -> retransmit oldest, double RTO.
- Problem 2: Derive Fast Retransmit Behavior
Explain why fast retransmit uses 3 duplicate ACKs, not 2 or 4. What are the trade‑offs?
Sample Answer
3 is a compromise: 2 might cause spurious retransmissions due to packet reordering; 4 would delay recovery too long. It is based on empirical observation that 3 duplicates indicates a lost segment.
- Problem 3: Flow Control and Congestion Control Interaction
How does the sender use both rwnd and cwnd? What happens if one is smaller than the other?
Sample Answer
Sender uses min(cwnd, rwnd) as the effective window. If rwnd < cwnd, flow control is the bottleneck; if cwnd < rwnd, congestion control is the bottleneck.
- Problem 4: Zero‑Window Probing and RTT Estimation
Should zero‑window probes affect the RTT estimation? Why or why not?
Sample Answer
No, because they are not normal data segments; they are probes. They may be retransmitted without ACK, so they should not be used for RTT measurement.
- Problem 5: Delayed ACK and Fast Retransmit
Can delayed ACKs interfere with fast retransmit? How?
Sample Answer
Delayed ACKs can reduce the number of duplicate ACKs sent, delaying fast retransmit. However, the standard ensures that duplicate ACKs are sent for out‑of‑order segments, so it usually works.
- Problem 6: SWS Avoidance Algorithm
Describe the receiver‑side SWS avoidance algorithm in detail, including the conditions for sending a window update.
Sample Answer
The receiver should send a window update only when the available space is at least the MSS (or half the buffer). This prevents advertising tiny windows.
- Problem 7: Timer Granularity and Performance
If the timer granularity is coarse (e.g., 100 ms), how does that affect TCP performance? Derive the impact on throughput for a given RTT.
Sample Answer
A coarse timer causes the RTO to be larger, increasing idle time and reducing throughput, especially when RTT is small.
- Problem 8: Cumulative ACK and Duplicate ACK Count
Why does the sender not count duplicate ACKs that acknowledge a byte beyond SendBase?
Sample Answer
If an ACK acknowledges beyond SendBase, it slides the window, so it's a new ACK, not a duplicate. Duplicates are only those with acknum == SendBase.
- Problem 9: Fast Recovery Details
Explain the difference between TCP Tahoe and TCP Reno in handling fast retransmit.
Sample Answer
Tahoe after fast retransmit sets cwnd to 1 MSS and goes to slow start. Reno uses fast recovery: sets cwnd to ssthresh+3*MSS, then grows linearly.
- Problem 10: Impact of Buffer Size on Throughput
Derive the relationship between receiver buffer size and achievable throughput for a given RTT and bandwidth. What is the minimum buffer size needed to achieve 100 Mbps over a 100 ms RTT?
Sample Answer
Buffer size must be at least BDP = bandwidth * RTT = 100e6 * 0.1 = 10,000,000 bits = 1.25 MB.
- Problem 11: Zero‑Window Deadlock Scenario
Describe a scenario where a zero‑window condition could lead to deadlock if persistent timers were not used.
Sample Answer
Receiver advertises window 0, sender stops. Receiver later frees buffer and sends ACK with window >0, but that ACK is lost. Sender waits forever; persistent timer breaks deadlock.
- Problem 12: ACK Piggybacking and Delayed ACKs
How do delayed ACKs interact with piggybacking on data?
Sample Answer
If the receiver has outgoing data, it can piggyback the ACK on that data; if not, it delays the ACK to allow for piggybacking.
- Problem 13: SACK and Reno Integration
How does SACK improve upon Reno’s fast recovery?
Sample Answer
SACK provides information about which segments have been received, allowing the sender to retransmit only the missing ones during recovery, improving efficiency.
- Problem 14: Window Update Loss
If a window update ACK is lost, the sender may have a stale small window. How does the system recover?
Sample Answer
The sender may use a zero‑window probe if the window is 0; if the window is non‑zero but small, the sender may eventually timeout or the receiver may send another update.
- Problem 15: Throughput with Loss and Fast Recovery
Derive a simplified expression for TCP throughput incorporating loss rate p, RTT, and MSS, considering fast retransmit/recovery.
Sample Answer
A common approximation: Throughput ≈ (1.22 * MSS) / (RTT * sqrt(p)). This is derived from the square root law for TCP Reno.
📌 Summary
- TCP reliable data transfer combines sliding windows, cumulative ACKs, timeouts, and fast retransmit.
- The sender uses a single timer for the oldest unacknowledged segment, with exponential backoff.
- Fast retransmit (triggered by 3 duplicate ACKs) recovers lost segments quickly.
- Flow control uses the advertised window (rwnd) to prevent receiver buffer overflow.
- Zero‑window conditions are handled by persistent timers and probes.
- Silly Window Syndrome is avoided by both sender and receiver using threshold‑based window advertisements and Nagle’s algorithm.
- Advanced features like SACK and window scaling improve performance.
In the next tutorial, we will cover TCP Connection Establishment and Termination.