Skip to content

Commit

Permalink
Implement Vsock connection
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander V. Nikolaev <[email protected]>
  • Loading branch information
avnik committed Sep 6, 2024
1 parent c993f4a commit d653c3a
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 12 deletions.
86 changes: 85 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
1 change: 1 addition & 0 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
13 changes: 13 additions & 0 deletions client/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Channel> {
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<Channel> {
let url = transport_config_to_url(&self.transport.address, self.tls.is_some());
Expand All @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions common/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
}
20 changes: 12 additions & 8 deletions common/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -198,7 +200,9 @@ impl TryFrom<pb::TransportConfig> 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 {
Expand Down Expand Up @@ -229,12 +233,12 @@ impl Into<pb::TransportConfig> 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,
},
}
}
}

0 comments on commit d653c3a

Please sign in to comment.