How the TCP protocol guarantees reliable transmission
Posted Jun 16, 2020 • 6 min read
How the TCP protocol guarantees reliable transmission
- The means of TCP protocol to ensure reliable transmission:
- The application data is divided into data blocks that TCP considers the most suitable for sending.
- TCP numbers each packet sent, the receiver sorts the data packets, and sends the ordered data to the application layer.
- Checksum:TCP will maintain the checksum of its header and data. This is an end-to-end checksum, the purpose is to detect any changes in the data transmission process. If the checksum of the received segment is in error, TCP will discard the segment and not acknowledge receipt of the segment.
- The receiving end of TCP will discard duplicate data.
- Flow control:Each side of the TCP connection has a fixed size of buffer space. The TCP receiving end only allows the sending end to send data that the receiving end buffer can accept. When the receiver has no time to process the sender's data, the sender can be prompted to reduce the sending rate to prevent packet loss. The flow control protocol used by TCP is a variable size sliding window protocol.(TCP uses sliding windows for flow control)
- Congestion control:When the network is congested, reduce the sending of data.
- ARQ protocol: is also to achieve reliable transmission, its basic principle is to stop sending after each packet is sent, waiting for the other party to confirm. Send the next packet after receiving the confirmation.
- Timeout retransmission:When TCP sends a segment, it starts a timer and waits for the destination to confirm receipt of the segment. If a confirmation cannot be received in time, the segment will be resent.
2. ARQ agreement
**Automatic Repeat-reQuest(ARQ) is one of the error correction protocols of the data link layer and the transport layer in the OSI model. It uses the two mechanisms of acknowledgment and timeout to achieve reliable information transmission on the basis of unreliable services. If the sender does not receive an acknowledgment frame within a period of time after sending, it will usually resend it. ARQ includes stop waiting ARQ protocol and continuous ARQ protocol.
2.1 Stop waiting for ARQ protocol
- The stop-and-wait protocol is for reliable transmission. Its basic principle is to stop sending after each packet is sent, waiting for the other party to confirm(reply ACK). If after a period of time(after the timeout period), the ACK confirmation is still not received, indicating that the transmission was not successful and needs to be resent until the next packet is sent after receiving the confirmation;
- In the stop-and-wait protocol, if the receiver receives a duplicate packet, it discards the packet, but at the same time, it also sends an acknowledgment;
Disadvantages:Low channel utilization and long waiting time
2.1.1 No error conditions
The sender sends the packet, the receiver receives it within the specified time, and responds with confirmation. The sender sends again.
2.1.2 An error occurs(timeout retransmission)
Stop-waiting overtime retransmission in the protocol means that as long as the acknowledgment has not been received for a certain period of time, the previously sent packets are retransmitted(think that the packets just sent have been lost). Therefore, every time a packet is sent, a timeout timer needs to be set, and its retransmission time should be longer than the average round-trip time of data transmission in the packet. This automatic retransmission method is often called Automatic Retransmission Request ARQ. In addition, if a repeated packet is received in the stop-and-wait protocol, the packet is discarded, but at the same time, an acknowledgment is also sent. Continuous ARQ protocol can improve channel utilization. Sending maintains a sending window, and all packets in the sending window can be sent continuously without waiting for the other party to confirm. The receiver generally uses cumulative acknowledgment to send an acknowledgment to the last packet arriving in sequence, indicating that all packets to this packet position have been received correctly.
2.1.3 Confirm lost and late
- Confirmation Loss:The confirmation message is lost during transmission. When A sends an M1 message and B receives it, B sends an M1 confirmation message to A, but it is lost during transmission. However, A does not know that after the timeout period expires, A retransmits the M1 message, and B takes the following two measures after receiving the message again:1. Discard the duplicate M1 message and do not deliver to the upper layer. 2. Send a confirmation message to A.(It will not be considered that it has been sent, and it will not be sent again. A can retransmit, which proves that B's confirmation message is lost).
- Confirmation is late:Confirmation message is late during transmission. A sends M1 message, B receives and sends confirmation. If no confirmation message is received within the timeout period, A retransmits the M1 message, and B still receives and continues to send the confirmation message(B receives 2 copies of M1). At this time, A receives the confirmation message sent by B for the second time. Then send other data. After a while, A received the confirmation message for M1 sent by B for the first time(A also received 2 confirmation messages). The processing is as follows:1. A receives the repeated confirmation and discards it directly. 2. After B receives the duplicate M1, it also directly discards the duplicate M1.
2.2 Continuous ARQ protocol
Continuous ARQ protocol can improve channel utilization. The sender maintains a send window, and all packets in the send window can be sent continuously without waiting for the other party to confirm. The receiver generally uses cumulative acknowledgment to send an acknowledgment to the last packet that arrives in sequence, indicating that all packets up to this packet have been received correctly.
Advantages:The channel utilization rate is high and easy to realize, even if the confirmation is lost, there is no need to retransmit.
Disadvantages:Cannot reflect to the sender the information of all packets that the receiver has received correctly. For example:the sender sent 5 messages, and the third one in the middle was lost(No. 3). At this time, the receiver can only confirm the first two. The sender cannot know the whereabouts of the last three packets, but has to retransmit the last three all at once. This is also called Go-Back-N(back-off N), which means that it needs to back-off to retransmit the N messages that have been sent.
3. Sliding window and flow control
TCP uses a sliding window to achieve flow control. Flow control is to control the sending rate of the sender and ensure that the receiver has the time to receive it.The window field in the confirmation message sent by the receiver can be used to control the size of the sender window, thus affecting the sender's sending rate. If the window field is set to 0, the sender cannot send data.
4. Congestion control
During a certain period of time, if the demand for a resource in the network exceeds the available part that the resource can provide, the performance of the network will deteriorate. This situation is called congestion. 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. There is a prerequisite for congestion control, that is, the network can withstand the existing network load. Congestion control is a global process involving all hosts, all routers, and all factors related to reducing network transmission performance. In contrast, flow control is often the control of point-to-point traffic and is an end-to-end problem. All flow control has to do is to suppress the rate at which the sending end sends data so that the receiving end has time to receive it.
For congestion control, the TCP sender maintains a state variable of congestion window(cwnd). The size of the congestion control window depends on the degree of network congestion and changes dynamically. The sender makes its sending window the smaller of the congestion window and the receiving window of the receiver.
TCP's congestion control uses four algorithms, namely slow start, congestion avoidance, fast retransmission and fast recovery. At the network layer, routers can also use appropriate packet dropping strategies(such as active queue management AQM) to reduce the occurrence of network congestion.
- Slow start:The idea of the slow start algorithm is that when the host starts to send data, if a large number of data bytes are immediately injected into the network, it may cause network congestion, because the network is not yet known. Experience has shown that a better method is to probe first, that is, to gradually increase the transmission window from small to large, that is, to gradually increase the value of the congestion window from small to large. The initial value of cwnd is 1, and cwnd doubles every time it passes a propagation round.
- Congestion avoidance:The idea of the congestion avoidance algorithm is to make the congestion window cwnd increase slowly, that is, every time a round-trip time RTT passes, the sent cwnd is increased by 1.
- Fast retransmission and fast recovery:In TCP/IP, fast retransmit and recovery(FRR) is a congestion control algorithm that can quickly recover lost data packets. Without FRR, if a packet is lost, TCP will use a timer to request transmission pause. During this period of suspension, no new or duplicate packets are sent. With FRR, if the receiver receives an out-of-sequence data segment, it will immediately send a repeat acknowledgment to the transmitter. If the transmitter receives three repeated acknowledgments, it will assume that the data segment indicated by the acknowledgment is missing and immediately retransmit these missing data segments. With FRR, there will be no delay due to the suspension requested during the retransmission. When a single packet is lost, fast retransmission and recovery(FRR) can work most effectively. When multiple data packets are lost within a short period of time, it cannot work very efficiently.