How would one disable Nagle's algorithm in Linux? [closed]

走远了吗. 提交于 2019-11-27 08:44:11

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 ).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!