As I know we have a socket option SO_TCP_NODELAY to disable Nagle Algorithm, and I found it is implemented in TCP/IP stack at https://elixir.bootlin.com/linux/v4.4.90/source/net/ipv4/tcp.c#L2401 . However, I do not know where the source code the Nagle Algorithm is implemented in kernel. If you know, could you please help me?


Short answer: tcp_nagle_check() in net_ipv4_tcp_output.c

You can see the conditions for avoiding Nagle in the code:

/* Return false, if packet can be sent now without violation Nagle's rules:
 * 1. It is full sized. (provided by caller in %partial bool)
 * 2. Or it contains FIN. (already checked by caller)
 * 3. Or TCP_CORK is not set, and TCP_NODELAY is set.
 * 4. Or TCP_CORK is not set, and all sent packets are ACKed.
 *    With Minshall's modification: all sent small packets are ACKed.
static bool tcp_nagle_check(bool partial, const struct tcp_sock *tp,
                            int nonagle)
        return partial &&
                ((nonagle & TCP_NAGLE_CORK) ||
                 (!nonagle && tp->packets_out && tcp_minshall_check(tp)));

A possible call stack to this function might be tcp_sendmsg_locked()->tcp_push()->__tcp_push_pending_frames()->tcp_write_xmit()->tcp_nagle_test()->tcp_nagle_check() tcp_sendmsg_locked() (in net/ipv4/tcp.c) is the main function for sending data. On each call to the function it gets a data segment from a socket and prepares it for transmission. The Nagle algorithm is an optimization for aggregating the data arriving through the socket in order to optimize transmit flow. You might also want to check out TCP corking and auto-corking, as they work in a similar fashion to Nagle but might take priority over the NO_DELAY option. In order to make sure your packets are sent without any delay, capture them (with tcpdump) and verify that the sent packet size is just a little bit larger than the data size you send from your application (due to added headers).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.