问题
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.
回答1:
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.
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 ...
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.
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.
Something a bit cleaner would be to activate the low latency mode of 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
).
来源:https://stackoverflow.com/questions/17842406/how-would-one-disable-nagles-algorithm-in-linux