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

Tracking: Implement sock_accept and basic networking #3730

Closed
4 of 8 tasks
haraldh opened this issue Jan 27, 2022 · 7 comments
Closed
4 of 8 tasks

Tracking: Implement sock_accept and basic networking #3730

haraldh opened this issue Jan 27, 2022 · 7 comments

Comments

@haraldh
Copy link
Contributor

haraldh commented Jan 27, 2022

With the addition of sock_accept() in wasi-0.11.0, wasmtime can now implement basic networking for pre-opened sockets.

Todo

        Ok((
            TcpStream::from_inner(unsafe { Socket::from_raw_fd(fd as _) }),
            // WASI has no concept of SocketAddr yet
            // return an unspecified IPv4Addr
            SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 0),
        ))
@haraldh haraldh changed the title Implement sock_accept and basic networking Tracking: Implement sock_accept and basic networking Jan 27, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 28, 2022
wasi: implement `sock_accept` and enable networking

With the addition of `sock_accept()` to snapshot1, simple networking via a passed `TcpListener` is possible. This PR implements the basics to make a simple server work.

See also:
* [wasmtime tracking issue](bytecodealliance/wasmtime#3730)
* [wasmtime PR](bytecodealliance/wasmtime#3711)

TODO:
* [ ] Discussion of `SocketAddr` return value for `::accept()`

```rust
        Ok((
            TcpStream::from_inner(unsafe { Socket::from_raw_fd(fd as _) }),
            // WASI has no concept of SocketAddr yet
            // return an unspecified IPv4Addr
            SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 0),
        ))
```
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 28, 2022
wasi: implement `sock_accept` and enable networking

With the addition of `sock_accept()` to snapshot1, simple networking via a passed `TcpListener` is possible. This PR implements the basics to make a simple server work.

See also:
* [wasmtime tracking issue](bytecodealliance/wasmtime#3730)
* [wasmtime PR](bytecodealliance/wasmtime#3711)

TODO:
* [ ] Discussion of `SocketAddr` return value for `::accept()`

```rust
        Ok((
            TcpStream::from_inner(unsafe { Socket::from_raw_fd(fd as _) }),
            // WASI has no concept of SocketAddr yet
            // return an unspecified IPv4Addr
            SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 0),
        ))
```
@huangjj27
Copy link

How is this going on? I try to use just std::net::TcpListener to handle TcpStream and it raises errors that wasi is not supported yet.

@hlubek
Copy link

hlubek commented Mar 7, 2023

Is there a way to use sock_accept (and reading / writing) without having a busy wait loop?
With wasmtime I couldn't see that poll_oneoff could be used to wait on the preopened fd to wait for a new connection.

@Kerosin3
Copy link

How is this going on? I try to use just std::net::TcpListener to handle TcpStream and it raises errors that wasi is not supported yet.
passing --tcplisten=127.0.0.1:10000 to wasmtime and then

let mut server = { 
let s = unsafe { std::net::TcpStream::from_raw_fd(3) };
  s.set_nonblocking(true).context("failed to set non-blocking flag on socket")?;
  TcpListener::from_std(stdlistener)
}

makes the passed socket available to use in server app.

btw I dont know how to perform connect when writing a client app.
passing --tcplisten=127.0.0.1:10000 to client wasm app in case server is running is not possible. (socket already in use)
but enarx example seems to achieve this functionality https://github.com/enarx/codex/blob/main/demos/chat-client/rust/src/main.rs#L32
I still haven't figured out whether this functionality implemented in wasi .. If anyone can provide any info about how pass a socket to wasm client via wasmtime to perform a connection.. would be great

@maxwellflitton
Copy link

@Kerosin3 did you have any luck with this? I just want the TCP to be listening from the wasmtime runtime

@alexcrichton
Copy link
Member

I'm actually going to go ahead and close this as more-or-less not planned at this time. The wasip1 proposal's implementation of sockets was never fully completed, however the wasip2 implementation does have a full suite of socket-based APIs for use. If it works I'd recommend using the wasip2-based APIs.

@FrankReh
Copy link
Contributor

FrankReh commented May 2, 2024

I've actually seen the mio Wasm tcp listener just work with the latest wasmtime, once I used the -S preview2=n -S tcplisten=... options to run. I had to modify some lines within the mio src too since it is now treating warnings as errors and the wasi build target of mio was causing all sorts of warnings (well at least two, my memory of what all the failures were two days ago is fuzzy).

So the mio Wasm example is able to listen to a socket opened up by wasmtime and interact with nc processes I opened from other terminals. It's cool and I actually didn't expect it to work, so I may dig a little deeper to understand how it works with a Wasm module (a preview1 image).

@alexcrichton
Copy link
Member

I commented in another issue as well but if it aligns with your goals one thing that might be good is to implement mio's support for WASI in terms of the wasip2 target with wasi-sockets. Otherwise though I'm glad you were able to find the -Spreview2=n flag for local testing for now (sorry again for the bad name...)

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

No branches or pull requests

7 participants