Skip to content

Commit

Permalink
Add option to test vsock
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 10, 2024
1 parent d653c3a commit 9fb6e65
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 6 deletions.
15 changes: 12 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ serde = { version = "1.0.202", features = ["derive"]}
serde_json = "1.0.120"
x509-parser = { version = "0.16" }

tokio-listener = {version = "0.4.3", features = ["multi-listener", "tonic012"]}
tokio-listener = {version = "0.4.3", features = ["multi-listener", "tonic012", "vsock"], path="/home/avn/work/unikie/tokio-listener"}

# GIVC subparts
givc-common = { path="common" }
Expand Down
11 changes: 10 additions & 1 deletion src/bin/givc-admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use clap::Parser;
use givc::admin;
use givc::endpoint::TlsConfig;
use givc_common::pb::reflection::ADMIN_DESCRIPTOR;
use givc::utils::vsock::parse_vsock_addr;
use std::net::SocketAddr;
use std::path::PathBuf;
use tonic::transport::Server;
Expand All @@ -16,9 +17,12 @@ struct Cli {
#[arg(long, env = "PORT", default_missing_value = "9000", value_parser = clap::value_parser!(u16).range(1..))]
port: u16,

#[arg(long, help = "Additionally listen UNIX socket")]
#[arg(long, help = "Additionally listen UNIX socket (path)")]
unix: Option<String>,

#[arg(long, help = "Additionally listen Vsock socket (cid:port format)")]
vsock: Option<String>,

#[arg(long, env = "TLS", default_missing_value = "false")]
use_tls: bool,

Expand Down Expand Up @@ -84,6 +88,11 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
addrs.push(unix_sock_addr)
}

if let Some(vsock) = cli.vsock {
let vsock_addr = tokio_listener::ListenerAddress::Vsock(parse_vsock_addr(&vsock)?);
addrs.push(vsock_addr)
}

let listener = tokio_listener::Listener::bind_multiple(&addrs, &sys_opts, &user_opts).await?;

builder
Expand Down
14 changes: 13 additions & 1 deletion src/bin/givc-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use clap::{Parser, Subcommand};
use givc::endpoint::TlsConfig;
use givc::types::*;
use givc_client::AdminClient;
use givc_common::address::EndpointAddress;
use givc::utils::vsock::parse_vsock_addr;
use serde::ser::Serialize;
use std::path::PathBuf;
use std::time;
Expand All @@ -23,6 +25,9 @@ struct Cli {
#[arg(long, env = "NAME", default_missing_value = "admin.ghaf")]
name: String, // for TLS service name

#[arg(long)]
vsock: Option<String>,

#[arg(long, env = "CA_CERT")]
cacert: Option<PathBuf>,

Expand Down Expand Up @@ -128,7 +133,14 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
},
))
};
let admin = AdminClient::new(cli.addr, cli.port, tls);

// FIXME; big kludge, but allow to test vsock connection
let admin = if let Some(vsock) = cli.vsock {
info!("Connection diverted to VSock");
AdminClient::from_endpoint_address(EndpointAddress::Vsock(parse_vsock_addr(&vsock)?), tls)
} else {
AdminClient::new(cli.addr, cli.port, tls)
};

match cli.command {
Commands::Test { test } => test_subcommands(test, admin).await?,
Expand Down
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod auth;
pub mod naming;
pub mod tonic;
pub mod vsock;
pub mod x509;
14 changes: 14 additions & 0 deletions src/utils/vsock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use anyhow::bail;
use tokio_vsock::{VsockAddr, VMADDR_CID_HOST, VMADDR_CID_LOCAL};

pub fn parse_vsock_addr(addr: &str) -> anyhow::Result<VsockAddr> {
if let Some((cid, port)) = addr.split_once(":") {
let cid = match cid {
"local" => VMADDR_CID_LOCAL,
"host" => VMADDR_CID_HOST,
cid => cid.parse()?,
};
return Ok(VsockAddr::new(cid, port.parse()?));
};
bail!("Address {addr} should be in CID:PORT format")
}

0 comments on commit 9fb6e65

Please sign in to comment.