There are many network protocols based on TCP protocol, such as TELNET,SSH, FTP, HTTP and so on. These protocols can be broadly divided into two broad categories based on data throughput: (1) interactive data types, such as TELNET,SSH, which in most cases are only small-traffic data exchanges, such as pressing the keyboard, echoing some text, and so on. (2) data block types, such as FTP, this type of protocol requires TCP to carry the data as far as possible, the throughput of the data to maximize, and as far as possible to improve efficiency. For both cases, TCP gives two different strategies for data transfer.1.TCP of interactive Data flow
For applications with high interactivity requirements, TCP gives two strategies to improve delivery efficiency and reduce network burden: (1) a piggyback ack. (2) Nagle algorithm (send data as much as possible). Usually, in the case of a fast network, such as using the Lo interface for Telnet communication, when the letter key and request Echo, the client and the server will go through the send key data, the server sends the key data ACK, server-side send echo data, and so on. The client sends the ACK of the echo data, and the data traffic in it will be 40bit + 41bit+41bit+40bit = 162bit, and if it is inside the WAN, this small packet of TCP traffic will cause a significant network burden.1.1. How to send a piggyback ack
This strategy is that when the host receives a TCP datagram from the remote host, it usually does not send an ACK datagram immediately, but waits for a short time, if the host also has a TCP datagram sent to the remote host during this time, then the ACK datagram "piggyback" sent out, The original two TCP datagrams are integrated into one send. In general, this time is 200ms. It is obvious to see that this strategy can improve the utilization of TCP datagrams a lot.1.2.Nagle algorithm
BBS people should have feelings, that is, when the network is slow to post, and sometimes after typing a string of strings, after a period of time, the client "crazy" like suddenly back to show a lot of content, as if the data passed over the same, this is the role of Nagle algorithm.
The Nagle algorithm is that when host a sends a TCP datagram to Host B and enters the state of the ACK datagram waiting for Host B, there can be only one TCP datagram in the output buffer of TCP, and the datagram collects the subsequent data and consolidates it into a large datagram. Wait until the ACK packet of Host B is arrived, send out the data "a brain". While this description is somewhat inaccurate, it is image and easy to understand, and we can also appreciate the benefits of this strategy for low-cost network burdens.
When writing the socket program, you can close the algorithm by Tcp_nodelay. And, using this algorithm to see the situation, such as the TCP-based X Window protocol, if the processing of mouse events or the use of this algorithm, then the "delay" can be very large.2.TCP of block data streams
For FTP such a high demand for data throughput, you will always want to send as much data as possible to the other host, even if it is a little "delay" does not matter. TCP also provides a comprehensive set of policies to support such requirements. The 16 bits in the TCP protocol represent the size of the window, which is the core of these policies.2.1. Issues with ACK when transmitting data
Before explaining the sliding window, it is necessary to look at the ACK response strategy, in general, the sending side sends a TCP datagram, then the receiving end should send an ACK datagram. But in fact it is not the case, the sender will continuously send the data to fill the receiver's buffer, and the recipient of the data as long as the sending of an ACK message back and forth should be, this is the cumulative characteristics of the ACK, this feature greatly reduces the burden on the sending and receiving end.2.2. Sliding window
The sliding window is essentially the data that describes the buffer size of the recipient's TCP datagram, and the sender calculates the maximum amount of data it can send based on that data. If the sender receives a TCP datagram with a window size of 0 for the receiver, the sender stops sending the data until the receiver sends a datagram with a window size of 0. The P211 and P212 in the book explain this very well.
On the sliding window protocol, the book also introduces three terms, namely:
TCP is to use this window, slowly moving from the left side of the data to the right, the window within the scope of the data sent out (but not to send all, just the data in the window can be sent. ）。 This is the meaning of the window. This is explained in Figure 20-6. The size of the window can be set through the socket, 4096 is not the most ideal window size, while 16384 can make the throughput greatly increased.2.3. Data congestion
The above strategy for intra-LAN transmission is also possible, but the use of the WAN can be problematic, the biggest problem is when the transmission of bottlenecks (for example, must go through a slip slow link) generated by a large number of data congestion problems (congestion), in order to solve this problem, The TCP sender needs to confirm the maximum data throughput of the lines on both sides of the connection. This is the so-called congestion window.
Congestion window principle is very simple, the TCP sender first send a datagram, and then wait for the other side of the response, get a response to the size of the window doubled, and then send two consecutive datagrams, wait until the other side of the response, then the window doubled (first 2 of the number of times, to a certain extent it becomes the current growth, This is called slow-start), sending more datagrams, until a timeout error occurs, so that the sender knows the traffic on both sides of the line load capacity, also determine the size of the congestion window, the sender of the size of the congestion window to send data. To observe this phenomenon is very easy, we generally download the data at the time, the speed is slowly "rushed up"
The above is the approximate process of TCP data transmission, although not meticulous, but enough to describe the principle of TCP, the focus is TCP traffic control principle, sliding window, congestion window, ACK cumulative confirmation and other knowledge points.
The sliding window is detailed:
TCP sliding window (send window and receive window)
TCP's sliding window mainly has two functions, one is to provide TCP reliability, and the other is to provide TCP flow control characteristics. at the same time, the sliding window mechanism also embodies the design idea of TCP for byte stream.
The TCP window is a 16bit bit field that represents the byte capacity of the window, that is, TCP's standard window is 2^16-1=65535 bytes maximum.
In addition, a TCP window enlargement factor is included in the TCP option field, Option-kind is 3,option-length 3 bytes, and the value range of Option-data is 0-14. The window enlargement factor is used to enlarge the TCP window, and the original 16bit window can be enlarged to 31bit.
Fundamentals of sliding windows
1) for the sender of a TCP session, any time in its sending cache data can be divided into 4 classes, "has been sent and received the ACK of the", "has been sent but not received the end of the Ack", "not sent but the end is allowed to send", "not sent and the peer is not allowed to send." the two parts of the data that have been sent but have not received an ACK and are not sent but are sent to the end are called the Send window (the middle two parts).
When a new ACK is received from the receiving party for subsequent bytes in the Send window, the window slides and the sliding principle is the same.
The window slides when ack=36 is received.
2) for TCP receiver, there are 3 kinds in its receive cache at a certain moment. "received", "not received ready to receive", "not received is not ready to receive" (due to ACK directly by the TCP stack reply, the default no application delay, there is no "received not reply Ack"). where "receive ready to receive" is called a receive window.
Send window relationship to receive window
TCP is a duplex protocol in which both sides of a session can receive and send data at the same time. Each side of the TCP session maintains a "Send Window" and a "receive window". The respective "Receive window" size depends on the application, system, and hardware limitations (TCP transfer rate cannot be greater than the data processing rate applied). The respective "Send window" requirements depend on the "receive window" advertised by the peer, which requires the same.
Sliding window for flow-oriented reliability
The most basic transmission reliability comes from the "confirm retransmission" mechanism.
The reliability of the TCP sliding window is also based on the "Confirm retransmission".
The Send window will move the left edge of the sending window only if it receives an ACK acknowledgment of the byte in the sending window for this paragraph.
The receive window moves the left edge only if all previous segments are confirmed. The window does not move and is not acknowledged for subsequent bytes when there is a previous byte that is not received but a subsequent byte is received. This ensures that the data is re-transmitted to the peer.
Flow control characteristics of sliding window
TCP sliding window is dynamic, we can imagine a common math problem in primary school, a pool, volume V, hourly intake of water V1, the amount of water V2. When the pool is full, it is not allowed to inject again, if there is a hydraulic system to control the size of the pool, then you can control the water injection rate and amount. Such a pool is similar to a TCP window. According to the change of its processing ability, the application of TCP receive window size control on the side to the terminal of the sending window traffic limit.
The application notifies the TCP protocol stack through the API to narrow the TCP receive window when it is needed, such as low memory. The TCP stack then includes a new window size notification to the peer when the next segment is sent, and a window to the notification to change the Send window to reduce the sending rate.
TCP/IP (11) Windows and Race control