Analysis of realization principle of TCP protocol reliability data transmission

Source: Internet
Author: User

http://blog.csdn.net/chexlong/article/details/6123087

The TCP protocol is a connection-oriented protocol that provides reliable data transfer between different host processes. The TCP protocol assumes that the lower layer protocol (such as IP protocol) used by the network stack is unreliable, and its own mechanism guarantees the reliability of the data transmission. In the current network stack protocol family, in applications that require reliable data transmission, the TCP protocol is preferred and sometimes the only option. The TCP protocol is based on the concept of the network packet Transport protocol, which was first proposed by Cerf and Kahn[1]. The TCP protocol is designed to conform to a layered protocol structure that works in the transport layer in the Iso/osi seven-tier network model, using services provided by network layer protocols, such as the most common IP protocols. The Network layer protocol tries its best to transmit the data provided by the upper layer but does not guarantee the reliability of the data transmission. The reliability guarantee must be provided by the upper layer protocol (such as the TCP protocol). the main tasks of the Network layer protocol are:

1> enables packet routing between different networks (hosts).

The 2> provides packet fragmentation on the sending side (or broker) to satisfy the packet size PMTU (PATH-MTU).

The 3> provides packet fragmentation reorganization on the receiving side.

4> is responsible for packet priority, security and other issues.

Transport Layer Protocol (mainly for TCP protocol) The main work done is (not all transport layer protocols need to provide these features such as UDP protocol does not provide reliable data transmission):

1> provides multiplexing.

2> implements the basic data transfer function.

3> establish a communication channel.

4> provides flow control .

5> provides data reliability Transmission Assurance .

Data reliability transmission is one of the most important aspects, and it is also the most important characteristic that the TCP protocol distinguishes from other protocols. The so-called data reliability transmission not only refers to the successful transfer of data from the local host to the remote host, the data reliability transmission includes the following content:

The 1> is capable of handling corrupted issues during data transfer.

2> is able to handle data deduplication problems.

3> is able to identify data loss and resolve it effectively.

4> is able to handle the problem of receiving end data scrambling.

1. basic principle of reliability data transmission for TCP Protocol

TCP protocol must provide a solution to all of these issues in order to guarantee the transmission of the data it claims to be reliable. The TCP protocol specification and most current TCP protocol implementation code all using data retransmission and data acknowledgment response mechanism to complete TCP The reliability data transfer of the Protocol . Data time-out retransmission and data response mechanism basic premise is the There seems to be a problem, it should be the transmission of each package ) , which is what we usually call serial numbers. data time-out retransmission is the sending end of a data packets sent out, after a fixed period of time if not received acknowledgement of the packet, then (assuming that the packet is lost during transmission) resend the packet. And the data acknowledgment answer refers to the receiving end after successfully receiving a valid packet, send a confirmation reply packet to the sending side host, the acknowledgment answer packet contains the answer sequence number refers to the received data in the data time-out retransmission and The data acknowledgment answer and the is TCP The protocol provides Reliability data transfer Core Essence .

1) Meaning of the answer sequence number in the Data Acknowledgement response packet

The answer sequence number is not the meaning shown on its surface, it actually refers to the sequence number of the next byte that the receiving side wants to receive. So the receiving side after successfully receiving some data, the reply sequence number sent by the reply packet is set to the serial number of the last byte in the data plus one. Therefore, the answer sequence number, called the request sequence number, is sometimes more appropriate in its meaning. The answer sequence number is set in the answer serial number field in the TCP header. The Serial Number field in the TCP header indicates the sequence number (n) of the first byte of the data contained in the packet containing the TCP header. If the receiving end receives the packet successfully and there is no loss of the packet, the answer sequence number in the reply packet sent by the receiving end should be: N+len. Where Len is the data length of the received packet. The answer sequence number is also the sequence number of the first byte in the next packet to be sent by the sending side (which is why the answer sequence number is called the request sequence number above).

2) Cumulative effect in data acknowledgement response

The response of the receiving end to the received data in the TCP protocol is cumulative. The cumulative meaning is two:

The 1> answer sequence number is gradually incremented , which is consistent with the increment of the transmit-side data number.

2> cannot make a leap-forward data response .

The so-called non-spanning data response can be described as an example of packet chaos arrival. If the transmission path chosen by the sender is different, the packet with the larger serial number is sent to the receiving end first, and the packet with the smaller sequence number is temporarily delayed in the network due to a line problem (or router failure), at which point the receiving end cannot answer the data with the larger serial number. If the receiving end needs to send a reply packet, the answer sequence number should still be set to the request of a packet with a smaller sequence number (note that the answer serial number refers to the sequence number of the next byte that the receiving side wants to receive, so it is more appropriate to call the answer packet the data request packet during the data transfer). For example, if the receiver's current answer sequence number is 201, indicating that the receiver is waiting for the sending side to send the data starting from 201, then the sender sends two packets sequentially, the first packet has a data sequence number range of 201-300, and the second packet has a data sequence number range of 301-400. If the second packet arrives at the receiving end first because a different transmission path is selected, and the first packet is delayed in the network for a period of time, the receiving side is not allowed to answer the second packet, that is, the acknowledgement reply packet that answers the sequence number 401 cannot be sent. Instead, the reply packet that answers the serial number 201 is sent continuously until the data arrives for that sequence number. What we call the fast retransmission mechanism is that the sender needs to re-transmit the data represented by the serial number immediately after receiving a reply packet of 3 identical serial numbers. Because there is a high likelihood of packet loss, as in the previous example, if the first packet is lost in the network and the same packet is sent end multiplicity because the same line was selected and failed to reach the receiving end, the receiving end will continuously send an answer packet that answers the sequence number 201 without setting the answer sequence number to 401. Note at this point the receiving end has received the data from the serial number from 301-400.

3) The retransmission response mechanism is combined with the serial number:

1> can handle the problem of data being destroyed during transmission.

First, verify that the data in the packet has errors by verifying the received packet. If so, simply discard or send a reply packet to re-request the data. The sending side will resend the data after waiting for a period of time. In essence, the resolution of data transmission errors is done through the retransmission mechanism.

2> can handle receiving duplicate data problems.

The data duplication problem can be found by using serial number first. Since each transmitted data is given a unique serial number, if the two data reached has an overlapping sequence number (as caused by the retransmission of the sending packet), then the data duplication problem occurs, and only one copy of the remaining copy is discarded at this time. Data overlap in multiple packets is resolved in a similar manner. Essentially, the resolution of the data duplication problem is done by checking the serial number.

3> can detect data loss and solve it effectively.

First, it must be stated that the concept of packet loss here refers to the fact that the packet that should arrive is not arrived at a reasonable time, not what we normally understand. So packet loss and packet scrambling are sometimes difficult to distinguish between judgment and software processing. Data loss judgment is speculative, we can not determine that a packet must be lost in the transmission process, mostly delayed in the network, that is, the real problem is only the packet chaos arrives. The main basis for distinguishing between the two is whether the serial number "hole" caused by this potentially lost packet can be filled within a reasonable time. One obvious result of possible data loss is the serial number discontinuity of the data received at the receiving end. If the receiver receives only packets from 1 to 100, and then receives a packet with a sequence number from 200 to 300, and in a reasonable

Time (which basically excludes the problem of Chaos), the serial number from 101 to 199 of the data has not arrived, it means that packets containing serial numbers from 101 to 199 are likely to be lost during transmission (or have a very abnormal delay). Another interference factor for the loss of data packets is the retransmission mechanism of the sending side, if a serial number of the packet is lost in the network, resulting in a packet with a sequence number that arrives at the receiving end in advance, it will temporarily cause the serial number to be discontinuous, but because the sending side does not receive the acknowledgement reply, The packet with the previous sequence number is re-sent, and if the receiving end receives a retransmission packet, it simply causes the packet to arrive at the receiving end. Regardless of the substance, if the software implementation determines that the packet is lost, the receiving end will force the sender to resend the data by continuously sending the request packet (also known as the reply packet) to the data response packet and the cumulative effect of the data response, as described in the previous article. Usually the sender itself will spontaneously retransmit these data that are not confirmed by the other side, but due to the retransmission mechanism using the exponential backoff algorithm, each retransmission interval will be doubled, so through the sender of the proactive retransmission mechanism to recover longer, and the receiving side through the continuous transmission of these lost data requests, After receiving three such request packets (three request packets with the same request sequence number-that is, the answer sequence number mentioned above), the sender will immediately trigger the retransmission of the data, which is called fast recovery or fast retransmission mechanism. In essence, the solution to the data loss problem is done through the data retransmission mechanism. In this process, the serial number and the data acknowledgment answer play a key role.

4> is able to handle the problem of receiving end data scrambling.

If there are multiple transmission paths on both sides of the communication, there is a possibility of data disorder, that is, the data with the larger serial number arrives before the data with the smaller serial number, and the sending end is sent by the sequence number from small to large. The essence of data chaos is that the data have arrived successfully, but the order of arrival is not satisfactory. The solution to this problem is relatively simple, just to reorder the data. Essentially, the problem of data scrambling is solved by sequencing the data sequence number.

2. Basic principle of reliability data transmission software for TCP protocol

From the above, the serial number, data time-out retransmission and data acknowledgement response mechanism guarantee the TCP protocol reliability transmission requirements. Because of the need to number the data sent, but also need to answer the data received, so the communication between the use of TCP protocol must be a mechanism to understand the other party's initial sequence number. The legitimacy of the received data can only be judged from the outset if the exact initial sequence number is known. You also need to maintain a serial number for each other's answer locally to follow each other's data requests at any time. When the last communication channel is closed, it is possible to know whether the number of local sends has been fully received by the other side, and the other reply serial number is also playing an important role in controlling the local data flux: The local sending serial number minus the other answer serial number can immediately know how much of the data currently sent has not been answered.

In summary, reliability transmission requires both parties to maintain the following serial numbers:

Snd. The next serial number that the NXT will send locally. This variable corresponds to the serial Number field in the TCP header. A sequence number that represents the first byte of the data contained in the packet. Each time a packet is sent, the variable needs to be updated: SND.NXT = SND. NXT + The length of data contained in the packet sent snd.acked

The serial number that the other party has answered so far to send the data locally, in other words, SND. Acked+1 represents the first (smallest) sequence number in a dataset that has been sent out locally but has not yet been answered by the other. RCV. The next serial number that NXT locally wants to receive. The serial number is called the answer sequence number, also known as the request sequence number, in the local sent response message, the TCP header in the answer Sequence number field is set to the value of the variable, indicating the local want to receive from the other side of the next byte sequence number.

Figure 1 () shows the TCP header format. The Serial Number field corresponds to the previous SND.NXT variable, and the answer Sequence number field corresponds to the RCV.NXT variable in the previous article. The ACK flag bit is set to 1 to indicate that this is a reply packet. In fact, for the TCP protocol, after a successful connection is made, the ACK flag bits of all packets sent thereafter are set to 1, that is, sending the answer while the normal data is being transmitted, so processing can reduce the number of packets transmitted over the network.

3. The necessity of establishing a connection with the TCP protocol

Figure 1TCP The SYN flag bit in the header format is used only in the process of establishing a TCP connection, the process of TCP connection is called a "three-way handshake" connection, that is, the common communication between the two sides need to transmit three packets to successfully establish a TCP connection. We will usually establish a connection as a leading process that uses the TCP protocol as a matter of course, but seldom question the need to establish a connection process. In fact, as explained in some of the above, the use of TCP protocol must first establish a connection is to ensure the reliability of TCP protocol transmission of the basic premise (of course, because the TCP protocol is a stateful protocol, must be a mechanism for communication between the state of the synchronization, and establish a connection is such a mechanism). As to why three packets are required, the reason is that the exchange of information during the establishment of a connection must use at least three packets, and from the analysis below, it is necessary to use up to four packets to establish a connection. Again, the SYN flag bit is used only in the three (or four) packets that make the connection, and once the connection is established, all packets sent thereafter cannot be set to the SYN flag bit. From the point of view of guaranteeing the data reliability transmission, the TCP protocol needs to exchange some information before the formal data transmission, which is the initial serial number of the two parties (additional information including the maximum message length notification, etc.).

As mentioned above, the use of serial numbers is very important to the TCP protocol, before the formal data transmission, the two parties must obtain the other party's initial byte data number, so that it is possible to judge the legality of the data received, there are other data duplication, data overlap and other issues of further identification and resolution. Therefore, the exchange of their respective initial serial number must be completed before the official data transmission, we are the name of the process for the connection establishment process. The update of the respective States of the TCP protocol is mainly an aid to the reliability assurance of software design, and is not the main concern of this so-called establishment process.

The exchange of the initial serial number requires four packets from the most direct point of view:

1> host A sends its initial serial number to Host B.

2> host B confirms the initial sequence number that it sent to host a.

3> host B sends its initial serial number to host A.

4> host A confirms the initial sequence number that it sent to Host B.

We will <2><3> two steps together, that is, B to a to confirm its (before a) initial sequence number of the same time send its (that is, b own) initial sequence number. The so-called acknowledgment packet will set the ACK flag bit of the packet to 1. Note that the SYN flag bit in these three (or four) packets is set to 1, and the SYN flag bit is set to four only in these three (or 1) packets.

Here is a question: that is, A/b host in the notification of their initial serial number at the same time can transmit some of the normal data, the principle can (TCP protocol specification does not say no, but most of the implementation of the initial serial number is not attached to the normal data, but as a separate process, This formally establishes the connection one says.

Summary

TCP protocol claims reliable data transmission, its underlying implementation mechanism mainly includes three aspects: the use of serial numbers to transmit data, data time-out retransmission, data acknowledgement response. This paper mainly expounds why these three aspects can realize the reliability transmission and explain the internal mechanism briefly, then puts forward the basic principle of the implementation of the TCP protocol reliability data transmission, and at the end of the article from the point of view of guaranteeing the reliability data transmission, it simply explains why a connection process is needed when using the TCP protocol. TCP protocol is a complex of TCP/IP protocol family, one of the most important sources of its complexity is that it needs to provide reliable data transmission, this paper aims to reduce the reader's understanding of TCP protocol in the basic implementation principle of the TCP protocol to ensure the reliability of data transmission.

Note: This article is from "Linux kernel network stack source code scenario analysis"

Analysis of realization principle of TCP protocol reliability data transmission

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.