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 Answer
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
update: tcp_low_latency was removed in kernel v4.14 and above.
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
).
-
Your answer is correct but why do you cast
&flag
tochar *
?setsockopt
expects aconst void *
value pointer, so casting to a type is not required (every type can implicitly cast tovoid *
): pubs.opengroup.org/onlinepubs/9699919799/functions/… Actually you even violate the C standard, as casting a pointer of type A to a pointer of type B where neither type isvoid *
is actually undefined behavior by ISO-C. You have a pointer to an int but you claim it's a pointer to achar *
(will work on almost all systems but is technically undefined)– MeckiJul 18, 2017 at 18:58 -
This code is not mine, but a copy-paste the second link as I stated. You might want to contact www.unixguide.net about this issue. I remember seeing a couple of different signatures for
setsockopt()
, some of which (like the first link I provided) use achar *
for theflag
parameter. I don't mind changing the answer, as long as there are references to a crediblesetsockopt()
prototype and an a code example to perform the desired operation.– SoravuxJul 24, 2017 at 19:52 -
agreed with Soravux : from Microsoft MSDN: int setsockopt( SOCKET s, int level, int optname, const char *optval, int optlen ); from linux man pages int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen); from vxWorks socklib STATUS setsockopt ( int s, int level, int optname, char * optval, int optlen) and from IBM aix 7.1 int setsockopt (Socket, Level, OptionName, OptionValue, OptionLength) int Socket, Level, OptionName; const void * OptionValue; socklen_t OptionLength;– PierreApr 12, 2018 at 17:38
-
6Note that as of kernel v4.14 the tcp_low_latency no longer does anything. github.com/torvalds/linux/commit/… Feb 18, 2019 at 18:07