vmm: Add virtio-balloon support

This commit adds new option balloon to memory config.
Set it to on will open the balloon function.

Signed-off-by: Hui Zhu <teawater@antfin.com>
This commit is contained in:
Hui Zhu
2020-03-20 11:43:37 +08:00
committed by Rob Bradford
parent 0d72aa782f
commit 8b6b97b86f
4 changed files with 79 additions and 1 deletions

View File

@@ -70,6 +70,7 @@ pub struct MemoryManager {
snapshot: Mutex<Option<GuestMemoryLoadGuard<GuestMemoryMmap>>>,
shared: bool,
hugepages: bool,
balloon: Option<Arc<Mutex<virtio_devices::Balloon>>>,
}
#[derive(Debug)]
@@ -122,6 +123,9 @@ pub enum Error {
/// The number of external backing files doesn't match the number of
/// memory regions.
InvalidAmountExternalBackingFiles,
/// Failed to virtio-balloon resize
VirtioBalloonResizeFail(virtio_devices::balloon::Error),
}
const ENABLE_FLAG: usize = 0;
@@ -338,6 +342,7 @@ impl MemoryManager {
snapshot: Mutex::new(None),
shared: config.shared,
hugepages: config.hugepages,
balloon: None,
}));
guest_memory.memory().with_regions(|_, region| {
@@ -645,6 +650,10 @@ impl MemoryManager {
Ok(region)
}
pub fn set_balloon(&mut self, balloon: Arc<Mutex<virtio_devices::Balloon>>) {
self.balloon = Some(balloon);
}
pub fn guest_memory(&self) -> GuestMemoryAtomic<GuestMemoryMmap> {
self.guest_memory.clone()
}
@@ -790,6 +799,23 @@ impl MemoryManager {
Ok(())
}
pub fn balloon_resize(&mut self, expected_ram: u64) -> Result<(), Error> {
if let Some(balloon) = &self.balloon {
let balloon_size = if expected_ram < self.current_ram {
self.current_ram - expected_ram
} else {
0
};
balloon
.lock()
.unwrap()
.resize(balloon_size)
.map_err(Error::VirtioBalloonResizeFail)?;
}
Ok(())
}
/// In case this function resulted in adding a new memory region to the
/// guest memory, the new region is returned to the caller. The virtio-mem
/// use case never adds a new region as the whole hotpluggable memory has