Ten core mechanisms of TCP [unfinished]

How TCP works internally

1. Acknowledgment response

Acknowledgment is the core mechanism for reliable transmission.
Reliable transmission does not mean that the data can be sent 100% of the time, but the data is transmitted as much as possible. If the transmission fails, you need to know that the transmission is not successful.
A sends a piece of data to B, and B will return an acknowledgment message (ACK) after receiving it. After A receives the acknowledgment, he knows that the data just now has been successfully sent to B.
However, in the network, the problem of sending first and arriving later is easy to occur, so the arrival order of response messages will change. At this time, it is necessary to number the transmitted data and response messages to solve this problem. TCP is byte-oriented, so the serial number of TCP is also numbered according to bytes. The sequence numbers of the bytes are added sequentially.
Any piece of data (including the response message) is a serial number.
The confirmation sequence number is only available in the response message. The value of the acknowledgment sequence number field of an ordinary message has no meaning.

2. Timeout retransmission

The confirmation response only considers the smooth transmission. If the packet is lost, we have to consider how to retransmit it.
Packet loss involves two situations:

  1. Sent data is lost
  2. The returned acknowledgment response ACK is missing.
    The sender can't judge the kind of packet loss, so the handling of these two cases is the same----introduceretransmission mechanism

Retransmission mechanism: After packet loss, resend the same data again.
But how to judge whether the current transmission is a packet loss, or whether the ack is slow and has not been received yet.
TCP introduces atime threshold, when the sender sends a piece of data, it will wait for the ACK, and start timing at this time. If the ACK is not received within the time threshold, no matter whether the ACK is on the way or has been lost at this time, it is considered to have lost the packet .

What should be done if the receiver receives duplicate messages due to retransmission?

TCP will perform special processing for the transmission of such repeated data: deduplication.
TCP has a "receive buffer" such a storage space (a section of memory in the receiver's operating system kernel).
Each TCP socket object has a receive buffer (and a send buffer).
Host B receives host A In fact, the network card of B reads the data, and then puts the data into the receiving buffer of the corresponding socket of B. Think of this receive buffer as a blocking queue (priority queue/ordered queue). Since the data may be sent first, TCP uses this receiving buffer to reorder the received data, so that the data read by the application is guaranteed to be in order (consistent with the sending order).
According to the serial number of the data, TCP can easily identify whether the two pieces of data in the current receiving buffer are duplicated.
If it is repeated, the subsequent data will be discarded directly. This ensures that the data read by the application calling read must not be repeated.
Subsequent applications use getInputStream and further use read to read data from the receive buffer.

Reliable transmission is the core part of TCP. The reliable transmission of TCP is reflected by confirmation response + timeout
retransmission. The confirmation response describes the smooth transmission situation; the timeout retransmission is when there is a problem with the transmission. The two cooperate with each other to jointly support the reliability of the overall TCP.

3. Connection Management

TCP's connection establishment process (three-way handshake) and disconnection process (four waved hands)

The so-called three-way handshake is essentially a "four-way" interaction. Both parties in the communication need to initiate a "connection establishment" request to the other party, and at the same time, each respond to an ack to the other party. Here, there are actually four information exchanges in total.
But the two interactions in the middle can be combined into one interaction. Therefore, a "three-way handshake" is formed.

The important role of the three-way handshake is to verify whether the sending and receiving capabilities of the two communicating parties are normal.

Because TCP is connected, TCP needs to establish a connection/disconnect. The process of establishing a connection is a three-way handshake.

The meaning of the three-way handshake:

  1. Let the two parties in the communication establish their own identification with the other party.
  2. Verify the sending and receiving capabilities of the communicating parties
  3. During the handshake, the two parties negotiate some important parameters. (In the process of TCP communication, some data communication parties need to synchronize with each other. At this time, such an interaction process is required. Just use the three-way handshake to complete the data synchronization.)

insert image description here

When closing the window, B will return ack immediately after receiving fin (completed by the kernel), and the application will trigger B to send fin when it calls the close method at the right time.

4. Sliding window mechanism

The above-mentioned confirmation response, timeout retransmission, and connection management all provide reliable support for TCP.
The actual cost of introducing reliability is to reduce the transmission efficiency.
The sliding window mechanism essentially reduces the time spent waiting for the confirmation response to ack.
insert image description here
In case of packet loss:

  1. ack lost
    After using window control, some acknowledgments do not need to be resent even if they are lost.
    The acknowledgment indicates that all data from this sequence number onwards have been confirmed to arrive.
  2. Data lost
    If a message segment is lost: if the receiving host receives data with a sequence number that is not the one it should receive, it will return an acknowledgment for the currently received data.
    When the window is large and the message segment is lost, the acknowledgment response with the same sequence number will be repeatedly returned.
    When the sending host receives the same confirmation response three times in a row, it will retransmit the corresponding data. This mechanism is called a high-speed retransmission mechanism.

5. Flow control

Flow control is a window size mechanism for intervening sends.

The larger the window of the sliding window, the higher the transmission efficiency (one piece of time, the more acks to wait for)
However, the window cannot be infinitely large

  1. Does not wait for ack at all, whether the reliability can be guaranteed.
  2. The window is too large and consumes a lot of system resources.
  3. Sent too fast for the receiver to handle.

The processing capability of the receiver is an important constraint basis for the window size. The sending speed of the sender cannot exceed the processing capacity of the receiver.

What flow control does is: coordinate the sending rate of the sender according to the processing capability of the receiver.
How to measure the receiver's processing power: the remaining size of the receiver's buffer

6. Congestion Control

Flow control and congestion control together determine the sender's window size.

Flow control takes into account the processing power of the receiver.
Congestion control describes the processing capabilities of intermediate nodes during transmission.

In essence, congestion control is to gradually find an appropriate window size (appropriate sending rate) through experiments

  1. In the initial stage, since the initial window is small, the size of the window will double in each round without packet loss. (index increase)
  2. When the growth rate reaches the threshold (initial ssthresh), the exponential growth becomes linear growth. (The premise of growth is not to lose packets)
  3. Once the packet is lost during the transmission (network congestion), it means that the sending rate at this time is close to the network limit. At this time, the window size is reduced to a small value at once. (The window size at the time of congestion / 2 is the new ssthresh value), repeat the previous exponential growth and linear growth again.

The congestion window is not a fixed value, but is always changing dynamically, and gradually reaches a dynamic balance process as time goes by.

7. Delayed response

Delayed response is also a mechanism to improve efficiency.
Basic improvements for sliding windows. On the premise that the receiver can handle it, enlarge the window size as much as possible.

Delay: After receiving the data, instead of returning ACK immediately, it returns after a while.
During the waiting time, the receiver's application program can consume some data in the receiving buffer, and the remaining space will be larger at this time.

In fact, the solution adopted for delayed response is that under the sliding window, ACK no longer returns every piece of data, for example, one ACK can be returned every other piece of data.
The change of the size of the remaining space is a complex process, which depends on both the sender's sending and the receiver's processing.

8. Piggybacking

way to improve efficiency.

The most typical mode of server client program is: one question and one answer

Based on the delayed response, it is more likely to be merged at the same timing.

9. Byte stream oriented

Oriented to the byte stream, it introduces a troublesome problem: the problem of sticky packets. The packet refers to the data packet of the application layer.
TCP is oriented to the byte stream.
From the perspective of the transport layer, TCP comes one by one. Put it in the buffer From
the perspective of the application layer, what you see is just a series of continuous byte data.

When the application reads, it only sees a series of continuous byte data. Where can I read a complete application layer datagram? This leads to the sticky packet problem.

How to avoid the sticky package problem: clear the boundary between two packages

  1. agreed delimiter
  2. agree on the length of each packet

10. Abnormal situations

Force majeure occurred during the transmission.

  1. process crash

  2. When the host is shut down (normal process shutdown),
    the kernel will continue to complete four waved hands, which is still a normal disconnection process

  3. The host is powered off (unplug the power supply)

  4. The network cable is disconnected
    , too late to wave,

Guess you like

Origin blog.csdn.net/weixin_44431128/article/details/128981038