From 62026e4beebb51f7a34a0cf7af8c2ef6d527c2ff Mon Sep 17 00:00:00 2001 From: Christoph Herzog Date: Fri, 3 Feb 2023 15:29:03 +0100 Subject: [PATCH] wasix: Remove websocket support Removes the websocket support from wasix. Websockets are a high level concept. They should not be integrated so deeply into the core of wasix. For client connections, we will want to add support for websocket clients into the custom wasix_http_client bindings. For servers, the support should go into the custom wcgi server bindings. --- lib/sys-utils/src/lib.rs | 2 - lib/sys-utils/src/tungstenite_websocket.rs | 90 ---------------------- lib/vnet/src/lib.rs | 35 --------- lib/wasi-local-networking/src/lib.rs | 2 +- lib/wasi/src/fs/inode_guard.rs | 1 - lib/wasi/src/lib.rs | 4 +- lib/wasi/src/net/socket.rs | 20 +---- lib/wasi/src/runtime/mod.rs | 23 +----- lib/wasi/src/runtime/ws.rs | 24 ------ lib/wasi/src/syscalls/wasix/mod.rs | 2 - lib/wasi/src/syscalls/wasix/ws_connect.rs | 56 -------------- lib/wasi/wia/wasixx_32v1.txt | 1 - lib/wasi/wia/wasixx_64v1.txt | 1 - 13 files changed, 5 insertions(+), 256 deletions(-) delete mode 100644 lib/sys-utils/src/tungstenite_websocket.rs delete mode 100644 lib/wasi/src/runtime/ws.rs delete mode 100644 lib/wasi/src/syscalls/wasix/ws_connect.rs diff --git a/lib/sys-utils/src/lib.rs b/lib/sys-utils/src/lib.rs index 2da8e3926..eb291915f 100644 --- a/lib/sys-utils/src/lib.rs +++ b/lib/sys-utils/src/lib.rs @@ -1,3 +1 @@ pub mod memory; - -pub mod tungstenite_websocket; diff --git a/lib/sys-utils/src/tungstenite_websocket.rs b/lib/sys-utils/src/tungstenite_websocket.rs deleted file mode 100644 index 48d3c892e..000000000 --- a/lib/sys-utils/src/tungstenite_websocket.rs +++ /dev/null @@ -1,90 +0,0 @@ -// FIXME: either just delete, or actually use somewhere! - -// use async_trait::async_trait; -// use futures::stream::SplitSink; -// use futures::stream::SplitStream; -// use futures::SinkExt; -// use futures_util::StreamExt; -// use wasmer_os::wasmer_wasi::WasiRuntimeImplementation; -// use std::pin::Pin; -// use std::sync::Arc; -// use std::sync::Mutex; -// use tokio::net::TcpStream; -// #[cfg(feature = "tokio_tungstenite")] -// use tokio_tungstenite::{ -// connect_async, tungstenite::protocol::Message, -// MaybeTlsStream, WebSocketStream -// }; -// use wasmer_os::wasmer_wasi::WebSocketAbi; - -// #[allow(unused_imports)] -// use tracing::{debug, error, info, instrument, span, trace, warn, Level}; - -// pub struct TerminalWebSocket { -// sink: SplitSink>, Message>, -// stream: Option>>>, -// on_close: Arc>>>, -// } - -// impl TerminalWebSocket { -// pub async fn new(url: &str) -> Result { -// let url = url::Url::parse(url) -// .map_err(|err| err.to_string())?; - -// let (ws_stream, _) = connect_async(url).await -// .map_err(|err| format!("failed to connect - {}", err))?; -// let (sink, stream) = ws_stream.split(); - -// Ok( -// TerminalWebSocket { -// sink, -// stream: Some(stream), -// on_close: Arc::new(Mutex::new(None)), -// } -// ) -// } -// } - -// #[async_trait] -// impl WebSocketAbi for TerminalWebSocket { -// fn set_onopen(&mut self, mut callback: Box) { -// // We instantly notify that we are open -// callback(); -// } - -// fn set_onclose(&mut self, callback: Box) { -// let mut guard = self.on_close.lock().unwrap(); -// guard.replace(callback); -// } - -// fn set_onmessage(&mut self, callback: Box) + Send + 'static>, runtime: &dyn WasiRuntimeImplementation) -// { -// if let Some(mut stream) = self.stream.take() { -// let on_close = self.on_close.clone(); -// runtime.task_shared(Box::new(move || Pin::new(Box::new(async move { -// while let Some(msg) = stream.next().await { -// match msg { -// Ok(Message::Binary(msg)) => { -// callback(msg); -// } -// a => { -// debug!("received invalid msg: {:?}", a); -// } -// } -// } -// let on_close = on_close.lock().unwrap(); -// if let Some(on_close) = on_close.as_ref() { -// on_close(); -// } -// })))); -// } -// } - -// async fn send(&mut self, data: Vec) -> Result<(), String> { -// self.sink -// .send(Message::binary(data)) -// .await -// .map_err(|err| err.to_string())?; -// Ok(()) -// } -// } diff --git a/lib/vnet/src/lib.rs b/lib/vnet/src/lib.rs index 5c900cda0..eaaace9f1 100644 --- a/lib/vnet/src/lib.rs +++ b/lib/vnet/src/lib.rs @@ -33,13 +33,6 @@ pub struct IpRoute { #[async_trait::async_trait] #[allow(unused_variables)] pub trait VirtualNetworking: fmt::Debug + Send + Sync + 'static { - /// Establishes a web socket connection - /// (note: this does not use the virtual sockets and is standalone - /// functionality that works without the network being connected) - async fn ws_connect(&self, url: &str) -> Result> { - Err(NetworkError::Unsupported) - } - /// Bridges this local network with a remote network, which is required in /// order to make lower level networking calls (such as UDP/TCP) async fn bridge( @@ -283,34 +276,6 @@ pub enum StreamSecurity { DoubleEncryption, } -/// Interface used for sending and receiving data from a web socket -#[async_trait::async_trait] -pub trait VirtualWebSocket: fmt::Debug + Send + Sync + 'static { - /// Sends out a datagram or stream of bytes on this socket - async fn send(&mut self, data: Bytes) -> Result; - - /// FLushes all the datagrams - fn flush(&mut self) -> Result<()>; - - /// Recv a packet from the socket - async fn recv(&mut self) -> Result; - - /// Recv a packet from the socket - fn try_recv(&mut self) -> Result>; - - /// Polls the socket for when there is data to be received - fn poll_read_ready( - &mut self, - cx: &mut std::task::Context<'_>, - ) -> std::task::Poll>; - - /// Polls the socket for when the backpressure allows for writing to the socket - fn poll_write_ready( - &mut self, - cx: &mut std::task::Context<'_>, - ) -> std::task::Poll>; -} - /// Connected sockets have a persistent connection to a remote peer #[async_trait::async_trait] pub trait VirtualConnectedSocket: VirtualSocket + fmt::Debug + Send + Sync + 'static { diff --git a/lib/wasi-local-networking/src/lib.rs b/lib/wasi-local-networking/src/lib.rs index 5710fbe02..acba7b10c 100644 --- a/lib/wasi-local-networking/src/lib.rs +++ b/lib/wasi-local-networking/src/lib.rs @@ -16,7 +16,7 @@ use wasmer_vnet::{ io_err_into_net_error, IpCidr, IpRoute, NetworkError, Result, SocketReceive, SocketReceiveFrom, SocketStatus, StreamSecurity, TimeType, VirtualConnectedSocket, VirtualConnectionlessSocket, VirtualIcmpSocket, VirtualNetworking, VirtualRawSocket, VirtualSocket, VirtualTcpListener, - VirtualTcpSocket, VirtualUdpSocket, VirtualWebSocket, + VirtualTcpSocket, VirtualUdpSocket, }; #[derive(Debug, Default)] diff --git a/lib/wasi/src/fs/inode_guard.rs b/lib/wasi/src/fs/inode_guard.rs index cdfe98513..6bd482bd2 100644 --- a/lib/wasi/src/fs/inode_guard.rs +++ b/lib/wasi/src/fs/inode_guard.rs @@ -142,7 +142,6 @@ impl std::fmt::Debug for InodeValFilePollGuard { } InodeSocketKind::UdpSocket(..) => write!(f, "guard-udp-socket"), InodeSocketKind::Raw(..) => write!(f, "guard-raw-socket"), - InodeSocketKind::WebSocket(..) => write!(f, "guard-web-socket"), _ => write!(f, "guard-socket"), }, _ => write!(f, "guard-socket (locked)"), diff --git a/lib/wasi/src/lib.rs b/lib/wasi/src/lib.rs index 7a05bb377..64a343fe3 100644 --- a/lib/wasi/src/lib.rs +++ b/lib/wasi/src/lib.rs @@ -100,7 +100,7 @@ pub use crate::{ }, runtime::{ task_manager::{VirtualTaskManager, VirtualTaskManagerExt}, - PluggableRuntimeImplementation, SpawnedMemory, WasiRuntimeImplementation, WebSocketAbi, + PluggableRuntimeImplementation, SpawnedMemory, WasiRuntimeImplementation, }, wapm::parse_static_webc, }; @@ -410,7 +410,6 @@ fn wasix_exports_32(mut store: &mut impl AsStoreMut, env: &FunctionEnv) "call_reply" => Function::new_typed_with_env(&mut store, env, call_reply::), "call_fault" => Function::new_typed_with_env(&mut store, env, call_fault), "call_close" => Function::new_typed_with_env(&mut store, env, call_close), - "ws_connect" => Function::new_typed_with_env(&mut store, env, ws_connect::), "port_bridge" => Function::new_typed_with_env(&mut store, env, port_bridge::), "port_unbridge" => Function::new_typed_with_env(&mut store, env, port_unbridge), "port_dhcp_acquire" => Function::new_typed_with_env(&mut store, env, port_dhcp_acquire), @@ -543,7 +542,6 @@ fn wasix_exports_64(mut store: &mut impl AsStoreMut, env: &FunctionEnv) "call_reply" => Function::new_typed_with_env(&mut store, env, call_reply::), "call_fault" => Function::new_typed_with_env(&mut store, env, call_fault), "call_close" => Function::new_typed_with_env(&mut store, env, call_close), - "ws_connect" => Function::new_typed_with_env(&mut store, env, ws_connect::), "port_bridge" => Function::new_typed_with_env(&mut store, env, port_bridge::), "port_unbridge" => Function::new_typed_with_env(&mut store, env, port_unbridge), "port_dhcp_acquire" => Function::new_typed_with_env(&mut store, env, port_dhcp_acquire), diff --git a/lib/wasi/src/net/socket.rs b/lib/wasi/src/net/socket.rs index 8be78835f..5ff4a4358 100644 --- a/lib/wasi/src/net/socket.rs +++ b/lib/wasi/src/net/socket.rs @@ -15,7 +15,7 @@ use tokio::sync::OwnedRwLockWriteGuard; use wasmer_types::MemorySize; use wasmer_vnet::{ DynVirtualNetworking, TimeType, VirtualConnectedSocket, VirtualIcmpSocket, VirtualRawSocket, - VirtualTcpListener, VirtualTcpSocket, VirtualUdpSocket, VirtualWebSocket, + VirtualTcpListener, VirtualTcpSocket, VirtualUdpSocket, }; use wasmer_wasi_types::wasi::{ Addressfamily, Errno, Fdflags, Rights, SockProto, Sockoption, Socktype, @@ -53,7 +53,6 @@ pub enum InodeSocketKind { connect_timeout: Option, accept_timeout: Option, }, - WebSocket(Box), Icmp(Box), Raw(Box), TcpListener(Box), @@ -365,7 +364,6 @@ impl InodeSocket { InodeSocketKind::UdpSocket(sock) => { sock.close().map_err(net_error_into_wasi_err)?; } - InodeSocketKind::WebSocket(_) => {} InodeSocketKind::Raw(_) => {} InodeSocketKind::PreSocket { .. } => return Err(Errno::Notconn), InodeSocketKind::Closed => return Err(Errno::Notconn), @@ -388,7 +386,6 @@ impl InodeSocket { .await .map_err(net_error_into_wasi_err)?; } - InodeSocketKind::WebSocket(_) => {} InodeSocketKind::Raw(sock) => { VirtualRawSocket::flush(sock.deref_mut()) .await @@ -458,7 +455,6 @@ impl InodeSocket { let inner = self.inner.read().await; Ok(match &inner.kind { InodeSocketKind::PreSocket { .. } => WasiSocketStatus::Opening, - InodeSocketKind::WebSocket(_) => WasiSocketStatus::Opened, InodeSocketKind::TcpListener(_) => WasiSocketStatus::Opened, InodeSocketKind::TcpStream(_) => WasiSocketStatus::Opened, InodeSocketKind::UdpSocket(_) => WasiSocketStatus::Opened, @@ -942,11 +938,6 @@ impl InodeSocket { let mut inner = self.inner.write().await; let ret = match &mut inner.kind { - InodeSocketKind::WebSocket(sock) => sock - .send(Bytes::from(buf)) - .await - .map(|_| buf_len) - .map_err(net_error_into_wasi_err), InodeSocketKind::Raw(sock) => sock .send(Bytes::from(buf)) .await @@ -1009,10 +1000,6 @@ impl InodeSocket { } } let data = match &mut inner.kind { - InodeSocketKind::WebSocket(sock) => { - let read = sock.recv().await.map_err(net_error_into_wasi_err)?; - read.data - } InodeSocketKind::Raw(sock) => { let read = sock.recv().await.map_err(net_error_into_wasi_err)?; read.data @@ -1092,8 +1079,7 @@ impl InodeSocket { match &mut guard.kind { InodeSocketKind::TcpStream(..) | InodeSocketKind::UdpSocket(..) - | InodeSocketKind::Raw(..) - | InodeSocketKind::WebSocket(..) => true, + | InodeSocketKind::Raw(..) => true, _ => false, } } else { @@ -1112,7 +1098,6 @@ impl InodeSocketInner { InodeSocketKind::TcpStream(socket) => socket.poll_read_ready(cx), InodeSocketKind::UdpSocket(socket) => socket.poll_read_ready(cx), InodeSocketKind::Raw(socket) => socket.poll_read_ready(cx), - InodeSocketKind::WebSocket(socket) => socket.poll_read_ready(cx), InodeSocketKind::Icmp(socket) => socket.poll_read_ready(cx), InodeSocketKind::PreSocket { .. } => { std::task::Poll::Ready(Err(wasmer_vnet::NetworkError::IOError)) @@ -1132,7 +1117,6 @@ impl InodeSocketInner { InodeSocketKind::TcpStream(socket) => socket.poll_write_ready(cx), InodeSocketKind::UdpSocket(socket) => socket.poll_write_ready(cx), InodeSocketKind::Raw(socket) => socket.poll_write_ready(cx), - InodeSocketKind::WebSocket(socket) => socket.poll_write_ready(cx), InodeSocketKind::Icmp(socket) => socket.poll_write_ready(cx), InodeSocketKind::PreSocket { .. } => { std::task::Poll::Ready(Err(wasmer_vnet::NetworkError::IOError)) diff --git a/lib/wasi/src/runtime/mod.rs b/lib/wasi/src/runtime/mod.rs index 6fd11efab..dded62285 100644 --- a/lib/wasi/src/runtime/mod.rs +++ b/lib/wasi/src/runtime/mod.rs @@ -1,12 +1,10 @@ mod stdio; pub mod task_manager; -mod ws; use self::task_manager::StubTaskManager; pub use self::{ stdio::*, - task_manager::{SpawnType, SpawnedMemory, VirtualTaskManager, VirtualTaskManagerExt}, - ws::*, + task_manager::{SpawnType, SpawnedMemory, VirtualTaskManager}, }; use std::{ @@ -157,25 +155,6 @@ where None } - /// Make a web socket connection to a particular URL - #[cfg(not(feature = "host-ws"))] - fn web_socket( - &self, - url: &str, - ) -> Pin, String>>>> { - Box::pin(async move { Err("not supported".to_string()) }) - } - - /// Make a web socket connection to a particular URL - #[cfg(feature = "host-ws")] - fn web_socket( - &self, - url: &str, - ) -> Pin, String>>>> { - let url = url.to_string(); - Box::pin(async move { Box::new(TerminalWebSocket::new(url.as_str())).await }) - } - /// Writes output to the console fn stdout(&self, data: &[u8]) -> Pin> + Send + Sync>> { let data = data.to_vec(); diff --git a/lib/wasi/src/runtime/ws.rs b/lib/wasi/src/runtime/ws.rs deleted file mode 100644 index 85a52c3f4..000000000 --- a/lib/wasi/src/runtime/ws.rs +++ /dev/null @@ -1,24 +0,0 @@ -#[cfg(feature = "async_ws")] -use async_trait::async_trait; - -use crate::WasiRuntimeImplementation; - -// This ABI implements a general purpose web socket -#[cfg_attr(feature = "async_ws", async_trait)] -pub trait WebSocketAbi { - fn set_onopen(&mut self, callback: Box); - - fn set_onclose(&mut self, callback: Box); - - fn set_onmessage( - &mut self, - callback: Box) + Send + 'static>, - runtime: &dyn WasiRuntimeImplementation, - ); - - #[cfg(feature = "async_ws")] - async fn send(&mut self, data: Vec) -> Result<(), String>; - - #[cfg(not(feature = "async_ws"))] - fn send(&mut self, data: Vec) -> Result<(), String>; -} diff --git a/lib/wasi/src/syscalls/wasix/mod.rs b/lib/wasi/src/syscalls/wasix/mod.rs index ba1743041..ac27678b0 100644 --- a/lib/wasi/src/syscalls/wasix/mod.rs +++ b/lib/wasi/src/syscalls/wasix/mod.rs @@ -78,7 +78,6 @@ mod thread_sleep; mod thread_spawn; mod tty_get; mod tty_set; -mod ws_connect; pub use bus_call::*; pub use bus_close::*; @@ -160,4 +159,3 @@ pub use thread_sleep::*; pub use thread_spawn::*; pub use tty_get::*; pub use tty_set::*; -pub use ws_connect::*; diff --git a/lib/wasi/src/syscalls/wasix/ws_connect.rs b/lib/wasi/src/syscalls/wasix/ws_connect.rs deleted file mode 100644 index ccfb86a97..000000000 --- a/lib/wasi/src/syscalls/wasix/ws_connect.rs +++ /dev/null @@ -1,56 +0,0 @@ -use super::*; -use crate::syscalls::*; - -/// ### `ws_connect()` -/// Connects to a websocket at a particular network URL -/// -/// ## Parameters -/// -/// * `url` - URL of the web socket destination to connect to -/// -/// ## Return -/// -/// Returns a socket handle which is used to send and receive data -pub fn ws_connect( - mut ctx: FunctionEnvMut<'_, WasiEnv>, - url: WasmPtr, - url_len: M::Offset, - ret_sock: WasmPtr, -) -> Result { - debug!( - "wasi[{}:{}]::ws_connect", - ctx.data().pid(), - ctx.data().tid() - ); - let mut env = ctx.data(); - let memory = env.memory_view(&ctx); - let url = unsafe { get_input_str_ok!(&memory, url, url_len) }; - - let net = env.net(); - let tasks = env.tasks.clone(); - let socket = wasi_try_ok!(__asyncify(&mut ctx, None, async move { - net.ws_connect(url.as_str()) - .await - .map_err(net_error_into_wasi_err) - })?); - env = ctx.data(); - - let (memory, state, mut inodes) = env.get_memory_and_wasi_state_and_inodes_mut(&ctx, 0); - - let kind = Kind::Socket { - socket: InodeSocket::new(InodeSocketKind::WebSocket(socket)), - }; - - let inode = - state - .fs - .create_inode_with_default_stat(inodes.deref_mut(), kind, false, "socket".into()); - let rights = Rights::all_socket(); - let fd = wasi_try_ok!(state - .fs - .create_fd(rights, rights, Fdflags::empty(), 0, inode)); - - wasi_try_mem_ok!(ret_sock.write(&memory, fd)); - - Ok(Errno::Success) -} diff --git a/lib/wasi/wia/wasixx_32v1.txt b/lib/wasi/wia/wasixx_32v1.txt index da5e31e22..288acb945 100644 --- a/lib/wasi/wia/wasixx_32v1.txt +++ b/lib/wasi/wia/wasixx_32v1.txt @@ -72,7 +72,6 @@ (func (import "wasix_32v1" "call_reply") (param i64 i32 i32 i32) (result i32)) (func (import "wasix_32v1" "call_fault") (param i64 i32) (func (import "wasix_32v1" "call_close") (param i64) - (func (import "wasix_32v1" "ws_connect") (param i32 i32 i32) (result i32)) (func (import "wasix_32v1" "http_request") (param i32 i32 i32 i32 i32 i32 i32 i32) (result i32)) (func (import "wasix_32v1" "http_status") (param i32 i32) (func (import "wasix_32v1" "port_bridge") (param i32 i32 i32 i32 i32) (result i32)) diff --git a/lib/wasi/wia/wasixx_64v1.txt b/lib/wasi/wia/wasixx_64v1.txt index e904bbb19..10f419502 100644 --- a/lib/wasi/wia/wasixx_64v1.txt +++ b/lib/wasi/wia/wasixx_64v1.txt @@ -72,7 +72,6 @@ (func (import "wasix_64v1" "call_reply") (param i64 i32 i64 i64) (result i32)) (func (import "wasix_64v1" "call_fault") (param i64 i32) (func (import "wasix_64v1" "call_close") (param i64) - (func (import "wasix_64v1" "ws_connect") (param i64 i64 i64) (result i32)) (func (import "wasix_64v1" "http_request") (param i64 i64 i64 i64 i64 i64 i32 i64) (result i32)) (func (import "wasix_64v1" "http_status") (param i32 i64) (func (import "wasix_64v1" "port_bridge") (param i64 i64 i64 i64 i32) (result i32))