Services Connections UDP Transport Layer Mahalingam Ramkumar Mississippi State University, MS September 29, 2014 Ramkumar TL Services Connections UDP Outline 1 Services 2 Connections TCP Header TCP Transmission Policy Timer Management 3 User Datagram Protocol Ramkumar TL Services Connections UDP Transport Layer Services Provides service to application layer by making the network layer easier to use Service can be connection oriented or connectionless Example TCP (connection oriented) UDP (connectionless) over IP. Port numbers are access points for transport service Ramkumar TL Services Connections UDP Connection Oriented Service Reliable delivery of data In the right order Acknowledged delivery Hide implementation details from top layer Ramkumar TL Services Connections UDP Interface Provided to Application Layer listen(), accept() connect() send() recv() close() Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management Connection Establishment in TCP Client sends connection request Server acknowledges Issues due to unreliable lower layer? Packets can get lost, delayed, duplicated FLAGS and seq/ack numbers (for counting bytes sent/received) on TCP header. Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management SYN/ACK/FIN flags, seq/ack number SYN flag set: sequence number is the starting sequence number SYN is not set: sequence number is the byte count for first data byte included the packet ACK flag set when acknowledgement number is valid; indicates next byte expected FIN set for closing a connection Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management Three-Way Handshake SYN and ACK flags; seq and ack numbers Both sender and receiver choose initial sequence number Connection request (SYN, seq no x) from initiator (client) SYN-ACK (SYN, seq y , ACK, x + 1) response (from server) ACK (ACK, y + 1) from client. SYN — SYN/ACK — ACK sequence. Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management Four-Way Handshake Two unidirectional connections What if one side is not ready to disconnect (not finished sending)? Four-way handshake A → B: B → A: B → A: A → B: FIN ACK, ack number (next byte expected + 1) FIN ACK, ack number (next byte expected + 1) FIN — ACK, FIN — ACK (four packets) or FIN — ACK/FIN — ACK (three packets) Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management Sequence and ACK numbers To keep track of application data bytes sent/received Client C , server S C → S : seq 1+ packet with 1000 application bytes (1000 byte payload) S → C : ack 1001 (the next byte I am expecting from you is 1001). C → S : seq 1001+ packet with 500 application bytes S → C : ack 1501 (the next byte expected is 1501). C → S : seq 1501+ packet with 200 application bytes. S → C : ack 1501 (the next byte expected is 1501 probably because it did not receive the third packet from the client). Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management Both ways at the same time! C → S : seq 1 + 100 payload bytes S → C : seq 1 ack 101, 1500 payload bytes C → S : seq 101 ack 1501, 200 payload bytes S → C : seq 1501 ack 301, 1500 payload bytes C → S : ack 3001 (no payload) Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management ACK/SEQ numbers Offset by starting sequence numbers C , qc , S, qs C → S : SYN, seq qc S → C : SYN, seq qs , ACK, ack qc + 1 C → S : ACK, ack qs + 1 (completes 3-way handshake) C → S : seq qc + 1 + 100 payload bytes S → C : seq qs + 1 ack 101, +1500 payload bytes C → S : seq qc + 101 ack qs + 1501, +200 payload bytes S → C : seq qs + 1501 ack qc + 301, +1500 payload bytes C → S : ack qs + 3001 (no payload) Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management SYN/ACK Flags with SEQ/ACK numbers → SYN, seq(qc ) ← SYN, seq(qs ), ACK, ack(qc + 1) → seq(qc + 1), ACK, ack(qs + 1) → seq(qc + 1), ACK, ack(qs + 1), 100 data bytes ← seq(qs + 1), ACK, ack(qc + 101), 1500 data bytes → seq(qc + 101), ACK, ack(qs + 1501) ← seq(qs + 1501), ACK, ack(qc + 101), 1400 data bytes → seq(qc + 101), ACK, ack(qs + 2901), FIN ← seq(qc + 2901), ACK, ack(qc + 102), 1200 data bytes → ACK, ack(qs + 4101) ← seq(qs + 4101), ACK, ack(qc + 102), FIN → ACK, ack(qs + 4102) CLIENT SERVER Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management All together connect() → SYN, seq(qc ) accept() . . . ← SYN, seq(qs ), ACK, ack(qc + 1) → seq(qc + 1), ACK, ack(qs + 1) send() recv() → seq(qc + 1), ACK, ack(qs + 1), 100 data bytes recv() ← seq(qs + 1), ACK, ack(qc + 101), 1500 data bytes send() → seq(qc + 101), ACK, ack(qs + 1501) ← seq(qs + 1501), ACK, ack(qc + 101), 1400 data bytes shutdown(WR) → seq(qc + 101), ACK, ack(qs + 2901), FIN recv() ← seq(qc + 2901), ACK, ack(qc + 102), 1200 data bytes → ACK, ack(qs + 4101) close() ← seq(qs + 4101), ACK, ack(qc + 102), FIN close() → ACK, ack(qs + 4102) CLIENT SERVER Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management TCP Header 32 Bits Source port Destination port Sequence number Acknowledgement number U A P R S F R C S S Y I G K H T N N TCP header length Window size Checksum Urgent pointer Options (0 or more 32-bit words) Data (optional) Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management TCP Header Fields Source port and destination port Sequence Number - chosen randomly for the first packet (connection establishment). In subsequent packets containing data, it indicates the offset of the first (application data) byte included in the packet. Acknowledgment number indicates next byte expected (offset by the sequence number). A’s ack. to B offset by B’s initial sequence number Header length (4 bits) - in number of 32 bit words. Actually indicates start of application data. Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management TCP Header - FLAGS Six unused bits Six flags, URG, ACK, PSH, RST, SYN, FIN PSH, URG are not used a lot in practice (and therefore the urgent pointer too) SYN used for connection establishment FIN used for disconnect ACK is used during connection setting-up, acknowledgement of data received, and during connection-tearing-up RST used to indicate rejection of connection Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management TCP Header - Window Size 16 bits. Window size is the number of bytes the receiver is willing to accept. Window size in A’s TCP packet to B says the maximum window size B can use for sending data to A (and vice-versa) The receiver determines the window size for each one-way connection (A to B and B to A) Window size is dynamic. Window size 0 indicates “stop sending data - I’m swamped” Could later send another packet with a larger window size to resume sending. If A has advertised a window size of 500 and the last acknowledged byte is 1000, B cannot send beyond byte 1500 to A. Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management TCP Header - Checksum Checksum of header, data and a ”conceptual pseudo-header” Includes Source and Dest IP addresses, TCP segment length, IP protocol field. 32 Bits Source address Destination address 00000000 Protocol = 6 Ramkumar TCP segment length TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management Checksum - Some Finer Points TCP segment does not include length IP layer (in the final destination) passes this info to TCP layer, along with the entire TCP packet TCP layer is also provided with the Source and Dest IP addresses by the IP layer All three needed to validate checksum Violates protocol hierarchy Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management TCP Header - Options Specify maximum TCP payload - used very frequently. SRP instead of GBN - possibility of including NACKs before sending ACK - RFC 1106 Scaling Window size For high bandwidth / high delay (or both) 64-kB window is not sufficient. On a T3 line 12msec to output 64kB If RTT is 50 msec, nothing can be sent for 38 msec. RFC 1323 Allows left shift of window size upto 14 bits - max window size 230 . Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management PSH and URG PSH - a way for applications to request TCP layer to stop buffering packets and just send the data out. A set PSH flag indicates to the receiver that the packet should be delivered immediately to the application above. URG and Urgent pointer. Very similar to push conceptually. But can specify a particular byte offset in a TCP packet that needs to be attended to urgently - like a software interrupt. Urgent pointer indicates offset. Once again, at the receiver side, the TCP layer delivers the data to the application ASAP and indicates the offset. Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management TCP Connection Establishment Case (b) - only one connection is established! Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management Connection Release Each simplex connection released independently 1 2 3 4 5 (Assume B → A ACK x; A → B ACK y were last two ACK packets exchanged between A and B). A → B, FIN, seq x B → A, ACK x + 1 B → A, FIN, seq y A → B, ACK y + 1 2 and 3 can be combined, B → A, ACK, FIN. Timeout when ACKs are not received Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management PSH Flag PSH flag is actually used a lot in telnet How many bytes are exchanged between A and B for sending one character? For each character, 20 bytes IP header + 20 bytes TCP header + 1 = 41 bytes! Each way Two ACKs each way if we type real slow - 40 + 40 = 80 bytes Total of 162 bytes for sending a single character Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management Window Management Client A, server B Client has reserved a 10000 byte buffer for the connection Client indicates this by setting window size 10000 in the SYN packet Server B allocates 4000 bytes. Indicates this by setting window size 4000 in the SYN-ACK packet. Window size indicates how many bytes beyond the last acknowledged byte can be sent. Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management Window Management ... After connection establishment, A sends 1000 bytes to B B stores the bytes in the buffer - has only 3000 bytes of free buffer. B sends an ACK with window size 3000. A sends another 3000 bytes B sends ack with window size 0 A waits. B’s TCP layer passes the data to the application layer. Buffer is now empty. B sends a new “window advertisement” to A with window size 4000. Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management Window Management Application does a 2K write Sender Receiver Receiver's buffer 0 4K Empty 2K S EQ = 0 2K ACK = 2048 WIN = 2048 Application does a 2K write 2K SEQ = 2048 Full Sender is blocked ACK = ACK = 4096 W Application reads 2K IN = 0 048 IN = 2 4096 W 2K Sender may send up to 2K 1K SEQ = 4096 Ramkumar 1K TL 2K Services Connections UDP TCP Header TCP Transmission Policy Timer Management Silly Window Syndrome Receiver's buffer is full Application reads 1 byte Room for one more byte Header Header 1 Byte Window update segment sent New byte arrives Receiver's buffer is full Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management TCP Specifications Apart from the header protocol, TCP specifies very little Example, defaults Everything else is implementation dependent Some of them may ignore PSH. Most do not support URG TCP does not specify when ACKs should be sent or how many bytes to buffer. Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management TCP Congestion Control Hosts realize that there is congestion when packets are not acknowledged Could occur due to two reasons 1 2 Low capacity receiver at the other end Network congestion The first is handled during the connection establishment process - using window sizes The second? Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management Congestion Window Every sending host chooses an additional window size Congestion Window Initially set to the maximum packet size the host can handle (MTU 1500 for Ethernet based hosts). The window size chosen is the minimum of Congestion window WC and the advertised window size by the other side Wa . First packet sent is up to MTU size Assume Wa is very large If successful in sending first packet (ack received before timer goes off) congestion window size doubled to 2 MTUs On success, doubled to 4 MTU and so on till failure happens then window size is halved. Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management TCP Congestion Window Failure is detected if 3 duplicate ACKs received or time out occurs. Additional parameter, threshold Window size, initially set to 64 kB Exponential increase, only till the threshold is released. After this increase is linear, for example 64 MTU to 65, 66 and so on. Whenever failure occurs (say at N MTUs), threshold is halved (in both TCP Tahoe and TCP Reno) And WC set to N/2 MTU (TCP Tahoe, fast recovery) or 1 MTU (TCP Reno, slow recovery) ICMP source quench packets (we will see this later in the network layer) are also treated as time-outs for this purpose. Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management TCP Reno (Slow Recovery) 44 Timeout 40 Congestion window (kilobytes) 36 Threshold 32 28 Threshold 24 20 16 12 8 4 0 0 2 4 6 8 10 14 12 Transmission number Ramkumar TL 16 18 20 22 24 Services Connections UDP TCP Header TCP Transmission Policy Timer Management TCP Timers Retransmission Timer - decides timeout How? Need to estimate RTT. Chicken and Egg Problem? Not really - use a large timeout initially! T 0.3 T1 0.3 Probability 0.2 Probability 0.2 T2 0.1 0 0.1 0 30 40 10 20 Round-trip time (msec) 50 0 0 (a) 10 20 30 40 Round-trip time (msec) (b) Ramkumar TL 50 Services Connections UDP TCP Header TCP Transmission Policy Timer Management Estimating Round-Trip Time (RTT) Jacobson’s algorithm 1 2 3 4 5 6 Time for receiving acknowledgement M RTT = αRTT + (1 − α)RTT α = 7/8. Timeout is βRTT . How do we choose β? β should be based on the standard deviation of the RTT estimate. Estimate of standard deviation D = αD + (1 − α) | RTT − M |. Timeout = RTT + 4 × D What happens when time outs occur? Is the ACK for the original or for the re-transmission? Making a wrong judgment here could affect the estimate very badly! Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management Karn’s Algorithm Do not update RTT for segments that have been retransmitted Whenever timeout occurs double RTT Keep doing it till timeout does not occur! Used in most TCP implementations (TCP does not specify what to do). Ramkumar TL Services Connections UDP TCP Header TCP Transmission Policy Timer Management Two more Timers! TCP uses 3 timers - retransmission, persistence and keep-alive Persistence Timer - for prevention of deadlock if window size re-advertisement is lost. When persistence times goes off, sender transmits a probe (even though receiver had previously informed the sender to stop sending by advertising window size 0), and receiver sends the window size as response. Keep-alive Timer - Connections may be idle for a long time (telnet connections). On expiry of keep-alive timer a probe is sent to detect if the other side is still alive. If so timer is started again (else connection is terminated). Ramkumar TL Services Connections UDP UDP Does very little over what IP does Provides port numbers No flow control, error control, retransmission Applications have to do everything 32 Bits Source port Destination port UDP length UDP checksum Ramkumar TL Services Connections UDP UDP Checksum similar to TCP DNS RPC (Remote procedure calls). eg gethostbyname() The need for 3 packets for establishing connection and 3 or 4 for tearing them down makes TCP very expensive for many applications Especially when networks are often highly reliable. Multicasting - TCP cannot be used. UDP is used for RTP (real-time transport protocol) Ramkumar TL Services Connections UDP Transactional TCP TCP has high overhead for scenarios where the transaction is simple (small request, small response, like DNS) UDP is efficient but highly insecure Even servers that did not actually receive the query can pretend to have received, and send a “response” Transaction TCP is actually a different way of using TCP Query and responses are sent with connection request packets Three-way handshake simultaneously completes connection establishment, acknowledged delivery of data and termination! Ramkumar TL Services Connections UDP Transactional TCP Client 1 Server 1 SYN SYN) SYN, ACK( 3 Time 4 5 2 Server SYN, reques t, FIN) SYN, ACK( 3 ACK(SYN) request Time t + FIN) reply FIN 6 7 8 ACK(FIN) (a) (b) Ramkumar TL FIN , reply, FIN ACK(FIN) FIN ACK(reques 9 Client 2