Skip to content

Commit 15ea431

Browse files
committed
feat: add traditional mem balloon device
1 parent 2639863 commit 15ea431

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

src/balloon.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use volatile::access::{ReadOnly, ReadWrite};
2+
use volatile_macro::VolatileFieldAccess;
3+
4+
pub use super::features::balloon::F;
5+
use crate::le32;
6+
7+
/// Traditional Memory Balloon Device Configuration Layout
8+
///
9+
/// Use [`ConfigVolatileFieldAccess`] to work with this struct.
10+
#[doc(alias = "virtio_balloon_config")]
11+
#[cfg_attr(
12+
feature = "zerocopy",
13+
derive(
14+
zerocopy_derive::KnownLayout,
15+
zerocopy_derive::Immutable,
16+
zerocopy_derive::FromBytes,
17+
)
18+
)]
19+
#[derive(VolatileFieldAccess)]
20+
#[repr(C)]
21+
pub struct Config {
22+
#[access(ReadOnly)] // TODO: Not actually specified by the spec?
23+
num_pages: le32,
24+
25+
#[access(ReadWrite)]
26+
actual: le32,
27+
28+
#[access(ReadOnly)]
29+
free_page_hint_cmd_id: le32,
30+
31+
#[access(ReadWrite)]
32+
poison_val: le32,
33+
}

src/features.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,51 @@ pub mod vsock {
524524
impl crate::FeatureBits for F {}
525525
}
526526

527+
pub mod balloon {
528+
use crate::le128;
529+
530+
feature_bits! {
531+
/// Traditional Memory Balloon Device Feature Bits
532+
#[doc(alias = "VIRTIO_BALLOON_F")]
533+
pub struct F: le128 {
534+
/// Host has to be told before pages from the balloon are used.
535+
#[doc(alias = "VIRTIO_BALLOON_F_MUST_TELL_HOST")]
536+
const MUST_TELL_HOST = 1 << 0;
537+
538+
/// A virtqueue for reporting guest memory statistics is present.
539+
#[doc(alias = "VIRTIO_BALLOON_F_STATS_VQ")]
540+
const STATS_VQ = 1 << 1;
541+
542+
/// Deflate of the balloon is always? permitted on guest out of memory condition.
543+
///
544+
/// TODO: Spec is a bit confusing on this feature, see https://github.com/oasis-tcs/virtio-spec/issues/228
545+
#[doc(alias = "VIRTIO_BALLOON_F_DEFLATE_ON_OOM")]
546+
const DEFLATE_ON_OOM = 1 << 2;
547+
548+
/// The device has support for free page hinting.
549+
/// A virtqueue for providing hints as to what memory is currently free is present.
550+
/// Configuration field [`free_page_hint_cmd_id`](`crate::balloon::Config::free_page_hint_cmd_id`) is valid.
551+
#[doc(alias = "VIRTIO_BALLOON_F_FREE_PAGE_HINT")]
552+
const FREE_PAGE_HINT = 1 << 3;
553+
554+
/// A hint to the device, that the driver will immediately write
555+
/// [`poison_val`] to pages after deflating them.
556+
/// Configuration field [`poison_val`] is valid.
557+
///
558+
/// [`poison_val`]: crate::balloon::Config::poison_val
559+
#[doc(alias = "VIRTIO_BALLOON_F_PAGE_POISON")]
560+
const PAGE_POISON = 1 << 4;
561+
562+
/// The device has support for free page reporting.
563+
/// A virtqueue for reporting free guest memory is present.
564+
#[doc(alias = "VIRTIO_BALLOON_F_PAGE_REPORTING")]
565+
const PAGE_REPORTING = 1 << 5;
566+
}
567+
}
568+
569+
impl crate::FeatureBits for F {}
570+
}
571+
527572
#[cfg(test)]
528573
mod tests {
529574
use super::*;

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
//! | Block Device | ❌ | |
6868
//! | Console Device | ✅ | [`console`] |
6969
//! | Entropy Device | ❌ | |
70-
//! | Traditional Memory Balloon Device | | |
70+
//! | Traditional Memory Balloon Device | | [`balloon`] |
7171
//! | SCSI Host Device | ❌ | |
7272
//! | GPU Device | ❌ | |
7373
//! | Input Device | ❌ | |
@@ -95,6 +95,7 @@ mod bitflags;
9595
#[macro_use]
9696
pub mod volatile;
9797
pub mod console;
98+
pub mod balloon;
9899
#[cfg(any(feature = "mmio", feature = "pci"))]
99100
mod driver_notifications;
100101
mod features;

0 commit comments

Comments
 (0)