Flow control
For the sender and receiver, TCP puts the sent data into the send buffer and the received data into the receive buffer, and controls the sending of data on the sender according to the size of the receive buffer
The sliding window
TCP realizes flow control through sliding Windows. Sliding Windows are divided into receiving Windows and sending Windows. The sending window contains the parts that have been sent but have not been confirmed and the parts that have not been sent but can be sent
Describe the flow control process
Every time the receiver to receive the sender after sending a data packet, will return a confirmation message, at this time, again confirmation message will be returned in a receive buffer memory size remaining in the receiver, the sender after receive the confirmation message, according to the receiver buffer size to change its send window size, this is a process of flow control.
After the receiver buffer without residual memory, the sender will modify send window is zero, avoid massive packet loss, caused great waste of network resources, when the receiver buffer after have new memory available, will send a notification message to the sender, the sender, after receiving the message will send window size change yourself, continue to send data.
However, if the notification packet is lost due to some network reasons, the sender will always wait for the receiver to send the non-zero window notification, and the receiver will also wait for the new data of the sender, forming a deadlock state. To avoid this situation, when the receiving window is zero, the sender will start a timer. The current size of the receiving buffer is requested at intervals to change the size of the sending window.
Congestion control
Important points:
- Slow start
- Congestion avoidance
- The fast retransmission
- Fast recovery
Slow start
TCP does not send packets at the maximum speed at the beginning to prevent large-scale packet loss due to network instability. Therefore, TCP slowly ADAPTS to the current network at a conservative speed at the beginning. When the sender receives a transmission turn, the congestion window doubles. However, the sending capacity of the sending end is limited after all, so it is impossible to keep increasing (although the sending window takes the smaller value between the congestion window and the receiving window), so we need a threshold (SSthresh), after reaching this threshold, we will stop using slow start and switch to using the congestion avoidance algorithm.
Tips: The first slow start threshold will be a large value, then after the first send timeout set the slow start threshold to half of the current congestion window, congestion window value to 1, and restart the slow start
Congestion avoidance
In the congestion avoidance algorithm, the congestion window is no longer doubled, but only increased by 1, to approach the limit slowly
The fast retransmission
A technology that can be retransmitted before the retransmission timer expires. If the receiving end receives a packet segment out of order, TCP will immediately generate an ACK message to let the sender know that it has received an out-of-order packet and tell the seQ of the packet it wants to receive. When receiving three or more consecutive repeated packets, the receiving end determines that the modified packets are lost and retransmits the lost packets immediately without waiting for the timer.
Fast recovery
The fast recovery algorithm thinks that your current network is not that bad, you can still receive the next three or more messages, there is no need to restart the slow start so hard, just reduce the congestion window a little bit.
cwnd = cwnd /2; // The congestion window becomes normal
ssthresh = cwnd; // Set the slow start threshold to the current congestion window size
// The congestion window will be increased using the congestion avoidance algorithm. Avoid a slow start, can save a lot of time for resource loading
Copy the code
Quick recovery problem
If multiple packets are lost, fast retransmission and fast recovery are triggered for multiple times, and the slow start threshold and congestion window are reduced for multiple times, reducing transmission efficiency
Congestion control summary
In fact, fast retransmission and fast recovery only avoid reducing the congestion window when the congestion is not serious, resulting in the decrease of TCP transmission efficiency.