Skip to content
This repository was archived by the owner on Nov 6, 2024. It is now read-only.

Add versioning to all bindings and conditionally compiled serde support to x86 bindings relevant to Firecracker snapshotting #14

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ license = "Apache-2.0"
kvm-v4_14_0 = []
kvm-v4_20_0 = []
fam-wrappers = ["vmm-sys-util"]
with-serde = ["serde", "serde_derive"]

[dependencies]
serde = { version = ">=1.0.27", optional = true }
serde_derive = { version = ">=1.0.27", optional = true }
vmm-sys-util = { version = ">=0.2.0", optional = true }

[dev-dependencies]
serde_json = ">=1.0.9"
48 changes: 48 additions & 0 deletions src/arm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,51 @@ pub mod bindings {
#[cfg(all(not(feature = "kvm-v4_14_0"), not(feature = "kvm-v4_20_0")))]
pub use super::bindings_v4_20_0::*;
}

#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a proposal for a new crate at:
rust-vmm/community#76
which aims to build a common serde framework for snapshot/live migration/live upgrading.
It helps to simplify the code as
#[derive(Deserialize, Serialize)]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 👍 I think I don't fully understand what the new crate wants to achieve, I added a couple of questions to start from in the PR.

#[cfg_attr(test, derive(Debug, PartialEq))]
/// Composite version of the autogenerated bindings.
pub struct Version {
/// Architecture.
pub arch: &'static str,
/// Kernel version.
pub kernel_ver: &'static str,
/// Crate version.
pub crate_ver: &'static str,
}

#[allow(unused)]
static VERSION: Version = Version {
arch: "aarch",

#[cfg(feature = "kvm-v4_14_0")]
kernel_ver: "v4.14.0",
#[cfg(feature = "kvm-v4_20_0")]
kernel_ver: "v4.20.0",
#[cfg(all(not(feature = "kvm-v4_14_0"), not(feature = "kvm-v4_20_0")))]
kernel_ver: "v4.20.0",

crate_ver: env!("CARGO_PKG_VERSION"),
};

#[cfg(test)]
mod tests {
#[cfg(feature = "with-serde")]
extern crate serde_json;

use super::{Version, VERSION};

#[test]
fn test_version() {
assert_eq!(VERSION.arch, "aarch");

#[cfg(feature = "kvm-v4_14_0")]
assert_eq!(VERSION.kernel_ver, "v4.14.0");
#[cfg(feature = "kvm-v4_20_0")]
assert_eq!(VERSION.kernel_ver, "v4.20.0");
#[cfg(all(not(feature = "kvm-v4_14_0"), not(feature = "kvm-v4_20_0")))]
assert_eq!(VERSION.kernel_ver, "v4.20.0");

assert_eq!(VERSION.crate_ver, env!("CARGO_PKG_VERSION"));
}
}
48 changes: 48 additions & 0 deletions src/arm64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,51 @@ pub mod bindings {
#[cfg(all(not(feature = "kvm-v4_14_0"), not(feature = "kvm-v4_20_0")))]
pub use super::bindings_v4_20_0::*;
}

#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
#[cfg_attr(test, derive(Debug, PartialEq))]
/// Composite version of the autogenerated bindings.
pub struct Version {
/// Architecture.
pub arch: &'static str,
/// Kernel version.
pub kernel_ver: &'static str,
/// Crate version.
pub crate_ver: &'static str,
}

#[allow(unused)]
static VERSION: Version = Version {
arch: "aarch64",

#[cfg(feature = "kvm-v4_14_0")]
kernel_ver: "v4.14.0",
#[cfg(feature = "kvm-v4_20_0")]
kernel_ver: "v4.20.0",
#[cfg(all(not(feature = "kvm-v4_14_0"), not(feature = "kvm-v4_20_0")))]
kernel_ver: "v4.20.0",

crate_ver: env!("CARGO_PKG_VERSION"),
};

#[cfg(test)]
mod tests {
#[cfg(feature = "with-serde")]
extern crate serde_json;

use super::{Version, VERSION};

#[test]
fn test_version() {
assert_eq!(VERSION.arch, "aarch64");

#[cfg(feature = "kvm-v4_14_0")]
assert_eq!(VERSION.kernel_ver, "v4.14.0");
#[cfg(feature = "kvm-v4_20_0")]
assert_eq!(VERSION.kernel_ver, "v4.20.0");
#[cfg(all(not(feature = "kvm-v4_14_0"), not(feature = "kvm-v4_20_0")))]
assert_eq!(VERSION.kernel_ver, "v4.20.0");

assert_eq!(VERSION.crate_ver, env!("CARGO_PKG_VERSION"));
}
}
34 changes: 34 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,51 @@
))]
extern crate vmm_sys_util;

#[cfg(feature = "with-serde")]
extern crate serde;

#[cfg(feature = "with-serde")]
extern crate serde_derive;

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
mod x86;
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
pub use self::x86::bindings::*;
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
pub use self::x86::Version;

#[cfg(target_arch = "aarch")]
mod arm;
#[cfg(target_arch = "aarch")]
pub use self::arm::bindings::*;
#[cfg(target_arch = "aarch")]
pub use self::arm::Version;

#[cfg(target_arch = "aarch64")]
mod arm64;
#[cfg(target_arch = "aarch64")]
pub use self::arm64::bindings::*;
#[cfg(target_arch = "aarch64")]
pub use self::arm64::Version;

#[cfg(all(test, feature = "with-serde"))]
mod tests {
extern crate serde_json;

use super::Version;

#[test]
fn test_version_ser_deser() {
let ver = Version {
arch: "x86_64",
kernel_ver: "v4.20.0",
crate_ver: "v0.2.0",
};
let ver_str = "{\"arch\":\"x86_64\",\"kernel_ver\":\"v4.20.0\",\"crate_ver\":\"v0.2.0\"}";
let ver_ser = serde_json::to_string(&ver).unwrap();
assert_eq!(ver_ser, ver_str.to_string());

let ver_deser = serde_json::from_str::<Version>(ver_str).unwrap();
assert_eq!(ver, ver_deser);
}
}
28 changes: 28 additions & 0 deletions src/x86/bindings_v4_14_0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// SPDX-License-Identifier: Apache-2.0
/* automatically generated by rust-bindgen */

#[cfg(feature = "with-serde")]
use serde_derive::{Deserialize, Serialize};

#[repr(C)]
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct __BindgenBitfieldUnit<Storage, Align>
Expand Down Expand Up @@ -893,6 +896,7 @@ fn bindgen_test_layout_kvm_pic_state() {
}
#[repr(C)]
#[derive(Copy, Clone)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct kvm_ioapic_state {
pub base_address: __u64,
pub ioregsel: __u32,
Expand Down Expand Up @@ -1233,6 +1237,7 @@ impl Default for kvm_ioapic_state {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct kvm_regs {
pub rax: __u64,
pub rbx: __u64,
Expand Down Expand Up @@ -1481,6 +1486,7 @@ impl Default for kvm_lapic_state {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct kvm_segment {
pub base: __u64,
pub limit: __u32,
Expand Down Expand Up @@ -1641,6 +1647,7 @@ fn bindgen_test_layout_kvm_segment() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct kvm_dtable {
pub base: __u64,
pub limit: __u16,
Expand Down Expand Up @@ -1691,6 +1698,7 @@ fn bindgen_test_layout_kvm_dtable() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct kvm_sregs {
pub cs: kvm_segment,
pub ds: kvm_segment,
Expand Down Expand Up @@ -2094,9 +2102,12 @@ fn bindgen_test_layout_kvm_msr_entry() {
}
#[repr(C)]
#[derive(Debug, Default)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct kvm_msrs {
pub nmsrs: __u32,
#[cfg_attr(feature = "with-serde", serde(skip))]
pub pad: __u32,
#[cfg_attr(feature = "with-serde", serde(skip))]
pub entries: __IncompleteArrayField<kvm_msr_entry>,
}
#[test]
Expand Down Expand Up @@ -2421,9 +2432,12 @@ fn bindgen_test_layout_kvm_cpuid_entry2() {
}
#[repr(C)]
#[derive(Debug, Default)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct kvm_cpuid2 {
pub nent: __u32,
#[cfg_attr(feature = "with-serde", serde(skip))]
pub padding: __u32,
#[cfg_attr(feature = "with-serde", serde(skip))]
pub entries: __IncompleteArrayField<kvm_cpuid_entry2>,
}
#[test]
Expand Down Expand Up @@ -2471,6 +2485,7 @@ fn bindgen_test_layout_kvm_cpuid2() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct kvm_pit_channel_state {
pub count: __u32,
pub latched_count: __u16,
Expand Down Expand Up @@ -2773,6 +2788,7 @@ fn bindgen_test_layout_kvm_pit_state() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct kvm_pit_state2 {
pub channels: [kvm_pit_channel_state; 3usize],
pub flags: __u32,
Expand Down Expand Up @@ -2864,6 +2880,7 @@ fn bindgen_test_layout_kvm_reinject_control() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct kvm_vcpu_events {
pub exception: kvm_vcpu_events__bindgen_ty_1,
pub interrupt: kvm_vcpu_events__bindgen_ty_2,
Expand All @@ -2875,6 +2892,7 @@ pub struct kvm_vcpu_events {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct kvm_vcpu_events__bindgen_ty_1 {
pub injected: __u8,
pub nr: __u8,
Expand Down Expand Up @@ -2959,6 +2977,7 @@ fn bindgen_test_layout_kvm_vcpu_events__bindgen_ty_1() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct kvm_vcpu_events__bindgen_ty_2 {
pub injected: __u8,
pub nr: __u8,
Expand Down Expand Up @@ -3028,6 +3047,7 @@ fn bindgen_test_layout_kvm_vcpu_events__bindgen_ty_2() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct kvm_vcpu_events__bindgen_ty_3 {
pub injected: __u8,
pub pending: __u8,
Expand Down Expand Up @@ -3097,6 +3117,7 @@ fn bindgen_test_layout_kvm_vcpu_events__bindgen_ty_3() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct kvm_vcpu_events__bindgen_ty_4 {
pub smm: __u8,
pub pending: __u8,
Expand Down Expand Up @@ -3251,6 +3272,7 @@ fn bindgen_test_layout_kvm_vcpu_events() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct kvm_debugregs {
pub db: [__u64; 4usize],
pub dr6: __u64,
Expand Down Expand Up @@ -3356,6 +3378,7 @@ impl Default for kvm_xsave {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct kvm_xcr {
pub xcr: __u32,
pub reserved: __u32,
Expand Down Expand Up @@ -3406,6 +3429,7 @@ fn bindgen_test_layout_kvm_xcr() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct kvm_xcrs {
pub nr_xcrs: __u32,
pub flags: __u32,
Expand Down Expand Up @@ -3861,8 +3885,10 @@ impl Default for kvm_irq_level {
}
#[repr(C)]
#[derive(Copy, Clone)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct kvm_irqchip {
pub chip_id: __u32,
#[cfg_attr(feature = "with-serde", serde(skip))]
pub pad: __u32,
pub chip: kvm_irqchip__bindgen_ty_1,
}
Expand Down Expand Up @@ -6659,6 +6685,7 @@ fn bindgen_test_layout_kvm_vapic_addr() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct kvm_mp_state {
pub mp_state: __u32,
}
Expand Down Expand Up @@ -8722,6 +8749,7 @@ fn bindgen_test_layout_kvm_irqfd() {
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct kvm_clock_data {
pub clock: __u64,
pub flags: __u32,
Expand Down
Loading