如何在Linux中禁用Nagle的算法? [英] How would one disable Nagle's algorithm in Linux?
问题描述
有没有一种方法可以通过命令行来完成? man tcp告诉我需要设置tcp_nodelay = 1,但是无法在/proc/sys/net/ipv4下创建tcp_nodelay文件.请让我知道是否可以在Linux中禁用Nagle.
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.
推荐答案
此标志(TCP_NODELAY
)是一个选项,可以在每个套接字上启用,并在创建TCP套接字时应用.这样做是有目的的:Nagle的算法通常很有用,有助于处理网络拥塞.我怀疑您想在系统范围内禁用它,因为您的系统可能会因这种停用而受到影响.
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.
要针对给定的套接字禁用它,您可以按照此处在C中:
To disable it for a given socket, you can apply the option TCP_NODELAY
as explained here and here in 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 ...
您可能必须适应您的编程语言,但是基本上它将TCP_NODELAY
标志选项设置为套接字sock
,有效地禁用了Nagle的算法.这在具有支持TCP标准的套接字的任何操作系统上均有效.
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.
如果您仍然想在系统范围内禁用Nagle的算法,则有两个选项可用.首先,您可以使用相应的标志重新编译内核(有关此信息,请参见发行手册).第二个选项是创建一个在每个现有连接上设置TCP_NODELAY
标志的软件,类似于此代码.每次在系统上创建新的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.
一种更清洁的方法是激活TCP的低延迟模式:
Something a bit cleaner would be to activate the low latency mode of TCP:
echo 1 > /proc/sys/net/ipv4/tcp_low_latency
这将给TCP堆栈一个提示,说明为降低等待时间做出哪些决定(我想这是您通过禁用Nagle的算法要实现的目标).默认情况下,它设置为优化带宽(将从/proc/sys/net/ipv4/tcp_low_latency
中读取"0").
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
).
这篇关于如何在Linux中禁用Nagle的算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!