1. Checksum

The TCP checksum includes the TCP header and data

The calculation method is as follows:

1) Set the test and field to 0

2) The sender divides the entire message segment into multiple 16-bit segments

3) Sum all segments in binary. If the high 16 bits are not 0, add the high 16 bits and the low 16 bits repeatedly until the high 16 bits of the sum are 0, thus obtaining a value of 16 bits

4) Invert the 16bit value and store it in the checksum field

5) The receiver adds the 16-bit segments in the same way, resulting in a 16-bit value, which is correct if the final result contains all 1 bits

2. Serial number and confirmation number

3. Retransmission timed out

If no acknowledgement is received within a certain period of time after the packet is sent, the sender retransmits the packet. The sender or ack may be lost

Using the serial number can achieve the effect of weight reduction

Retransmission time: indicates the round trip time (RTT) of a packet between the sending and receiving of a packet. The timeout retransmission time (RTO) is slightly greater than the RTT. In Linux, the timeout time changes dynamically and the connection is forcibly closed when the retransmission times reach a certain number

4. Connection management

TCP three handshakes and four waves

5. Flow control

It solves the packet loss problem caused by too fast sending rate

The speed at which the receiver processes data is limited. If the speed at which the sender sends data is too fast and the buffer at the receiver is full, packet loss and retransmission will occur

The TCP header has a 16-bit window length. The receiver will add the remaining size of its buffer into the 16-bit window length in the ack message

But the sender will periodically send a window probe, so that the receiver tells the sender the size of the window

6. Congestion control

If the network is congested, resending may burden the network, and the data segment on the sender may exceed the maximum lifetime and not reach the receiver

Five state machines of congestion control: Open, Disorder, CWR, Recovery and Loss

There are four algorithms for congestion control: slow start, congestion avoidance, congestion occurrence and fast recovery

Slow start:

At the beginning of TCP’s establishment, the speed was increased bit by bit to test the network’s capacity

Exponential growth in the initial stage; Set a slow start threshold value, when the exponential growth reaches the threshold value, stop exponential growth, increase in a linear way; When the linear increase reaches network congestion, the multiplication decreases immediately. The serial port of congestion is set back to 1 to start a new round of slow startup. At the same time, the threshold of the new round becomes half of the original threshold

1) Define the congestion window size CWND as 1 at the start of sending;

2) Each time an ACK response is received, the congestion window increases by 1;

3) When a round trip delay time passes, the size of the congestion window increases exponentially, multiplied by 2

4) There is also a threshold (SSTHRESH), when CWND >= SSTHRESH, the “congestion avoidance algorithm” will enter

Congestion avoidance:

1) Received an ACK, CWND = CWND +1/ CWND

2) Each time a round-trip delay time RRT passes, the CWND size increases by one

After the slow start threshold is exceeded, the congestion surface algorithm can avoid window congestion caused by excessive window growth, but slowly increase

Congestion :(fast retransmission)

Generally speaking, TCP congestion control considers that packet loss is caused by network congestion by default. Therefore, packet loss is considered to be in the congestion state. There are two ways to identify packet loss: one is to retransmit the RTO timeout, and the other is to receive three duplicate ACK packets

1) The principle of timeout retransmission is that after sending a data, a timer is started. If no ACK is received within a certain period of time, the data will be sent again until it is successfully sent

When retransmitting RTO due to timeout, TCP will retransmit data packets. TCP thinks this is a bad situation, so it sets the slow start threshold ssTHRESH to the current general CWND, that is, SSTHRESH = CWND /2, CWND is reset to 1, and the slow start process starts. It is not conducive to the stable transmission of network data, so the Reno algorithm is optimized and TCP enables the fast retransmission algorithm

2) If the sender receives more than three duplicate ACKS, TCP considers that data is lost and does not need the timeout retransmission timer, so it is called fast retransmission

The CWND size is reduced to half the current size, ssTHRESH is set to the reduced CWND size, and then the fast recovery algorithm is entered

Quick recovery:

1) CWND = CWND +3MSS, add 3MSS cause received 3 repeated ACK

2) Retransmit packets specified by DACKs

3) If DACKs are received, the CWND size increases by one

4) If a new ACK is received indicating that the retransmitted packet was successful, exit the fast recovery algorithm, set CWND to SSTHRESH, and enter the congestion avoidance algorithm

MSS: indicates the maximum length of a packet segment. When the connection is established, the MSS will be sent to the other party (the MSS option can only be used in the SYN section!!). Indicates the maximum length of the TCP packet segment to be received by the peer end.

Reference:

Segmentfault.com/a/119000001…

www.cnblogs.com/peanutk/p/1…

Blog.51cto.com/lingdandan/…