mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2025-03-20 07:58:55 +00:00
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:
parent
ab4b30edd3
commit
7f96eb2b67
@ -43,6 +43,7 @@ use std::io::{Read, Write};
|
||||
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
|
||||
use std::os::unix::net::UnixListener;
|
||||
use std::os::unix::net::UnixStream;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::mpsc::{Receiver, RecvError, SendError, Sender};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::{result, thread};
|
||||
@ -811,6 +812,14 @@ impl Vmm {
|
||||
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(
|
||||
&mut self,
|
||||
receive_data_migration: VmReceiveMigrationData,
|
||||
@ -820,34 +829,16 @@ impl Vmm {
|
||||
receive_data_migration.receiver_url
|
||||
);
|
||||
|
||||
let url = url::Url::parse(&receive_data_migration.receiver_url)
|
||||
.map_err(|e| MigratableError::MigrateReceive(anyhow!("Error parsing URL: {}", e)))?;
|
||||
|
||||
let mut socket = match url.scheme() {
|
||||
"unix" => {
|
||||
let path = url.to_file_path().map_err(|_| {
|
||||
MigratableError::MigrateReceive(anyhow!("Error extracting path from URL"))
|
||||
})?;
|
||||
let listener = UnixListener::bind(&path).map_err(|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 path = Self::socket_url_to_path(&receive_data_migration.receiver_url)?;
|
||||
let listener = UnixListener::bind(&path).map_err(|e| {
|
||||
MigratableError::MigrateReceive(anyhow!("Error binding to UNIX socket: {}", e))
|
||||
})?;
|
||||
let (mut 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))
|
||||
})?;
|
||||
|
||||
let mut started = false;
|
||||
let mut vm: Option<Vm> = None;
|
||||
@ -968,21 +959,10 @@ impl Vmm {
|
||||
send_data_migration.destination_url
|
||||
);
|
||||
if let Some(ref mut vm) = self.vm {
|
||||
let url = url::Url::parse(&send_data_migration.destination_url)
|
||||
.map_err(|e| MigratableError::MigrateSend(anyhow!("Error parsing URL: {}", e)))?;
|
||||
let mut socket = match url.scheme() {
|
||||
"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"
|
||||
)))
|
||||
}
|
||||
};
|
||||
let path = Self::socket_url_to_path(&send_data_migration.destination_url)?;
|
||||
let mut socket = UnixStream::connect(&path).map_err(|e| {
|
||||
MigratableError::MigrateSend(anyhow!("Error connecting to UNIX socket: {}", e))
|
||||
})?;
|
||||
|
||||
// Start the migration
|
||||
Request::start().write_to(&mut socket)?;
|
||||
|
Loading…
x
Reference in New Issue
Block a user