virtio-devices: vhost_user: Retry connecting the backend

When in client mode, the VMM will retry connecting the backend for a
minute before giving up.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2021-06-01 17:45:00 +02:00
parent b4eb015d86
commit 48c8649891
2 changed files with 26 additions and 5 deletions

View File

@ -56,7 +56,7 @@ pub enum Error {
/// Failed to open vhost device.
VhostUserOpen(VhostError),
/// Connection to socket failed.
VhostUserConnect(vhost::Error),
VhostUserConnect,
/// Get features failed.
VhostUserGetFeatures(VhostError),
/// Get queue max number failed.

View File

@ -8,6 +8,8 @@ use std::convert::TryInto;
use std::os::unix::io::AsRawFd;
use std::os::unix::net::UnixListener;
use std::sync::Arc;
use std::thread::sleep;
use std::time::{Duration, Instant};
use std::vec::Vec;
use vhost::vhost_user::message::{VhostUserProtocolFeatures, VhostUserVirtioFeatures};
use vhost::vhost_user::{Master, VhostUserMaster};
@ -212,7 +214,7 @@ pub fn connect_vhost_user(
num_queues: u64,
unlink_socket: bool,
) -> Result<Master> {
Ok(if server {
if server {
if unlink_socket {
std::fs::remove_file(socket_path).map_err(Error::RemoveSocketPath)?;
}
@ -222,8 +224,27 @@ pub fn connect_vhost_user(
info!("Waiting for incoming vhost-user connection...");
let (stream, _) = listener.accept().map_err(Error::AcceptConnection)?;
Master::from_stream(stream, num_queues)
Ok(Master::from_stream(stream, num_queues))
} else {
Master::connect(socket_path, num_queues).map_err(Error::VhostUserConnect)?
})
let now = Instant::now();
// Retry connecting for a full minute
let err = loop {
let err = match Master::connect(socket_path, num_queues) {
Ok(m) => return Ok(m),
Err(e) => e,
};
sleep(Duration::from_millis(100));
if now.elapsed().as_secs() < 60 {
break err;
}
};
error!(
"Failed connecting the backend after trying for 1 minute: {:?}",
err
);
Err(Error::VhostUserConnect)
}
}