Chapitre 7
Chapter 07: Transport - TCP, UDP et QUIC
Chapter 07: Transport - TCP, UDP et QUIC
Chapter 07: Transport - TCP, UDP et QUIC
1. Transport Layer Overview
The transport layer provides end-to-end communication services between applications. Two main protocols:
- TCP: Reliable, connection-oriented, ordered delivery
- UDP: Unreliable, connectionless, low overhead
Diagramme en cours de génération...
2. TCP (Transmission Control Protocol)
2.1 TCP Header
Diagramme en cours de génération...
TCP Flags:
| Flag | Bit | Meaning |
|---|---|---|
| NS | 0 | ECN-nonce concealment |
| CWR | 1 | Congestion Window Reduced |
| ECE | 2 | ECN Echo |
| URG | 3 | Urgent pointer |
| ACK | 4 | Acknowledgment |
| PSH | 5 | Push data |
| RST | 6 | Reset connection |
| SYN | 7 | Synchronize (connection establishment) |
| FIN | 8 | Finish (connection termination) |
2.2 TCP 3-Way Handshake
Diagramme en cours de génération...
Sequence numbers:
- Initial Sequence Number (ISN) is random
- SYN consumes one sequence number
- Each byte of data consumes a sequence number
- ACK = next expected sequence number
2.3 TCP Connection Termination
Diagramme en cours de génération...
2.4 TCP State Machine
Diagramme en cours de génération...
2.5 TCP Sliding Window
Diagramme en cours de génération...
Window Scaling (RFC 7323):
- Window field is 16 bits (max 65535 bytes)
- Scaling factor shifts the window value
- Scaling factor negotiated during handshake
2.6 TCP Congestion Control
Diagramme en cours de génération...
TCP Reno Phases:
- Slow Start: Exponential growth (cwnd doubles per RTT)
- Congestion Avoidance: Linear growth (cwnd += 1 MSS per RTT)
- Fast Retransmit: 3 duplicate ACKs → retransmit, cwnd = cwnd/2
- Fast Recovery: cwnd halved, then linear growth
Diagramme en cours de génération...
BBR (Bottleneck Bandwidth and Round-trip propagation time):
- Model-based (not loss-based)
- Probes for available bandwidth
- Much higher throughput than Cubic
- Google's algorithm, used in YouTube, QUIC
CUBIC:
- Default Linux TCP congestion control
- Cube function for window growth
- More aggressive than Reno after window reduction
- Better for high-BDP links
2.7 TCP Options
| Option | Kind | Description |
|---|---|---|
| EOL | 0 | End of options |
| NOP | 1 | No operation (padding) |
| MSS | 2 | Maximum Segment Size |
| WS | 3 | Window Scale |
| SACK Permitted | 4 | Selective ACK allowed |
| SACK | 5 | Selective ACK |
| TS | 8 | Timestamp |
| Fast Open | 34 | TCP Fast Open (TFO) |
TCP Fast Open (TFO):
- Send data in SYN packet (0-RTT handshake)
- Requires TFO cookie
- Reduces latency for repeat connections
Selective ACK (SACK):
- Acknowledge non-contiguous data
- More efficient than cumulative ACK
- Especially important for high-BDP links
2.8 TCP Timers
| Timer | Purpose | Value |
|---|---|---|
| Retransmission (RTO) | Timeout for unacked data | Based on RTT (usually 200ms-1s) |
| Persist | Avoid deadlock (zero window) | Increasing, up to 60s |
| Keepalive | Check if peer is alive | 2 hours + 9 probes × 75s |
| TIME_WAIT | 2MSL wait | 60s (typical) |
| Delayed ACK | Wait for data piggyback | 40-200ms |
3. UDP (User Datagram Protocol)
3.1 UDP Header
Diagramme en cours de génération...
Header size: 8 bytes (versus 20-60 for TCP)
3.2 UDP Characteristics
| Feature | UDP | TCP |
|---|---|---|
| Connection | Connectionless | Connection-oriented |
| Reliability | None (best effort) | Reliable (retransmission) |
| Ordering | No ordering | Ordered delivery |
| Flow control | None | Sliding window |
| Congestion control | None | Reno, Cubic, BBR |
| Header size | 8 bytes | 20-60 bytes |
| Overhead | Low | High |
| Use cases | DNS, VoIP, streaming | HTTP, email, file transfer |
3.3 UDP Use Cases
| Application | Port | Why UDP |
|---|---|---|
| DNS | 53 | Single query/response, fast |
| DHCP | 67/68 | Broadcast, no connection |
| NTP | 123 | Time sync, precise timing |
| VoIP (RTP) | 16384-32767 | Real-time, tolerate loss |
| Streaming | Dynamic | Real-time, loss tolerant |
| SNMP | 161/162 | Simple query/response |
| QUIC | 443 (via UDP) | Multiplexed, reliable over UDP |
| Syslog | 514 | Logging, loss tolerant |
4. QUIC (Quick UDP Internet Connections)
4.1 QUIC Overview
QUIC (RFC 9000) is a transport protocol developed by Google, used by HTTP/3.
Diagramme en cours de génération...
4.2 QUIC Features
| Feature | Description |
|---|---|
| 0-RTT | Send data immediately (repeat connections) |
| Multiplexing | Multiple streams, no HOL blocking |
| Connection migration | Survive IP address changes |
| Built-in encryption | TLS 1.3 mandatory |
| Monolithic | Combines transport + security |
| User-space | Implemented in applications, not kernel |
4.3 QUIC vs TCP
Diagramme en cours de génération...
4.4 Connection Migration
Diagramme en cours de génération...
5. SCTP (Stream Control Transmission Protocol)
Key features:
- Multi-homing (multiple IPs per connection)
- Multi-streaming (multiple streams in one connection)
- Ordered and unordered delivery
- Built-in heartbeat
Used primarily in telecom (SS7 over IP) and WebRTC (data channels).
6. Socket Programming
6.1 TCP Socket Flow
Diagramme en cours de génération...
6.2 Python TCP Example
Server:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('0.0.0.0', 8888))
s.listen(5)
while True:
conn, addr = s.accept()
data = conn.recv(1024)
conn.send(data) # echo
conn.close()
Client:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 8888))
s.send(b'Hello')
data = s.recv(1024)
s.close()
7. Summary
| Feature | TCP | UDP | QUIC |
|---|---|---|---|
| Transport | TCP | UDP | QUIC over UDP |
| Reliability | Yes | No | Yes |
| Encryption | Optional (TLS) | No (DTLS) | Mandatory (TLS 1.3) |
| Multiplexing | No (HTTP/2 fixes) | No | Yes |
| 0-RTT | No (TFO partial) | N/A | Yes |
| Connection migration | No | N/A | Yes |
| Head-of-line blocking | Yes (TCP) | No | No |
| Kernel | Yes | Yes | User-space |
8. Exercises
Exercise 1: TCP Handshake
Given a TCP connection with ISN=1000 (client) and ISN=5000 (server), write the sequence/ack numbers for each step of the 3-way handshake.
Exercise 2: Window Calculation
A TCP connection has Bandwidth=10 Gbps, RTT=50 ms. What is the minimum window size needed to fill the pipe? What window scale factor is needed?
Exercise 3: QUIC vs TCP
List 3 advantages of QUIC over TCP.
Exercise 4: UDP Analysis
An application sends 100-byte messages every 10ms. Compare the overhead (header proportion) between TCP and UDP.
Exercise 5: Congestion Control
Describe how TCP Reno responds to a single packet loss in congestion avoidance.