vmm: Use Response::ok_or_abandon() in migration logic

The use of this method removes duplicated code yet provides clarity on
the logic.

Signed-off-by: Rob Bradford <rbradford@rivosinc.com>
This commit is contained in:
Rob Bradford 2024-07-05 11:10:19 +01:00 committed by Liu Wei
parent e97cee99ef
commit de1abe0e30
2 changed files with 30 additions and 65 deletions

View File

@ -949,15 +949,10 @@ impl Vmm {
table.write_to(socket)?; table.write_to(socket)?;
// And then the memory itself // And then the memory itself
vm.send_memory_regions(&table, socket)?; vm.send_memory_regions(&table, socket)?;
let res = Response::read_from(socket)?; Response::read_from(socket)?.ok_or_abandon(
if res.status() != Status::Ok { socket,
warn!("Error during dirty memory migration"); MigratableError::MigrateSend(anyhow!("Error during dirty memory migration")),
Request::abandon().write_to(socket)?; )?;
Response::read_from(socket).ok();
return Err(MigratableError::MigrateSend(anyhow!(
"Error during dirty memory migration"
)));
}
Ok(true) Ok(true)
} }
@ -976,15 +971,10 @@ impl Vmm {
// Start the migration // Start the migration
Request::start().write_to(&mut socket)?; Request::start().write_to(&mut socket)?;
let res = Response::read_from(&mut socket)?; Response::read_from(&mut socket)?.ok_or_abandon(
if res.status() != Status::Ok { &mut socket,
warn!("Error starting migration"); MigratableError::MigrateSend(anyhow!("Error starting migration")),
Request::abandon().write_to(&mut socket)?; )?;
Response::read_from(&mut socket).ok();
return Err(MigratableError::MigrateSend(anyhow!(
"Error starting migration"
)));
}
// Send config // Send config
let vm_config = vm.get_config(); let vm_config = vm.get_config();
@ -1031,15 +1021,10 @@ impl Vmm {
socket socket
.write_all(&config_data) .write_all(&config_data)
.map_err(MigratableError::MigrateSocket)?; .map_err(MigratableError::MigrateSocket)?;
let res = Response::read_from(&mut socket)?; Response::read_from(&mut socket)?.ok_or_abandon(
if res.status() != Status::Ok { &mut socket,
warn!("Error during config migration"); MigratableError::MigrateSend(anyhow!("Error during config migration")),
Request::abandon().write_to(&mut socket)?; )?;
Response::read_from(&mut socket).ok();
return Err(MigratableError::MigrateSend(anyhow!(
"Error during config migration"
)));
}
// Let every Migratable object know about the migration being started. // Let every Migratable object know about the migration being started.
vm.start_migration()?; vm.start_migration()?;
@ -1059,15 +1044,10 @@ impl Vmm {
table.write_to(&mut socket)?; table.write_to(&mut socket)?;
// And then the memory itself // And then the memory itself
vm.send_memory_regions(&table, &mut socket)?; vm.send_memory_regions(&table, &mut socket)?;
let res = Response::read_from(&mut socket)?; Response::read_from(&mut socket)?.ok_or_abandon(
if res.status() != Status::Ok { &mut socket,
warn!("Error during memory migration"); MigratableError::MigrateSend(anyhow!("Error during dirty memory migration")),
Request::abandon().write_to(&mut socket)?; )?;
Response::read_from(&mut socket).ok();
return Err(MigratableError::MigrateSend(anyhow!(
"Error during memory migration"
)));
}
// Try at most 5 passes of dirty memory sending // Try at most 5 passes of dirty memory sending
const MAX_DIRTY_MIGRATIONS: usize = 5; const MAX_DIRTY_MIGRATIONS: usize = 5;
@ -1094,27 +1074,17 @@ impl Vmm {
socket socket
.write_all(&snapshot_data) .write_all(&snapshot_data)
.map_err(MigratableError::MigrateSocket)?; .map_err(MigratableError::MigrateSocket)?;
let res = Response::read_from(&mut socket)?; Response::read_from(&mut socket)?.ok_or_abandon(
if res.status() != Status::Ok { &mut socket,
warn!("Error during state migration"); MigratableError::MigrateSend(anyhow!("Error during state migration")),
Request::abandon().write_to(&mut socket)?; )?;
Response::read_from(&mut socket).ok();
return Err(MigratableError::MigrateSend(anyhow!(
"Error during state migration"
)));
}
// Complete the migration // Complete the migration
Request::complete().write_to(&mut socket)?; Request::complete().write_to(&mut socket)?;
let res = Response::read_from(&mut socket)?; Response::read_from(&mut socket)?.ok_or_abandon(
if res.status() != Status::Ok { &mut socket,
warn!("Error completing migration"); MigratableError::MigrateSend(anyhow!("Error completing migration")),
Request::abandon().write_to(&mut socket)?; )?;
Response::read_from(&mut socket).ok();
return Err(MigratableError::MigrateSend(anyhow!(
"Error completing migration"
)));
}
info!("Migration complete"); info!("Migration complete");
// Let every Migratable object know about the migration being complete // Let every Migratable object know about the migration being complete

View File

@ -93,7 +93,7 @@ use vm_memory::{Address, ByteValued, GuestMemoryRegion, ReadVolatile};
use vm_memory::{ use vm_memory::{
Bytes, GuestAddress, GuestAddressSpace, GuestMemory, GuestMemoryAtomic, WriteVolatile, Bytes, GuestAddress, GuestAddressSpace, GuestMemory, GuestMemoryAtomic, WriteVolatile,
}; };
use vm_migration::protocol::{Request, Response, Status}; use vm_migration::protocol::{Request, Response};
use vm_migration::{ use vm_migration::{
protocol::MemoryRangeTable, snapshot_from_id, Migratable, MigratableError, Pausable, Snapshot, protocol::MemoryRangeTable, snapshot_from_id, Migratable, MigratableError, Pausable, Snapshot,
Snapshottable, Transportable, Snapshottable, Transportable,
@ -2241,15 +2241,10 @@ impl Vm {
MigratableError::MigrateSend(anyhow!("Error sending memory fd: {}", e)) MigratableError::MigrateSend(anyhow!("Error sending memory fd: {}", e))
})?; })?;
let res = Response::read_from(socket)?; Response::read_from(socket)?.ok_or_abandon(
if res.status() != Status::Ok { socket,
warn!("Error during memory fd migration"); MigratableError::MigrateSend(anyhow!("Error during memory fd migration")),
Request::abandon().write_to(socket)?; )?;
Response::read_from(socket).ok();
return Err(MigratableError::MigrateSend(anyhow!(
"Error during memory fd migration"
)));
}
} }
Ok(()) Ok(())