【Computer Network】TCP Flow Control and Congestion Control

1. TCP flow control

The so-called flow control is to let the sender's sending rate not be too fast, and let the receiver have time to receive.

1. Use sliding window to achieve flow control

Note: Each segment is 100 bytes long, and the initial value of the data segment sequence number is set to 1.

The uppercase ACK above the arrow indicates the acknowledgment bit ACK in the header, and the lowercase ack indicates the value of the field. The acknowledgment number field is meaningful only when ACK = 1.

From the above figure, it can be seen that the receiver's host B performs three flow control. 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 not allowed to send data. This sender pause will continue until host B re-sends a new window value.


2. Continuous timer

Consider a situation: in the figure, the segment with the new receive window value sent by B to A is lost, A has been waiting to receive the notification of the non-zero window sent by B, and B has also been waiting for A to send The data. If no other measures are taken, this deadlock situation of waiting for each other will continue forever.

To solve this problem, TCP has a persistence timer for each connection.

(1) As long as one side of the TCP connection receives the zero window notification from the other side, it starts the continuous timer.

(2) If the time set by the continuous timer expires, a zero-window probe segment (carrying only 1 byte of data) is sent, and the other party gives the current window value when confirming the probe segment. .

(3) If the window value is still zero, the party receiving this segment resets the persistence timer . If the window is not zero, then the deadlock stalemate can be broken.

3. Efficiency transmission must be considered

The same mechanism can be used to control the timing of sending TCP segments. E.g:

(1) The first mechanism: TCP maintains a variable equal to the maximum segment length MMS. As long as the data stored in the cache reaches MSS bytes, it is assembled into a TCP segment and sent out.

(2) The second mechanism: The application process of the sender specifies the request to send the segment, that is, the push (PUSH) operation supported by TCP.

(3) The third medium: a timer expires on the sender. At this time, the currently existing cached data is loaded into the message segment (but the length cannot exceed MSS) and sent out.


Nagle's algorithm:

If the sending application process sends the data to be sent to the TCP sending buffer byte by byte, the sender sends the first data byte and buffers the data bytes that arrive later. When the sender receives the confirmation of the first data character, it assembles all the data in the sending buffer into a segment and sends it out, and at the same time continues to buffer the data that arrives later. Continue sending the next segment only after receiving an acknowledgment for the previous segment. When the data arrives faster and the network speed is slower, this method can significantly reduce the network bandwidth used. Nagle's algorithm also stipulates that a segment is sent as soon as the arriving data has reached half the size of the send window or has reached the maximum length of the segment. In this way, the throughput of the network can be effectively improved.

Confused Window Syndrome:

The TCP receiver's buffer is full, and the interactive application process only reads 1 byte from the receive buffer at a time (this frees up 1 byte of receive buffer space), then acknowledges the sender and resets the window. Set to 1 byte (but the datagram sent is 40 bytes long). Then the sender comes with 1 byte of data (note that the sender's IP datagram is 41 bytes long). The receiver sends back an acknowledgment, still setting the window to 1 byte. In this way, the efficiency of the network is very low.

To solve this problem, you can make the receiver wait for a period of time, so that either the receive buffer has enough buffer space to accommodate a longest segment, or wait until the receive buffer has half of the free space. As long as one of these two situations occurs, the receiver sends an acknowledgment message and notifies the sender of the size of the current window. In addition, the sender should not send segments that are too small, but instead accumulate data into segments that are large enough, or half the size of the receiver's cache.


2. TCP congestion control

Congestion control and flow control are closely related, and there are some differences between them. The so-called congestion control is to prevent too much data from being injected into the network , so that the routers or links in the network will not be overloaded . All that congestion control needs to do is that the network can withstand existing network compliance. Congestion control is a global process. On the contrary, flow control often refers to the control of point-to-point traffic , which is an end-to-end problem (the receiver controls the sender) . All flow control does is to throttle the rate at which the sender sends data so that the receiver has time to receive it.


Congestion Control Method

1. Slow start and congestion avoidance

The slow start of slow does not mean that the growth rate of cwnd is slow, but that cwnd = 1 is set first when TCP starts to send a segment, so that the sender only sends one segment at the beginning (the purpose is to detect the network first. congestion), and then gradually increase cwnd. This is of course much slower than suddenly injecting many segments into the network at once according to a large cwnd.

In order to prevent the network congestion caused by the excessive growth of the congestion window cwnd, it is also necessary to set a slow start threshold ssthresh state variable. The slow start threshold is used as follows:

(1) When cwmd < ssthresh, use the slow start algorithm above.

(2) When cwnd > ssthresh, stop using the slow start algorithm and use the congestion avoidance algorithm instead.

(3) When cwnd == ssthresh, either the slow start algorithm or the congestion avoidance algorithm can be used.

The idea of ​​the congestion avoidance algorithm is to increase the congestion window cwnd slowly, and increase the sender's congestion window cwnd by 1 every time a round-trip time (RTT) passes, instead of doubling it, so that cwnd grows slowly according to a linear law. The congestion window grows at a much slower rate.

Slow start threshold setting:

Whether in the slow start phase or in the congestion avoidance phase, as long as the sender judges that the network is congested (the basis is that the acknowledgment is not received on time), the slow start threshold ssthresh is set to half of the sending window when the congestion window occurs (but not less than 2). Then reset the congestion window cwnd to 1 and execute the slow start algorithm. The purpose of this is to quickly reduce the number of packets sent by the host to the network, so that the congested router has enough time to process the backlogged packets in the queue.

2. Fast retransmission and slow recovery

The fast retransmission algorithm first requires the receiver to send a duplicate acknowledgment immediately after receiving an out-of-sequence segment (in order to make the sender know that a segment has not reached the other party as soon as possible ) instead of waiting for it to send data before piggybacking Acknowledgment (in order for both senders to know early that a segment has not reached the other party) instead of waiting for a piggybacked acknowledgment when sending data by itself.

The fast retransmission algorithm stipulates that as long as the sender receives three repeated acknowledgments in a row , it should immediately retransmit the segment that has not been received by the other party, without continuing to wait for the set retransmission timer to expire.

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

(1) When the sender receives three consecutive repeated acknowledgments, the "multiplication reduction" algorithm is executed to halve the slow start threshold. This is to prevent network congestion. Note that the slow start algorithm is not executed next.

(2) When executing the fast recovery algorithm, reset cwnd to the value after the slow start threshold ssthresh is halved, and then start to execute the congestion avoidance algorithm, so that the congestion window increases slowly.


Three, common interview questions:

Congestion avoidance
  Let the congestion window cwind grow slowly, and increase the sender's congestion window cwind by 1 every time a round-trip time (RTT) elapses, instead of doubling it. In this way, the congestion window cwind grows slowly and linearly, which is much slower than the growth rate of the congestion window of the slow start algorithm. 
  Regardless of the slow-start start phase or the congestion avoidance phase, as long as the sender judges that the network is congested (the basis is that no acknowledgment is received), the slow-start threshold ssthresh should be set to half of the sender's window value when congestion occurs (but not less than 2). Then reset the congestion window cwind to 1 and execute the slow start algorithm. The purpose is to quickly reduce the number of packets sent by the host to the network, so that the congested router has enough time to process the backlog of packets in the queue.

Control process

  -[1]. The TCP connection is initialized, and the congestion window cwind is set to 1 segment, that is, cwind=1; 
  -[2]. The slow start algorithm is executed, and cwind grows exponentially until cwind == ssthresh, the start Execute the congestion avoidance algorithm, and cwind grows linearly; 

  -[3]. When the network is congested, update the ssthresh value to half of the ssthresh value before the congestion, reset cwind to 1, and execute according to [2].

flow control:

If the sender sends the data too fast, the receiver may not receive it in time, which will result in data loss.

The flow control of TCP is realized by using the sliding window mechanism . The receiver will include the size of its own receiving window in the returned ACK to control the data transmission of the sender .

但是当某个ACK报文丢失了,就会出现A等待B确认,并且B等待A发送数据的死锁状态。为了解决这种问题,TCP引入了持续计时器(Persistence timer,当A收到rwnd=0时,就启用该计时器,时间到了则发送一个1字节的探测报文,询问B是很忙还是上个ACK丢失了,然后B回应自身的接收窗口大小,返回仍为0(A重设持续计时器继续等待)或者会重发rwnd=x


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324602513&siteId=291194637