Transmission Control Protocol: TCP Transmission Control Protocol: TCP Wolfgang Schreiner Engineering for Computer-based Learning Polytechnic University Hagenberg Wolfgang.Schreiner@fhs-hagenberg.ac.at http://cbl.fhs.hagenberg.ac.at/˜schreine Wolfgang Schreiner FH Hagenberg — CBL Transmission Control Protocol: TCP Transmission Control Protocol: TCP • IP provides a connectionless packet delivery service. – Unreliable packet delivery to specific host. – UDP adds delivery to multiple ports within a host. • TCP bases a reliable stream delivery service on IP. – Delivery to multiple ports within a host (like UDP). – Streams: connections between application programs through which sequences of bytes flow. – Reliable: the sequence of bytes sent is the same as the sequence of bytes received. • Application programming interface (API) to use TCP. – Not part of standard: specific to OS/programming lanugage. Protocol used in most application programs. Wolfgang Schreiner 1 Transmission Control Protocol: TCP Stream Service • Stream Orientation. – Application programs see stream of bytes flowing from sender to receiver. • Virtual circuit connection. – Sender and receiver interact with their OS to set up a connection. – Protocol software on both sides exchanges messages to make sure that both sides are ready and to allocate resources for the communication. – During communication, messages are exchanged to make sure that data are received correctly. • Buffered transfer. – Application software may transmit individual bytes across the stream. – To make transfer more efficient and to minimize network traffic, protocol software collects data from stream to fill large datagrams before transmission. – Push mechanisms may be used to force transmission of not yet transmitted data. Wolfgang Schreiner 2 Transmission Control Protocol: TCP Stream Service • Unstructured streams. – Protocol software considers a stream as a sequence of bytes without any structure. – Application programs must agree on the interpretation of stream format (record structures) for communication of application data. • Full duplex connection. – A connection provides two streams, one stream in each direction. – From the application point of view, the two streams do not interact with each other. – Internally, the control software can piggyback control information for one stream into the datagrams of the other stream. – Larger efficiency than having a separate connection in each direction. Main characteristics of the TCP delivery service. Wolfgang Schreiner 3 Transmission Control Protocol: TCP TCP Sophisticated transport protocol for reliable stream delivery. • Various facets: – Format of data and acknowledgements. – Initialization and termination of stream transfer. – Procedure that sender and receiver follow to guarantuee reliability. – Distinction among multiple destinations of a given machine. – Recovery from errors like lost or duplicated packets. • Very few assumptions on underlying network: – Implementation on top of the IP datagram delivery service. – Implementation on physical networks: Ethernet, phone lines, . . . Well investigated protocol implemented on various delivery systems. Wolfgang Schreiner 4 Transmission Control Protocol: TCP TCP Connections Use of protocol port numbers. • A TCP connection is identified by a pair of endpoints. – An endpoint is a pair (host IP address, port number). • Examples: – Connection 1: (18.26.0.36, 1069) and (128.10.2.3, 25). – Connection 2: (128.9.0.32, 1184) and (128.10.2.3, 53). – Connection 3: (127.2.254.139, 1184) and (128.10.2.3, 53). • Different connections may share the endpoints! – Multiple clients may be connected to the same service. The basic TCP abstraction is the connection, not the endpoint. Wolfgang Schreiner 5 Transmission Control Protocol: TCP TCP Port Numbers Linux/Unix: /etc/services ftp-data ftp ssh telnet smtp http pop3 nntp ntp imap snmp 20/tcp 21/tcp 22/tcp 23/tcp 25/tcp 80/tcp 110/tcp 119/tcp 123/tcp 143/tcp 161/tcp mail imap2 # # # # # # # # # # # File Transfer [Default Data] File Transfer [Control] SSH Remote Login Protocol Telnet Simple Mail Transfer World Wide Web HTTP Post Office Protocol - Version 3 Network News Transfer Protocol Network Time Protocol Internet Message Access Protocol Simple Network Management Protocol Reserved TCP ports as well as freely available ports. Wolfgang Schreiner 6 Transmission Control Protocol: TCP TCP Aspects • Reliability: – Packages are received as they have been sent. – Technique: sliding window protocol. • Flow control: – Packages are not sent faster than they can be received. – Technique: TCP variant of sliding window protocol. • Timeout and retransmission: – Lost packages are retransmitted as soon as possible but not more than necessary. – Technique: Adaptive retransmission. • Congestion control: – Packages are not sent faster than they can be forwarded by routers. – Technique: Slow startup and multiplicative decrease. Wolfgang Schreiner 7 Transmission Control Protocol: TCP Reliability How to build a reliable service on top of unreliable packet delivery? • Recipient sends acknowledgement (ACK) messages to sender. – On receipt of each packet, an ACK message is sent. – Sender waits for ACK message before sending next packet. • Sender may have to retransmit a packet. – Sender starts a timer when sending a packet. – If timer expires before ACK message is received, packet is retransmitted. • Sequence numbers are used to detect packet duplication. – Each packet receives a sequence number. – Each ACK message takes sequence number from received packet. Positive acknowledgement with retransmission. Wolfgang Schreiner 8 Transmission Control Protocol: TCP Normal Situation Events At Sender Site Network Messages Events At Receiver Site Send Packet 1 Receive Packet 1 Send ACK 1 Receive ACK 1 Send Packet 2 Receive Packet 2 Send ACK 2 Receive ACK 2 Figure 13.1 A protocol using positive acknowledgement with retransmission in which the sender awaits an acknowledgement for each packet sent. Vertical distance down the figure represents increasing time and diagonal lines across the middle represent network packet transmission. Wolfgang Schreiner 9 Transmission Control Protocol: TCP Error Situation Events At Sender Site Send Packet 1 Start Timer Network Messages Events At Receiver Site Packet lost Packet should arrive ACK should be sent ACK would normally arrive at this time Timer Expires Retransmit Packet 1 Start Timer Receive Packet 1 Send ACK 1 Receive ACK 1 Cancel Timer Figure 13.2 Timeout and retransmission that occurs when a packet is lost. The dotted lines show the time that would be taken by the transmission of a packet and its acknowledgement, if the packet was not lost. Wolfgang Schreiner 10 Transmission Control Protocol: TCP Simple Positive Acknowledgement Protocol Simple protocol does not utilize the network very well. • There is only one message at a time in the network. – Either a packet or an ACK message. – But network is capable of simultaneous communication in both directions. • The network is idle at some times. – Software needs time to construct outgoing datagrams and to process incoming datagrams. Network bandwidth is wasted by waiting for ACK messages. Wolfgang Schreiner 11 Transmission Control Protocol: TCP The Sliding Window Protocol • Better form of positive acknowledgement and retransmission. – Sender may transmit multiple packets before waiting for an acknowledgement. • Windows of packets of small fixed size N . – All packets within window may be transmitted. – If first packet inside the window is acknowledged, window slides one element further. – At most N packets have not yet been acknowledged and may have to be retransmitted. initial window 1 2 3 4 5 6 7 8 9 10 . . . 7 8 9 10 . . . (a) window slides 1 2 3 4 5 6 (b) Figure 13.3 (a) A sliding window protocol with eight packets in the window, and (b) The window sliding so that packet 9 can be sent when an acknowledgement has been received for packet 1. Only unacknowledged packets are retransmitted. Wolfgang Schreiner 12 Transmission Control Protocol: TCP Performance of Sliding Window Protocol Events At Sender Site Network Messages Events At Receiver Site Send Packet 1 Send Packet 2 Receive Packet 1 Send ACK 1 Send Packet 3 Receive Packet 2 Send ACK 2 Receive ACK 1 Receive Packet 3 Send ACK 3 Receive ACK 2 Receive ACK 3 Figure 13.4 An example of three packets transmitted using a sliding window protocol. The key concept is that the sender can transmit all packets in the window without waiting for an acknowledgement. • Depends on window size N and on speed of packet delivery. – N = 1: simple positive acknowledgement protocol. – By increasing N , network idle time may be eliminiated. – Steady state: sender transmits packets as fast as network can transfer them. Well tuned sliding window protocol keeps network saturated. Wolfgang Schreiner 13 Transmission Control Protocol: TCP The TCP Sliding Window Mechanism current window 1 2 • Operates on byte level: 3 4 5 6 . . . . . . . . . . . 7 8 9 10 11 . . . Figure 13.6 An example of the TCP sliding window. Octets through 2 have been sent and acknowledged, octets 3 through 6 have been sent but not acknowledged, octets 7 though 9 have not been sent but will be sent without delay, and octets 10 and higher cannot be sent until the window moves. – Bytes in window are sent from first to last. – First bytes have to be acknowledged before window may slide. • Bytes are not transmitted individually but in segments. – Segment = sequence of bytes transmitted as an IP datagram. – Each ACK message for a segment describes how many bytes have been received so far. TCP translates byte stream into sequence of segments. Wolfgang Schreiner 14 Transmission Control Protocol: TCP The TCP Sliding Window Mechanism The size of a window is variable. • Receiver delivers window advertisements to sender: – On setup of a connection, the receiver informs the sender about the size of its window (i.e., how many bytes the receiver is willing to accept = size of buffer on receiver). – The sender sends at most as many bytes as determined by the window advertisement. – Every ACK message from the receiver contains a new window advertisement; the sender adapts its window size correspondingly. • Solves the problem of flow control: – As the window slides, the receiver may adjust the speed of the sender to its own speed by modifying the window size. Flow control in addition to reliable transfer. Wolfgang Schreiner 15 Transmission Control Protocol: TCP Variable Window Size Sender Receiver Application does a 2K write Receiver's buffer 0 4K Empty 2K S EQ = 0 2K ACK = 2048 WIN = 2048 Application does a 3K write 2K SEQ = 2048 Full Sender is blocked Application reads 2K IN = 0 096 W 4 ACK = 40 ACK = 96 WIN = 2048 2K Sender may send up to 2K 1K Wolfgang Schreiner SEQ = 4096 1K 2K 16 Transmission Control Protocol: TCP Variable Window Size Sender Events Receiver Events advertise window=2500 send data octets 1-1000 send data octets 1001-2000 send data octets 2001-2500 ack up to 1000, window=1500 ack up to 2000, window=500 ack up to 2500, window=0 receive ack for 1000 receive ack for 2000 receive ack for 2500 application reads 2000 octets ack up to 2500, window=2000 send data octets 2501-3500 send data octets 3501-4500 ack up to 3500, window=1000 ack up to 4500, window=0 receive ack for 3500 receive ack for 4500 application reads 1000 octets ack up to 4500, window=1000 receive ack for 4500 .. . Wolfgang Schreiner 17 Transmission Control Protocol: TCP TCP Segment Format • Segment = unit of TCP transfer. – Part of a stream, acknowledgements, opening and closing connections, . . . • Piggybacking – Acknowledgment may travel in same segment as stream data. 0 4 10 16 SOURCE PORT 24 31 DESTINATION PORT SEQUENCE NUMBER ACKNOWLEDGEMENT NUMBER HLEN RESERVED CODE BITS CHECKSUM WINDOW URGENT POINTER OPTIONS (IF ANY) PADDING DATA ... Figure 13.7 The format of a TCP segment with a TCP header followed by data. Segments are used to establish connections as well as to carry data and acknowledgements. Wolfgang Schreiner 18 Transmission Control Protocol: TCP TCP Header Fields SOURCE/DESTINATION PORT: 16 bit. • Identification of connection. SEQUENCE NUMBER: 32 bit. • Position of segment in sender’s byte stream. • Refers to stream from sender to receiver. ACKNOWLEDGEMENT NUMBER: 32 bit. • Number of byte that sender expects to receive next. • Refers to stream from receiver to sender! WINDOW: 16 bit. • Window announcement: how many bytes is sender willing to accept? • Refers to stream from receiver to sender! Wolfgang Schreiner 19 Transmission Control Protocol: TCP TCP Header Fields HLEN: 4 bit. • Number of words in header. CODE BITS: 6 bit. • Determine purpose and contents of segment. • ACK: acknowledgment field is valid; URG: urgent pointer field is valid; SYN: synchronize sequence numbers; PSH: push data to application; . . . URGENT POINTER 16 bit. • Support for sending out of band data; e.g. a keyboard sequence that interrupts the program at the other end. • URGENT Pointer specifies position in segment where urgent data ends. • On receipt of segment with urgent data, TCP software tells application program to go into “urgent mode” and immediately delivers the urgent data. Wolfgang Schreiner 20 Transmission Control Protocol: TCP TCP Checksum Computation Similar to UDP checksum computation. • A UDP pseudo header is added before the checksum is computed: – Contains IP destination address. – Checksum is computed over UDP pseudo header and UDP datagram. – Receiver can decide whether segment has reached the correct destination. 0 8 16 31 SOURCE IP ADDRESS DESTINATION IP ADDRESS ZERO PROTOCOL TCP LENGTH Figure 13.9 The format of the pseudo header used in TCP checksum computations. At the receiving site, this information is extracted from the IP datagram that carried the segment. Wolfgang Schreiner 21 Transmission Control Protocol: TCP Acknowledgements and Retransmission What exactly does an ACK message acknowledge? • ACK message refers to position in stream. – Number of bytes from the beginning of stream that have arrived so far (without gap). – There may be additional bytes later in the stream (after a gap). • Cumulative scheme. – ACK message reports how much of the stream has accumulated. – Position in subsequent ACK messages may only increase. • Example: – DATA 0 → DATA 1500 → DATA 3000 → DATA 4500 → . . . – ACK 1500 → ACK 3000 → ACK 4500 → ACK 6000 → . . . Cumulative scheme has advantages and disadvantages. Wolfgang Schreiner 22 Transmission Control Protocol: TCP Acknowledgements and Retransmission • Advantage: – Lost ACK messages do not necessarily force re-transmission of data segment. – DATA 0 → DATA 1500 → DATA 3000 → DATA 4500 → . . . – ACK 1500 → × → ACK 4500 → ACK 6000 → . . . – ACK 4500 supersedes the lost ACK 3000. • Disadvantage: – If data segment is lost, ACKs of later segments cannot increase value. – DATA 0 → DATA 1500 → × → DATA 4500 → DATA 6000 – ACK 1500 → ACK 3000 → ACK 3000 → ACK 3000 – Sender does not know whether datagrams after the lost DATA 3000 have been received. – Sender transmits DATA 3000 again and waits for ACK message before it transmits the next datagram (ACK 7500 → DATA 7500). If segment is lost, we get simple positive acknowledgement protocol. Wolfgang Schreiner 23 Transmission Control Protocol: TCP Timeout What happens if a data segment is lost? • TCP starts a timer for each segment sent. – If segment is acknowledged by some ACK message, everything is fine. – If timer expires without acknowledgement (timeout), the segment is retransmitted. • What is an appropriate timeout value? – We operate in an internet with possibly long and varying delays! 10 s x . .. . . 6s Time 4s x.. x. xx.. . . . . . . . . . . . . . . . . . . x 2s xx. . . . . . x. .. . x.. x. .. 8s x. . x.. .. x. . . . xx. x. x. . . x.. x. . .. . x . . . . x . . . . . . . . . . . x . . . . . . . . . . . x. x.. . . xx . . . . . . . . . . . . . . . . . . xx xx 10 . . . . . . . . . . . . . . x x x. . x . x. x.. . . . . . . . x. x. . . x.. . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x x x xx xxxxxx x.. . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . .. . . xx x 30 40 x x x x x x xxx x x x x xx 50 xx x x x x 20 . . . . . . . . . . . . . . .. . .. . .. . .. . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . .. . . .. .. . . . . . . . .. . . x . . x x x x.. x. x. x. x. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . .. . . . . . . . . . . 60 70 x xxx xx x x x x x xx x x x 80 90 100 Datagram Number Wolfgang Schreiner Figure 13.10 A plot of Internet round trip times as measured for 100 successive IP datagrams. Although the Internet now operates with much lower delay, the delays still vary over time. 24 Transmission Control Protocol: TCP Adaptive Retransmission TCP monitors performance of connection and deduces timeout values. • TCP collects data needed for adaptation: – Time at which data segment is sent and time at which ACK message arrives. – Difference gives the round trip time RTT . – From all sample round trip times, TCP computes an average round trip time. – 0 < α < 1 (e.g. α = 0.78): new RTT = (α ∗ old RTT ) + ((1 − α) ∗ sample RTT ) – New samples let RTT change slowly. • Timeout value is a multiple of the roundtrip time. – β > 1 (e.g. β = 2): Timeout = β ∗ RTT . – To cope with large variations, TCP uses RTT variance instead of constant β. Wolfgang Schreiner 25 Transmission Control Protocol: TCP Adaptive Retransmission 100 80 60 40 ............. ............. . . . ... ...... ...... .. . ... . .. ... . . .. . ..... ... ....... . . . ... . .. . . . . .. .. . ...... ..... .. .. . . .. . . . ..... ... ..... .. ..... ... . . . . .. . ... . . . . . .. . ..... . .. .. ........ .. . ....... . . ... ... ....... 20 20 40 60 80 100 120 140 160 180 200 Datagram Number Figure 13.11 A set of 200 (randomly generated) roundtrip times shown as dots, and the TCP retransmission timer shown as a solid line. The timeout increases when delay varies. Wolfgang Schreiner 26 Transmission Control Protocol: TCP Cngestion Control • TCP must react to congestion in network: – Severe delay caused by overload of datagrams at a a router. – Delays increase because router begins to enqueue datagrams until it can route them. – Since router has only finite memory, it ultimately starts to drop datagrams. – Since datagrams are dropped, sender retransmit datagrams which yields a congestion collapse. • Solution: reduce transmission rate when congestion occurs. – In addition to window size announced by receiver, sender maintains a congestion window: window size = min(announced window , congestion window ). – Slow-start: start the congestion window at the size of a single segment; increase it by one segment each time an ACK message arrives until a treshold of 64 KB is reached. – Congestion avoidance: if treshold is reached, increase congestion window by one segment only if all segments in window have been acknowledged. – Multiplicative decrease: if segment is lost, set treshold to half the size of congestion window, set congestion window to 1, double timeout value, and go into slow-start mode again. Wolfgang Schreiner 27 Transmission Control Protocol: TCP Congestion Control 44 Timeout 40 Congestion window (kilobytes) 36 Threshold 32 28 Threshold 24 20 16 12 8 4 0 0 Wolfgang Schreiner 2 4 6 8 10 12 14 Transmission number 16 18 20 22 24 28 Transmission Control Protocol: TCP Router Response to Congestion What should a router do when too many packets arrive? • Early strategy: tail drop. – If a new packet arrives and the buffer is full, drop the packet. – Disadvantage: since datagrams are multiplexed from different connections, one segment is dropped from each connection; all connections go into slow-start at the same time. • Nowadays strategy: random early discard. – If the buffer is not full but the size exceeds a minimum threshold T , discard an arriving datagram with propability p. – Consequence: router slowly starts dropping datagrams as congestion increases (without waiting that buffer becomes full). – Key is careful choice of threshold value T and probability p. Router operation is optimized to TCP congestion control strategy. Wolfgang Schreiner 29 Transmission Control Protocol: TCP Three-Way Handshake How is a TCP connection initially established? • Both sides must signal that they are ready to transfer data. – Both sides must also agree on initial sequence numbers. – Initial sequence numbers are randomly chosen; if they were always 0, problems might arise due to duplicated packets from previous connection requests. Host 1 Host 2 SYN (SEQ Host 1 Host 2 SYN (SE Q = x) = x) ) EQ = y Time SYN (S EQ SYN (S K=x+ = y, AC 1) (SEQ SYN SYN (SE Q= CK = = y, A x,A CK (SEQ = x + 1, ACK = y + 1) =y x + 1) +1 ) Similar protocol for closing TCP connections. Wolfgang Schreiner 30