TCP - Connection, Reliability and Flow Control
In questa pagina 9
In questa pagina 5
TCP is a connection-oriented, reliable transport protocol. Compared with UDP (UDP - User Datagram ProtocolUDP is the connectionless, unreliable transport protocol that adds to IP only process-to-process communication through ports and an optional checksum; its header is 8 bytes (source port, destination port, length, checksum), the checksum covers a pseudo-header (IP addresses, protocol 17, length), the UDP header and the data as a one's complement sum (0 means not computed and a computed 0 is sent as 0xFFFF), and UDP has no connection setup, sequence numbers, flow, error or congestion control, which makes it suitable for short messages (DNS, DHCP, RIP, SNMP) and real-time applications.UDP - User Datagram Protocol →) it adds three explicit phases and reliability mechanisms:
- connection set-up: three-way handshake;
- data transfer: a byte stream, as if a dedicated circuit existed;
- connection release: no network resources are freed, only state in the end hosts.
For reliability TCP combines ideas from Go-Back-N and Selective Repeat: checksum, byte/sequence/acknowledgment numbers (retransmit lost or corrupted packets, reorder out-of-order ones), cumulative and selective acknowledgments. Congestion control: TCP Congestion ControlCongestion arises when fast links feed a slow one, router queues fill and packets are lost or delayed, possibly up to network collapse; TCP controls its congestion window cwnd using loss feedback (timeout or three duplicate ACKs): slow start doubles cwnd every RTT up to the threshold ssthresh, then congestion avoidance adds 1 MSS per RTT, and on a loss ssthresh becomes W/2; variants differ in reaction to three dupACKs: Tahoe restarts from cwnd=1, Reno uses fast recovery (ssthresh=cwnd/2, cwnd=ssthresh+3, +1 per further dupACK), NewReno handles partial ACKs and recovers several losses in one window, SACK acknowledges received blocks and resends only what is missing.TCP Congestion Control →; timeout estimation: TCP Retransmission Timeout EstimationTCP retransmits a segment when its ACK does not arrive within the retransmission timeout RTO, which must track the round-trip time: too short causes spurious retransmissions, too long slows recovery; the RTT is measured with one timer per connection, smoothed with an exponential moving average SRTT = (1-alpha) SRTT + alpha rtt with alpha = 1/8, the mean deviation MAD = (1-rho) MAD + rho |rtt - SRTT| with rho = 1/4 is added four times, RTO = SRTT + 4 MAD with a minimum of 1 s, Karn's algorithm ignores samples of retransmitted segments and doubles the RTO at each consecutive timeout up to 64 times, and for a Gaussian RTT the mean deviation equals sigma times the square root of 2 over pi, about 0.797 sigma.TCP Retransmission Timeout Estimation →.
Services
- Process-to-process with sockets and ports (Transport Layer - Ports and MultiplexingThe transport layer provides logical end-to-end communication between application processes on different hosts and exists only in end hosts; the network layer delivers to the right computer (IP address) and transport to the right process (16-bit port number, 0 to 65535); an IP address plus a port is a socket and the four-tuple of IPs and ports identifies a connection, with multiplexing and demultiplexing done by UDP on the destination socket and by TCP on the whole four-tuple, and the protocols are UDP (connectionless, unreliable), TCP (connection-oriented, reliable) and SCTP.Transport Layer - Ports and Multiplexing →). Full-duplex: each end has a send and a receive buffer. One connection per pair of processes (four-tuple).
- Connection-oriented but the connection is logical: segments travel in IP datagrams that may be lost, corrupted, reordered, take different paths.
- Byte stream: no message boundaries.
The segment
32-bit words; 20 fixed bytes plus up to 40 bytes of options.
| word | fields |
|---|---|
| 1 | source port (16) - destination port (16) |
| 2 | sequence number (32) |
| 3 | acknowledgment number (32) |
| 4 | HLEN (4) - reserved (6) - flags (6) - window (16) |
| 5 | checksum (16) - urgent pointer (16) |
| 6... | options and padding (up to 40 bytes) |
- Sequence number (SN): number of the first data byte in the segment. At opening each side picks a random initial sequence number (ISN) in .
- Acknowledgment number (AN): the next byte expected; cumulative: AN means all bytes up to were received. ACK and data can share a segment (piggybacking).
- HLEN: in 4-byte words, between 5 (20 bytes) and 15 (60 bytes).
- Flags: URG, ACK, PSH, RST (reset), SYN (synchronise sequence numbers), FIN (sender finished); ECN adds CWR and ECE.
- Window: free receiver buffer space in bytes (rwnd), at most ; decided by the receiver, obeyed by the sender.
- Checksum: as UDP but mandatory, with pseudo-header (protocol ).
- Urgent pointer: valid only with URG. Options: end of option, no operation, MSS, SACK, timestamp.
MSS
The MSS (maximum segment size) option gives the maximum TCP data per segment (default 536, maximum 65 535). To avoid IP fragmentation it should be the largest datagram the host can handle minus the headers.
Formula. (20-byte headers).
Ethernet: bytes; MTU 512 gives 472. A full segment carries data bytes in a -byte datagram: useful.
Sequence and acknowledgment numbers
Example. File of 5000 bytes, ISN , segments of 1000 bytes: segment has SN , i.e. , , , , . After receiving segment 3 in full the receiver answers AN (last byte , next expected ).
Property. A segment without data (a plain ACK) consumes no sequence numbers; SYN and FIN consume one each, since they must be acknowledged.
Opening: three-way handshake
Example with client ISN and server ISN :
- SYN: client sends SYN, SN .
- SYN + ACK: server sends SN (its own ISN: full duplex, each direction has its own), AN , rwnd .
- ACK: client sends SN , AN , rwnd . A plain ACK consumes no number; it may piggyback the first data.
Timing. With negligible transmission times and propagation : with piggyback the client can start data after ; without piggyback the third segment must first reach the server: (with ms: 20 or 30 ms).
Data exchange example. Client sends bytes - and - (AN ); server sends - with AN ; client replies ACK with AN .
Closing
Three-way: client FIN (SN ), server FIN+ACK (SN , AN ), client final ACK (AN ). Four-way with half-close: client FIN, server only ACK and keeps sending data while the client sends only ACKs; when done the server sends its FIN and the client ACKs and closes.
Windows
Send window. Like Selective Repeat but in bytes; the sender can send segments as soon as the process delivers them; TCP uses one timer. Regions: acknowledged bytes, bytes in flight (from the first unacknowledged byte), usable window, bytes outside.
Formula. ; a new byte can be sent if . rwnd depends on the receiver, cwnd on the network.
Example: rwnd MSS and cwnd MSS give swnd MSS; when cwnd reaches 7, swnd .
Receive window. The process reads data at its own pace, so part of the buffer holds bytes already acknowledged but not yet read; acknowledgments are cumulative.
Formula. .
Flow control
The receive window closes when bytes arrive that the application does not consume and opens when it reads. The send window closes if an ACK shows smaller rwnd, opens if rwnd grows, and should never shrink from the right edge.
Example. Receiver buffer 800 bytes, bytes numbered from 101.
| step | event | rwnd | sender may send |
|---|---|---|---|
| 0 | start | 800 | 101 - 900 |
| 1 | sender sends SN , 200 bytes; AN | 600 | 301 - 900 |
| 2 | sender sends SN , 300 bytes; AN | 300 | 601 - 900 |
| 3 | application reads 100 bytes; AN | 400 | 601 - 1000 |
| 4 | application reads 200 more; AN | 600 | 601 - 1200 |
Deadlock and persist timer. With rwnd the sender stops; the later ACK with rwnd could be lost, blocking both sides. When rwnd the sender starts a persist timer (initially 500 ms); on expiry it sends a 1-byte probe, which makes the receiver repeat its next expected byte and window. Each failure doubles the timer, up to 60 s: s, i.e. s.
Small segments and Nagle. One byte of data in a segment gives bytes, efficiency . Nagle's algorithm: after sending the first segment, the sender accumulates data and waits either for the ACK or for a full MSS before sending.
Silly window syndrome. The opposite problem, with a slow receiver announcing tiny windows. Remedies on the receiver: delayed ACK (acknowledge only when decent buffer space exists) and Clark's algorithm (ACK immediately but advertise window until at least one MSS or half the buffer is free).
Error control
The application expects the stream delivered completely, in order, without errors, losses and duplicates. TCP uses checksum, acknowledgment and timeout, plus retransmission. Out-of-order segments are kept, and the missing byte is acknowledged again.
Retransmission on RTO. One timer per connection; when it expires the segment at the head of the queue (lowest SN) is resent and the timer restarted. The RTO is dynamic: TCP Retransmission Timeout EstimationTCP retransmits a segment when its ACK does not arrive within the retransmission timeout RTO, which must track the round-trip time: too short causes spurious retransmissions, too long slows recovery; the RTT is measured with one timer per connection, smoothed with an exponential moving average SRTT = (1-alpha) SRTT + alpha rtt with alpha = 1/8, the mean deviation MAD = (1-rho) MAD + rho |rtt - SRTT| with rho = 1/4 is added four times, RTO = SRTT + 4 MAD with a minimum of 1 s, Karn's algorithm ignores samples of retransmitted segments and doubles the RTO at each consecutive timeout up to 64 times, and for a Gaussian RTT the mean deviation equals sigma times the square root of 2 over pi, about 0.797 sigma.TCP Retransmission Timeout Estimation →.
Fast retransmit. The receiver sends a duplicate ACK for every out-of-order segment. Three duplicate ACKs (the original plus three copies) strongly suggest a loss: the segment indicated by the ACK is resent immediately.
Example (timeout). Segments of 100 bytes cover -; segment 3 (-) is lost. Receiver ACKs after the first two; the out-of-order - produces one more ACK (dupACK). One dupACK is not enough: after the timeout - is resent and the receiver replies ACK .
Example (fast retransmit). Six segments of 100 bytes (-); the third (-) is lost.
| arrives | ACK sent | note |
|---|---|---|
| - | ||
| - | ||
| - | 1st duplicate | |
| - | 2nd duplicate | |
| - | 3rd duplicate: fast retransmit | |
| - (resent) | everything received |
ACK confirms bytes - at once (cumulative ACK).
Delayed ACKs. TCP normally delays the ACK hoping to piggyback it; one ACK every data packets (usually ) or on a short timer. enters the send-rate model (Analytical Model of TCP Send RateThe analytical model gives the long-term send rate B (segments per second) of a TCP Reno flow as a function of the loss probability p, the RTT, the delayed-ACK parameter b and the timeout T0; time is divided into rounds of one RTT, the window cycle between two losses signalled by three duplicate ACKs (TDP) has mean window E[W] = (2-3b)/(3b) + sqrt(((3b-2)/(3b))^2 + 8(1-p)/(3bp)) and the rate is B = E[Y]/E[A], which for small p gives the square-root formula B = (1/RTT) sqrt(3/(2bp)) (about 1.22/(RTT sqrt p) for b=1 and 0.87/(RTT sqrt p) for b=2); timeouts add the probability Q that a loss ends in timeout, giving B = (E[Y] + Q E[R]) / (E[A] + Q E[Z_TO]), and a maximum window Wmax caps the rate at Wmax/RTT.Analytical Model of TCP Send Rate →).
Window, BDP and maximum throughput
The channel is used continuously if the window lets the sender transmit until the first ACK returns. With segment transmission time : , i.e.
Definition (BDP). Bandwidth-delay product , the bits in flight; the ideal window is .
If bandwidth is wasted; if larger, packets queue in routers, RTT grows and losses may follow.
Example. Mbit/s, bits, ms per hop: ms, ms; , so segments.
Formula (maximum throughput).
Example. Mbit/s, RTT ms, MSS bytes: bits kB segments needed in flight. With the 16-bit window ( bytes, no scaling) throughput is at most Mbit/s, only of the channel.
Versione ripasso
TCP: connection-oriented, reliable byte stream, full duplex, one connection per four-tuple (Transport Layer - Ports and MultiplexingThe transport layer provides logical end-to-end communication between application processes on different hosts and exists only in end hosts; the network layer delivers to the right computer (IP address) and transport to the right process (16-bit port number, 0 to 65535); an IP address plus a port is a socket and the four-tuple of IPs and ports identifies a connection, with multiplexing and demultiplexing done by UDP on the destination socket and by TCP on the whole four-tuple, and the protocols are UDP (connectionless, unreliable), TCP (connection-oriented, reliable) and SCTP.Transport Layer - Ports and Multiplexing →).
Segment
- Header 20-60 bytes: ports, SN (first data byte; random ISN), AN (next byte expected, cumulative), HLEN (words, 5-15), flags (URG, ACK, PSH, RST, SYN, FIN), window (16 bit, max ), checksum (mandatory, pseudo-header protocol 6), urgent pointer, options (MSS, SACK, timestamp).
- (Ethernet ).
- Plain ACK uses no sequence numbers; SYN and FIN use one.
Handshake and closing
- SYN () SYN+ACK (, ) ACK (, ). Data can start after with piggyback, without.
- Close: FIN, FIN+ACK, ACK (or four-way with half-close).
Flow control
; .
- Window closes as data arrives unread, opens when the application reads. Example buffer 800: rwnd .
- rwnd : persist timer 500 ms, 1-byte probes, doubling up to 60 s.
- Nagle: accumulate until ACK or full MSS. Silly window: delayed ACK or Clark (advertise 0 until one MSS or half buffer free).
Error control
- Checksum, ACK, RTO (TCP Retransmission Timeout EstimationTCP retransmits a segment when its ACK does not arrive within the retransmission timeout RTO, which must track the round-trip time: too short causes spurious retransmissions, too long slows recovery; the RTT is measured with one timer per connection, smoothed with an exponential moving average SRTT = (1-alpha) SRTT + alpha rtt with alpha = 1/8, the mean deviation MAD = (1-rho) MAD + rho |rtt - SRTT| with rho = 1/4 is added four times, RTO = SRTT + 4 MAD with a minimum of 1 s, Karn's algorithm ignores samples of retransmitted segments and doubles the RTO at each consecutive timeout up to 64 times, and for a Gaussian RTT the mean deviation equals sigma times the square root of 2 over pi, about 0.797 sigma.TCP Retransmission Timeout Estimation →), fast retransmit after 3 duplicate ACKs. Out-of-order segments are kept. Example: segment - lost: ACK repeated, resend, ACK .
- Delayed ACK: one per packets.
BDP and throughput
(BDP). .
- Mbit/s, bits, RTT ms: . Mbit/s, RTT ms: BDP kB segments; a -byte window gives Mbit/s ().
- Typical errors: AN is the next byte, not the last received; forgetting SYN/FIN consume a number; confusing rwnd (receiver) with cwnd (network).
Teoria collegata
- Client-Server Programming
- Internet Structure, Packet Switching and the TCP-IP Stack
- Analytical Model of TCP Send Rate
- E-mail - SMTP, POP3, IMAP and MIME
- Firewalls
- HTTP and the Web
- IPv4 Datagram and Fragmentation
- Public-Key Cryptography, RSA and TLS
- TCP Congestion Control
- TCP Retransmission Timeout Estimation
- Transport Layer - Ports and Multiplexing