vsock: Use PathBuf for socket paths instead of Strings

Fields `uds_path` and `socket` now use PathBuf. This allows for better
filesystem compatibility.

Signed-off-by: Luigi Leonardi <leonardi@redhat.com>
This commit is contained in:
Luigi Leonardi 2025-02-19 19:04:22 +01:00 committed by Stefano Garzarella
parent 1b3f650c48
commit 0b6314f4b6
4 changed files with 91 additions and 203 deletions

View File

@ -12,6 +12,7 @@ use std::{
any::Any, any::Any,
collections::HashMap, collections::HashMap,
convert::TryFrom, convert::TryFrom,
path::PathBuf,
process::exit, process::exit,
sync::{Arc, RwLock}, sync::{Arc, RwLock},
thread, thread,
@ -81,12 +82,12 @@ struct VsockParam {
/// Unix socket to which a hypervisor connects to and sets up the control path with the device. /// Unix socket to which a hypervisor connects to and sets up the control path with the device.
#[arg(long, conflicts_with = "config", conflicts_with = "vm")] #[arg(long, conflicts_with = "config", conflicts_with = "vm")]
socket: String, socket: PathBuf,
/// Unix socket to which a host-side application connects to. /// Unix socket to which a host-side application connects to.
#[cfg(not(feature = "backend_vsock"))] #[cfg(not(feature = "backend_vsock"))]
#[arg(long, conflicts_with = "config", conflicts_with = "vm")] #[arg(long, conflicts_with = "config", conflicts_with = "vm")]
uds_path: Option<String>, uds_path: Option<PathBuf>,
/// Unix socket to which a host-side application connects to. /// Unix socket to which a host-side application connects to.
#[cfg(feature = "backend_vsock")] #[cfg(feature = "backend_vsock")]
@ -97,7 +98,7 @@ struct VsockParam {
conflicts_with = "config", conflicts_with = "config",
conflicts_with = "vm" conflicts_with = "vm"
)] )]
uds_path: Option<String>, uds_path: Option<PathBuf>,
/// The vsock CID to forward connections from guest /// The vsock CID to forward connections from guest
#[cfg(feature = "backend_vsock")] #[cfg(feature = "backend_vsock")]
@ -142,8 +143,8 @@ struct VsockParam {
#[derive(Clone, Debug, Deserialize)] #[derive(Clone, Debug, Deserialize)]
struct ConfigFileVsockParam { struct ConfigFileVsockParam {
guest_cid: Option<u64>, guest_cid: Option<u64>,
socket: String, socket: PathBuf,
uds_path: Option<String>, uds_path: Option<PathBuf>,
#[cfg(feature = "backend_vsock")] #[cfg(feature = "backend_vsock")]
forward_cid: Option<u32>, forward_cid: Option<u32>,
#[cfg(feature = "backend_vsock")] #[cfg(feature = "backend_vsock")]
@ -207,8 +208,8 @@ fn parse_vm_params(s: &str) -> Result<VsockConfig, VmArgsParseError> {
"guest_cid" | "guest-cid" => { "guest_cid" | "guest-cid" => {
guest_cid = Some(val.parse().map_err(VmArgsParseError::ParseInteger)?) guest_cid = Some(val.parse().map_err(VmArgsParseError::ParseInteger)?)
} }
"socket" => socket = Some(val.to_string()), "socket" => socket = Some(PathBuf::from(val)),
"uds_path" | "uds-path" => uds_path = Some(val.to_string()), "uds_path" | "uds-path" => uds_path = Some(PathBuf::from(val)),
#[cfg(feature = "backend_vsock")] #[cfg(feature = "backend_vsock")]
"forward_cid" | "forward-cid" => { "forward_cid" | "forward-cid" => {
@ -286,9 +287,7 @@ impl VsockArgs {
for p in vms_param.drain(..) { for p in vms_param.drain(..) {
#[cfg(feature = "backend_vsock")] #[cfg(feature = "backend_vsock")]
let backend_info = match (p.uds_path, p.forward_cid) { let backend_info = match (p.uds_path, p.forward_cid) {
(Some(path), None) => { (Some(path), None) => BackendType::UnixDomainSocket(path),
BackendType::UnixDomainSocket(path.trim().to_string())
}
(None, Some(cid)) => { (None, Some(cid)) => {
let listen_ports: Vec<u32> = match p.forward_listen { let listen_ports: Vec<u32> = match p.forward_listen {
None => Vec::new(), None => Vec::new(),
@ -312,7 +311,7 @@ impl VsockArgs {
let config = VsockConfig::new( let config = VsockConfig::new(
p.guest_cid.unwrap_or(DEFAULT_GUEST_CID), p.guest_cid.unwrap_or(DEFAULT_GUEST_CID),
p.socket.trim().to_string(), p.socket,
backend_info, backend_info,
p.tx_buffer_size.unwrap_or(DEFAULT_TX_BUFFER_SIZE), p.tx_buffer_size.unwrap_or(DEFAULT_TX_BUFFER_SIZE),
p.queue_size.unwrap_or(DEFAULT_QUEUE_SIZE), p.queue_size.unwrap_or(DEFAULT_QUEUE_SIZE),
@ -346,9 +345,7 @@ impl TryFrom<VsockArgs> for Vec<VsockConfig> {
_ => cmd_args.param.map_or(Err(CliError::NoArgsProvided), |p| { _ => cmd_args.param.map_or(Err(CliError::NoArgsProvided), |p| {
#[cfg(feature = "backend_vsock")] #[cfg(feature = "backend_vsock")]
let backend_info = match (p.uds_path, p.forward_cid) { let backend_info = match (p.uds_path, p.forward_cid) {
(Some(path), None) => { (Some(path), None) => BackendType::UnixDomainSocket(path),
BackendType::UnixDomainSocket(path.trim().to_string())
}
(None, Some(cid)) => { (None, Some(cid)) => {
let listen_ports: Vec<u32> = match p.forward_listen { let listen_ports: Vec<u32> = match p.forward_listen {
None => Vec::new(), None => Vec::new(),
@ -372,7 +369,7 @@ impl TryFrom<VsockArgs> for Vec<VsockConfig> {
Ok(vec![VsockConfig::new( Ok(vec![VsockConfig::new(
p.guest_cid, p.guest_cid,
p.socket.trim().to_string(), p.socket,
backend_info, backend_info,
p.tx_buffer_size, p.tx_buffer_size,
p.queue_size, p.queue_size,
@ -482,13 +479,14 @@ mod tests {
use assert_matches::assert_matches; use assert_matches::assert_matches;
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::Write;
use std::path::Path;
use tempfile::tempdir; use tempfile::tempdir;
impl VsockArgs { impl VsockArgs {
fn from_args_unix( fn from_args_unix(
guest_cid: u64, guest_cid: u64,
socket: &str, socket: &Path,
uds_path: &str, uds_path: &Path,
tx_buffer_size: u32, tx_buffer_size: u32,
queue_size: usize, queue_size: usize,
groups: &str, groups: &str,
@ -496,8 +494,8 @@ mod tests {
VsockArgs { VsockArgs {
param: Some(VsockParam { param: Some(VsockParam {
guest_cid, guest_cid,
socket: socket.to_string(), socket: socket.to_path_buf(),
uds_path: Some(uds_path.to_string()), uds_path: Some(uds_path.to_path_buf()),
#[cfg(feature = "backend_vsock")] #[cfg(feature = "backend_vsock")]
forward_cid: None, forward_cid: None,
@ -516,7 +514,7 @@ mod tests {
#[cfg(feature = "backend_vsock")] #[cfg(feature = "backend_vsock")]
fn from_args_vsock( fn from_args_vsock(
guest_cid: u64, guest_cid: u64,
socket: &str, socket: &Path,
forward_cid: u32, forward_cid: u32,
forward_listen: &str, forward_listen: &str,
tx_buffer_size: u32, tx_buffer_size: u32,
@ -526,7 +524,7 @@ mod tests {
VsockArgs { VsockArgs {
param: Some(VsockParam { param: Some(VsockParam {
guest_cid, guest_cid,
socket: socket.to_string(), socket: socket.to_path_buf(),
uds_path: None, uds_path: None,
forward_cid: Some(forward_cid), forward_cid: Some(forward_cid),
forward_listen: Some(forward_listen.to_string()), forward_listen: Some(forward_listen.to_string()),
@ -552,8 +550,8 @@ mod tests {
fn test_vsock_config_setup_unix() { fn test_vsock_config_setup_unix() {
let test_dir = tempdir().expect("Could not create a temp test directory."); let test_dir = tempdir().expect("Could not create a temp test directory.");
let socket_path = test_dir.path().join("vhost4.socket").display().to_string(); let socket_path = test_dir.path().join("vhost4.socket");
let uds_path = test_dir.path().join("vm4.vsock").display().to_string(); let uds_path = test_dir.path().join("vm4.vsock");
let args = VsockArgs::from_args_unix(3, &socket_path, &uds_path, 64 * 1024, 1024, "group1"); let args = VsockArgs::from_args_unix(3, &socket_path, &uds_path, 64 * 1024, 1024, "group1");
let configs = Vec::<VsockConfig>::try_from(args); let configs = Vec::<VsockConfig>::try_from(args);
@ -581,7 +579,7 @@ mod tests {
fn test_vsock_config_setup_vsock() { fn test_vsock_config_setup_vsock() {
let test_dir = tempdir().expect("Could not create a temp test directory."); let test_dir = tempdir().expect("Could not create a temp test directory.");
let socket_path = test_dir.path().join("vhost4.socket").display().to_string(); let socket_path = test_dir.path().join("vhost4.socket");
let args = let args =
VsockArgs::from_args_vsock(3, &socket_path, 1, "1234+4321", 64 * 1024, 1024, "group1"); VsockArgs::from_args_vsock(3, &socket_path, 1, "1234+4321", 64 * 1024, 1024, "group1");
@ -647,13 +645,10 @@ mod tests {
let config = configs.first().unwrap(); let config = configs.first().unwrap();
assert_eq!(config.get_guest_cid(), 3); assert_eq!(config.get_guest_cid(), 3);
assert_eq!( assert_eq!(config.get_socket_path(), socket_paths[0]);
config.get_socket_path(),
socket_paths[0].display().to_string()
);
assert_eq!( assert_eq!(
config.get_backend_info(), config.get_backend_info(),
BackendType::UnixDomainSocket(uds_paths[0].display().to_string()) BackendType::UnixDomainSocket(uds_paths[0].clone())
); );
assert_eq!(config.get_tx_buffer_size(), 65536); assert_eq!(config.get_tx_buffer_size(), 65536);
assert_eq!(config.get_queue_size(), 1024); assert_eq!(config.get_queue_size(), 1024);
@ -661,13 +656,10 @@ mod tests {
let config = configs.get(1).unwrap(); let config = configs.get(1).unwrap();
assert_eq!(config.get_guest_cid(), 4); assert_eq!(config.get_guest_cid(), 4);
assert_eq!( assert_eq!(config.get_socket_path(), socket_paths[1]);
config.get_socket_path(),
socket_paths[1].display().to_string()
);
assert_eq!( assert_eq!(
config.get_backend_info(), config.get_backend_info(),
BackendType::UnixDomainSocket(uds_paths[1].display().to_string()) BackendType::UnixDomainSocket(uds_paths[1].clone())
); );
assert_eq!(config.get_tx_buffer_size(), 65536); assert_eq!(config.get_tx_buffer_size(), 65536);
assert_eq!(config.get_queue_size(), 1024); assert_eq!(config.get_queue_size(), 1024);
@ -675,13 +667,10 @@ mod tests {
let config = configs.get(2).unwrap(); let config = configs.get(2).unwrap();
assert_eq!(config.get_guest_cid(), 5); assert_eq!(config.get_guest_cid(), 5);
assert_eq!( assert_eq!(config.get_socket_path(), socket_paths[2]);
config.get_socket_path(),
socket_paths[2].display().to_string()
);
assert_eq!( assert_eq!(
config.get_backend_info(), config.get_backend_info(),
BackendType::UnixDomainSocket(uds_paths[2].display().to_string()) BackendType::UnixDomainSocket(uds_paths[2].clone())
); );
assert_eq!(config.get_tx_buffer_size(), 32768); assert_eq!(config.get_tx_buffer_size(), 32768);
assert_eq!(config.get_queue_size(), 256); assert_eq!(config.get_queue_size(), 256);
@ -736,13 +725,10 @@ mod tests {
let config = configs.first().unwrap(); let config = configs.first().unwrap();
assert_eq!(config.get_guest_cid(), 3); assert_eq!(config.get_guest_cid(), 3);
assert_eq!( assert_eq!(config.get_socket_path(), socket_paths[0]);
config.get_socket_path(),
socket_paths[0].display().to_string()
);
assert_eq!( assert_eq!(
config.get_backend_info(), config.get_backend_info(),
BackendType::UnixDomainSocket(uds_paths[0].display().to_string()) BackendType::UnixDomainSocket(uds_paths[0].clone())
); );
assert_eq!(config.get_tx_buffer_size(), 65536); assert_eq!(config.get_tx_buffer_size(), 65536);
assert_eq!(config.get_queue_size(), 1024); assert_eq!(config.get_queue_size(), 1024);
@ -750,13 +736,10 @@ mod tests {
let config = configs.get(1).unwrap(); let config = configs.get(1).unwrap();
assert_eq!(config.get_guest_cid(), 4); assert_eq!(config.get_guest_cid(), 4);
assert_eq!( assert_eq!(config.get_socket_path(), socket_paths[1]);
config.get_socket_path(),
socket_paths[1].display().to_string()
);
assert_eq!( assert_eq!(
config.get_backend_info(), config.get_backend_info(),
BackendType::UnixDomainSocket(uds_paths[1].display().to_string()) BackendType::UnixDomainSocket(uds_paths[1].clone())
); );
assert_eq!(config.get_tx_buffer_size(), 65536); assert_eq!(config.get_tx_buffer_size(), 65536);
assert_eq!(config.get_queue_size(), 1024); assert_eq!(config.get_queue_size(), 1024);
@ -764,13 +747,10 @@ mod tests {
let config = configs.get(2).unwrap(); let config = configs.get(2).unwrap();
assert_eq!(config.get_guest_cid(), 5); assert_eq!(config.get_guest_cid(), 5);
assert_eq!( assert_eq!(config.get_socket_path(), socket_paths[2]);
config.get_socket_path(),
socket_paths[2].display().to_string()
);
assert_eq!( assert_eq!(
config.get_backend_info(), config.get_backend_info(),
BackendType::UnixDomainSocket(uds_paths[2].display().to_string()) BackendType::UnixDomainSocket(uds_paths[2].clone())
); );
assert_eq!(config.get_tx_buffer_size(), 32768); assert_eq!(config.get_tx_buffer_size(), 32768);
assert_eq!(config.get_queue_size(), 256); assert_eq!(config.get_queue_size(), 256);
@ -781,10 +761,7 @@ mod tests {
let config = configs.get(3).unwrap(); let config = configs.get(3).unwrap();
assert_eq!(config.get_guest_cid(), 6); assert_eq!(config.get_guest_cid(), 6);
assert_eq!( assert_eq!(config.get_socket_path(), socket_paths[3]);
config.get_socket_path(),
socket_paths[3].display().to_string()
);
assert_eq!( assert_eq!(
config.get_backend_info(), config.get_backend_info(),
BackendType::Vsock(VsockProxyInfo { BackendType::Vsock(VsockProxyInfo {
@ -830,10 +807,10 @@ mod tests {
let config = &configs[0]; let config = &configs[0];
assert_eq!(config.get_guest_cid(), 4); assert_eq!(config.get_guest_cid(), 4);
assert_eq!(config.get_socket_path(), socket_path.display().to_string()); assert_eq!(config.get_socket_path(), socket_path);
assert_eq!( assert_eq!(
config.get_backend_info(), config.get_backend_info(),
BackendType::UnixDomainSocket(uds_path.display().to_string()) BackendType::UnixDomainSocket(uds_path.clone())
); );
assert_eq!(config.get_tx_buffer_size(), 32768); assert_eq!(config.get_tx_buffer_size(), 32768);
assert_eq!(config.get_queue_size(), 256); assert_eq!(config.get_queue_size(), 256);
@ -862,10 +839,10 @@ mod tests {
let config = &configs[0]; let config = &configs[0];
assert_eq!(config.get_guest_cid(), DEFAULT_GUEST_CID); assert_eq!(config.get_guest_cid(), DEFAULT_GUEST_CID);
assert_eq!(config.get_socket_path(), socket_path.display().to_string()); assert_eq!(config.get_socket_path(), socket_path);
assert_eq!( assert_eq!(
config.get_backend_info(), config.get_backend_info(),
BackendType::UnixDomainSocket(uds_path.display().to_string()) BackendType::UnixDomainSocket(uds_path)
); );
assert_eq!(config.get_tx_buffer_size(), DEFAULT_TX_BUFFER_SIZE); assert_eq!(config.get_tx_buffer_size(), DEFAULT_TX_BUFFER_SIZE);
assert_eq!(config.get_queue_size(), DEFAULT_QUEUE_SIZE); assert_eq!(config.get_queue_size(), DEFAULT_QUEUE_SIZE);
@ -914,13 +891,10 @@ mod tests {
let config = &configs[0]; let config = &configs[0];
assert_eq!(config.get_guest_cid(), 4); assert_eq!(config.get_guest_cid(), 4);
assert_eq!( assert_eq!(config.get_socket_path(), socket_path_unix);
config.get_socket_path(),
socket_path_unix.display().to_string()
);
assert_eq!( assert_eq!(
config.get_backend_info(), config.get_backend_info(),
BackendType::UnixDomainSocket(uds_path.display().to_string()) BackendType::UnixDomainSocket(uds_path.clone())
); );
assert_eq!(config.get_tx_buffer_size(), 32768); assert_eq!(config.get_tx_buffer_size(), 32768);
assert_eq!(config.get_queue_size(), 256); assert_eq!(config.get_queue_size(), 256);
@ -931,10 +905,7 @@ mod tests {
let config = &configs[1]; let config = &configs[1];
assert_eq!(config.get_guest_cid(), 5); assert_eq!(config.get_guest_cid(), 5);
assert_eq!( assert_eq!(config.get_socket_path(), socket_path_vsock);
config.get_socket_path(),
socket_path_vsock.display().to_string()
);
assert_eq!( assert_eq!(
config.get_backend_info(), config.get_backend_info(),
BackendType::Vsock(VsockProxyInfo { BackendType::Vsock(VsockProxyInfo {
@ -966,13 +937,10 @@ mod tests {
let config = &configs[0]; let config = &configs[0];
assert_eq!(config.get_guest_cid(), DEFAULT_GUEST_CID); assert_eq!(config.get_guest_cid(), DEFAULT_GUEST_CID);
assert_eq!( assert_eq!(config.get_socket_path(), socket_path_unix);
config.get_socket_path(),
socket_path_unix.display().to_string()
);
assert_eq!( assert_eq!(
config.get_backend_info(), config.get_backend_info(),
BackendType::UnixDomainSocket(uds_path.display().to_string()) BackendType::UnixDomainSocket(uds_path)
); );
assert_eq!(config.get_tx_buffer_size(), DEFAULT_TX_BUFFER_SIZE); assert_eq!(config.get_tx_buffer_size(), DEFAULT_TX_BUFFER_SIZE);
assert_eq!(config.get_queue_size(), DEFAULT_QUEUE_SIZE); assert_eq!(config.get_queue_size(), DEFAULT_QUEUE_SIZE);
@ -1017,16 +985,8 @@ mod tests {
let test_dir = tempdir().expect("Could not create a temp test directory."); let test_dir = tempdir().expect("Could not create a temp test directory.");
let vhost_socket_path = test_dir let vhost_socket_path = test_dir.path().join("test_vsock_server.socket");
.path() let vsock_socket_path = test_dir.path().join("test_vsock_server.vsock");
.join("test_vsock_server.socket")
.display()
.to_string();
let vsock_socket_path = test_dir
.path()
.join("test_vsock_server.vsock")
.display()
.to_string();
let config = VsockConfig::new( let config = VsockConfig::new(
CID, CID,
@ -1051,11 +1011,7 @@ mod tests {
let test_dir = tempdir().expect("Could not create a temp test directory."); let test_dir = tempdir().expect("Could not create a temp test directory.");
let vhost_socket_path = test_dir let vhost_socket_path = test_dir.path().join("test_vsock_server.socket");
.path()
.join("test_vsock_server.socket")
.display()
.to_string();
let config = VsockConfig::new( let config = VsockConfig::new(
CID, CID,
@ -1084,36 +1040,16 @@ mod tests {
let configs = [ let configs = [
VsockConfig::new( VsockConfig::new(
3, 3,
test_dir test_dir.path().join("test_vsock_server1.socket"),
.path() BackendType::UnixDomainSocket(test_dir.path().join("test_vsock_server1.vsock")),
.join("test_vsock_server1.socket")
.display()
.to_string(),
BackendType::UnixDomainSocket(
test_dir
.path()
.join("test_vsock_server1.vsock")
.display()
.to_string(),
),
CONN_TX_BUF_SIZE, CONN_TX_BUF_SIZE,
QUEUE_SIZE, QUEUE_SIZE,
vec![DEFAULT_GROUP_NAME.to_string()], vec![DEFAULT_GROUP_NAME.to_string()],
), ),
VsockConfig::new( VsockConfig::new(
3, 3,
test_dir test_dir.path().join("test_vsock_server2.socket"),
.path() BackendType::UnixDomainSocket(test_dir.path().join("test_vsock_server2.vsock")),
.join("test_vsock_server2.socket")
.display()
.to_string(),
BackendType::UnixDomainSocket(
test_dir
.path()
.join("test_vsock_server2.vsock")
.display()
.to_string(),
),
CONN_TX_BUF_SIZE, CONN_TX_BUF_SIZE,
QUEUE_SIZE, QUEUE_SIZE,
vec![DEFAULT_GROUP_NAME.to_string()], vec![DEFAULT_GROUP_NAME.to_string()],
@ -1187,13 +1123,13 @@ mod tests {
assert_matches!(error, CliError::NoArgsProvided); assert_matches!(error, CliError::NoArgsProvided);
assert_eq!(format!("{error:?}"), "NoArgsProvided"); assert_eq!(format!("{error:?}"), "NoArgsProvided");
let args = VsockArgs::from_args_unix(0, "", "", 0, 0, ""); let args = VsockArgs::from_args_unix(0, &PathBuf::new(), &PathBuf::new(), 0, 0, "");
assert_eq!(format!("{args:?}"), "VsockArgs { param: Some(VsockParam { guest_cid: 0, socket: \"\", uds_path: Some(\"\"), forward_cid: None, forward_listen: None, tx_buffer_size: 0, queue_size: 0, groups: \"\" }), vm: None, config: None }"); assert_eq!(format!("{args:?}"), "VsockArgs { param: Some(VsockParam { guest_cid: 0, socket: \"\", uds_path: Some(\"\"), forward_cid: None, forward_listen: None, tx_buffer_size: 0, queue_size: 0, groups: \"\" }), vm: None, config: None }");
let param = args.param.unwrap().clone(); let param = args.param.unwrap().clone();
assert_eq!(format!("{param:?}"), "VsockParam { guest_cid: 0, socket: \"\", uds_path: Some(\"\"), forward_cid: None, forward_listen: None, tx_buffer_size: 0, queue_size: 0, groups: \"\" }"); assert_eq!(format!("{param:?}"), "VsockParam { guest_cid: 0, socket: \"\", uds_path: Some(\"\"), forward_cid: None, forward_listen: None, tx_buffer_size: 0, queue_size: 0, groups: \"\" }");
let args = VsockArgs::from_args_vsock(0, "", 1, "", 0, 0, ""); let args = VsockArgs::from_args_vsock(0, &PathBuf::new(), 1, "", 0, 0, "");
assert_eq!(format!("{args:?}"), "VsockArgs { param: Some(VsockParam { guest_cid: 0, socket: \"\", uds_path: None, forward_cid: Some(1), forward_listen: Some(\"\"), tx_buffer_size: 0, queue_size: 0, groups: \"\" }), vm: None, config: None }"); assert_eq!(format!("{args:?}"), "VsockArgs { param: Some(VsockParam { guest_cid: 0, socket: \"\", uds_path: None, forward_cid: Some(1), forward_listen: Some(\"\"), tx_buffer_size: 0, queue_size: 0, groups: \"\" }), vm: None, config: None }");
let param = args.param.unwrap().clone(); let param = args.param.unwrap().clone();
@ -1201,8 +1137,8 @@ mod tests {
let config = ConfigFileVsockParam { let config = ConfigFileVsockParam {
guest_cid: None, guest_cid: None,
socket: String::new(), socket: PathBuf::new(),
uds_path: Some(String::new()), uds_path: Some(PathBuf::new()),
forward_cid: None, forward_cid: None,
forward_listen: None, forward_listen: None,
tx_buffer_size: None, tx_buffer_size: None,
@ -1214,7 +1150,7 @@ mod tests {
let config = ConfigFileVsockParam { let config = ConfigFileVsockParam {
guest_cid: None, guest_cid: None,
socket: String::new(), socket: PathBuf::new(),
uds_path: None, uds_path: None,
forward_cid: Some(1), forward_cid: Some(1),
forward_listen: Some(String::new()), forward_listen: Some(String::new()),

View File

@ -442,8 +442,7 @@ impl VsockThreadBackend {
fn handle_new_guest_conn<B: BitmapSlice>(&mut self, pkt: &VsockPacket<B>) { fn handle_new_guest_conn<B: BitmapSlice>(&mut self, pkt: &VsockPacket<B>) {
match &self.backend_info { match &self.backend_info {
BackendType::UnixDomainSocket(uds_path) => { BackendType::UnixDomainSocket(uds_path) => {
let port_path = format!("{}_{}", uds_path, pkt.dst_port()); let port_path = format!("{}_{}", uds_path.display(), pkt.dst_port());
UnixStream::connect(port_path) UnixStream::connect(port_path)
.and_then(|stream| stream.set_nonblocking(true).map(|_| stream)) .and_then(|stream| stream.set_nonblocking(true).map(|_| stream))
.map_err(Error::UnixConnect) .map_err(Error::UnixConnect)
@ -590,7 +589,7 @@ mod tests {
let vsock_peer_path = test_dir.path().join("test_vsock_thread_backend.vsock_1234"); let vsock_peer_path = test_dir.path().join("test_vsock_thread_backend.vsock_1234");
let _listener = UnixListener::bind(&vsock_peer_path).unwrap(); let _listener = UnixListener::bind(&vsock_peer_path).unwrap();
let backend_info = BackendType::UnixDomainSocket(vsock_socket_path.display().to_string()); let backend_info = BackendType::UnixDomainSocket(vsock_socket_path.clone());
test_vsock_thread_backend(backend_info); test_vsock_thread_backend(backend_info);
@ -626,31 +625,19 @@ mod tests {
let test_dir = tempdir().expect("Could not create a temp test directory."); let test_dir = tempdir().expect("Could not create a temp test directory.");
let vsock_socket_path = test_dir let vsock_socket_path = test_dir.path().join("test_vsock_thread_backend.vsock");
.path()
.join("test_vsock_thread_backend.vsock")
.display()
.to_string();
let sibling_vhost_socket_path = test_dir let sibling_vhost_socket_path = test_dir
.path() .path()
.join("test_vsock_thread_backend_sibling.socket") .join("test_vsock_thread_backend_sibling.socket");
.display()
.to_string();
let sibling_vsock_socket_path = test_dir let sibling_vsock_socket_path = test_dir
.path() .path()
.join("test_vsock_thread_backend_sibling.vsock") .join("test_vsock_thread_backend_sibling.vsock");
.display()
.to_string();
let sibling2_vhost_socket_path = test_dir let sibling2_vhost_socket_path = test_dir
.path() .path()
.join("test_vsock_thread_backend_sibling2.socket") .join("test_vsock_thread_backend_sibling2.socket");
.display()
.to_string();
let sibling2_vsock_socket_path = test_dir let sibling2_vsock_socket_path = test_dir
.path() .path()
.join("test_vsock_thread_backend_sibling2.vsock") .join("test_vsock_thread_backend_sibling2.vsock");
.display()
.to_string();
let cid_map: Arc<RwLock<CidMap>> = Arc::new(RwLock::new(HashMap::new())); let cid_map: Arc<RwLock<CidMap>> = Arc::new(RwLock::new(HashMap::new()));

View File

@ -3,6 +3,7 @@
use std::{ use std::{
collections::{HashMap, HashSet}, collections::{HashMap, HashSet},
io::{self, Result as IoResult}, io::{self, Result as IoResult},
path::PathBuf,
sync::{Arc, Mutex, RwLock}, sync::{Arc, Mutex, RwLock},
}; };
@ -156,7 +157,7 @@ pub(crate) struct VsockProxyInfo {
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub(crate) enum BackendType { pub(crate) enum BackendType {
/// unix domain socket path /// unix domain socket path
UnixDomainSocket(String), UnixDomainSocket(PathBuf),
/// the vsock CID and ports /// the vsock CID and ports
#[cfg(feature = "backend_vsock")] #[cfg(feature = "backend_vsock")]
Vsock(VsockProxyInfo), Vsock(VsockProxyInfo),
@ -167,7 +168,7 @@ pub(crate) enum BackendType {
/// is allowed to configure the backend. /// is allowed to configure the backend.
pub(crate) struct VsockConfig { pub(crate) struct VsockConfig {
guest_cid: u64, guest_cid: u64,
socket: String, socket: PathBuf,
backend_info: BackendType, backend_info: BackendType,
tx_buffer_size: u32, tx_buffer_size: u32,
queue_size: usize, queue_size: usize,
@ -179,7 +180,7 @@ impl VsockConfig {
/// parameters to be fed into the vsock-backend server. /// parameters to be fed into the vsock-backend server.
pub fn new( pub fn new(
guest_cid: u64, guest_cid: u64,
socket: String, socket: PathBuf,
backend_info: BackendType, backend_info: BackendType,
tx_buffer_size: u32, tx_buffer_size: u32,
queue_size: usize, queue_size: usize,
@ -206,8 +207,8 @@ impl VsockConfig {
/// Return the path of the unix domain socket which is listening to /// Return the path of the unix domain socket which is listening to
/// requests from the guest. /// requests from the guest.
pub fn get_socket_path(&self) -> String { pub fn get_socket_path(&self) -> PathBuf {
String::from(&self.socket) self.socket.clone()
} }
pub fn get_tx_buffer_size(&self) -> u32 { pub fn get_tx_buffer_size(&self) -> u32 {
@ -466,21 +467,13 @@ mod tests {
let test_dir = tempdir().expect("Could not create a temp test directory."); let test_dir = tempdir().expect("Could not create a temp test directory.");
let vhost_socket_path = test_dir let vhost_socket_path = test_dir.path().join("test_vsock_backend_unix.socket");
.path() let vsock_socket_path = test_dir.path().join("test_vsock_backend.vsock");
.join("test_vsock_backend_unix.socket")
.display()
.to_string();
let vsock_socket_path = test_dir
.path()
.join("test_vsock_backend.vsock")
.display()
.to_string();
let config = VsockConfig::new( let config = VsockConfig::new(
CID, CID,
vhost_socket_path.to_string(), vhost_socket_path.clone(),
BackendType::UnixDomainSocket(vsock_socket_path.to_string()), BackendType::UnixDomainSocket(vsock_socket_path.clone()),
CONN_TX_BUF_SIZE, CONN_TX_BUF_SIZE,
QUEUE_SIZE, QUEUE_SIZE,
groups_list, groups_list,
@ -503,14 +496,10 @@ mod tests {
let test_dir = tempdir().expect("Could not create a temp test directory."); let test_dir = tempdir().expect("Could not create a temp test directory.");
let vhost_socket_path = test_dir let vhost_socket_path = test_dir.path().join("test_vsock_backend.socket");
.path()
.join("test_vsock_backend.socket")
.display()
.to_string();
let config = VsockConfig::new( let config = VsockConfig::new(
CID, CID,
vhost_socket_path.to_string(), vhost_socket_path.clone(),
BackendType::Vsock(VsockProxyInfo { BackendType::Vsock(VsockProxyInfo {
forward_cid: 1, forward_cid: 1,
listen_ports: vec![9001, 9002], listen_ports: vec![9001, 9002],
@ -535,21 +524,13 @@ mod tests {
let test_dir = tempdir().expect("Could not create a temp test directory."); let test_dir = tempdir().expect("Could not create a temp test directory.");
let vhost_socket_path = test_dir let vhost_socket_path = test_dir.path().join("test_vsock_backend_failures.socket");
.path() let vsock_socket_path = test_dir.path().join("test_vsock_backend_failures.vsock");
.join("test_vsock_backend_failures.socket")
.display()
.to_string();
let vsock_socket_path = test_dir
.path()
.join("test_vsock_backend_failures.vsock")
.display()
.to_string();
let config = VsockConfig::new( let config = VsockConfig::new(
CID, CID,
"/sys/not_allowed.socket".to_string(), PathBuf::from("/sys/not_allowed.socket"),
BackendType::UnixDomainSocket("/sys/not_allowed.vsock".to_string()), BackendType::UnixDomainSocket(PathBuf::from("/sys/not_allowed.vsock")),
CONN_TX_BUF_SIZE, CONN_TX_BUF_SIZE,
QUEUE_SIZE, QUEUE_SIZE,
groups.clone(), groups.clone(),
@ -562,8 +543,8 @@ mod tests {
let config = VsockConfig::new( let config = VsockConfig::new(
CID, CID,
vhost_socket_path.to_string(), vhost_socket_path.clone(),
BackendType::UnixDomainSocket(vsock_socket_path.to_string()), BackendType::UnixDomainSocket(vsock_socket_path.clone()),
CONN_TX_BUF_SIZE, CONN_TX_BUF_SIZE,
QUEUE_SIZE, QUEUE_SIZE,
groups, groups,
@ -610,8 +591,8 @@ mod tests {
fn test_vhu_vsock_structs() { fn test_vhu_vsock_structs() {
let unix_config = VsockConfig::new( let unix_config = VsockConfig::new(
0, 0,
String::new(), PathBuf::new(),
BackendType::UnixDomainSocket(String::new()), BackendType::UnixDomainSocket(PathBuf::new()),
0, 0,
0, 0,
vec![String::new()], vec![String::new()],
@ -621,7 +602,7 @@ mod tests {
#[cfg(feature = "backend_vsock")] #[cfg(feature = "backend_vsock")]
let vsock_config = VsockConfig::new( let vsock_config = VsockConfig::new(
0, 0,
String::new(), PathBuf::new(),
BackendType::Vsock(VsockProxyInfo { BackendType::Vsock(VsockProxyInfo {
forward_cid: 1, forward_cid: 1,
listen_ports: vec![9001, 9002], listen_ports: vec![9001, 9002],

View File

@ -819,6 +819,7 @@ mod tests {
use std::collections::HashMap; use std::collections::HashMap;
use std::io::Read; use std::io::Read;
use std::io::Write; use std::io::Write;
use std::path::PathBuf;
use tempfile::tempdir; use tempfile::tempdir;
use vm_memory::GuestAddress; use vm_memory::GuestAddress;
use vmm_sys_util::eventfd::EventFd; use vmm_sys_util::eventfd::EventFd;
@ -908,13 +909,8 @@ mod tests {
#[test] #[test]
fn test_vsock_thread_unix() { fn test_vsock_thread_unix() {
let test_dir = tempdir().expect("Could not create a temp test directory."); let test_dir = tempdir().expect("Could not create a temp test directory.");
let backend_info = BackendType::UnixDomainSocket( let backend_info =
test_dir BackendType::UnixDomainSocket(test_dir.path().join("test_vsock_thread.vsock"));
.path()
.join("test_vsock_thread.vsock")
.display()
.to_string(),
);
test_vsock_thread(backend_info); test_vsock_thread(backend_info);
test_dir.close().unwrap(); test_dir.close().unwrap();
} }
@ -938,7 +934,7 @@ mod tests {
let test_dir = tempdir().expect("Could not create a temp test directory."); let test_dir = tempdir().expect("Could not create a temp test directory.");
let t = VhostUserVsockThread::new( let t = VhostUserVsockThread::new(
BackendType::UnixDomainSocket("/sys/not_allowed.vsock".to_string()), BackendType::UnixDomainSocket(PathBuf::from("/sys/not_allowed.vsock")),
3, 3,
CONN_TX_BUF_SIZE, CONN_TX_BUF_SIZE,
groups.clone(), groups.clone(),
@ -946,11 +942,7 @@ mod tests {
); );
assert!(t.is_err()); assert!(t.is_err());
let vsock_socket_path = test_dir let vsock_socket_path = test_dir.path().join("test_vsock_thread_failures.vsock");
.path()
.join("test_vsock_thread_failures.vsock")
.display()
.to_string();
let mut t = VhostUserVsockThread::new( let mut t = VhostUserVsockThread::new(
BackendType::UnixDomainSocket(vsock_socket_path), BackendType::UnixDomainSocket(vsock_socket_path),
3, 3,
@ -981,11 +973,7 @@ mod tests {
assert!(t.process_rx(&vring, true).is_err()); assert!(t.process_rx(&vring, true).is_err());
// trying to use a CID that is already in use should fail // trying to use a CID that is already in use should fail
let vsock_socket_path2 = test_dir let vsock_socket_path2 = test_dir.path().join("test_vsock_thread_failures2.vsock");
.path()
.join("test_vsock_thread_failures2.vsock")
.display()
.to_string();
let t2 = VhostUserVsockThread::new( let t2 = VhostUserVsockThread::new(
BackendType::UnixDomainSocket(vsock_socket_path2), BackendType::UnixDomainSocket(vsock_socket_path2),
3, 3,
@ -1004,11 +992,7 @@ mod tests {
let cid_map: Arc<RwLock<CidMap>> = Arc::new(RwLock::new(HashMap::new())); let cid_map: Arc<RwLock<CidMap>> = Arc::new(RwLock::new(HashMap::new()));
let test_dir = tempdir().expect("Could not create a temp test directory."); let test_dir = tempdir().expect("Could not create a temp test directory.");
let vsock_path = test_dir let vsock_path = test_dir.path().join("test_vsock_thread.vsock");
.path()
.join("test_vsock_thread.vsock")
.display()
.to_string();
let t = VhostUserVsockThread::new( let t = VhostUserVsockThread::new(
BackendType::UnixDomainSocket(vsock_path.clone()), BackendType::UnixDomainSocket(vsock_path.clone()),