How Would One Disable Nagle's Algorithm in Linux

How to disable Nagle algorithm on Unix Socket in C language?

The Nagle algorithm is a TCP/IP specific algorithm used to reduce packet overhead. There is no TCP/IP involved in a UNIX socket and thus the Nagle algorithm cannot be applied there. Which also means that it cannot be disabled.

When should I use TCP_NODELAY and when TCP_CORK?

First of all not both of them disables Nagle's algorithm.

Nagle's algorithm is for reducing more number of small network packets in wire. The algorithm is: if data is smaller than a limit (usually MSS), wait until receiving ACK for previously sent packets and in the mean time accumulate data from user. Then send the accumulated data.

if [ data > MSS ]
send(data)
else
wait until ACK for previously sent data and accumulate data in send buffer (data)
And after receiving the ACK send(data)

This will help in applications like telnet. However, waiting for the ACK may increase latency when sending streaming data. Additionally, if the receiver implements the 'delayed ACK policy', it will cause a temporary deadlock situation. In such cases, disabling Nagle's algorithm is a better option.

So TCP_NODELAY is used for disabling Nagle's algorithm.

TCP_CORK aggressively accumulates data. If TCP_CORK is enabled in a socket, it will not send data until the buffer fills to a fixed limit. Similar to Nagle's algorithm, it also accumulates data from user but until the buffer fills to a fixed limit not until receiving ACK. This will be useful while sending multiple blocks of data. But you have to be more careful while using TCP_CORK.

Until 2.6 kernel, both of these options are mutually exclusive. But in later kernel, both of them can exist together. In such case, TCP_CORK will be given more preference.

Ref:

  • http://baus.net/on-tcp_cork/
  • http://ccr.sigcomm.org/archive/2001/jan01/ccr-200101-mogul.pdf

Is it possible to tell whether an application has disabled Nagle's algorithm?

The most direct way would be to trace the setsockopt system call.

Looking from the outside you can only notice when it disables Nagle and behaves bad (sends lots of small fragments rapidly). If it disables Nagle and behaves well, you cannot notice from outside.

Disable TCP Delayed ACKs

You could setsockopt(sockfd, IPPROTO_TCP, TCP_QUICKACK, (int[]){1}, sizeof(int)) after every recv you perform. It appears that TCP_QUICKACK is only reset when there is data being sent or received; if you're not sending any data, then it will only get reset when you receive data, in which case you can simply set it again.

You can check this in the 14th field of /proc/net/tcp; if it is not 1, ACKs should be sent immediately... if I'm reading the TCP code correctly. (I'm not an expert at this either.)



Related Topics



Leave a reply



Submit