vmm: simplify receiving memory fds

... and nuke some Option<> while I was there. Given that HashMap has a
usable default and we end up passing an empty HashMap anyway, just get
rid of the Option.

On-behalf-of: SAP julian.stecklina@sap.com
Signed-off-by: Julian Stecklina <julian.stecklina@cyberus-technology.de>
This commit is contained in:
Julian Stecklina
2025-11-03 16:27:12 +01:00
committed by Rob Bradford
parent 3d43245608
commit 1861bc49e7
3 changed files with 28 additions and 34 deletions
+24 -30
View File
@@ -820,11 +820,30 @@ impl Vmm {
}) })
} }
/// Try to receive a file descriptor from a socket. Returns the slot number and the file descriptor.
fn vm_receive_memory_fd(
socket: &mut SocketStream,
) -> std::result::Result<(u32, File), MigratableError> {
if let SocketStream::Unix(unix_socket) = socket {
let mut buf = [0u8; 4];
let (_, file) = unix_socket.recv_with_fd(&mut buf).map_err(|e| {
MigratableError::MigrateReceive(anyhow!("Error receiving slot from socket: {e}"))
})?;
file.ok_or_else(|| MigratableError::MigrateReceive(anyhow!("Failed to receive socket")))
.map(|file| (u32::from_le_bytes(buf), file))
} else {
Err(MigratableError::MigrateReceive(anyhow!(
"Unsupported socket type"
)))
}
}
fn vm_receive_config<T>( fn vm_receive_config<T>(
&mut self, &mut self,
req: &Request, req: &Request,
socket: &mut T, socket: &mut T,
existing_memory_files: Option<HashMap<u32, File>>, existing_memory_files: HashMap<u32, File>,
) -> std::result::Result<Arc<Mutex<MemoryManager>>, MigratableError> ) -> std::result::Result<Arc<Mutex<MemoryManager>>, MigratableError>
where where
T: Read + Write, T: Read + Write,
@@ -2126,7 +2145,7 @@ impl RequestHandler for Vmm {
let mut started = false; let mut started = false;
let mut memory_manager: Option<Arc<Mutex<MemoryManager>>> = None; let mut memory_manager: Option<Arc<Mutex<MemoryManager>>> = None;
let mut existing_memory_files = None; let mut existing_memory_files = vec![];
loop { loop {
let req = Request::read_from(&mut socket)?; let req = Request::read_from(&mut socket)?;
match req.command() { match req.command() {
@@ -2148,7 +2167,7 @@ impl RequestHandler for Vmm {
memory_manager = Some(self.vm_receive_config( memory_manager = Some(self.vm_receive_config(
&req, &req,
&mut socket, &mut socket,
existing_memory_files.take(), HashMap::from_iter(existing_memory_files.drain(..)),
)?); )?);
} }
Command::State => { Command::State => {
@@ -2190,34 +2209,9 @@ impl RequestHandler for Vmm {
continue; continue;
} }
match &mut socket { existing_memory_files.push(Self::vm_receive_memory_fd(&mut socket)?);
SocketStream::Unix(unix_socket) => {
let mut buf = [0u8; 4];
let (_, file) = unix_socket.recv_with_fd(&mut buf).map_err(|e| {
MigratableError::MigrateReceive(anyhow!(
"Error receiving slot from socket: {e}"
))
})?;
if existing_memory_files.is_none() { Response::ok().write_to(&mut socket)?;
existing_memory_files = Some(HashMap::default());
}
if let Some(ref mut existing_memory_files) = existing_memory_files {
let slot = u32::from_le_bytes(buf);
existing_memory_files.insert(slot, file.unwrap());
}
Response::ok().write_to(&mut socket)?;
}
SocketStream::Tcp(_tcp_socket) => {
// For TCP sockets, we cannot transfer file descriptors
warn!(
"MemoryFd command received over TCP socket, which is not supported"
);
Response::error().write_to(&mut socket)?;
}
}
} }
Command::Complete => { Command::Complete => {
info!("Complete Command Received"); info!("Complete Command Received");
+3 -3
View File
@@ -995,7 +995,7 @@ impl MemoryManager {
phys_bits: u8, phys_bits: u8,
#[cfg(feature = "tdx")] tdx_enabled: bool, #[cfg(feature = "tdx")] tdx_enabled: bool,
restore_data: Option<&MemoryManagerSnapshotData>, restore_data: Option<&MemoryManagerSnapshotData>,
existing_memory_files: Option<HashMap<u32, File>>, existing_memory_files: HashMap<u32, File>,
) -> Result<Arc<Mutex<MemoryManager>>, Error> { ) -> Result<Arc<Mutex<MemoryManager>>, Error> {
trace_scoped!("MemoryManager::new"); trace_scoped!("MemoryManager::new");
@@ -1030,7 +1030,7 @@ impl MemoryManager {
&data.guest_ram_mappings, &data.guest_ram_mappings,
&zones, &zones,
prefault, prefault,
existing_memory_files.unwrap_or_default(), existing_memory_files,
config.thp, config.thp,
)?; )?;
let guest_memory = let guest_memory =
@@ -1263,7 +1263,7 @@ impl MemoryManager {
#[cfg(feature = "tdx")] #[cfg(feature = "tdx")]
false, false,
Some(&mem_snapshot), Some(&mem_snapshot),
None, Default::default(),
)?; )?;
mm.lock() mm.lock()
+1 -1
View File
@@ -1056,7 +1056,7 @@ impl Vm {
#[cfg(feature = "tdx")] #[cfg(feature = "tdx")]
tdx_enabled, tdx_enabled,
None, None,
None, Default::default(),
) )
.map_err(Error::MemoryManager)? .map_err(Error::MemoryManager)?
}; };