Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added TcpFastOpenConnect sockopt #2247

Merged
merged 1 commit into from
Dec 3, 2023

Conversation

xonatius
Copy link
Contributor

@xonatius xonatius commented Dec 3, 2023

From tcp man page:

TCP_FASTOPEN_CONNECT (since Linux 4.11)
       This option enables an alternative way to perform Fast
       Open on the active side (client).  When this option is
       enabled, connect(2) would behave differently depending on
       if a Fast Open cookie is available for the destination.

       If a cookie is not available (i.e. first contact to the
       destination), connect(2) behaves as usual by sending a SYN
       immediately, except the SYN would include an empty Fast
       Open cookie option to solicit a cookie.

       If a cookie is available, connect(2) would return 0
       immediately but the SYN transmission is deferred.  A
       subsequent write(2) or sendmsg(2) would trigger a SYN with
       data plus cookie in the Fast Open option.  In other words,
       the actual connect operation is deferred until data is
       supplied.

       Note: While this option is designed for convenience,
       enabling it does change the behaviors and certain system
       calls might set different errno values.  With cookie
       present, write(2) or sendmsg(2) must be called right after
       connect(2) in order to send out SYN+data to complete 3WHS
       and establish connection.  Calling read(2) right after
       connect(2) without write(2) will cause the blocking socket
       to be blocked forever.

       The application should either set TCP_FASTOPEN_CONNECT
       socket option before write(2) or sendmsg(2), or call
       write(2) or sendmsg(2) with MSG_FASTOPEN flag directly,
       instead of both on the same connection.

       Here is the typical call flow with this new option:

           s = socket();
           setsockopt(s, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, 1, ...);
           connect(s);
           write(s); /* write() should always follow connect()
                      * in order to trigger SYN to go out. */
           read(s)/write(s);
           /* ... */
           close(s);

What does this PR do

Checklist:

  • I have read CONTRIBUTING.md
  • I have written necessary tests and rustdoc comments
  • A change log has been added if this PR modifies nix's API

From [tcp](https://man7.org/linux/man-pages/man7/tcp.7.html) man page:
```
TCP_FASTOPEN_CONNECT (since Linux 4.11)
       This option enables an alternative way to perform Fast
       Open on the active side (client).  When this option is
       enabled, connect(2) would behave differently depending on
       if a Fast Open cookie is available for the destination.

       If a cookie is not available (i.e. first contact to the
       destination), connect(2) behaves as usual by sending a SYN
       immediately, except the SYN would include an empty Fast
       Open cookie option to solicit a cookie.

       If a cookie is available, connect(2) would return 0
       immediately but the SYN transmission is deferred.  A
       subsequent write(2) or sendmsg(2) would trigger a SYN with
       data plus cookie in the Fast Open option.  In other words,
       the actual connect operation is deferred until data is
       supplied.

       Note: While this option is designed for convenience,
       enabling it does change the behaviors and certain system
       calls might set different errno values.  With cookie
       present, write(2) or sendmsg(2) must be called right after
       connect(2) in order to send out SYN+data to complete 3WHS
       and establish connection.  Calling read(2) right after
       connect(2) without write(2) will cause the blocking socket
       to be blocked forever.

       The application should either set TCP_FASTOPEN_CONNECT
       socket option before write(2) or sendmsg(2), or call
       write(2) or sendmsg(2) with MSG_FASTOPEN flag directly,
       instead of both on the same connection.

       Here is the typical call flow with this new option:

           s = socket();
           setsockopt(s, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, 1, ...);
           connect(s);
           write(s); /* write() should always follow connect()
                      * in order to trigger SYN to go out. */
           read(s)/write(s);
           /* ... */
           close(s);
```
@xonatius xonatius marked this pull request as ready for review December 3, 2023 08:19
Copy link
Member

@asomers asomers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice docs.

@SteveLauC SteveLauC added this pull request to the merge queue Dec 3, 2023
Merged via the queue into nix-rust:master with commit db5353c Dec 3, 2023
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants