Skip to content

Commit

Permalink
Use field for vsock address, add docs
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 17, 2024
1 parent 4191836 commit 569b673
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ fn listen_abstract(a: &String, usr_opts: &UserOptions) -> Result<ListenerImpl, s
}

#[cfg(all(any(target_os = "linux", target_os = "android", target_os = "macos"), feature = "vsock"))]
fn listen_vsock((cid, port): &(u32, u32)) -> Result<ListenerImpl, std::io::Error> {
fn listen_vsock(cid: u32, port: u32) -> Result<ListenerImpl, std::io::Error> {
use tokio_vsock::{VsockAddr, VsockListener};
let vs = VsockAddr::new(*cid, *port);
let vs = VsockAddr::new(cid, port);
let listener = VsockListener::bind(vs)?;
Ok(ListenerImpl::Vsock(ListenerImplVsock{ s: listener}))
}
Expand Down Expand Up @@ -364,7 +364,7 @@ impl Listener {
listen_from_fd_named(usr_opts, fdname, sys_opts)?
}
#[cfg(all(any(target_os = "linux", target_os = "android", target_os = "macos"), feature = "vsock"))]
ListenerAddress::Vsock(vs) => listen_vsock(vs)?,
ListenerAddress::Vsock{ cid, port } => listen_vsock(*cid, *port)?,
#[allow(unreachable_patterns)]
_ => {
#[allow(unused_imports)]
Expand Down Expand Up @@ -423,7 +423,7 @@ impl Listener {
}
}
},
ListenerAddress::Vsock(_) => {
ListenerAddress::Vsock{..} => {
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos"))]
{
MissingCompileTimeFeature {
Expand Down
18 changes: 13 additions & 5 deletions src/listener_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,16 @@ pub enum ListenerAddress {
///
/// Special name `*` means to bind all passed addresses simultaneously, if `multi-listener` crate feature is enabled.
FromFdNamed(String),
/// Pair of CID and port
Vsock((u32, u32)),
/// The VSOCK address family facilitates communication between virtual machines and the host they are running on.
/// A socket address is defined as a combination of a 32-bit Context Identifier (CID) and a 32-bit port number. The CID identifies
/// the source or destination, which is either a virtual machine or the host.
///
/// [vsock manual]: https://www.man7.org/linux/man-pages/man7/vsock.7.html
/// (Implemented only on Linux and Darwin)
Vsock {
cid: u32,
port: u32,
},
}

pub(crate) const SD_LISTEN_FDS_START: i32 = 3;
Expand Down Expand Up @@ -86,7 +94,7 @@ impl FromStr for ListenerAddress {
Ok(ListenerAddress::FromFdNamed(x.to_owned()))
} else if let Some(vsock) = s.strip_prefix("vsock:") {
if let Some((Ok(cid), Ok(port))) = vsock.split_once(":").map(|(cid, port)| (cid.parse(), port.parse())) {
Ok(ListenerAddress::Vsock((cid, port)))
Ok(ListenerAddress::Vsock{ cid, port })
} else {
Err("Invalid tokio-listener vsock: cid:port pair expected")
}
Expand Down Expand Up @@ -129,7 +137,7 @@ impl Display for ListenerAddress {
ListenerAddress::FromFdNamed(name) => {
write!(f, "sd-listen:{name}")
}
ListenerAddress::Vsock((cid, port)) => {
ListenerAddress::Vsock{ cid, port } => {
write!(f, "vsock:{cid}:{port}")
}
}
Expand Down Expand Up @@ -166,6 +174,6 @@ pub(crate) fn check_env_for_fd(fdnum: i32) -> Option<()> {
#[cfg(all(any(target_os = "linux", target_os = "android", target_os = "macos"), feature = "vsock"))]
impl std::convert::From<tokio_vsock::VsockAddr> for ListenerAddress {
fn from(vs: tokio_vsock::VsockAddr) -> Self {
ListenerAddress::Vsock((vs.cid(), vs.port()))
ListenerAddress::Vsock{ cid: vs.cid(), port: vs.port() }
}
}

0 comments on commit 569b673

Please sign in to comment.