“This is the 25th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”

TCP flow control

Receiver Means by which a user controls the sending rate and packet size of a sender. The sliding window mechanism is used to control the flow of the sender. Without this limitation, the sender sends a lot of data even when the receiver is too busy to receive it, resulting in a lot of packet loss.

Let A send data to B. When establishing A connection, B tells A: “My receive window is RWND = 400″(where RWND stands for Receiver window). Therefore, the sender’s send window cannot exceed the value of the receive window given by the receiver. Note that TCP’s window units are bytes, not message segments. Assume that each packet segment is 100 bytes long and the initial sequence number of the data packet segment is 1. Uppercase ACK indicates the ACK bit in the header, and lowercase ACK indicates the ACK value of the acknowledgement field.

As can be seen from the figure, B has carried out flow control for three times. The first time the window is reduced to RWND = 300, the second time to RWND = 100, and finally to RWND = 0, that is, the sender is no longer allowed to send data. The sender pauses until host B reissues a new window value. ACK=1 is set for the three packet segments sent by B to A. The ACK field is valid only when ACK=1.

TCP has a persistence timer for each connection. As soon as one side of the TCP connection receives a zero window notification from the other, a persistent timer is started. If the duration of the timer expires, a zero window control message segment (with 1 byte of data) is sent, and the party receiving this message segment resets the duration timer.

TCP congestion control

1. Slow start and congestion avoidance

The sender maintains a state variable of congestion window CWND (Congestion Window). The size of the congestion window depends on the level of congestion on the network and changes dynamically. The sender makes its send window equal to the congestion window.

The principle for the sender to control the congestion window is: as long as there is no congestion on the network, the congestion window will be larger to send more packets. But whenever there is congestion on the network, the congestion window is reduced to reduce the number of packets injected into the network.

– Slow start algorithm

If a host immediately injects large numbers of bytes of data into the network when it starts sending data, it can cause congestion because it is not clear how overloaded the network is.

Therefore, the better method is to first detect, that is, gradually increase the sending window from small to large, that is, gradually increase the value of congestion window from small to large.

Usually, the congestion window CWND is set to a maximum MSS value at the beginning of sending a packet segment. After each acknowledgement of a new message segment is received, the congestion window is increased by at most one MSS value. Gradually increasing the congestion window CWND of the sender in this way can make the packet injection rate more reasonable.

The congestion window CWND doubles with each transmission turn.

In addition, the slow start “slow” does not mean that the CWND growth rate is slow. Rather, it means that the CWND is set to 1 when TCP starts to send packets, so that the sender sends only one packet segment at the beginning (to test the network congestion), and then gradually increases the CWND.

To prevent network congestion caused by excessive CWND growth of congestion window, a slow start threshold SSthRESH status variable needs to be set. The slow start threshold ssthresh is used as follows:

When CWND < SSTHRESH, use the slow start algorithm described above.

When CWND > SSTHRESH, stop using the slow-start algorithm and use the congestion avoidance algorithm instead.

When CWND = SSTHRESH, either the slow start algorithm or the congestion avoidance algorithm can be used.

– Congestion avoidance algorithm

Let the congestion window CWND grow slowly, that is, every round trip time RTT increases the sender’s congestion window CWND by 1 instead of doubling it. The congestion window CWND grows slowly in a linear way, which is much slower than the congestion window growth rate of the slow-start algorithm.

The slow start threshold ssthRESH should be set to half (but not less than 2) of the sender window value when congestion occurs, as long as the sender determines that the network is congested, whether in the slow start phase or the congestion avoidance phase. Then the congestion window CWND is reset to 1 and the slow start algorithm is performed.

The goal is to quickly reduce the number of packets sent to the network by the host so that the congested router has enough time to clear the backlog of packets in the queue.

In the figure below, the process of congestion control mentioned above is illustrated by specific numerical values. The send window is now the same size as the congestion window.

2. Fast retransmission and recovery

Fast retransmission

The fast retransmission algorithm first requires the receiver to send repeated acknowledgement immediately after receiving a segment out of order (so that the sender can know that a segment has not reached the other party in time) rather than wait until the data is sent before carrying on the acknowledgement.

The recipient sent confirmation after receiving M1 and M2 respectively. Now assume that the receiver does not receive M3 but then receives M4.

Obviously, the receiver cannot acknowledge M4 because M4 is the received out-of-order message segment. According to the principle of reliable transmission, the receiver can either do nothing or send an acknowledgement of M2 at an appropriate time.

However, according to the provisions of the fast retransmission algorithm, the receiver should send the repeated acknowledgement of M2 in time, so that the sender can know in advance that the packet segment M3 does not reach the receiver. The sender then sent the M5 and M6. After receiving these two packets, the receiver also sends a duplicate acknowledgement to M2. In this way, the sender has received four confirmations of M2 from the receiver, the last three of which are repeated confirmations.

The fast retransmission algorithm also stipulates that the sender should immediately retransmit the unreceived message segment M3 as long as it receives three consecutive repeated acknowledgements, instead of waiting for the expiration of the retransmission timer set by M3.

Because the sender retransmits the unacknowledged segment as soon as possible, fast retransmission can improve the overall network throughput by about 20%.

Fast recovery

The fast recovery algorithm is used in conjunction with fast retransmission. The process has the following two points:

When the sender receives three consecutive repeated acknowledgements, the multiplication reduction algorithm is performed to halve the slow start threshold SSTHRESH.

The difference from slow start is that instead of executing the slow start algorithm (i.e. the congestion window CWND is now not set to 1), instead, the CWND value is set to the value halved after the slow start threshold SSTHRESH, and then the congestion avoidance algorithm (” add up “) is started, making the congestion window slowly and linearly increase.

Timeout retransmission

B detects an error when receiving M1 and discards M1, or M1 is lost during transmission. In both cases, B doesn’t send any information.

As long as A has not received confirmation after A period of time, the packet that was just sent is considered lost, because the packet that was sent before is retransmitted. This is called timeout retransmission.

Timeout retransmission is implemented by setting a timeout timer every time a packet is sent. Destroy the timer if it has not timed out, otherwise regroup.

In this case, A does nothing.