编程知识 cdmana.com

Nagle algorithm and TCP_ CORK,TCP_ NODELAY,TCP_ QUICKACK

1. Nagel Algorithm

TCP/IP Agreement , No matter how much data is sent , Always put the protocol header before the data , meanwhile , The other party receives the data , Also need to send ACK confirm . In order to maximize the network bandwidth ,TCP Always want to send as much data as possible .( A connection is set MSS Parameters , therefore ,TCP/IP I hope I can do it every time MSS Size data blocks to send data ).Nagle The algorithm is designed to send as large a chunk of data as possible , Avoid flooding the network with small chunks of data .

Nagle The basic definition of an algorithm is any time , There should be at most one unconfirmed segment . So-called “ Short paragraph ”, It means less than MSS Size of data block , So-called “ Has not been confirmed ”, When a block of data is sent out , I didn't get a message from the other party ACK Confirm that the data has been received .

Nagle Rule of algorithm ( May refer to tcp_output.c In the document tcp_nagle_check Function Comments ):

      (1) If the packet length reaches MSS, Is allowed to send ;
      (2) If it should contain FIN, Is allowed to send ;
      (3) Set up TCP_NODELAY Options , Is allowed to send ;
      (4) Not set TCP_CORK Option , If all the small packets sent ( Packet length less than MSS) All is confirmed , Is allowed to send ;
      (5) None of these conditions has been met , But a timeout occurred ( It's usually 200ms), Send immediately .

Nagle The algorithm allows only one that is not ACK The packet of exists in the network , It doesn't care about the size of the bag , So it's actually an extended stop - Such agreement , It's just that it's based on packet stop - Waiting , Instead of stopping based on bytes - Waiting .
Nagle The algorithm consists entirely of TCP Agreed ACK The mechanism decides , This will bring some problems , For example, if the opposite end ACK Reply quickly ,Nagle It doesn't actually splice too many packets , Although avoid network congestion , The overall utilization of the network is still very low .

Nagle The algorithm is silly window syndrome(SWS) A half set of prevention algorithms .SWS The algorithm prevents sending a small amount of data ,Nagle The algorithm is its implementation in the sender , And what the receiver has to do is not announce a small increase in buffer space , Do not notify small window , Unless there is a significant increase in buffer space . Here a significant increase is defined as a full-size segment (MSS) Or grow to more than half of the largest window .

Be careful :BSD The implementation of is to allow the last segment of a large write to be sent on a free link , in other words , When more than 1 individual MSS When data is sent , The kernel sends out in turn n individual MSS Data packets of , And then send the small packets at the end , In the meantime, there will be no delay waiting .( Suppose the network is not blocked and the receiving window is large enough )

for instance , Like before blog The experiment in , In limine client End calls socket Of write The operation will be a int Type data ( be called A block ) Write to the network , Because the connection is idle at this time ( That is to say, there are no unconfirmed paragraphs ), So the int Type data will be sent immediately to server End , next ,client The end calls write Operation write ‘\r\n’( abbreviation B block ), This is the time ,A The block ACK No return , So it can be thought that there is an unconfirmed fragment , therefore B The block was not immediately sent , Have been waiting for A The block ACK received ( Probably 40ms after ),B The block is sent . The whole process is as shown in the figure :

Nagle  Algorithm  - Shiney - Shiney

There is also a problem hidden here , Namely A Of block data ACK Why? 40ms And then I received it ? This is because TCP/IP It's not just nagle Algorithm , One more TCP Confirmation delay mechanism . When Server After receiving the data , It doesn't immediately go to client End send ACK, It's about putting ACK There is a delay in sending for a period of time ( Assuming that t), It hopes that t Within time server The end will go to client The end sends response data , such ACK It can be sent with the reply data , It's like answering data with ACK In the past . In the time before me ,t Is probably 40ms. That explains why ’\r\n’(B block ) Always in A After block 40ms To send out .

Of course ,TCP Confirmation delay 40ms It's not always the same ,TCP The delay acknowledgement time of the connection is generally initialized to a minimum value 40ms, Then according to the retransmission timeout of the connection (RTO)、 Parameters such as the time interval between the last received packet and the current received packet are continuously adjusted . in addition Can be set by TCP_QUICKACK Option to cancel the confirmation delay .
About TCP For a detailed description of confirmation delay, please refer to :http://www.javashuo.com/article/p-dkfnhmye-uu.html

2. TCP_NODELAY Options

By default , Send data using Negale Algorithm . Although this improves the network throughput , But the real-time performance is reduced , It's not allowed in some highly interactive applications , Use TCP_NODELAY Options can prohibit Negale Algorithm .
here , Every packet that the application submits to the kernel is immediately sent out . It should be noted that , Although it's forbidden Negale Algorithm , But network transmission is still subject to TCP Confirm the impact of the delay mechanism .

3.TCP_QUICKACK and TCP_NODELAY

If you set TCP_QUICKACK Set up , Previously set up TCP_NODELAY,QUICKACK Options will override NODELAY Can I use ? Connect :

int i =1;
setsockopt( iSock, IPPROTO_TCP, TCP_NODELAY, (void *)&i,sizeof(i));

Every time :

int i =1;
setsockopt( iSock, IPPROTO_TCP, TCP_QUICKACK, (void *)&i,sizeof(i));

Yes TCP_QUICKACK The invocation will be called earlier TCP_NODELAY Do you ?
There is no direct relationship between the two options , They're just for different purposes .

TCP_NODELAY Designed to ban / Enable segment buffering , To send data to peers as quickly as possible , So it's usually used to improve network utilization . TCP_QUICKACK Used to send acknowledgement as early as possible under some protocol level exchanges , Not stability / Perpetual , follow-up TCP Business ( It could happen in the following circumstances ) The divergence between user settings and stack behavior can be ignored based on the actual protocol level or any actual situation .

Be careful :TCP_NODELAY It's portable , and TCP_QUICKACK No ( Only in Linux 2.4.4 Work under the ).

4. TCP_CORK Options

So-called CORK That's what the plug means , To understand vividly is to use CORK Plug the connection , So that the data is not sent out first , Wait until the plug is removed before you send it out . After setting this option , The kernel will try to splice small packets into one big one ( One MTU) Send it out , Of course, if after a certain time ( It's usually 200ms, The value has yet to be confirmed ), The kernel is still not assembled into one MTU You have to send the existing data ( It's impossible to keep the data waiting ).
However ,TCP_CORK It may not be as perfect as you think ,CORK It doesn't plug the connection completely . In fact, the kernel does not know when the application layer will send the second batch of data for splicing with the first batch of data to achieve MTU Size , So the kernel gives a time limit , No stitching into a big bag in that time ( Try to get close to MTU) Words , The kernel will send it unconditionally . In other words, if the interval between sending packets is not short enough ,TCP_CORK It doesn't work at all , Instead, it lost the real-time of the data ( Each packet data will be sent after a certain delay time ).

4. Nagle Algorithm and CORK Algorithm difference

Nagle Algorithm and CORK The algorithm is very similar , But their focus is different ,Nagle The algorithm mainly avoids the network because of too many packets ( The proportion of agreement headers is very large ) And congestion , and CORK The algorithm is to improve the utilization of the network , So that the overall ratio of protocol header is as small as possible . So the two are consistent in avoiding sending packets , At the user level ,Nagle The algorithm is completely user independent socket The control of , You can only simply set up TCP_NODELAY And disable it ,CORK The algorithm is also set or cleared TCP_CORK Enable or disable , However Nagle The algorithm is concerned with network congestion , As long as all ACK Come back , and CORK Algorithms can care about content , In the premise that the interval between sending and sending packets is very short ( Very important , Otherwise, the kernel will help you send out the scattered packets ), Even if you send multiple small packets in a decentralized way , You can also enable CORK The algorithm splices the contents into a package , If you use Nagle Algorithmic words , You may not be able to do this .

版权声明
本文为[osc_ 2mvp7jnu]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224084836730n.html

Scroll to Top