Transport Layer TCP and UDP IS250 Spring 2010 chuang@ischool.berkeley.edu Transport Layer Functions 1. 2. 3. 4. 5. Addressing (ports) Data integrity (error detection) Reliable data transport Flow control Congestion control John Chuang 2 TCP Transmission Rate Q: Why limit ourselves to send only four packets at a time? Q: how much data can be sent before ACK received? John Chuang 3 Flow Control Queue Producer Stream of messages Consumer Flow control (“Slow down please!”) Note: We don’t want packet to travel the entire network only to be dropped at the destination. John Chuang 4 Flow Control: Two Approaches Stop-and-go John Chuang Sliding Window 5 Sliding Window Recipient explicitly requests lower send rate by specifying window size (or MaxUnackedPackets) Source stops sending when number of unacknowledged data equal to window size window_size acknowledged John Chuang sent can be sent outside window 6 TCP Header: Flow Control 0 16 TCP Header Source Port Number (16) 31 Destination Port Number (16) Sequence Number (32) Acknowledgement Number (32) Hdr Len Reserved (6) Flags (6) (4) TCP Checksum (16) Window Size (16) Urgent Pointer (16) Options (if any) Padding Data … John Chuang 7 TCP Flow Control John Chuang 8 Throughput as Function of Window Size Sender Receiver Time Throughput = John Chuang Window Size Roundtrip Time 9 Transport Layer Functions 1. 2. 3. 4. 5. Addressing (ports) Data integrity (error detection) Reliable data transport Flow control Congestion control John Chuang 10 Network Congestion If link is congested - Router queue fills up - Drops packets Source does not receive ACK - Resends packets - Makes congestion worse John Chuang 11 TCP Congestion Control Use packet drop as indicator of congestion Do not send all data to receiver at once Voluntary source-imposed policy (RFC 2581) - slow start (SS) congestion avoidance (CA) fast retransmission fast recovery TCP Tahoe: SS + CA + fast retransmission TCP Reno: all four Other variants: TCP SACK, TCP Vegas, TCP Westwood, … John Chuang 12 TCP Congestion + Flow Control window_size acknowledged sent can be sent outside window Control transmission rate by setting window size Window size set to be smaller of: - rwnd: receiver window (flow control) - cwnd: congestion window (congestion control) win = min(rwnd, cwnd) rwnd set by receiver Question: how does sender set cwnd? John Chuang 13 Congestion Window Size cwnd Congestion Avoidance Slow Start Time TCP congestion control is an algorithm for sender to adaptively adjust window size At steady state, cwnd oscillates around the optimal window size 14 Slow Start Whenever starting traffic on a new connection, or whenever increasing traffic after congestion was experienced: - Set cwnd =1 (one segment) - Each time a segment is acknowledged increment cwnd by one (cwnd++). Does Slow Start increment slowly? Not really. In fact, the increase of cwnd is exponential John Chuang 15 Slow Start Example The congestion window size grows very rapidly cwnd = 1 segment 1 ACK for segmen cwnd = 2 segment 2 segment 3 ACK for segmen TCP slows down the increase of cwnd when cwnd >= ssthresh John Chuang cwnd = 4 t1 ts 2 + 3 segment 4 segment 5 segment 6 segment 7 ACK for segmen ts 4+5+6+7 cwnd = 8 16 Congestion Avoidance Slows down “Slow Start” - If cwnd > ssthresh then each time a segment is acknowledged increment cwnd by 1/cwnd (cwnd += 1/cwnd). So cwnd is increased by one only if all segments have been acknowledged. (We will learn later how to set ssthresh) John Chuang 17 Slow Start/Congestion Avoidance Example Assume that ssthresh = 8 14 cw n d = 1 cw n d = 2 cw n d = 4 10 cw n d = 8 8 6 ssthresh 4 2 cw n d = 9 6 t= 4 t= 2 t= 0 0 t= Cwnd (in segments) 12 Roundtrip times cw n d = 1 0 John Chuang 18 TCP Congestion Control Pseudocode Initially: cwnd = 1; ssthresh = infinite; New ack received: if (cwnd < ssthresh) /* Slow Start*/ cwnd = cwnd + 1; else /*Congestion Avoidance*/ cwnd = cwnd + 1/cwnd; Timeout: /* Multiplicative decrease */ ssthresh = 0.5 * win; cwnd = 1; win = min(cwnd, rwnd); while (next < unack + win) transmit next packet; SEQ # unack next win 19 The big picture cwnd Timeout Congestion Avoidance Slow Start 1 Time 20 Cumulative and Duplicate ACKs TCP uses cumulative ACK ACK N means all bytes up to N-1 have been received segment 1 cw n d = 1 ACK 2 cw n d = 2 t ACK for segmen 1 segment 2 segment 3 ACK 4 cw n d = 4 ACK 3 segment 4 segment 5 Duplicate ACKs may be due to - packets reordering - lost packet John Chuang segment 6 segment 7 cwnd = 2 cw n d = 8 ACK 4 ACK 4 ACK 4 ts 4+5+6+7 ACK for segmen 21 Fast Retransmit/Fast Recovery cwnd Congestion Avoidance Slow Start 1 Retransmit after 3 duplicated ACKs Time - Don’t want for timeout No need to slow start again - halve cwnd At steady state, cwnd oscillates around the optimal window size. 22 TCP Congestion Control Shortcomings “Fairness criterion” - Is “equal division” of resources always desirable? Estimating congestion by retransmission is flawed for wireless links Depends on accurate implementation -cheating possible Application can avoid congestion control by using UDP John Chuang 23