Introduction
In the TCP/IP architecture, it is not reliable to send packets to the destination host, regardless of whether the data transmission is correct or not, it does not validate, does not confirm, and does not guarantee the order of packets. This problem is solved by the Transport Layer TCP protocol, which provides a reliable and error-free communication service for the Internet.
I. OSI reference model and TCP/IP Reference Model
The OSI model (open System Interconnection Reference Model) has been developed based on the recommendations of the International Organization for Standardization (ISO), which is divided into 1 seven layers.
TCP/IP was originally developed for the ARPANET network architecture, mainly by two important protocols, namely TCP protocol and IP protocol named.
Although TCP/IP is not an ISO-advocated standard, it has a wide range of commercial applications, so TCP/IP is a de facto standard. Because the Internet has been recognized all over the world, the TCP/IP system used by the Internet occupies a special important position in the field of computer network.
Figure 1 OSI model and TCP/IP model
The TCP/IP protocol system is divided into four levels. Because the TCP/IP protocol set does not consider the specific physical transmission media, so in the TCP/IP standard does not have the data link layer and the physical layer to make the stipulation, but only then the lowest layer is named as the network interface layer, only prescribes the interface with the physical network. Thus, if the network interface layer is not considered, then the TCP/IP system is actually only three levels: application layer, Transport layer and internetwork layer.
The application layer has many well-known protocols such as Telnet, File Transfer Protocol FTP, Simple Mail delivery protocol SMTP, and so on.
The transport layer uses two different protocols. One is connection-oriented Transmission Control protocol TCP, and the other is a non-connected User Datagram Protocol UDP. The unit of data transmitted by the transport layer is a message or data stream.
The primary protocol of the Internetwork is the non-connected network interconnection protocol IP. The data units that the layer transmits are grouped. There are three protocols used in conjunction with IP protocols: Internet Control Message Protocol ICMP, Address Resolution Protocol ARP and Inverse Address Resolution Protocol RARP.
Ii. Introduction to the TCP protocol
TCP is a protocol designed to provide reliable, end-to-end byte-stream traffic on unreliable internet. TCP services can be obtained by creating a communication port called a socket, respectively, on both the sender and receiver sides.
The TCP protocol is a reliable connection-oriented transport layer protocol that transmits data from a node to any machine on the Internet without error in the form of a byte stream. The sender's TCP divides the stream of bytes submitted by the user into a separate message for sending, and the receiver's TCP will reassemble the received message to the receiving user. TCP handles the problem of traffic control at the same time to prevent the fast sender from "drowning" slow receivers. Once the datagram is corrupted or lost, it is usually TCP to retransmit it, not the application or IP protocol.
Third, the transmission of TCP datagrams
1. TCP Datagram Header
The sending and receiving TCP entities Exchange data in the form of datagrams. A datagram contains a fixed 20-byte header, an optional part, and 0 or more bytes of data. TCP must communicate with lower-level IPs (using IP-defined methods) and high-rise applications (using the Tcp-ulp meta-language). TCP must also communicate with other TCP software over the network. Figure 2 shows the format of the TCP data header:
Figure 2 TCP Data header structure
Source port, Destination port: 16 bits long. Identifies the remote and local port numbers.
Sequence Number: 32 bits long. Indicates the order in which datagrams are sent.
Confirmation Number: 32 bits long. The serial number of the next datagram that you want to receive.
TCP head Length: 4 bits long. Indicates how many 32-bit words are contained in a TCP header.
The next 6 bits are unused.
URG: The message carries the emergency data, urgent offset is valid.
Ack:ack Position 1 indicates that the confirmation number is legal. If the ACK is 0, the datagram does not contain confirmation information and the confirmation field is omitted.
PSH: Represents the data with the push flag. The receiver therefore requests that the datagram be sent to the application without waiting for the buffer to be filled before it is delivered.
RST: Used to reset errors that occur due to host crashes or other causes. It can also be used to reject illegal datagrams or deny connection requests.
SYN: Used to establish a connection.
FIN: Used to release the connection.
Window Size: 16 bits long. The window size segment indicates how many bytes can be sent after the byte has been confirmed.
Checksum: 16 bits long. is set to ensure high reliability. It verifies the sum of headers, data, and pseudo-TCP headers.
Available options: 0 or more 32-bit words. Includes options such as maximum TCP load, window scale, and selection of re-send datagrams.
2. Reliable Transmission
When TCP emits a packet, it starts a timeout timer and revokes the set timeout timer if it receives confirmation before the timeout timer expires. If you do not receive a confirmation in time, we think that the packet just sent is missing and will resend the packet, which is called time-out retransmission.
The way to maintain reliability in TCP is to confirm and retransmit the mechanism so that reliable communication can be achieved on unreliable transmission networks.
3. Transport policy
If the sender sends the data too fast, the receiver may be too late to receive it, which can result in loss of data. The so-called flow control is to let the sender of the transmission rate is not too fast, to let the receiving party in time to receive. The sliding window mechanism can be used to control the sender's traffic conveniently on the TCP connection.
In TCP, a sliding window is used for transmission control, and the size of the sliding window means that the receiver has a large buffer that can be used to receive data. The sender can determine how many bytes of data should be sent by sliding the size of the window. When the sliding window is 0 o'clock, the sender generally can no longer send datagrams. Except in two cases, it is possible to send emergency data, for example, to allow a user to terminate a running process on a remote machine. Another scenario is that the sender can send a 1-byte datagram to notify the receiver to re-declare the next byte it wants to receive and the size of the sender's sliding window.
4. Congestion control
Congestion control: prevents excessive data from being injected into the network, which can prevent routers or links in the network from overloading.
Congestion control methods: Slow Start (slow-start), congestion avoidance (congestion avoidance), fast retransmission (quick retransmit), and fast recovery (fast recovery).
The sender maintains a state variable of the congested window CWnd (congestion windows). The size of the congestion window depends on the degree of congestion of the network and is dynamically changing. The sender makes its own sending window equal to the Congestion window.
Slow start algorithm: When the host begins to send data, it is not clear how the network is being loaded. Therefore, a better method is to first detect, that is, from small to large gradually increase the sending window, that is, from small to large gradually increase the congestion window value.
Typically, the congestion window CWnd is set to the value of the MSS of the maximum packet segment when the message segment is just beginning to be sent. Every passing round, the congestion window is doubled. Slow start "Slow" does not mean that CWnd's growth rate is slow, but rather when TCP begins to send a message segment set cwnd=1, so that the sender at the beginning only send a message segment (to test the network congestion), and then gradually increase the CWnd.
In order to prevent the congested window CWnd from growing too large to cause network congestion, you also need to set a slow-start threshold ssthresh state variable (how to set Ssthresh). The use of the slow start threshold Ssthresh is as follows:
When CWnd < Ssthresh, use the slow-start algorithm described above.
When CWnd > Ssthresh, stop using the slow start algorithm and use the congestion avoidance algorithm instead.
When CWnd = Ssthresh, you can either use the slow-start algorithm or use congestion control to avoid the algorithm.
Congestion avoidance algorithm: Let the congestion window CWnd grow slowly, that is, each time a round trip through the RTT to the sender of the congestion window CWnd plus 1, instead of doubling. In this way, the congestion window CWnd grows slowly by linear law, and the congestion window grows much slower than the slow-start algorithm.
The slow start threshold Ssthresh is set to half of the value of the sender window when congestion occurs (but not less than 2), as long as the sender determines that the network is congested (based on the absence of acknowledgement), either in the slow start or in the congestion avoidance phase. Then the congestion window CWnd is reset to 1 and the slow start algorithm is executed. The purpose of this is to quickly reduce the number of packets sent to the network by the host, so that the congested routers have enough time to complete the backlog of packets in the queue.
For example, the above-mentioned congestion control process is illustrated with specific numerical values. The size of the sending window is now as large as the congested window.
Reference documents:
[1] How TCP transfers work.
Http://hi.baidu.com/qu_wj/item/e2d76d4418daf236fa8960d4
[2] TCP traffic control and congestion control.
Http://blog.sina.com.cn/s/blog_6988593e01015wu0.html
[3] Shehiren. Computer Network (5th edition) [M]. Beijing: Electronic Industry Press, 2007
How TCP Transports work