TCP is one of the most commonly used transport layer protocols, providing connection-oriented, reliable transport services to the upper layer protocols. In order to provide the maximum throughput possible, TCP uses the sliding window and congestion window mechanism to control the amount of data delivered to the network at a certain point in time, so as to occupy as much available bandwidth as possible and do not cause congestion. There is usually a bottleneck in the network, where packets are backlogged or even dropped. Therefore, the key to TCP flow control is to control the backlog of packets at the bottleneck, and too fast backlog will lead to congestion. How does congestion form?What causes congestion?How does the sender perceive congestion?How can I alleviate congestion?
byte in flight
Before we begin this article, let's introduce the concept of byte in flight A more figurative translation of "bytes in transit"1 refers to the number of bytes that have been sent but have not yet received an acknowledgment from the ACK, which is approximately equal to the sum of the number of bytes of packets currently being transmitted and backlogged in the network. The size of bytes in flight has a direct impact on the backlog of packets at the bottleneck. According to the concept of sliding window, it can be known that the size of byte in flight is limited by the sending window 2 (byte in flight <= sending window), and congestion is formed.
How is congestion formed?What may seem like a simple question, in fact, is not a clear picture in everyone's mind. In a nutshell, the main cause of congestion is that the data delivered by the data sender to the network exceeds the carrying capacity of the network. Just as the flow of a water pipe depends on the smallest part, the carrying capacity of the network depends on the path with the smallest available bandwidth on the network, that is, where there is a bottleneck. Network device interfaces often have buffers, and excess data cannot be sent from the bottleneck path, so they have to queue up in the buffer area, and the size of the buffer is limited, and the amount of backlog data exceeds the size of the buffer area, and the discarding will occur, which is like pouring water through a funnel.
Congestion can occur due to the following:
There is a bottleneck in the network that has a throughput that is less than the amount of data sent by a node per unit of time.
The receive buffer is greater than the available buffer at the bottleneck.
1. What is network congestion?
It refers to the situation that when the number of packets transmitted in the packet switching network is too large, the network transmission performance is degraded due to the limited resources of the storage node. When a network is congested, data loss, latency increases, throughput decreases, and even congestion collapse occurs in severe cases. Typically, network congestion occurs when there is an excessive increase in load on the network, resulting in degraded network performance.
Causes of congestion:
2. The cache of the exchange node is limited.
3. The processing power of the node is limited.
At the end of the day, network congestion occurs when we appeal to the need for > available resources.
2. How does TCP deal with network congestion?
In fact, to solve the above problems, we only need to ensure two points, first, the IP packets we send cannot be lost, and second, we must ensure that the sending speed is maximized.
Sliding window: TCP deals with network congestion, mainly involving a dynamically changing window (congestion window, can also be called sliding window), the number of windows is related to the degree of network congestion, when the network situation is good, the congestion window continues to increase, and the sender's window naturally increases, but the receiver's acceptance capacity is limited, and it does not change when the sender's window reaches a certain size. In the general TCP protocol, the number of congestion windows is 65536, when it is less than this value, the slow start algorithm is used to increase the number of IP packets sent, and when it is greater than or equal to this value, the congestion control algorithm is used to increase the number of IP packets sent.
How the sender determines congestion
When we send a packet, if we do not receive an acknowledgement packet for a period of time, then we can assume that the network is congested.
Slow Start Algorithm:
When a sender starts sending a datagram, congestion can occur if a large amount of data is immediately injected into the network. The slow-start algorithm detects the network status when the sender first starts to send datagrams, and if the network is in good condition, the sender can correctly accept the acknowledgement message every time it sends a message. Then increase the size of the congestion window from small to large, i.e., increase the number of IP packets sent. Generally, the number of IP packets sent is increased by power to the power. For example, if the sender sets cwnd (congestion window) = 1 to send the first packet segment m1, after the receiver receives m1, the sender receives the acknowledgment from the receiver and increases the cwnd to 2, then the sender sends m2 and m3, and the cwnd increases to 4 after the sender receives the acknowledgment sent by the receiver.
Congestion Control Algorithm:
It is to make the CWND increase slowly rather than double, and each time the round-trip time is experienced, the CWND increases by 1 instead of doubling, so that the CWND grows slowly, much slower than the slow start.
Congestion avoidance: As we mentioned above, the threshold for the number of congestion windows (ssthresh) is generally 65536, so there is the following algorithm:
1. When CWND < ssthresh, use the slow-start algorithm, 2. When CWND > ssthresh, use the congestion control algorithm and disable the slow-start algorithm.
3., when cwnd = ssthresh, both algorithms are fine.
When there is network congestion, the number of threshold ssthresh is halved, then the number of cwnd is changed to 1, and then the slow-start algorithm is used, which quickly reduces the number of hosts transmitting data to the network, so that the congested router can process the packets piled up in the queue.
Example: For example, if our CWND (i.e. the number of IP packets we want to send) is 1, then we have a threshold of 16.
1. At the beginning of the slow-start algorithm, the initial value of CWND is 1, and the congestion window will increase by 1 every time the sender receives an ACK, and when SSTHRESH = CWND, the congestion control algorithm will be started, and the congestion window will increase according to the law.
The fast retransmission algorithm requires that the receiver first send a duplicate acknowledgment as soon as it receives an out-of-order packet segment, rather than waiting for the receiver to send the data before piggybacking on the acknowledgement. The receiver successfully accepts M1 and M2 from the sender and sends ACK to ACK, but now the receiver does not receive M3 but receives M4, obviously the receiver cannot confirm M4 because M4 is an out-of-order packet segment. If the receiver does nothing according to the principle of reliable transmission, but according to the fast retransmission algorithm, when receiving packet segments such as M4 and M5, it repeatedly sends ACKs of M2 to the sender, and if the receiver receives three duplicate ACKs in a row, the sender does not have to wait for the retransmission timer to expire, because the sender retransmits the unacknowledged packet segments as soon as possible.
Fourth, recover quickly.
1. When the sender receives three acknowledgments in a row, the multiplicative reduction algorithm is executed to reduce the slow start threshold (ssthresh) by half, but the slow start algorithm is not executed next.
2. At this time, instead of executing the slow start algorithm, set the cwnd to half of the ssthresh, and then execute the congestion avoidance algorithm to make the congestion window increase slowly.