如何在 Linux 中禁用 Nagle 算法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17842406/
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
How would one disable Nagle's algorithm in Linux?
提问by Jason Marks
Is there a way to do it through the command line? man tcp tells me that I need to set tcp_nodelay=1, but I am unable to create the tcp_nodelay file under /proc/sys/net/ipv4. Please let me know if there's any way of disabling Nagle in Linux.
有没有办法通过命令行来做到这一点?man tcp 告诉我需要设置tcp_nodelay=1,但是我无法在/proc/sys/net/ipv4 下创建tcp_nodelay 文件。请让我知道是否有任何方法可以在 Linux 中禁用 Nagle。
回答by Soravux
This flag (TCP_NODELAY
) is an option that can be enabled on a per-socket basis and is applied when you create a TCP socket. This is done for a purpose: Nagle's algorithm is generally useful and helps handle network congestion. I doubt you want to disable it system-wide since your system will probably suffer from this deactivation.
此标志 ( TCP_NODELAY
) 是一个选项,可以在每个套接字的基础上启用,并在您创建 TCP 套接字时应用。这样做是有目的的:Nagle 的算法通常很有用,有助于处理网络拥塞。我怀疑您是否想在系统范围内禁用它,因为您的系统可能会受到这种停用的影响。
To disable it for a given socket, you can apply the option TCP_NODELAY
as explained hereand herein C:
要为给定的套接字禁用它,您可以TCP_NODELAY
按照此处和此处在 C 中的说明应用该选项:
int flag = 1;
int result = setsockopt(sock, /* socket affected */
IPPROTO_TCP, /* set option at TCP level */
TCP_NODELAY, /* name of option */
(char *) &flag, /* the cast is historical cruft */
sizeof(int)); /* length of option value */
if (result < 0)
... handle the error ...
You may have to adapt to your programming language, but basically it sets the TCP_NODELAY
flag option to the socket sock
, effectively disabling Nagle's algorithm. This is valid on any OS with sockets supporting the TCP standard.
您可能必须适应您的编程语言,但基本上它会将TCP_NODELAY
标志选项设置为 socket sock
,从而有效地禁用 Nagle 算法。这在具有支持 TCP 标准的套接字的任何操作系统上都有效。
If you still want to disable Nagle's algorithm system-wide, two options are available. First, you could recompile your kernel using the according flag (see your distribution manual for this). The second option is to create a software that sets the TCP_NODELAY
flag on every existing connection, similar to this code. The latter option should be executed each time a new TCP connection is created on the system.
如果您仍想在系统范围内禁用 Nagle 的算法,则有两个选项可用。首先,您可以使用相应的标志重新编译您的内核(请参阅您的分发手册)。第二种选择是创建一个软件,TCP_NODELAY
在每个现有连接上设置标志,类似于此代码。每次在系统上创建新的 TCP 连接时,都应执行后一个选项。
Something a bit cleaner would be to activate the low latency mode of TCP:
更简洁的方法是激活 TCP 的低延迟模式:
echo 1 > /proc/sys/net/ipv4/tcp_low_latency
This will give a hint to the TCP stack as to which decisions to make in order to lower the latency (Which I guess is what you are trying to achieve by disabling Nagle's algorithm). By default, it is set to optimize bandwidth ( "0" will be read from /proc/sys/net/ipv4/tcp_low_latency
).
这将向 TCP 堆栈提示做出哪些决定以降低延迟(我猜这就是您试图通过禁用 Nagle 算法来实现的目标)。默认情况下,它设置为优化带宽(将从中读取“0” /proc/sys/net/ipv4/tcp_low_latency
)。