TCP sliding window and congestion control

 

Sliding window protocol (Sliding Window Protocol), TCP protocol belongs to an application, for flow control of the data transmission network, in order to avoid congestion. This protocol allows the sender stops and waits for confirmation before sending more data packets. Because each sender does not have to send a packet acknowledgment wait stopped, so that the data transmission protocol can be accelerated, improving network throughput.

 

TCP flow control is performed by the sliding window concept. Imagine the speed of sending data at the sending end soon and the case of the receiving end receives the speed is very slow, in order to ensure that data is not lost, a clear need for traffic control, and coordinate the pace of work communication between the two sides. The so-called sliding window, the buffer size to be understood that the receiving end can provide. TCP use a sliding window to tell the sender to send the data it can provide much buffer. Since the window 16 is defined by bit, TCP can provide a maximum of 65535 bytes buffer the receiving end. Thus, the window size may be utilized and a sequence number data to calculate the maximum number of receivable data sequence. 

 

The sliding window is essentially a description of the data buffer size of the recipient of the TCP data sender to send up to calculate how long their data based on this data. If the sender receive window size is the recipient of TCP packets 0, then the sender will stop sending data and wait until the recipient does not send window size for the arrival of datagrams 0. 

 

Window close: When approaching from the left window to the right, this phenomenon occurs when data is sent and acknowledged.   
       Window open: when the right side of the window moves to the right along the time, this phenomenon occurs at the end of the process after receiving the data.   
        Window shrink: When moving to the left along the right side of the window, a phenomenon rarely occurs.   
        TCP is to use this window, slowly moving from the left to the right data, sends the data to be within the range of window out (but do not send all, only at the data in the window can be sent.). This is the meaning of the window. The size of the window can be developed through socket, 4096 is not the best window size, and 16384 can greatly increase the throughput.

 

A————C————B

 

As shown above, to establish a TCP connection between A and B, to achieve the sliding window has two functions: 

 

Because of symmetry, only consider the A window and the B terminal sends a receive window, the following two functions   

 

1, B end time to the reception data processing (control of the synchronous rates between different hosts), then, A B by receiving the terminal notification window and slow data transmission.   
2, B end time to process the received data, but somewhere between A and B such as C, so that the overall bandwidth performance between AB poor this case, according to the congestion of processing strategies terminal A (slow start, and increased slowly decreasing doubling ) to update the window to decide to send the data.   

 

Compared with a fixed-size sliding window protocol, TCP sliding window protocol uses a variable size in order to achieve better performance.   

 

A wide area network protocol is TCP, and routers and hosts in a WAN environment, have different requirements for performance and processing capability, in this case, sliding window protocol using a fixed window size may cause a loss in performance. TCP window size is specified by the recipient of the notice, to achieve the best performance and bandwidth by taking the slow start and congestion avoidance algorithms and other mechanisms.

 

1. "window" is a sequence of bytes corresponding to a period of the sender can be transmitted, which is called a continuous range "window";

 

2. "sliding" refers to this "range permitting transmission" is transmitted as the process changes, the way is in order "sliding."

 

    1. TCP protocol ends respectively sender A and recipient B, since a full-duplex protocol, so A and B should be maintained with a separate transmit and receive buffers, respectively, due to the close (A fat B reciprocity a and B send received), we send the case of examples B receives in a;

    2.         Send window is part of the transmission buffer, that part of the TCP protocol can be sent, in fact, all of the data needs to be sent the application layer are placed in the transmit buffer of the sender;

    3.       Transmission window has four related concepts: the acknowledgment has been sent and received data (transmission and no transmission window within the buffer zone), but no acknowledgment of the received data (located within the transmission window) has been sent, the sending but the data in the transmission data buffer and an outer transmission window has not been sent temporarily not allowed to send the

    4.        After each successful data transmission, the transmission window will move sequentially in the transmission buffer, containing the new data is ready to send to the window;

 

The initial TCP connection establishment, B A would tell its own receive window size, such as '20': 31-50 bytes of the send window.

 

 

The analysis window value B, A own window structure

 

A 11 bytes after sending, transmitting window position unchanged, B data packets received out of order:

 

 

A 11-byte data made

 

After only when A successfully transmitted data, i.e., data B transmitted has been confirmed, the sliding window will be moved away from the transmitted data; B is confirmed while successive data packets for the first packet is received out of order down to avoid repeat transfer network:

 

 

A new confirmation number received, the window slides forward

 

 

The numbers in the send window belong to has been sent but unacknowledged

 

The so-called flow control, the main information is transmitted to the recipient the sender, so that not too fast data transmission, a control end. The main way to back an ACK will contain its own receive window size, to control the size and use of data transmission the sender:

 

 

This relates to a situation which, if B had told his A buffer is full, then A stop sending data; after waiting for some time, the emergence of a surplus buffer B, then A sends a message to tell me A rwnd size is 400, but unfortunately the message is lost, so it was waiting for notification B of a || B a deadlock condition waiting to send data. To deal with this problem, TCP introduced the continuous timer (Persistence timer), when A receives the other side of zero window notification, the timer is enabled, it is time to send a 1-byte probe packets, the other party will At this time, its response to the reception window size, if the result is not 0, the timer is continuously reset, continues to wait.

 


 

 

 

传递效率
     一个显而易见的问题是:单个发送字节单个确认,和窗口有一个空余即通知发送方发送一个字节,无疑增加了网络中的许多不必要的报文(请想想为了一个字节数据而添加的40字节头部吧!),所以我们的原则是尽可能一次多发送几个字节,或者窗口空余较多的时候通知发送方一次发送多个字节。对于前者我们广泛使用Nagle算法,即:
*1. 若发送应用进程要把发送的数据逐个字节地送到TCP的发送缓存,则发送方就把第一个数据字节先发送出去,把后面的字节先缓存起来;
*2. 当发送方收到第一个字节的确认后(也得到了网络情况和对方的接收窗口大小),再把缓冲区的剩余字节组成合适大小的报文发送出去;
*3. 当到达的数据已达到发送窗口大小的一半或以达到报文段的最大长度时,就立即发送一个报文段;
     对于后者我们往往的做法是让接收方等待一段时间,或者接收方获得足够的空间容纳一个报文段或者等到接受缓存有一半空闲的时候,再通知发送方发送数据。

 

拥塞控制
     网络中的链路容量和交换结点中的缓存和处理机都有着工作的极限,当网络的需求超过它们的工作极限时,就出现了拥塞。拥塞控制就是防止过多的数据注入到网络中,这样可以使网络中的路由器或链路不致过载。常用的方法就是:
1. 慢开始、拥塞控制
2. 快重传、快恢复
     一切的基础还是慢开始,这种方法的思路是这样的:
-1. 发送方维持一个叫做“拥塞窗口”的变量,该变量和接收端口共同决定了发送者的发送窗口;
-2. 当主机开始发送数据时,避免一下子将大量字节注入到网络,造成或者增加拥塞,选择发送一个1字节的试探报文;
-3. 当收到第一个字节的数据的确认后,就发送2个字节的报文;
-4. 若再次收到2个字节的确认,则发送4个字节,依次递增2的指数级;
-5. 最后会达到一个提前预设的“慢开始门限”,比如24,即一次发送了24个分组,此时遵循下面的条件判定:
*1. cwnd < ssthresh, 继续使用慢开始算法;
*2. cwnd > ssthresh,停止使用慢开始算法,改用拥塞避免算法;
*3. cwnd = ssthresh,既可以使用慢开始算法,也可以使用拥塞避免算法;
-6. 所谓拥塞避免算法就是:每经过一个往返时间RTT就把发送方的拥塞窗口+1,即让拥塞窗口缓慢地增大,按照线性规律增长;
-7. 当出现网络拥塞,比如丢包时,将慢开始门限设为原先的一半,然后将cwnd设为1,执行慢开始算法(较低的起点,指数级增长);

 

 

上述方法的目的是在拥塞发生时循序减少主机发送到网络中的分组数,使得发生拥塞的路由器有足够的时间把队列中积压的分组处理完毕。慢开始和拥塞控制算法常常作为一个整体使用,而快重传和快恢复则是为了减少因为拥塞导致的数据包丢失带来的重传时间,从而避免传递无用的数据到网络。快重传的机制是:
-1. 接收方建立这样的机制,如果一个包丢失,则对后续的包继续发送针对该包的重传请求;
-2. 一旦发送方接收到三个一样的确认,就知道该包之后出现了错误,立刻重传该包;
-3. 此时发送方开始执行“快恢复”算法:
*1. 慢开始门限减半;
*2. cwnd设为慢开始门限减半后的数值;
*3. 执行拥塞避免算法(高起点,线性增长);

 

 

Guess you like

Origin www.cnblogs.com/liangyc/p/11628757.html