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.
-
Do you want this to be system-wide? – Vaughn Cato Jul 24 '13 at 18:49
-
Yea, ideally i would like it system wide – Jason Marks Jul 24 '13 at 19:05
1 Answers
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
).

- 5,301
- 1
- 36
- 57

- 9,653
- 2
- 27
- 25
-
Your answer is correct but why do you cast `&flag` to `char *`? `setsockopt` expects a `const void *` value pointer, so casting to a type is not required (every type can implicitly cast to `void *`): http://pubs.opengroup.org/onlinepubs/9699919799/functions/setsockopt.html Actually you even violate the C standard, as casting a pointer of type A to a pointer of type B where neither type is `void *` is actually undefined behavior by ISO-C. You have a pointer to an int but you claim it's a pointer to a `char *` (will work on almost all systems but is technically undefined) – Mecki Jul 18 '17 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 a `char *` for the `flag` parameter. I don't mind changing the answer, as long as there are references to a credible `setsockopt()` prototype and an a code example to perform the desired operation. – Soravux Jul 24 '17 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; – Pierre Apr 12 '18 at 17:38
-
6Note that as of kernel v4.14 the tcp_low_latency no longer does anything. https://github.com/torvalds/linux/commit/b6690b14386698ce2c19309abad3f17656bdfaea#diff-ebf8916b6537cea0d23e1e0cd6abab61R356 – Spitfire19 Feb 18 '19 at 18:07