Computer Network Learning 09 (TCP Transmission Reliability Guarantee)

1. How does TCP ensure the reliability of transmission?

  1. Data block-based transmission: Application data is divided into data blocks that TCP considers most suitable for sending, and then transmitted to the network layer. Data blocks are called message segments or segments.
  2. Reorder and deduplicate out-of-sequence packets: In order to ensure that no packet loss occurs, TCP gives each packet a sequence number. With the sequence number, the received data can be sorted according to the sequence number, and the data with duplicate sequence numbers can be removed. Data packet deduplication can be realized.
  3. Checksum: TCP will maintain a checksum of its headers and data. This is an end-to-end checksum to detect any changes in the data in transit. If there is an error in the checksum of the received segment, TCP will discard the segment and not acknowledge receipt of the segment.
  4. Timeout retransmission: After the sender sends data, it starts a timer and waits for the destination to confirm receipt of the segment. The receiving entity sends back a corresponding acknowledgment (ACK) for successfully received packets. If the sending entity does not receive an acknowledgment message within a reasonable round-trip time delay (RTT), then the corresponding data packet is assumed to have been lost open in new window and retransmitted.
  5. Flow control: Each side of the TCP connection has a fixed size buffer space, and the receiving end of TCP only allows the sending end to send data that the receiving end buffer can accept. When the receiver is too late to process the sender's data, it can prompt the sender to reduce the sending rate to prevent packet loss. The flow control protocol used by TCP is resizable 滑动窗口协议(TCP utilizes sliding windows for flow control).
  6. Congestion control: When the network is congested, reduce the sending of data.

2. How does TCP implement flow control?

TCP uses sliding windows to implement flow control. Flow control is to control the sending rate of the sender to ensure that the receiver has time to receive.

The window field in the confirmation message sent by the receiver can be used to control the window size of the sender, thereby affecting the sending rate of the sender. Setting the window field to 0 means that the sender cannot send data.

Why is flow control needed?

This is because when the two parties communicate, the speed of the sender is not necessarily equal to the speed of the receiver. If the sending speed of the sender is too fast, the receiver will not be able to handle it. If the receiver can't handle it , it can only store the unprocessed data in it 接收缓冲区(Receiving Buffers) (out-of-sequence data packets will also be stored in the buffer). If the buffer is full and the sender is still sending data madly, the receiver can only discard the received data packets. While packet loss occurs, precious network resources are wasted crazily. Therefore, we need to control the sending rate of the sender so that the receiver and the sender are in a dynamic balance.

Things to note here (common misunderstandings):

  • The sender is not equal to the client
  • The receiver is not the same as the server

TCP is full-duplex (Full-Duplex, FDX) communication, both parties can communicate in two directions, and the client and server may be both the sender and the server. Therefore, each end has a sending buffer and a receiving buffer, and each end maintains a sending window and a receiving window .

The size of the receiving window depends on the limitations of the application, system, and hardware (the TCP transmission rate cannot be greater than the data processing rate of the application). The sending window and the receiving window of both communication parties have the same requirements

The TCP send window can be divided into four parts:

  1. A TCP segment that has been sent and acknowledged (already sent and acknowledged);
  2. TCP segments that have been sent but not confirmed (already sent but not confirmed);
  3. TCP segments that have not been sent but are ready to be received by the receiver (can be sent);
  4. A TCP segment that has not been sent and the receiver is not ready to accept (not sendable).

TCP send window structure diagram:

insert image description here

  • SND.WND : Send window.
  • SND.UNA: Send Unacknowledged pointer, pointing to the first byte of the sending window.
  • SND.NXT: Send Next pointer, pointing to the first byte of the available window.

Available window size = SND.UNA + SND.WND - SND.NXT.

The TCP receive window can be divided into three parts:

  1. Received and acknowledged TCP segments (received and acknowledged);
  2. Waiting for reception and allowing the sender to send TCP segments (can receive unacknowledged);
  3. Unreceivable and the sender is not allowed to send TCP segments (unreceivable).

Diagram of TCP receive window structure:

insert image description here

The size of the receiving window is dynamically adjusted according to the speed at which the receiving end processes data. If the receiving end reads data quickly, the receiving window may be enlarged. Otherwise, it may shrink.

In addition, the size of the sliding window here is only for demonstration purposes, and the actual window size is usually much larger than this value.

3. How is TCP congestion control implemented?

In a certain period of time, if the demand for a certain 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 routers or links in the network will not be overloaded.

Congestion control has a premise that the network can bear the existing network load.

Congestion control is a global process involving all hosts, all routers, and all factors related to reducing network transmission performance. On the contrary, flow control is often the control of point-to-point traffic, which is an end-to-end problem .

What flow control has to do is to suppress the rate at which the sender sends data so that the receiver can receive it in time.

insert image description here

For congestion control, the TCP sender maintains a 拥塞窗口(cwnd)state variable of . 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 one of the congestion window and the receiving window of the receiver.

TCP congestion control uses four algorithms, viz 慢开始 、 拥塞避免 、快重传 和 快恢复. At the network layer, routers can also adopt appropriate packet discarding strategies (such as active queue management AQM) to reduce 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 injected into the network immediately, it may cause network congestion, because the compliance of the network is not yet known. Experience shows that a better method is to detect first, that is, gradually increase the sending window from small to large, that is, gradually increase the value of the congestion window from small to large. cwndThe initial value is doubled 1every time a propagation round passes cwnd.
  • Congestion avoidance: The idea of ​​the congestion avoidance algorithm is to cwndslowly increase the congestion window, that is, to add 1 to the sender's cwnd every time a round-trip time RTT passes.
  • Fast retransmit and fast recovery: In TCP/IP, fast retransmit and recovery (FRR) is a congestion control algorithm that recovers lost packets . Without FRR, if a packet is lost, TCP will use a timer to ask the transmission to pause . During this pause, no new or duplicate packets are sent. With FRR, if the receiver receives a data segment out of sequence, it immediately sends a duplicate acknowledgment to the sender . If the sender receives three duplicate acknowledgments, it assumes that the data segments indicated by the acknowledgments are lost and immediately retransmits the lost data segments. With FRR, there is no delay due to pauses required for retransmissions. Fast retransmission and recovery (FRR) works most effectively when there are individual packet losses. It does not work very efficiently when multiple data packets are lost within a short period of time.

4. Do you understand the ARQ protocol?

Automatic Repeat-reQuest (ARQ) is one of the error correction protocols of the data link layer and transport layer in the OSI model . It achieves reliable information transmission on the basis of unreliable services by using 确认and these two mechanisms. 超时If the sender does not receive an acknowledgment (Acknowledgements, we often call ACK) within a period of time after sending, it will usually resend until it receives an acknowledgment or retries more than a certain number of times.

ARQincluding stop waiting ARQ 协议and 连续 ARQ 协议.

4.1 Stop waiting for the ARQ protocol

The stop-and-wait protocol is to achieve reliable transmission. Its basic principle is to stop sending every time a packet is sent and wait 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, it means that the transmission was not successful and needs to be resent until the confirmation is received before sending the next packet;

In the stop-and-wait protocol, if the receiver receives a duplicate packet, it discards the packet, but sends an acknowledgment at the same time.

1) No error: the sender sends a packet, the receiver receives it within the specified time, and replies with confirmation. The sender sends again.

2) When an error occurs (timeout retransmission) : The timeout retransmission in the stop-waiting protocol means that as long as the confirmation is not received after a period of time, the previously sent packet is retransmitted (the packet sent just now is considered lost). Therefore, a timeout timer needs to be set every time a packet is sent, and its retransmission time should be longer than the average round-trip time of data in packet transmission. This automatic retransmission method is often called automatic repeat request ARQ . In addition, if a duplicate packet is received in the stop-and-wait protocol, the packet is discarded, but an acknowledgment is sent at the same time.

3) Lost acknowledgments and late acknowledgments:

  • Lost acknowledgments: Acknowledgment messages are lost in transit. When A sends an M1 message and B receives it, B sends an M1 acknowledgment message to A, but it is lost during transmission. But A does not know that after the timeout 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 not deliver it to the upper layer. 2. Send a confirmation message to A. (It will not be considered that it has been sent, so it will not be sent again. If A can retransmit, it proves that B's confirmation message is lost).
  • Acknowledgment Late: The acknowledgment message is late in transit. A sends the M1 message, B receives it and sends an acknowledgment. If the confirmation message is not received within the timeout period, A retransmits the M1 message, and B still receives and continues to send the confirmation message (B has received 2 copies of M1). At this time, A has received the confirmation message sent by B for the second time. Then send other data. After a while, A received the confirmation message sent by B for the first time to M1 (A also received 2 confirmation messages). The processing is as follows: 1. After receiving duplicate confirmations, A directly discards them. 2. After B receives the duplicate M1, it also directly discards the duplicate M1 .

4.2 Continuous ARQ protocol

Continuous ARQ protocol improves channel utilization. The sender maintains a sending window, and the packets within the sending window can be sent out continuously without waiting for confirmation from the other party . The receiver generally adopts cumulative confirmation, 对按序到达的最后一个分组发送确认,表明到这个分组为止的所有分组都已经正确收到了.

Advantages: High channel utilization, easy to implement, even if the confirmation is lost, there is no need to retransmit.

Disadvantages: It cannot reflect to the sender the information of all the packets that the receiver has received correctly. For example: the sender sent 5 messages, and the third message in the middle was lost (No. 3). At this time, the receiver can only confirm the sending of the first two messages. The sender has no way of knowing the whereabouts of the last three packets, but has to retransmit all the last three packets once. This is also called Go-Back-N (Go Back N), which means that you need to go back and retransmit the N messages that have been sent.

Guess you like

Origin blog.csdn.net/ldy007714/article/details/130412343