From ceaae3049c5aef6e1950c9820acf39db4dd8cd4b Mon Sep 17 00:00:00 2001 From: Fupan Li Date: Tue, 17 Jun 2025 10:53:55 +0800 Subject: [PATCH] runtime-rs: move the bytes_to_megs and megs_to_bytes to utils Since those two functions would be used by other hypervisors, thus move them into the utils crate. Signed-off-by: Fupan Li --- .../crates/hypervisor/src/qemu/inner.rs | 17 +++++------------ src/runtime-rs/crates/hypervisor/src/utils.rs | 8 ++++++++ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/runtime-rs/crates/hypervisor/src/qemu/inner.rs b/src/runtime-rs/crates/hypervisor/src/qemu/inner.rs index 8aa6c83277..4418f83f4b 100644 --- a/src/runtime-rs/crates/hypervisor/src/qemu/inner.rs +++ b/src/runtime-rs/crates/hypervisor/src/qemu/inner.rs @@ -7,10 +7,12 @@ use super::cmdline_generator::{get_network_device, QemuCmdLine, QMP_SOCKET_FILE} use super::qmp::Qmp; use crate::device::topology::PCIePort; use crate::{ - device::driver::ProtectionDeviceConfig, hypervisor_persist::HypervisorState, - utils::enter_netns, HypervisorConfig, MemoryConfig, VcpuThreadIds, VsockDevice, - HYPERVISOR_QEMU, + device::driver::ProtectionDeviceConfig, hypervisor_persist::HypervisorState, HypervisorConfig, + MemoryConfig, VcpuThreadIds, VsockDevice, HYPERVISOR_QEMU, }; + +use crate::utils::{bytes_to_megs, enter_netns, megs_to_bytes}; + use anyhow::{anyhow, Context, Result}; use async_trait::async_trait; use kata_sys_util::netns::NetnsGuard; @@ -453,15 +455,6 @@ impl QemuInner { "QemuInner::resize_memory(): asked to resize memory to {} MB", new_total_mem_mb ); - // stick to the apparent de facto convention and represent megabytes - // as u32 and bytes as u64 - fn bytes_to_megs(bytes: u64) -> u32 { - (bytes / (1 << 20)) as u32 - } - fn megs_to_bytes(bytes: u32) -> u64 { - bytes as u64 * (1 << 20) - } - let qmp = match self.qmp { Some(ref mut qmp) => qmp, None => { diff --git a/src/runtime-rs/crates/hypervisor/src/utils.rs b/src/runtime-rs/crates/hypervisor/src/utils.rs index d1fe47a5f1..2cdd91d5f9 100644 --- a/src/runtime-rs/crates/hypervisor/src/utils.rs +++ b/src/runtime-rs/crates/hypervisor/src/utils.rs @@ -192,6 +192,14 @@ impl std::fmt::Display for SocketAddress { } } +pub fn bytes_to_megs(bytes: u64) -> u32 { + (bytes / (1 << 20)) as u32 +} + +pub fn megs_to_bytes(bytes: u32) -> u64 { + bytes as u64 * (1 << 20) +} + #[cfg(test)] mod tests { use super::create_fds;