TCP - LogeshVel/learning_resources GitHub Wiki

TCP

Allows two endpoints to exchange data reliably and efficiently over a network even with the packet loss.

image

image

image

Four Tuple

  • Source IP
  • Dest IP
  • Source Port
  • Dest Port

image

Socket

IP:Port is socket.

image

Socket

TCP Header

image

image

image

image

image

image

image

image

Push flag in the sender side says don't put me in the buffer quickly send me on to the wire. In the receiver side, don't put me in buffer quikly send me to the application

image

In TCP, packets with the "Reset" (RST or R) flag are sent to abort a connection. Probably the most common reason you are seeing this is that an SYN packet is sent to a closed port.

But RST packets may be sent in other cases to indicate that a connection should be closed.

image

image

image

image

Connection State

There are 11 different TCP conenction states. Here is the few

image

Handshake

image

iRTT

iRTT - initial round trip time.

The time delay between the TCP SYN packet and the ACK packet.

image

Calculated by the wireshark. We can see that when we click on the SYN/ACK packet.

image

ISN

image

Relative Sequence number is set by the wireshark. Because the client will advertise the server the random sequence number that it will start counting the packets and the server also did the same. But wireshark shows that (raw) and also the relative sequence number that starts from 0. So for us (humans) it will be lot more easier.

image

Flags

image

Window size

It is TCP receive buffer values. Meaning that the receivercan receive that amount of bytes from the sender without sending the ACK.

image

image

TCP Options

image

SYN/ACK

image

Window Size

image

Server Side Options

image

Session Termination

Consider Endpoint A and B.

If A is sends Fin, B responds with ACK and the same happens in other direction B sends Fin, Responds with ACK.

image

Termination is kind of 4 way handshake.(2 pairs). But that can be done in only 2 packets. A sends Fin and B responds with FIN and ACK(ACKing the FIN and also it sends the FIN in the same packet).

image

TCP Window scale

image

image

image

MSS

image

Sometimes the MSS value of the SYN packets can be changed by the intermediate routers(if it is configured to do so). The reason is if the Client sends the MSS value as 1460 and sent it to the wire, it should come across the networks. once it reaches the router it will see the MSS and its MTU if the MSS is greater than the MTU then that router will reduce the MSS to its MTU - 40. The - 40 is for the IP headers and Ethernet headers.. Since MSS is Maximum segement size (TCP segment). The MTU size considers the whole packet size.

image

ISN

image

Sequence number

image

The Application data transported to the Transport layer. The transport layer then breaks the big chuck of data into pieces according to the MSS value they have negotiated.

Lets assume MSS as 1460 bytes

image

image

Acks for the received bytes.

The sequence number is a counter used to keep track of every byte sent outward by a host. If a TCP packet contains 1400 bytes of data, then the sequence number will be increased by 1400 after the packet is transmitted.

Sequence number - number of bytes sent by the host

The acknowledgement number is a counter to keep track of every byte that has been received. If 1000 bytes are received by a host, it increases the acknowledgement number by 1000 when it sends out a packet in response.

Acknowledge number - number of bytes received by the host

Reference

image

image

Packet Loss

image

Out of Order

image

TCP Receive Window

image

The Receive Window size is always been sent in all packets, which is used to say that i have this much amount of bytes in buffer. If the buffer size (receive window size) sent by the endpoint decreases that means the buffer is filling up.

image

image

image

image

Zero Window

The client is not processing the data and it will receive the data until the window size is equal to zero.

image

Window size 0 indicates that no more space left to receive data the Server has to stop sending the data.

After some time either the Client can request the data once it processed the data (got some space in buffer) else the Server can query the client like any update on the window size.

image

The Window size can actually limits the bandwidth. Even the Server and the network is ready to send the large data if the client has smaller window size then the Server won't send the data.

image

image

image

Bandwidth Delay product

b - bits

B - Bytes

image

If the TCP window size of the Client is equal to the Bandwidth Delay product then only we could make use of the available Bandwidth.

Retransmission

Time based retransmission

*Note: in the below example we have ack for the packet but in the real world its not the case. In real world the ACK is for bytes of data received. For simplicity we mentioned packet.

image

RTO - Retransmission Timeout

The Transmission control protocol (TCP) retransmission timeout (RTO) is calculated based on a round-trip time (RTT) with weighted smoothing.

This is the Standard retransmission time in TCP

image

Fast Retransmission by SACK

SACK (Selection ACK) must be supported for the Fast retransmission

image

When an endpoint receives 3 Dup ACKs with SACK (Dup ACK - The ACK number will be the same as the previous packet ACK number but in the TCP options the Seclection ACK will be done with the bytes ranges.)

The SACK supporting will be negotioted at the time of handshake.

image

Spurious retransmission

Basically “Spurious Retransmission” means that data was sent again that the receiver had already acknowledged but the Sender not got that ACK.

image

Duplicate Acknowledgements

image

Dup ACK with SACK

There can be the Dup ACK with the SACK to indicate the sender some intermidient seq have been missed(not receiver by the receiver)

image

The Dup ACK will be continously sent until the sender sends the missed sequences.

Possibly,

image

Packet Loss - Cause

image