UDP protocol realizes reliable network transmission strategy

1 Reliable transmission

The reliability of UDP is actually based on the strategy of reliable transmission of TCP, and the essence of it is simplified. First we need to understand how TCP guarantees reliability.

1.1 What are the common strategies for ensuring reliability?

(1) ACK mechanism
(2) retransmission mechanism
(3) sequence number mechanism
(4) rearrangement mechanism
(5) window mechanism

The ACK mechanism is easier to understand, that is, when a packet is received, an ACK is sent for confirmation. The sequence number mechanism and the rearrangement mechanism are also used more. The network may receive data packets out of order. At this time, it is necessary to add the sequence number of each packet at the beginning of transmission, and then rearrange it after receiving. The retransmission and window mechanisms are more complicated, and are described separately below.

1.2 Retransmission mechanism
1.2.1 Three strategies for retransmission
1. Stop-wait protocol
This is an old method with low efficiency, mainly because every time a frame of data is sent, it needs to receive a reply from the other party before sending the next one. Frame data, mainly for the mode of dialogue, one question and one answer.

insert image description here

2. The method of rollback N frame retransmission
is still in use at present, which is to send a series of consecutive packets. When there is a packet loss in the middle, it will reply to confirm the maximum value of continuously received packets, corresponding to the loss of packets. All packets need to be retransmitted.

insert image description here 

Private message me to receive the latest and most complete learning and improvement materials in 2022 , including ( C/C++ , Linux , FFmpeg , webRTC , rtmp , hls , rtsp , ffplay , srs )

 

3. Selective retransmission

It is found that the method of rolling back N frames from the above is a waste of bandwidth, so we think of one method, that is, we only retransmit the packets lost by the other party. You can tell the sender in the forward direction, which packet is lost, and just retransmit the lost packet.

1.2.2 The timing of retransmission
(1) If the sender does not receive the ack, it will retransmit the corresponding packet;
(2) If the sequence number received by the receiver is missing, then a message is sent to tell the sender to retransmit the corresponding packet.

1.2 Flow Control
1. Purpose of Flow Control The
main purpose of flow control is to ensure smooth network bandwidth and prevent bandwidth waste. The mechanism introduced is mainly to control flow on the sender side, that is, to control the sender's sending rate.
2. Flow control principle
Use the windows of the sender and receiver to adjust the current flow. Boundary condition, when the receiver window is 0, the sender needs to stop sending. That is, there is no space to receive, and the transmission needs to be terminated.
3. After termination of transmission, when will the transmission rate resume?
(1) When the receiver reads data, it needs to update the window to the sender.
(2) Send a probe packet to inquire at intervals. The receiver needs to respond with the window size.

1.3 Congestion Control
Two strategies are introduced here. Most people are familiar with this picture.

1.3.1 Slow start

Slow start: In fact, it is not slow, mainly because the starting point is low, starting from a packet, and then increasing exponentially.

1.3.2 Quick Recovery

Fast recovery: Fast recovery is not fast. It adopts a linear increase method, mainly starting from half of the threshold.

1.4 Basic Concepts
RTO:
Timer timeout time, that is, data packet retransmission is required after timeout.
Note:
tcp timeout calculation: first retransmission time: RTO2, after three consecutive packet loss: RTO8, the delay is very large.
RTT:
Network round-trip delay, you need to record the timestamps of the sender and receiver.

2 UDP reliability design

Based on the reliable transmission of the application layer, there is no essential oil in the reliable design of udp, and it needs to be based on specific usage scenarios.

2.1 Protocol Design

协议设计:
|同步字|总字节大小|分片数|分片编号|载荷大小|预留|荷载|
typedef struct packet
{
    int recv_pieces;								//当前已经接收的数量;
    int total_size; 								//总数据大小
    int left; 										//最后一片大小
    int paiece_size; 								//分片大小
    int recv_len;  									//接收数据长度
    uint8_t *recv_buf;								//保存接收数据
    uint8_t * send_pt; 								//指向发送数据buffer
   	uint8_t piece_buf[PIECE_FIX_SIZE+HEAD_SIZE+1] ;	//单帧的buf
   	circular_buffer_t *circular_buffer; 			//环形缓存
}packet;

2.2 Specific implementation
The specific implementation will not be written here, but there are several points that need to be paid attention to. Several points that need to be considered for UDP reliable transmission, one is the retransmission mechanism, the packet loss needs to be retransmitted, either ACK or NACK can be used. method; the second is the rearrangement mechanism, we must add a buffer to rearrange the data when we receive out-of-order data; the third is the timeout mechanism, which requires a retry if the reply from the other party is confiscated for a long time; fourth, the traffic Control, this part is generally not considered in the local area network, it is more complicated to implement, and the benefits are not so great.

3 UDP usage scenarios

  1. real-time consideration
  2. Resource consumption considerations

The specific scenarios mainly include the following aspects:

1 Audio and video calls (network delay, tcp cannot control retransmission, the delay is too large, udp can control the retransmission time);
2 Game development (real-time operation: King of Glory; transmission location, delay will cause lag)
3 DNS query (one question and one answer; one packet is enough, if the packet is lost, it can be retransmitted directly)
4 IoT device monitoring, using battery (active state power consumption, sleep, the amount of data sent is not large)
5 Heartbeat mechanism to monitor whether the device is offline heartbeat packet

4 Some pits that need to be paid attention to in UDP programming

4.1 UDP out-of-order problem
Generally, the receive buffer is used for sorting

4.2 Sending packet size
sendto sends 1400 bytes at a time.
It needs to be less than the minimum MTU. The Ethernet data frame is generally 1500 bytes
. Experience value: 1400 (real-time communication) 500 (game) mainly because the packet is relatively small and has a certain priority
4.3 Receive data
Recvfrom needs to read the message completely at a time,
udp can only receive one packet at a time, there is no boundary problem (message transmission),
tcp can receive a part of data at a time, and there is a sticky packet problem (streaming transmission)

Guess you like

Origin blog.csdn.net/m0_60259116/article/details/124331666