netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Alejandro Colomar (man-pages)" <alx.manpages@gmail.com>
To: "João Sampaio" <jpmelos@gmail.com>,
	"H.K. Jerry Chu" <hkchu@google.com>,
	"Michael Kerrisk (man-pages)" <mtk.manpages@gmail.com>
Cc: "David S. Miller" <davem@davemloft.net>,
	Networking <netdev@vger.kernel.org>,
	linux-man <linux-man@vger.kernel.org>
Subject: Re: Possible bug in `tcp(7)` regarding `TCP_USER_TIMEOUT`
Date: Sat, 15 Jan 2022 14:28:37 +0100	[thread overview]
Message-ID: <219d9c80-fa47-8359-0b11-e4713d99568a@gmail.com> (raw)
In-Reply-To: <CAO_YKa1+A+G-HazuXpepsAqa4CkmQFCWQs+zwSpYo11zuzvLCQ@mail.gmail.com>

Hello João,

On 1/15/22 13:23, João Sampaio wrote:
> Hello,
> 
> The manpage for TCP (`man 7 tcp`, [1]) reads, in the paragraphs about
> `TCP_USER_TIMEOUT`:
> 
> [1] https://man7.org/linux/man-pages/man7/tcp.7.html
> 
> ```
> This option can be set during any state of a TCP
> connection, but is effective only during the synchronized
> states of a connection (ESTABLISHED, FIN-WAIT-1, FIN-
> WAIT-2, CLOSE-WAIT, CLOSING, and LAST-ACK).
> ```
> 
> When I read that, I understand that that option should only apply to
> established TCP connections, and should not apply when trying to
> establish a TCP connection.
> 
> I wrote a simple program to test that:
> 
> ```
> #include <netdb.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
> #include <string.h>
> #include <time.h>
> #include <sys/socket.h>
> #include <netinet/in.h>
> #include <netinet/tcp.h>
> 
> // We'll skip error-checking since this is just for testing purposes.
> 
> void do_connect(int with_tcp_user_timeout) {
>     struct addrinfo hint;
>     struct addrinfo *ai;
>     int sock;
>     time_t start;
>     time_t end;
>     unsigned int timeout = 10000;
> 
>     memset(&hint, 0, sizeof(hint));
>     hint.ai_family = AF_INET;
>     hint.ai_socktype = SOCK_STREAM;
>     hint.ai_protocol = IPPROTO_TCP;
>     getaddrinfo("10.0.42.16", "5432", &hint, &ai);
> 
>     sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
>     if (with_tcp_user_timeout) {
>         setsockopt(
>             sock, IPPROTO_TCP, TCP_USER_TIMEOUT, &timeout,
>             sizeof(timeout)
>         );
>     }
> 
>     printf(
>         "Attempting to connect, with_user_tcp_timeout = %d... ",
>         with_tcp_user_timeout
>     );
>     fflush(stdout);
> 
>     start = time(NULL);
>     connect(sock, ai->ai_addr, ai->ai_addrlen);
>     end = time(NULL);
>     printf("Done!\n");
> 
>     freeaddrinfo(ai);
> 
>     printf(
>         "with_tcp_user_timeout = %d, took %ld seconds\n",
>         with_tcp_user_timeout, end - start
>     );
> }
> 
> int main(void) {
>     // Let's connect with TCP_USER_TIMEOUT first for faster useful
>     // first data.
>     do_connect(1);
>     do_connect(0);
> 
>     exit(EXIT_SUCCESS);
> }
> ```
> 
> This program gives me the following output:
> 
> ```
> $ gcc main.c -Wall -Wextra -Werror && ./a.out
> Attempting to connect, with_user_tcp_timeout = 1... Done!
> with_tcp_user_timeout = 1, took 10 seconds
> Attempting to connect, with_user_tcp_timeout = 0... Done!
> with_tcp_user_timeout = 0, took 130 seconds
> ```
> 
> So it seems like that option does apply when trying to establish a
> connection! This above output was obtained with Linux 5.15.8 and gcc
> 9.3.0.
> 
> Am I reading the manpage wrong, or is the manpage wrong? Or is this
> just an "undocumented behavior" that shouldn't be relied on and can
> be removed without warning in future releases?

I never used TCP_USER_TIMEOUT, so can't answer.  I added Jerry and David
(and netdev@) to the thread, which I guess should know how to answer you
better.

Thanks,

Alex

-- 
Alejandro Colomar
Linux man-pages maintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/

           reply	other threads:[~2022-01-15 13:28 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <CAO_YKa1+A+G-HazuXpepsAqa4CkmQFCWQs+zwSpYo11zuzvLCQ@mail.gmail.com>]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=219d9c80-fa47-8359-0b11-e4713d99568a@gmail.com \
    --to=alx.manpages@gmail.com \
    --cc=davem@davemloft.net \
    --cc=hkchu@google.com \
    --cc=jpmelos@gmail.com \
    --cc=linux-man@vger.kernel.org \
    --cc=mtk.manpages@gmail.com \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).