Today s Lecture EE 122 TCP Connection Setup Reliability Ion Stoica TAs Junda Liu DK Moon David Zats http inst eecs berkeley edu ee122 Materials with thanks to Vern Paxson Jennifer Rexford and colleagues at UC Berkeley 2 TCP Support for Reliable Delivery Reliable in order byte stream delivery and with good performance drop packets delay packets deliver packets out of order replicate packets Checksum Challenges the network can We ll only look at timeout based retransmission today 1 TCP Service Model How does TCP achieve correct operation Reliability in the face of IP s best effort service 3 way handshake to establish connections 3 way or 4 way handshake to terminate connections Retransmission to recover from loss Sequence numbers Weird but it does sometimes happen 3 Used to detect missing data and for putting the data back in order Retransmission corrupt packets Used to detect corrupted data at the receiver leading the receiver to drop the packet Sender retransmits lost or corrupted data Timeout based on estimates of round trip time Fast retransmit algorithm for rapid retransmission 4 1 TCP Header TCP Header Source port Destination port Source port These should be familiar Sequence number Acknowledgment HdrLen 0 Flags Advertised window Checksum Sequence number Acknowledgment HdrLen 0 Urgent pointer Destination port Flags Advertised window Checksum Options variable Urgent pointer Options variable Data Data 5 TCP Header Starting sequence number byte offset of data carried in this segment 6 TCP Header Source port Destination port Source port Sequence number Acknowledgment gives seq just beyond highest seq received in order Acknowledgment HdrLen 0 Flags Advertised window Checksum Urgent pointer E g if sender sends N in order bytes starting at seq S then ack for it will be S N Options variable Data 7 Destination port Sequence number Acknowledgment HdrLen 0 Flags Advertised window Checksum Urgent pointer Options variable Data 8 2 TCP Header TCP Header Source port Destination port Source port Sequence number Number of 4 byte words in TCP header 5 no options Sequence number Must Be Zero 6 bits reserved Acknowledgment HdrLen 0 Flags Advertised window Checksum Destination port Acknowledgment HdrLen 0 Urgent pointer Flags Advertised window Checksum Options variable Urgent pointer Options variable Data Data 9 TCP Header 10 TCP Header Source port Destination port Source port Sequence number We will get to these shortly Sequence number Buffer space available for receiving data Used for TCP s sliding window Acknowledgment HdrLen 0 Flags Advertised window Checksum Destination port Urgent pointer Options variable Interpreted as offset beyond Acknowledgment field s value Data 11 Acknowledgment HdrLen 0 Flags Advertised window Checksum Urgent pointer Options variable Data 12 3 TCP Header TCP Stream of Bytes Service Host A Destination port Sequence number Used with URG flag to indicate urgent data not discussed further Byte 80 Byte 3 Byte 2 Byte 1 Byte 0 Source port Acknowledgment HdrLen 0 Flags Advertised window Checksum Urgent pointer Options variable Host B Byte 80 Byte 3 Byte 2 Byte 1 Byte 0 Data 13 Provided Using TCP Segments 14 TCP Segment IP Data Host A TCP Data segment Byte 80 Byte 3 Byte 2 Byte 1 Byte 0 TCP Data Host B Segment sent when Byte 80 Byte 3 Byte 2 Byte 1 Byte 0 IP packet with a TCP header and data inside TCP header 20 bytes long IP Header 20 bytes TCP segment 15 No bigger than Maximum Transmission Unit MTU E g up to 1 500 bytes on an Ethernet TCP packet TCP Data IP Hdr IP packet 1 Segment full Max Segment Size 2 Not full but times out or 3 Pushed by application TCP Hdr No more than Maximum Segment Size MSS bytes E g up to 1460 consecutive bytes from the stream 16 4 Sequence Numbers Initial Sequence Number ISN Host A ISN initial sequence number Sequence number for the very first byte Sequence number 1st byte Host B TCP Data TCP HDR TCP Data ACK sequence number next expected byte TCP HDR IP addresses and port s uniquely identify a connection Eventually though these port s do get used again there is a chance an old packet is still in flight and might be associated with new connection TCP requires RFC793 changing ISN over time E g Why not just use ISN 0 Practical issue Set from 32 bit clock that ticks every 4 microseconds only wraps around once every 4 55 hours To establish a connection hosts exchange ISNs 17 18 Timing Diagram 3 Way Handshaking Passive Open Active Open Connection Establishment TCP s Three Way Handshake Server listen Client initiator connect SYN Seq Num x A eqNum y K S SYN AC ACK Ack ck x 1 y 1 accept 19 20 5 What if the SYN Packet Gets Lost TCP Header Source port Flags SYN ACK FIN RST PSH URG Destination port Sequence number Acknowledgment HdrLen 0 Flags Advertised window Checksum Suppose the SYN packet gets lost Eventually no SYN ACK arrives Urgent pointer Options variable See usr include netinet tcp h on Unix Systems Sender sets a timer and waits for the SYN ACK and retransmits the SYN if needed How should the TCP sender set the timer Data Packet is lost inside the network or Server discards the packet e g listen queue is full Sender has no idea how far away the receiver is Hard to guess a reasonable length of time to wait SHOULD RFCs 1122 2988 use default of 3 seconds 21 Other implementations instead use 6 seconds 22 Close Connection Two Army Problem Goal both sides agree to close the connection Two army problem Tearing Down Close the Connection 23 Two blue armies need to simultaneously attack the white army to win otherwise they will be defeated The blue army can communicate only across the area controlled by the white army which can intercept the messengers What is the solution 24 6 Normal Termination Both Together Can retransmit FIN ACK if lost FIN Data ACK SYN FIN ACK Data SYN ACK Timeout Avoid reincarnation ACK Until B likewise sends a FIN Which A then acks ACK time Timeout Avoid reincarnation Connection now closed Can retransmit FIN ACK if lost Connection now half closed Other host ack s the byte to confirm Closes A s side of the connection but not B s CK FIN occupies one byte in the sequence space SYN A FIN A time Finish FIN to close and receive remaining bytes A CK CK ACK SYN A A FIN B B ACK Normal Termination One Side A Connection now closed Same as before but B sets FIN with their ack of A s FIN 25 26 Abrupt Termination RST RST Data A CK Data Reliability TCP Retransmission time A sends a RESET RST to B A CK CK A SYN A SYN B E g because app process on A
View Full Document