Linux 什么时候应该使用 TCP_NODELAY,什么时候应该使用 TCP_CORK?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3761276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:36:22  来源:igfitidea点击:

When should I use TCP_NODELAY and when TCP_CORK?

linuxsocketstcp

提问by

I understood that both of them disable Nagle's algorithm.

我知道他们都禁用了 Nagle 的算法。

When should/ shouldn't I use each one of them?

我什么时候应该/不应该使用它们中的每一个?

采纳答案by theB

First of all not both of them disables Nagle's algorithm.

首先,并不是他们都禁用了 Nagle 的算法。

Nagle's algorithm is for reducing more number of small network packets in wire. The algorithm is: if data is smaller than a limit (usually MSS), wait until receiving ACK for previously sent packets and in the mean time accumulate data from user. Then send the accumulated data.

Nagle 的算法用于减少线路中更多的小网络数据包。算法是:如果数据小于限制(通常是MSS),则等待直到收到先前发送的数据包的ACK,同时累积来自用户的数据。然后发送累积的数据。

if [ data > MSS ]
    send(data)
else
    wait until ACK for previously sent data and accumulate data in send buffer (data)
    And after receiving the ACK send(data)

This will help in applications like telnet. However, waiting for the ACK may increase latency when sending streaming data. Additionally, if the receiver implements the 'delayed ACK policy', it will cause a temporary deadlock situation. In such cases, disabling Nagle's algorithm is a better option.

这将有助于 telnet 等应用程序。但是,等待 ACK 可能会增加发送流数据时的延迟。此外,如果接收方实施了“延迟 ACK 策略”,则会导致暂时的死锁情况。在这种情况下,禁用 Nagle 算法是更好的选择。

So TCP_NODELAY is used for disabling Nagle's algorithm.

因此 TCP_NODELAY 用于禁用 Nagle 算法。

TCP_CORK aggressively accumulates data. If TCP_CORK is enabled in a socket, it will not send data until the buffer fills to a fixed limit. Similar to Nagle's algorithm, it also accumulates data from user but until the buffer fills to a fixed limit not until receiving ACK. This will be useful while sending multiple blocks of data. But you have to be more careful while using TCP_CORK.

TCP_CORK 积极积累数据。如果在套接字中启用 TCP_CORK,则在缓冲区填充到固定限制之前,它不会发送数据。类似于 Nagle 的算法,它也从用户那里累积数据,但直到缓冲区填充到固定限制,直到收到 ACK。这在发送多个数据块时很有用。但是在使用 TCP_CORK 时必须更加小心。

Until 2.6 kernel, both of these options are mutually exclusive. But in later kernel, both of them can exist together. In such case, TCP_CORK will be given more preference.

直到 2.6 内核,这两个选项是互斥的。但是在以后的内核中,它们可以同时存在。在这种情况下,TCP_CORK 将获得更多优先权。

Ref:

参考:

回答by MarkR

It's an optimisation, so like any optimisation:

这是一种优化,所以像任何优化一样:

  1. Do not use it
  2. Wait until performance becomes a problem, then having determined that socket latency is definitely the cause of it, and testing proves that this will definitely fix it, AND this is the easiest way of fixing it, do it.
  1. 不要使用它
  2. 等到性能成为问题,然后确定套接字延迟肯定是它的原因,并且测试证明这肯定会修复它,这是修复它的最简单方法,就去做吧。

Basically the aim is to avoid having to send out several frames where a single frame can be used, with sendfile() and its friends.

基本上目的是避免发送几个可以使用单个帧的帧,使用 sendfile() 和它的朋友。

So for example, in a web server, you send the headers followed by the file contents, the headers will be assembled in-memory, the file will then be sent directly by the kernel. TCP_CORK allows you to have the headers and the beginning of the file sent in a single frame, even with TCP_NODELAY, which would otherwise cause the first chunk to be sent out immediately.

因此,例如,在 Web 服务器中,您发送标头,然后是文件内容,标头将在内存中组装,然后文件将直接由内核发送。TCP_CORK 允许您在单个帧中发送标头和文件开头,即使使用 TCP_NODELAY,否则会导致第一个块立即发送出去。

回答by fche

TCP_CORK is the opposite of TCP_NODELAY. The former forces packet-accumulation delay; the latter disables it.

TCP_CORK 与 TCP_NODELAY 正好相反。前者强制数据包累积延迟;后者禁用它。

回答by Hussein Galal

TCP_NODELAY

TCP_NODELAY

Used to disable Nagle's algorithm to improve TCP/IP networks and decrease the number of packets by waiting until an acknowledgment of previously sent data is received to send the accumulated packets.

用于禁用 Nagle 算法以改进 TCP/IP 网络并通过等待直到收到先前发送的数据的确认以发送累积的数据包来减少数据包的数量。

//From the tcp(7) manual:

//来自tcp(7)手册:

TCP_CORK(or TCP_NOPUSHin FreeBSD)

TCP_CORK(或TCP_NOPUSH在 FreeBSD 中)

If set, don't send out partial frames. All queued partial frames are sent when the option is cleared again. This is useful for prepending headers before calling sendfile(2), or for throughput optimization. As currently implemented, there is a ** 200-millisecond ceiling** on the time for which output is corked by TCP_CORK. If this ceiling is reached, then queued data is automatically transmitted. This option can be combined with TCP_NODELAYonly since Linux 2.5.71. This option should not be used in code intended to be portable.

如果设置,则不发送部分帧。当再次清除该选项时,将发送所有排队的部分帧。这对于在调用之前预先添加标头sendfile(2)或吞吐量优化很有用。按照目前的实施,输出被TCP_CORK. 如果达到此上限,则自动传输排队的数据。此选项只能与TCP_NODELAYLinux 2.5.71 一起使用。不应在旨在可移植的代码中使用此选项。