From 0030fbe2a8f6be0a58cb2570fa51d16b01730419 Mon Sep 17 00:00:00 2001 From: Hans Christian Schmitz Date: Wed, 16 Apr 2025 08:15:26 +0200 Subject: [PATCH] feat: add traditional mem balloon device --- src/balloon.rs | 33 +++++++++++++++++++++++++++++++++ src/features.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 3 ++- 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 src/balloon.rs diff --git a/src/balloon.rs b/src/balloon.rs new file mode 100644 index 0000000..b929687 --- /dev/null +++ b/src/balloon.rs @@ -0,0 +1,33 @@ +use volatile::access::{ReadOnly, ReadWrite}; +use volatile_macro::VolatileFieldAccess; + +pub use super::features::balloon::F; +use crate::le32; + +/// Traditional Memory Balloon Device Configuration Layout +/// +/// Use [`ConfigVolatileFieldAccess`] to work with this struct. +#[doc(alias = "virtio_balloon_config")] +#[cfg_attr( + feature = "zerocopy", + derive( + zerocopy_derive::KnownLayout, + zerocopy_derive::Immutable, + zerocopy_derive::FromBytes, + ) +)] +#[derive(VolatileFieldAccess)] +#[repr(C)] +pub struct Config { + #[access(ReadOnly)] + num_pages: le32, + + #[access(ReadWrite)] + actual: le32, + + #[access(ReadOnly)] + free_page_hint_cmd_id: le32, + + #[access(ReadWrite)] + poison_val: le32, +} diff --git a/src/features.rs b/src/features.rs index 1629c10..c1610ee 100644 --- a/src/features.rs +++ b/src/features.rs @@ -524,6 +524,55 @@ pub mod vsock { impl crate::FeatureBits for F {} } +pub mod balloon { + use crate::le128; + + feature_bits! { + /// Traditional Memory Balloon Device Feature Bits + #[doc(alias = "VIRTIO_BALLOON_F")] + pub struct F: le128 { + /// Host has to be told before pages from the balloon are used. + #[doc(alias = "VIRTIO_BALLOON_F_MUST_TELL_HOST")] + const MUST_TELL_HOST = 1 << 0; + + /// A virtqueue for reporting guest memory statistics is present. + #[doc(alias = "VIRTIO_BALLOON_F_STATS_VQ")] + const STATS_VQ = 1 << 1; + + /// Deflate balloon on guest out of memory condition. + /// + ///
+ /// + /// The specification is a bit confusing on this feature, see [oasis-tcs/virtio-spec#228](https://github.com/oasis-tcs/virtio-spec/issues/228). + /// + ///
+ #[doc(alias = "VIRTIO_BALLOON_F_DEFLATE_ON_OOM")] + const DEFLATE_ON_OOM = 1 << 2; + + /// The device has support for free page hinting. + /// A virtqueue for providing hints as to what memory is currently free is present. + /// Configuration field [`free_page_hint_cmd_id`](`crate::balloon::ConfigVolatileFieldAccess::free_page_hint_cmd_id`) is valid. + #[doc(alias = "VIRTIO_BALLOON_F_FREE_PAGE_HINT")] + const FREE_PAGE_HINT = 1 << 3; + + /// A hint to the device, that the driver will immediately write + /// [`poison_val`] to pages after deflating them. + /// Configuration field [`poison_val`] is valid. + /// + /// [`poison_val`]: crate::balloon::ConfigVolatileFieldAccess::poison_val + #[doc(alias = "VIRTIO_BALLOON_F_PAGE_POISON")] + const PAGE_POISON = 1 << 4; + + /// The device has support for free page reporting. + /// A virtqueue for reporting free guest memory is present. + #[doc(alias = "VIRTIO_BALLOON_F_PAGE_REPORTING")] + const PAGE_REPORTING = 1 << 5; + } + } + + impl crate::FeatureBits for F {} +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/lib.rs b/src/lib.rs index 1f4b08f..b7c3708 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -67,7 +67,7 @@ //! | Block Device | ❌ | | //! | Console Device | ✅ | [`console`] | //! | Entropy Device | ❌ | | -//! | Traditional Memory Balloon Device | ❌ | | +//! | Traditional Memory Balloon Device | ✅ | [`balloon`] | //! | SCSI Host Device | ❌ | | //! | GPU Device | ❌ | | //! | Input Device | ❌ | | @@ -94,6 +94,7 @@ extern crate alloc; mod bitflags; #[macro_use] pub mod volatile; +pub mod balloon; pub mod console; #[cfg(any(feature = "mmio", feature = "pci"))] mod driver_notifications;