Salta al contenuto
Note per Studenti TCP - Connection, Reliability and Flow Control Report

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:

  1. connection set-up: three-way handshake;
  2. data transfer: a byte stream, as if a dedicated circuit existed;
  3. 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

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 [0,232−1][0,2^{32}-1].
  • Acknowledgment number (AN): the next byte expected; cumulative: AN =5643=5643 means all bytes up to 56425642 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 216−1=65 5352^{16}-1=65\,535; decided by the receiver, obeyed by the sender.
  • Checksum: as UDP but mandatory, with pseudo-header (protocol =6=6).
  • 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. MSS=MTU−IP header−TCP header=MTU−40\text{MSS}=\text{MTU}-\text{IP header}-\text{TCP header}=\text{MTU}-40 (20-byte headers).

Ethernet: MSS=1500−40=1460\text{MSS}=1500-40=1460 bytes; MTU 512 gives 472. A full segment carries 14601460 data bytes in a 15001500-byte datagram: 1460/1500=97.3%1460/1500=97.3\% useful.

Sequence and acknowledgment numbers

Example. File of 5000 bytes, ISN =10 001=10\,001, segments of 1000 bytes: segment kk has SN =ISN+(k−1)⋅1000=\text{ISN}+(k-1)\cdot1000, i.e. 10 00110\,001, 11 00111\,001, 12 00112\,001, 13 00113\,001, 14 00114\,001. After receiving segment 3 in full the receiver answers AN =13 001=13\,001 (last byte 12 001+1000−1=13 00012\,001+1000-1=13\,000, next expected 13 00113\,001).

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 80008000 and server ISN 15 00015\,000:

  1. SYN: client sends SYN, SN =8000=8000.
  2. SYN + ACK: server sends SN =15 000=15\,000 (its own ISN: full duplex, each direction has its own), AN =8001=8001, rwnd =5000=5000.
  3. ACK: client sends SN =8001=8001, AN =15 001=15\,001, rwnd =10 000=10\,000. A plain ACK consumes no number; it may piggyback the first data.

Timing. With negligible transmission times and propagation τ\tau: with piggyback the client can start data after 2τ2\tau; without piggyback the third segment must first reach the server: 3τ3\tau (with τ=10\tau=10 ms: 20 or 30 ms).

Data exchange example. Client sends bytes 80018001-90009000 and 90019001-10 00010\,000 (AN 15 00115\,001); server sends 15 00115\,001-17 00017\,000 with AN 10 00110\,001; client replies ACK with AN 17 00117\,001.

Closing

Three-way: client FIN (SN =x=x), server FIN+ACK (SN =y=y, AN =x+1=x+1), client final ACK (AN =y+1=y+1). 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. swnd=min⁡(rwnd,cwnd)\text{swnd}=\min(\text{rwnd},\text{cwnd}); a new byte can be sent if last sent−last acknowledged≤swnd\text{last sent}-\text{last acknowledged}\le\text{swnd}. rwnd depends on the receiver, cwnd on the network.

Example: rwnd =6=6 MSS and cwnd =4=4 MSS give swnd =4=4 MSS; when cwnd reaches 7, swnd =6=6.

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. rwnd=buffer size−bytes waiting to be read by the process\text{rwnd}=\text{buffer size}-\text{bytes waiting to be read by the process}.

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 =101=101, 200 bytes; AN =301=301 600 301 - 900
2 sender sends SN =301=301, 300 bytes; AN =601=601 300 601 - 900
3 application reads 100 bytes; AN =601=601 400 601 - 1000
4 application reads 200 more; AN =601=601 600 601 - 1200

Deadlock and persist timer. With rwnd =0=0 the sender stops; the later ACK with rwnd >0>0 could be lost, blocking both sides. When rwnd =0=0 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: 0.5⋅2n0.5\cdot2^n s, i.e. 0.5,1,2,4,8,16,32,600.5,1,2,4,8,16,32,60 s.

Small segments and Nagle. One byte of data in a segment gives 20+20+1=4120+20+1=41 bytes, efficiency 1/41=2.4%1/41=2.4\%. 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 00 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 501501-900900; segment 3 (701701-800800) is lost. Receiver ACKs 701701 after the first two; the out-of-order 801801-900900 produces one more ACK 701701 (dupACK). One dupACK is not enough: after the timeout 701701-800800 is resent and the receiver replies ACK 901901.

Example (fast retransmit). Six segments of 100 bytes (101101-700700); the third (301301-400400) is lost.

arrives ACK sent note
101101-200200 201201
201201-300300 301301
401401-500500 301301 1st duplicate
501501-600600 301301 2nd duplicate
601601-700700 301301 3rd duplicate: fast retransmit
301301-400400 (resent) 701701 everything received

ACK 701701 confirms bytes 301301-700700 at once (cumulative ACK).

Delayed ACKs. TCP normally delays the ACK hoping to piggyback it; one ACK every bb data packets (usually b=2b=2) or on a short timer. bb 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 tx=L/Ct_x=L/C: W tx≥RTTW\,t_x\ge\text{RTT}, i.e. W⋅L≥C⋅RTT.W\cdot L\ge C\cdot\text{RTT}.

Definition (BDP). Bandwidth-delay product BDP=C⋅RTT\text{BDP}=C\cdot\text{RTT}, the bits in flight; the ideal window is W⋅L=BDPW\cdot L=\text{BDP}.

If W⋅L<BDPW\cdot L<\text{BDP} bandwidth is wasted; if larger, packets queue in routers, RTT grows and losses may follow.

Example. C=10C=10 Mbit/s, L=12 000L=12\,000 bits, τ=5\tau=5 ms per hop: tx=1.2t_x=1.2 ms, RTT=tx+2τ=11.2\text{RTT}=t_x+2\tau=11.2 ms; W≥11.2/1.2=9.33W\ge11.2/1.2=9.33, so W=10W=10 segments.

Formula (maximum throughput). throughput≤MSS⋅Wmax⁡RTT.\text{throughput}\le\frac{\text{MSS}\cdot W_{\max}}{\text{RTT}}.

Example. 100100 Mbit/s, RTT =40=40 ms, MSS =1460=1460 bytes: BDP=108⋅0.04=4⋅106\text{BDP}=10^8\cdot0.04=4\cdot10^6 bits =500=500 kB ≈343\approx343 segments needed in flight. With the 16-bit window (65 53565\,535 bytes, no scaling) throughput is at most 65 535⋅8/0.04=13.165\,535\cdot8/0.04=13.1 Mbit/s, only 13%13\% 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 65 53565\,535), checksum (mandatory, pseudo-header protocol 6), urgent pointer, options (MSS, SACK, timestamp).
  • MSS=MTU−40\text{MSS}=\text{MTU}-40 (Ethernet 14601460).
  • Plain ACK uses no sequence numbers; SYN and FIN use one.

Handshake and closing

  • SYN (SN=8000\text{SN}=8000) →\to SYN+ACK (SN=15 000\text{SN}=15\,000, AN=8001\text{AN}=8001) →\to ACK (SN=8001\text{SN}=8001, AN=15 001\text{AN}=15\,001). Data can start after 2τ2\tau with piggyback, 3τ3\tau without.
  • Close: FIN, FIN+ACK, ACK (or four-way with half-close).

Flow control

swnd=min⁡(rwnd,cwnd)\text{swnd}=\min(\text{rwnd},\text{cwnd}); rwnd=buffer−bytes not yet read\text{rwnd}=\text{buffer}-\text{bytes not yet read}.

  • Window closes as data arrives unread, opens when the application reads. Example buffer 800: rwnd 800→600→300→400→600800\to600\to300\to400\to600.
  • rwnd =0=0: 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

BDP and throughput

W⋅L≥C⋅RTTW\cdot L\ge C\cdot\text{RTT} (BDP). throughput≤MSS⋅Wmax⁡/RTT\text{throughput}\le\text{MSS}\cdot W_{\max}/\text{RTT}.

  • 1010 Mbit/s, L=12 000L=12\,000 bits, RTT 11.211.2 ms: W=10W=10. 100100 Mbit/s, RTT 4040 ms: BDP 500500 kB =343=343 segments; a 65 53565\,535-byte window gives 13.113.1 Mbit/s (13%13\%).
  • 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