The TCP Protocol
1. TCP Overview
TCP (Transmission Control Protocol) is the connection-oriented protocol of the transport layer and provides a reliable data transfer service.
Basic Characteristics of TCP
- Connection-oriented: a connection has to be established beforehand
- Reliable transmission: guarantees that data is delivered intact, in order, and without duplication
- Flow control: prevents the sender from going so fast that the receiver’s buffer overflows
- Congestion control: adapts to the state of the network and avoids congestion
Where TCP Applies
- File transfer: FTP, HTTP
- Web browsing: HTTPS
- Mail transfer: SMTP, POP3, IMAP
- Remote login: SSH, Telnet
2. The TCP Segment Structure
The TCP Header Format
TCP Header Format
The TCP header is at least 20 bytes and carries the sequence numbers, acknowledgements, and window that make TCP reliable
Field Descriptions
Port number fields:
- Source port (16 bits): the port number of the sending process
- Destination port (16 bits): the port number of the receiving process
Sequence number fields:
- Sequence number (32 bits): the sequence number of the first byte of this segment
- Acknowledgment number (32 bits): the sequence number of the next byte the receiver expects
Control fields:
- Data offset (4 bits): the length of the TCP header, in units of 4 bytes
- Reserved (6 bits): reserved for future use, set to 0
- Flags (6 bits):
- URG: the urgent pointer is valid
- ACK: the acknowledgment number is valid
- PSH: push flag
- RST: reset the connection
- SYN: synchronise sequence numbers
- FIN: finish the connection
Window and checksum fields:
- Window size (16 bits): the size of the receive window
- Checksum (16 bits): the checksum over the header and the data
- Urgent pointer (16 bits): the last byte of the urgent data
3. TCP Connection Management
The Three-Way Handshake (Establishing a Connection)
The procedure:
- SYN=1, seq=x: the client sends a connection request
- SYN=1, ACK=1, seq=y, ack=x+1: the server acknowledges the connection
- ACK=1, seq=x+1, ack=y+1: the client acknowledges the connection
State changes:
- Client: CLOSED → SYN_SENT → ESTABLISHED
- Server: CLOSED → LISTEN → SYN_RCVD → ESTABLISHED
The Four-Way Teardown (Releasing a Connection)
The procedure:
- FIN=1, seq=u: the client sends a termination request
- ACK=1, ack=u+1: the server acknowledges the termination request
- FIN=1, seq=v: the server sends a termination request
- ACK=1, ack=v+1: the client acknowledges the termination request
State changes:
- Client: ESTABLISHED → FIN_WAIT_1 → FIN_WAIT_2 → TIME_WAIT → CLOSED
- Server: ESTABLISHED → CLOSE_WAIT → LAST_ACK → CLOSED
4. Reliable Transmission in TCP
The Sequence Number Mechanism
- Byte numbering: every byte carries a unique sequence number
- Initial sequence number: chosen at random when the connection is established
- Sequence number wraparound: numbering restarts once the maximum is reached
The Acknowledgement Mechanism
- Cumulative acknowledgement: the acknowledgment number states the next byte expected
- Selective acknowledgement: acknowledges a particular segment
- Delayed acknowledgement: the acknowledgement is sent later, to reduce network overhead
Timeout and Retransmission
- Timeout: adjusted dynamically, based on the RTT
- Retransmission strategy: an exponential backoff algorithm
- Fast retransmit: retransmit immediately on three duplicate ACKs
The Sliding Window
- Send window: the range of data that may be sent but not yet acknowledged
- Receive window: the range of data that may be received
- Sliding: the window moves forward once an acknowledgement arrives
5. TCP Flow Control
The Purpose of Flow Control
- Preventing overload: stops the sender from going so fast that the receiver’s buffer overflows
- Matching rates: the sending rate matches the receiving rate
- Improving efficiency: makes full use of the receiver’s processing capacity
The Sliding Window Mechanism
- Window size: the size of the window advertised by the receiver
- Dynamic adjustment: adjusted to the receiver’s processing capacity
- Zero window: the receiver advertises a zero window when its buffer is full
Flow Control Algorithms
- Slow start: after the connection is established, start from 1 MSS
- Congestion avoidance: once the window reaches the threshold, grow it linearly
- Fast retransmit: retransmit immediately on three duplicate ACKs
- Fast recovery: after the retransmission, enter congestion avoidance
6. TCP Congestion Control
The Purpose of Congestion Control
- Avoiding congestion: prevents the network from becoming congested
- Fairness: several connections share the bandwidth fairly
- Efficiency: makes full use of the network’s resources
Congestion Control Algorithms
Slow start:
- The initial cwnd is 1 MSS
- cwnd doubles on every ACK received
- Until ssthresh is reached
Congestion avoidance:
- Once cwnd reaches ssthresh it grows linearly
- One MSS is added per RTT
- Until congestion occurs
Fast retransmit and fast recovery:
- Retransmit immediately on three duplicate ACKs
- After the retransmission, enter congestion avoidance
- Do not enter slow start
Congestion Control Parameters
- cwnd: the congestion window size
- ssthresh: the slow start threshold
- RTT: the round-trip time
- MSS: the maximum segment size
7. TCP Applications
Web Applications
- HTTP: the Hypertext Transfer Protocol
- HTTPS: HTTP over TLS
- Characteristic: reliable transmission, suitable for transferring documents
File Transfer
- FTP: the File Transfer Protocol
- SFTP: the SSH File Transfer Protocol
- Characteristic: large transfers, with a strong reliability requirement
Mail Transfer
- SMTP: the Simple Mail Transfer Protocol
- POP3: the Post Office Protocol
- IMAP: the Internet Message Access Protocol
Remote Login
- SSH: the Secure Shell protocol
- Telnet: the remote login protocol
- Characteristic: interactive communication, with a strong reliability requirement
Exercise 1
How does TCP achieve reliable transmission?
Approach
List the mechanisms in the order in which they act on a segment: numbering, acknowledging, retransmitting, windowing, and finally the two control loops that keep the pipe from overrunning either end.
Steps
- Describe the sequence number mechanism.
- Describe the acknowledgement mechanism.
- Describe timeout and retransmission.
- Describe the sliding window.
- Describe flow control.
- Describe congestion control.
Answer
TCP achieves reliable transmission through the following mechanisms:
-
Sequence number mechanism:
- Every byte carries a unique sequence number
- It identifies the order of the segments
- It allows out-of-order segments to be reassembled
-
Acknowledgement mechanism:
- Cumulative acknowledgement: the acknowledgment number states the next byte expected
- Delayed acknowledgement: reduces network overhead
- Selective acknowledgement: acknowledges particular segments
-
Timeout and retransmission:
- The timeout is adjusted dynamically
- An exponential backoff algorithm is used
- A fast retransmit mechanism is available
-
Sliding window:
- The send window controls the sending rate
- The receive window controls the receiving capacity
- The window size is adjusted dynamically
-
Flow control:
- Stops the sender from going too fast
- Matches the sending and receiving rates
- Avoids overflowing the receiver’s buffer
-
Congestion control:
- The slow start algorithm
- The congestion avoidance algorithm
- Fast retransmit and fast recovery
Exercise 2
How do TCP flow control and TCP congestion control differ?
Approach
Treat the two as two control loops with different controlled objects: one protects the receiver, the other protects the network. Compare them across goal, object, mechanism, parameter, algorithm, scope, and trigger.
Steps
- Compare the control goal.
- Compare the controlled object.
- Compare the control mechanism.
- Compare the control parameter.
- Compare the control algorithm.
- Compare the scope of influence.
- Compare the trigger condition.
Answer
The main differences are:
-
Control goal:
- Flow control: prevents the receiver’s buffer from overflowing
- Congestion control: prevents the network from becoming congested
-
Controlled object:
- Flow control: end-to-end control
- Congestion control: control of the network as a whole
-
Control mechanism:
- Flow control: based on the receive window size
- Congestion control: based on the state of congestion in the network
-
Control parameter:
- Flow control: the window size advertised by the receiver
- Congestion control: the congestion window size
-
Control algorithm:
- Flow control: the sliding window mechanism
- Congestion control: algorithms such as slow start and congestion avoidance
-
Scope of influence:
- Flow control: affects a single connection
- Congestion control: affects the whole network
-
Trigger condition:
- Flow control: the receiver’s buffer is full
- Congestion control: network congestion, packet loss, and so on
| Term | Abbreviation | Explanation |
|---|---|---|
| Transmission Control Protocol | TCP | A connection-oriented, reliable transport protocol |
| Segment | - | The protocol data unit of TCP |
| Three-way handshake | - | The TCP connection establishment procedure |
| Four-way handshake | - | The TCP connection release procedure |
| Sequence number | - | The number assigned to a data byte |
| Acknowledgment number | - | The sequence number of the next byte expected |
| Sliding window | - | The flow control mechanism |
| Congestion window | cwnd | The sending window used by congestion control |
| Slow start | - | One of the congestion control algorithms |
| Congestion avoidance | - | One of the congestion control algorithms |
| Fast retransmit | - | Retransmission triggered by three duplicate ACKs |
| Fast recovery | - | The recovery algorithm that follows a fast retransmit |
| Round-trip time | RTT | The time from sending to receiving an acknowledgement |
| Retransmission timeout | RTO | The time threshold that triggers a retransmission |
| Flow control | - | Prevents the receiver from being overloaded |
| Congestion control | - | Prevents the network from being overloaded |
| Reliable transmission | - | Guarantees that data arrives correctly and in order |
| SYN | Synchronize | The flag that synchronises sequence numbers |
| ACK | Acknowledgment | The acknowledgement flag |
| FIN | Finish | The flag that ends the connection |
