diff --git a/Cargo.lock b/Cargo.lock index 5179213..8ead6c2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -465,6 +465,21 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "futures" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + [[package]] name = "futures-channel" version = "0.3.30" @@ -472,6 +487,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" dependencies = [ "futures-core", + "futures-sink", ] [[package]] @@ -480,6 +496,23 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" +[[package]] +name = "futures-executor" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" + [[package]] name = "futures-macro" version = "0.3.30" @@ -509,9 +542,13 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" dependencies = [ + "futures-channel", "futures-core", + "futures-io", "futures-macro", + "futures-sink", "futures-task", + "memchr", "pin-project-lite", "pin-utils", "slab", @@ -554,6 +591,7 @@ dependencies = [ "tokio", "tokio-listener", "tokio-stream", + "tokio-vsock", "tonic", "tonic-reflection", "tonic-types", @@ -578,6 +616,7 @@ dependencies = [ "serde", "tokio", "tokio-stream", + "tokio-vsock", "tonic", "tonic-types", "tower", @@ -597,6 +636,7 @@ dependencies = [ "strum 0.26.3", "tokio", "tokio-stream", + "tokio-vsock", "tonic", "tonic-build", "tonic-types", @@ -889,6 +929,15 @@ version = "2.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" +[[package]] +name = "memoffset" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" +dependencies = [ + "autocfg", +] + [[package]] name = "mime" version = "0.3.17" @@ -938,6 +987,18 @@ dependencies = [ "libc", ] +[[package]] +name = "nix" +version = "0.27.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053" +dependencies = [ + "bitflags 2.5.0", + "cfg-if", + "libc", + "memoffset", +] + [[package]] name = "nom" version = "7.1.3" @@ -1653,7 +1714,7 @@ dependencies = [ "document-features", "futures-core", "futures-util", - "nix", + "nix 0.26.4", "pin-project", "socket2", "tokio", @@ -1708,6 +1769,19 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-vsock" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e336ac4b36df625d5429a735dd5847732fe5f62010e3ce0c50f3705d44730f8" +dependencies = [ + "bytes", + "futures", + "libc", + "tokio", + "vsock", +] + [[package]] name = "tonic" version = "0.12.2" @@ -1919,6 +1993,16 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" +[[package]] +name = "vsock" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dfb6e7a74830912f1f4a7655227c9ded1ea4e9136676311fedf54bedb412f35" +dependencies = [ + "libc", + "nix 0.27.1", +] + [[package]] name = "want" version = "0.3.1" diff --git a/Cargo.toml b/Cargo.toml index b2cffa8..af733dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ http-body = "0.4.2" hyper = "0.14" tokio = {version = "1.0", features = ["rt-multi-thread", "time", "macros"]} tokio-stream = "0.1" +tokio-vsock = "0.5" tonic = {version="0.12.2", features = ["tls"]} tonic-types = {version="0.12.2"} tonic-reflection = {version="0.12.2"} diff --git a/client/Cargo.toml b/client/Cargo.toml index b36cdc9..2e3a520 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -15,6 +15,7 @@ http-body = "0.4.2" hyper-util = { version = "0.1.4"} tokio = {version = "1.0", features = ["rt-multi-thread", "time", "macros"]} tokio-stream = "0.1" +tokio-vsock = "*" tonic = {version="0.12.2", features = ["tls"]} tonic-types = {version="0.12.2"} tower = {version = "0.4"} diff --git a/client/src/endpoint.rs b/client/src/endpoint.rs index 210d36d..95c63ff 100644 --- a/client/src/endpoint.rs +++ b/client/src/endpoint.rs @@ -4,6 +4,7 @@ use std::time::Duration; use anyhow::anyhow; use hyper_util::rt::TokioIo; use tokio::net::UnixStream; +use tokio_vsock::{VsockAddr, VsockStream}; use tonic::transport::{Certificate, Channel, ClientTlsConfig, Identity, ServerTlsConfig}; use tonic::transport::{Endpoint, Uri}; use tower::service_fn; @@ -88,6 +89,17 @@ async fn connect_unix_socket(endpoint: Endpoint, path: &String) -> anyhow::Resul Ok(ch) } +async fn connect_vsock_socket(endpoint: Endpoint, vs: &VsockAddr) -> anyhow::Result { + let vs = vs.to_owned(); + let ch = endpoint + .connect_with_connector(service_fn(move |_: Uri| async move { + let stream = VsockStream::connect(vs).await?; + Ok::<_, std::io::Error>(TokioIo::new(stream)) + })) + .await?; + Ok(ch) +} + impl EndpointConfig { pub async fn connect(&self) -> anyhow::Result { let url = transport_config_to_url(&self.transport.address, self.tls.is_some()); @@ -102,6 +114,7 @@ impl EndpointConfig { EndpointAddress::Tcp { .. } => endpoint.connect().await?, EndpointAddress::Unix(unix) => connect_unix_socket(endpoint, unix).await?, EndpointAddress::Abstract(abs) => connect_unix_socket(endpoint, abs).await?, + EndpointAddress::Vsock(vs) => connect_vsock_socket(endpoint, vs).await?, }; Ok(channel) } diff --git a/common/Cargo.toml b/common/Cargo.toml index bf6f4f5..5c176c3 100644 --- a/common/Cargo.toml +++ b/common/Cargo.toml @@ -14,6 +14,7 @@ http-body = "0.4.2" prost = "0.13" tokio = {version = "1.0", features = ["rt-multi-thread", "time", "macros"]} tokio-stream = "0.1" +tokio-vsock = "*" tonic = {version="0.12.2", features = ["tls"]} tonic-types = {version="0.12.2"} tracing = "0.1" diff --git a/common/src/address.rs b/common/src/address.rs index 2a46825..f7189c7 100644 --- a/common/src/address.rs +++ b/common/src/address.rs @@ -2,7 +2,7 @@ use std::convert::{Into, TryFrom}; //use std::net::SocketAddr; use std::path::PathBuf; -// use tokio_vsock::VsockAddr; +use tokio_vsock::VsockAddr; use crate::pb; @@ -13,7 +13,7 @@ pub enum EndpointAddress { addr: String, port: u16, }, - Unix(String), // "/path/to/sock" (same host only) + Unix(String), // "/path/to/sock" (same host only) Abstract(String), // "@abstract-socket-name" (same host only) - // VSock(VsockAddr), // cid+port. FIXME: cid have two magic numbers for host and local + Vsock(VsockAddr), // cid+port. FIXME: cid have two magic numbers for host and local } diff --git a/common/src/types.rs b/common/src/types.rs index 35d5fae..9da4b22 100644 --- a/common/src/types.rs +++ b/common/src/types.rs @@ -2,9 +2,11 @@ // Some of them would be rewritten, replaced, or even removed use super::address::EndpointAddress; use crate::pb; -use anyhow::{anyhow, bail}; use std::convert::{Into, TryFrom}; +use anyhow::{anyhow, bail}; +use tokio_vsock::VsockAddr; + #[derive(Debug, Copy, Clone, PartialEq)] pub struct UnitType { pub vm: VmType, @@ -198,7 +200,9 @@ impl TryFrom for EndpointEntry { }, "unix" => EndpointAddress::Unix(tc.address), "abstract" => EndpointAddress::Abstract(tc.address), - // "vsock" => unimplemented!(), + "vsock" => { + EndpointAddress::Vsock(VsockAddr::new(tc.address.parse()?, tc.port.parse()?)) + } unknown => bail!("Unknown protocol: {unknown}"), }; Ok(Self { @@ -229,12 +233,12 @@ impl Into for EndpointEntry { port: "".into(), name: self.tls_name, }, - // EndpointAddress::Vsock(vs) => pb::TransportConfig { - // protocol: "vsock".into(), - // address: vs.cid().to_string(), - // port: vs.port().to_string(), - // name: self.tls_name, - // } + EndpointAddress::Vsock(vs) => pb::TransportConfig { + protocol: "vsock".into(), + address: vs.cid().to_string(), + port: vs.port().to_string(), + name: self.tls_name, + }, } } }