Skip to content

wasi-sockets: Sync up UDP implementation & tests with TCP - #7423

Merged
alexcrichton merged 5 commits into
bytecodealliance:mainfrom
badeend:udp-tests
Nov 5, 2023
Merged

alexcrichton merged 5 commits into
bytecodealliance:mainfrom
badeend:udp-tests

Conversation

@badeend

@badeend badeend commented Oct 31, 2023

Copy link
Copy Markdown
Member

No major functional changes:

  • Moved the validation utility methods out of tcp.rs and into network.rs so that the UDP implementation can use them too.
  • Extracted most platform compatibility fixes out into separate functions and moved them to a place where they can be used by UDP too.
  • Applied those validations and compatibility fixes to the UDP implementation as well.
  • Copied the TCP tests and adjusted them to work with UDP

@badeend
badeend requested a review from a team as a code owner October 31, 2023 16:14
@badeend
badeend requested review from alexcrichton and removed request for a team October 31, 2023 16:14

@alexcrichton alexcrichton left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

Sort of a side-question as it's not directly related to this PR itself, where's all this error-handling code coming from? This seems extensive enough that the tests probably aren't exercising all the various cases here and I'm curious if you're porting from another library for example or something like that.

Comment on lines +19 to +32
/// Bind a socket on a specified port.
fn test_udp_bind_specific_port(net: &Network, ip: IpAddress) {
const PORT: u16 = 54321;

let bind_addr = IpSocketAddress::new(ip, PORT);

let sock = UdpSocket::new(ip.family()).unwrap();
sock.blocking_bind(net, bind_addr).unwrap();

let bound_addr = sock.local_address().unwrap();

assert_eq!(bind_addr.ip(), bound_addr.ip());
assert_eq!(bind_addr.port(), bound_addr.port());
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can this fail if the port is already bound? We've had issues with tests running concurrently in the past and I think the TCP test has a case where it handles failure to bind here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ah right. Hadn't noticed the TCP test changed recently. Yes, this UDP test needs the same update.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I've updated the test with the same workaround that's in the TCP test.

@github-actions github-actions Bot added the wasi Issues pertaining to WASI label Oct 31, 2023
@badeend

badeend commented Oct 31, 2023

Copy link
Copy Markdown
Member Author

where's all this error-handling code coming from? (...) I'm curious if you're porting from another library for example or something like that.

Not any single place but a combination of: "official" documentations, stack overflow, OS source code, trial-and-error, ..

This seems extensive enough that the tests probably aren't exercising all the various cases

Are you referring to the error mappings in network.rs -> tcp/udp_bind and network.rs -> tcp_accept? Those are indeed not being tested, because I don't know how to without hogging up system resources or them being unreliable.

@alexcrichton

Copy link
Copy Markdown
Member

Ah ok makes sense. It's not really anything in particular I'm worried about or looking at, but it's lots of little things like:

  • Some(Errno::AFNOSUPPORT) => ErrorCode::InvalidArgument, // Just in case our own validations weren't sufficient.
  • Some(Errno::MFILE) => ErrorCode::OutOfMemory, // We're not trying to create a new socket. Rewrite it to less surprising error code.
  • // Normalize Linux' non-standard behavior.
    // "Linux accept() passes already-pending network errors on the new socket as an error code from accept(). This behavior differs from other BSD socket implementations."
    #[cfg(target_os = "linux")]
    Some(
    Errno::CONNRESET
    | Errno::NETRESET
    | Errno::HOSTUNREACH
    | Errno::HOSTDOWN
    | Errno::NETDOWN
    | Errno::NETUNREACH
    | Errno::PROTO
    | Errno::NOPROTOOPT
    | Errno::NONET
    | Errno::OPNOTSUPP,
    ) => ErrorCode::ConnectionAborted,
  • // Most platforms (Linux, Windows, Fuchsia, Solaris, Illumos, Haiku, ESP-IDF, ..and more?) treat the value
    // passed to SO_SNDBUF/SO_RCVBUF as a performance tuning hint and silently clamp the input if it exceeds
    // their capability.
    // As far as I can see, only the *BSD family views this option as a hard requirement and fails when the
    // value is out of range. We normalize this behavior in favor of the more commonly understood
    // "performance hint" semantics. In other words; even ENOBUFS is "Ok".
    // A future improvement could be to query the corresponding sysctl on *BSD platforms and clamp the input
    // `size` ourselves, to completely close the gap with other platforms.
    Err(Errno::NOBUFS) => Ok(()),
  • // Linux doubles the value passed to setsockopt to allow space for bookkeeping overhead.
    // getsockopt returns this internally doubled value.
    // We'll half the value to at least get it back into the same ballpark that the application requested it in.
    value / 2

I'm not worried about any of these, mostly curious instead how they all came into existence and if you hit issues locally or if you're copying. Either way is fine, and what you say makes sense, so sounds good to me!

@badeend

badeend commented Nov 4, 2023

Copy link
Copy Markdown
Member Author

This is a standard POSIX error code for the bind syscall:
EAFNOSUPPORT: The specified address is not a valid address for the address family of the specified socket.
and complements our own validation slightly higher up in the function. The mapping is there just in case there is an edge case our validation logic didn't catch.


From the docs


  • // Normalize Linux' non-standard behavior.
    // "Linux accept() passes already-pending network errors on the new socket as an error code from accept(). This behavior differs from other BSD socket implementations."
    #[cfg(target_os = "linux")]
    Some(
    Errno::CONNRESET
    | Errno::NETRESET
    | Errno::HOSTUNREACH
    | Errno::HOSTDOWN
    | Errno::NETDOWN
    | Errno::NETUNREACH
    | Errno::PROTO
    | Errno::NOPROTOOPT
    | Errno::NONET
    | Errno::OPNOTSUPP,
    ) => ErrorCode::ConnectionAborted,

From the docs


  • // Most platforms (Linux, Windows, Fuchsia, Solaris, Illumos, Haiku, ESP-IDF, ..and more?) treat the value
    // passed to SO_SNDBUF/SO_RCVBUF as a performance tuning hint and silently clamp the input if it exceeds
    // their capability.
    // As far as I can see, only the *BSD family views this option as a hard requirement and fails when the
    // value is out of range. We normalize this behavior in favor of the more commonly understood
    // "performance hint" semantics. In other words; even ENOBUFS is "Ok".
    // A future improvement could be to query the corresponding sysctl on *BSD platforms and clamp the input
    // `size` ourselves, to completely close the gap with other platforms.
    Err(Errno::NOBUFS) => Ok(()),

CI discovered that for me. This is actively tested by:

assert!(matches!(sock.set_receive_buffer_size(0), Ok(_))); // Unsupported sizes should be silently capped.
assert!(matches!(sock.set_receive_buffer_size(u64::MAX), Ok(_))); // Unsupported sizes should be silently capped.
assert!(matches!(sock.set_send_buffer_size(0), Ok(_))); // Unsupported sizes should be silently capped.
assert!(matches!(sock.set_send_buffer_size(u64::MAX), Ok(_))); // Unsupported sizes should be silently capped.


  • // Linux doubles the value passed to setsockopt to allow space for bookkeeping overhead.
    // getsockopt returns this internally doubled value.
    // We'll half the value to at least get it back into the same ballpark that the application requested it in.
    value / 2

From the docs. This is actively tested by:

sock.set_receive_buffer_size(0x10000).unwrap();
assert_eq!(sock.receive_buffer_size().unwrap(), 0x10000);
sock.set_send_buffer_size(0x10000).unwrap();
assert_eq!(sock.send_buffer_size().unwrap(), 0x10000);

@alexcrichton

Copy link
Copy Markdown
Member

Thanks for the explanations! When you get a chance it might be goood to have some of that commentary in the code as well.

@alexcrichton
alexcrichton added this pull request to the merge queue Nov 5, 2023
Merged via the queue into bytecodealliance:main with commit 973a34b Nov 5, 2023
alexcrichton pushed a commit to alexcrichton/wasmtime that referenced this pull request Nov 7, 2023
…liance#7423)

* Refactor & move utilities. So that UDP can use them too.

* Minor tweak in error code while connecting

* Refactor: move portability workarounds out of the TCP implementation.

* Sync up UDP implementation and tests
based on the existing TCP implementation & tests.

* Make test less flaky in CI.
alexcrichton added a commit that referenced this pull request Nov 7, 2023
* Update wasm-tools dependencies (#7488)

Pull in bytecodealliance/wasm-tools#1277 to switch the defaults for some
WIT behaviors:

* Semicolons are now required by default in WIT files
* Wasm-encoded WIT packages now use the "new" format by default.

WIT files without semicolons can still be parsed with
`WIT_REQUIRE_SEMICOLONS=0`. The "old" format for wasm-encoded WIT
packages can be emitted via `WIT_COMPONENT_ENCODING_V2=0`. Note that for
wasm-encoded WIT packages both the old and the new format can be decoded
irregardless of env vars.

* wasi-sockets: Sync up UDP implementation & tests with TCP (#7423)

* Refactor & move utilities. So that UDP can use them too.

* Minor tweak in error code while connecting

* Refactor: move portability workarounds out of the TCP implementation.

* Sync up UDP implementation and tests
based on the existing TCP implementation & tests.

* Make test less flaky in CI.

* wasi-sockets: Simplify ip name lookup interface (#7483)

* Refactor ip_name_lookup test

* Update ip-name-lookup::resolve-addresses

- Remove the non-essential parameters
- Lift the restriction against parsing IP addresses. Implementations would still have to parse IP addresses to decide whether or not to return an error

* Deduplicate to_canonical

* Add release notes for 15.0.0 (#7487)

---------

Co-authored-by: Dave Bakker <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wasi Issues pertaining to WASI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants