Search code examples
cnetwork-programmingtcpmtu

C - Only send TCP packet if MTU is maxed out


is there any built in functionality (flag, parameter, whatever..) to send a TCP packet only then when its MTU is (nearly) full?

I hope I'm right with my assumption, that data is sent always and it doesn't matter how much payload it contains (tried to find that out via wireshark).

best regards


Solution

  • I hope I'm right with my assumption, that data is sent always

    No, a TCP stack typically bundles up data and send big segments (it doesn't wait forever though, often just a little while) - usually Nagles algorithm or some variant is employed.

    There might be other concerns too that impact how data are buffered up and sent, e.g. how congested the network is. Generally the TCP stack is very good at achiving max throughput, and normally you shouldn't try to outsmart it.

    If you need lower latency though, you can disable Nagle's algoritm, by setting the TCP_NODELAY socket option

    int ndelay = 1;
    setsockopt(sock,IPPROTO_TCP,TCP_NODELAY,(char *)&ndelay ,sizeof(ndelay));
    

    Linux provides the oposite as well, perhaps closer to what you're asking , by the means of TCP_CORK.

    TCP_CORK If set, don't send out partial frames. All queued partial frames are sent when the option is cleared again. This is useful for prepending headers before calling sendfile(2), or for throughput optimization. As currently implemented, there is a 200 millisecond ceiling on the time for which output is corked by TCP_CORK. If this ceiling is reached, then queued data is automatically transmitted. This option can be combined with TCP_NODELAY only since Linux 2.5.71. This option should not be used in code intended to be portable.