vmm: migration: Simplify url socket handling in migration code

Extract URL handling to a common function and simplify to remove url
crate dependency.

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2021-03-12 13:24:03 +00:00 committed by Sebastien Boeuf
parent ab4b30edd3
commit 7f96eb2b67

View File

@ -43,6 +43,7 @@ use std::io::{Read, Write};
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
use std::os::unix::net::UnixListener; use std::os::unix::net::UnixListener;
use std::os::unix::net::UnixStream; use std::os::unix::net::UnixStream;
use std::path::PathBuf;
use std::sync::mpsc::{Receiver, RecvError, SendError, Sender}; use std::sync::mpsc::{Receiver, RecvError, SendError, Sender};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::{result, thread}; use std::{result, thread};
@ -811,6 +812,14 @@ impl Vmm {
Ok(()) Ok(())
} }
fn socket_url_to_path(url: &str) -> result::Result<PathBuf, MigratableError> {
url.strip_prefix("unix:")
.ok_or_else(|| {
MigratableError::MigrateSend(anyhow!("Could not extract path from URL: {}", url))
})
.map(|s| s.into())
}
fn vm_receive_migration( fn vm_receive_migration(
&mut self, &mut self,
receive_data_migration: VmReceiveMigrationData, receive_data_migration: VmReceiveMigrationData,
@ -820,34 +829,16 @@ impl Vmm {
receive_data_migration.receiver_url receive_data_migration.receiver_url
); );
let url = url::Url::parse(&receive_data_migration.receiver_url) let path = Self::socket_url_to_path(&receive_data_migration.receiver_url)?;
.map_err(|e| MigratableError::MigrateReceive(anyhow!("Error parsing URL: {}", e)))?; let listener = UnixListener::bind(&path).map_err(|e| {
MigratableError::MigrateReceive(anyhow!("Error binding to UNIX socket: {}", e))
let mut socket = match url.scheme() { })?;
"unix" => { let (mut socket, _addr) = listener.accept().map_err(|e| {
let path = url.to_file_path().map_err(|_| { MigratableError::MigrateReceive(anyhow!("Error accepting on UNIX socket: {}", e))
MigratableError::MigrateReceive(anyhow!("Error extracting path from URL")) })?;
})?; std::fs::remove_file(&path).map_err(|e| {
let listener = UnixListener::bind(&path).map_err(|e| { MigratableError::MigrateReceive(anyhow!("Error unlinking UNIX socket: {}", e))
MigratableError::MigrateReceive(anyhow!("Error binding to UNIX socket: {}", e)) })?;
})?;
let (socket, _addr) = listener.accept().map_err(|e| {
MigratableError::MigrateReceive(anyhow!(
"Error accepting on UNIX socket: {}",
e
))
})?;
std::fs::remove_file(&path).map_err(|e| {
MigratableError::MigrateReceive(anyhow!("Error unlinking UNIX socket: {}", e))
})?;
socket
}
_ => {
return Err(MigratableError::MigrateReceive(anyhow!(
"Unsupported URL scheme"
)))
}
};
let mut started = false; let mut started = false;
let mut vm: Option<Vm> = None; let mut vm: Option<Vm> = None;
@ -968,21 +959,10 @@ impl Vmm {
send_data_migration.destination_url send_data_migration.destination_url
); );
if let Some(ref mut vm) = self.vm { if let Some(ref mut vm) = self.vm {
let url = url::Url::parse(&send_data_migration.destination_url) let path = Self::socket_url_to_path(&send_data_migration.destination_url)?;
.map_err(|e| MigratableError::MigrateSend(anyhow!("Error parsing URL: {}", e)))?; let mut socket = UnixStream::connect(&path).map_err(|e| {
let mut socket = match url.scheme() { MigratableError::MigrateSend(anyhow!("Error connecting to UNIX socket: {}", e))
"unix" => UnixStream::connect(url.to_file_path().map_err(|_| { })?;
MigratableError::MigrateSend(anyhow!("Error extracting path from URL"))
})?)
.map_err(|e| {
MigratableError::MigrateSend(anyhow!("Error connecting to UNIX socket: {}", e))
})?,
_ => {
return Err(MigratableError::MigrateReceive(anyhow!(
"Unsupported URL scheme"
)))
}
};
// Start the migration // Start the migration
Request::start().write_to(&mut socket)?; Request::start().write_to(&mut socket)?;