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 24, 2024
1 parent 36c8eb9 commit d760e69
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

11 changes: 10 additions & 1 deletion src/bin/givc-admin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clap::Parser;
use givc::admin;
use givc::endpoint::TlsConfig;
use givc::utils::vsock::parse_vsock_addr;
use givc_common::pb::reflection::ADMIN_DESCRIPTOR;
use std::net::SocketAddr;
use std::path::PathBuf;
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 = parse_vsock_addr(&vsock)?.into();
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
@@ -1,7 +1,9 @@
use clap::{Parser, Subcommand};
use givc::endpoint::TlsConfig;
use givc::types::*;
use givc::utils::vsock::parse_vsock_addr;
use givc_client::AdminClient;
use givc_common::address::EndpointAddress;
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 @@ -130,7 +135,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 d760e69

Please sign in to comment.