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

Commit 19d8ad7

Browse files
committed
Add serde support for various bindings
Add implementations of `serde::{Serialize, Deserialize}` for kvm-bindings structures commonly used for live migration/snapshotting. The selections of structures to add the derives to was determined by finding the minimal set that allows both Firecracker and CloudHypervisor to compile against this branch. Signed-off-by: Patrick Roy <[email protected]>
1 parent 33a89cc commit 19d8ad7

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ rustdoc-args = ["--cfg", "docsrs"]
1414

1515
[features]
1616
fam-wrappers = ["vmm-sys-util"]
17+
# If the 'fam-wrappers' feature is not enabled, the below 'vmm-sys-util/with-serde' flag will be ignored (e.g. it will not enable the vmm-sys-util dependency). Only if both 'fam-wrappers' and 'with-serde' are enaled is the 'with-serde' feature of vmm-sys-util enabled.
18+
with-serde = ["dep:serde", "serde/derive", "dep:serde-big-array", "vmm-sys-util/with-serde"]
19+
1720

1821
[dependencies]
1922
vmm-sys-util = { version = "0.12.1", optional = true }
23+
serde = { version = "1.0.0", optional = true, features = ["derive"] }
24+
serde-big-array = { version = "0.5.1", optional = true}

src/arm64/bindings.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,7 @@ pub type __wsum = __u32;
955955
pub type __poll_t = ::std::os::raw::c_uint;
956956
#[repr(C)]
957957
#[derive(Debug, Default, Copy, Clone, PartialEq)]
958+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
958959
pub struct user_pt_regs {
959960
pub regs: [__u64; 31usize],
960961
pub sp: __u64,
@@ -1019,6 +1020,7 @@ fn bindgen_test_layout_user_pt_regs() {
10191020
#[repr(C)]
10201021
#[repr(align(16))]
10211022
#[derive(Debug, Default, Copy, Clone, PartialEq)]
1023+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
10221024
pub struct user_fpsimd_state {
10231025
pub vregs: [__uint128_t; 32usize],
10241026
pub fpsr: __u32,
@@ -1499,6 +1501,7 @@ fn bindgen_test_layout_user_za_header() {
14991501
#[repr(C)]
15001502
#[repr(align(16))]
15011503
#[derive(Debug, Default, Copy, Clone, PartialEq)]
1504+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
15021505
pub struct kvm_regs {
15031506
pub regs: user_pt_regs,
15041507
pub sp_el1: __u64,
@@ -1574,6 +1577,7 @@ fn bindgen_test_layout_kvm_regs() {
15741577
}
15751578
#[repr(C)]
15761579
#[derive(Debug, Default, Copy, Clone, PartialEq)]
1580+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
15771581
pub struct kvm_vcpu_init {
15781582
pub target: __u32,
15791583
pub features: [__u32; 7usize],
@@ -6511,6 +6515,7 @@ fn bindgen_test_layout_kvm_vapic_addr() {
65116515
}
65126516
#[repr(C)]
65136517
#[derive(Debug, Default, Copy, Clone, PartialEq)]
6518+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
65146519
pub struct kvm_mp_state {
65156520
pub mp_state: __u32,
65166521
}
@@ -8786,6 +8791,7 @@ fn bindgen_test_layout_kvm_reg_list() {
87868791
}
87878792
#[repr(C)]
87888793
#[derive(Debug, Default, Copy, Clone, PartialEq)]
8794+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
87898795
pub struct kvm_one_reg {
87908796
pub id: __u64,
87918797
pub addr: __u64,

src/x86_64/bindings.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ where
8282
}
8383
#[repr(C)]
8484
#[derive(Default)]
85+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
8586
pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>, [T; 0]);
8687
impl<T> __IncompleteArrayField<T> {
8788
#[inline]
@@ -1426,6 +1427,7 @@ impl ::std::fmt::Debug for kvm_ioapic_state {
14261427
}
14271428
#[repr(C)]
14281429
#[derive(Debug, Default, Copy, Clone, PartialEq)]
1430+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
14291431
pub struct kvm_regs {
14301432
pub rax: __u64,
14311433
pub rbx: __u64,
@@ -1643,7 +1645,9 @@ fn bindgen_test_layout_kvm_regs() {
16431645
}
16441646
#[repr(C)]
16451647
#[derive(Debug, Copy, Clone, PartialEq)]
1648+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
16461649
pub struct kvm_lapic_state {
1650+
#[cfg_attr(feature = "with-serde", serde(with = "serde_big_array::BigArray"))]
16471651
pub regs: [::std::os::raw::c_char; 1024usize],
16481652
}
16491653
#[test]
@@ -1682,6 +1686,7 @@ impl Default for kvm_lapic_state {
16821686
}
16831687
#[repr(C)]
16841688
#[derive(Debug, Default, Copy, Clone, PartialEq)]
1689+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
16851690
pub struct kvm_segment {
16861691
pub base: __u64,
16871692
pub limit: __u32,
@@ -1844,6 +1849,7 @@ fn bindgen_test_layout_kvm_segment() {
18441849
}
18451850
#[repr(C)]
18461851
#[derive(Debug, Default, Copy, Clone, PartialEq)]
1852+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
18471853
pub struct kvm_dtable {
18481854
pub base: __u64,
18491855
pub limit: __u16,
@@ -1896,6 +1902,7 @@ fn bindgen_test_layout_kvm_dtable() {
18961902
}
18971903
#[repr(C)]
18981904
#[derive(Debug, Default, Copy, Clone, PartialEq)]
1905+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
18991906
pub struct kvm_sregs {
19001907
pub cs: kvm_segment,
19011908
pub ds: kvm_segment,
@@ -2481,6 +2488,7 @@ fn bindgen_test_layout_kvm_fpu() {
24812488
}
24822489
#[repr(C)]
24832490
#[derive(Debug, Default, Copy, Clone, PartialEq)]
2491+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
24842492
pub struct kvm_msr_entry {
24852493
pub index: __u32,
24862494
pub reserved: __u32,
@@ -2533,6 +2541,7 @@ fn bindgen_test_layout_kvm_msr_entry() {
25332541
}
25342542
#[repr(C)]
25352543
#[derive(Debug, Default)]
2544+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
25362545
pub struct kvm_msrs {
25372546
pub nmsrs: __u32,
25382547
pub pad: __u32,
@@ -2885,6 +2894,7 @@ fn bindgen_test_layout_kvm_cpuid() {
28852894
}
28862895
#[repr(C)]
28872896
#[derive(Debug, Default, Copy, Clone, PartialEq)]
2897+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
28882898
pub struct kvm_cpuid_entry2 {
28892899
pub function: __u32,
28902900
pub index: __u32,
@@ -2992,6 +3002,7 @@ fn bindgen_test_layout_kvm_cpuid_entry2() {
29923002
}
29933003
#[repr(C)]
29943004
#[derive(Debug, Default)]
3005+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
29953006
pub struct kvm_cpuid2 {
29963007
pub nent: __u32,
29973008
pub padding: __u32,
@@ -3044,6 +3055,7 @@ fn bindgen_test_layout_kvm_cpuid2() {
30443055
}
30453056
#[repr(C)]
30463057
#[derive(Debug, Default, Copy, Clone, PartialEq)]
3058+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
30473059
pub struct kvm_pit_channel_state {
30483060
pub count: __u32,
30493061
pub latched_count: __u16,
@@ -3341,6 +3353,7 @@ fn bindgen_test_layout_kvm_pit_state() {
33413353
}
33423354
#[repr(C)]
33433355
#[derive(Debug, Default, Copy, Clone, PartialEq)]
3356+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
33443357
pub struct kvm_pit_state2 {
33453358
pub channels: [kvm_pit_channel_state; 3usize],
33463359
pub flags: __u32,
@@ -3434,6 +3447,7 @@ fn bindgen_test_layout_kvm_reinject_control() {
34343447
}
34353448
#[repr(C)]
34363449
#[derive(Debug, Default, Copy, Clone, PartialEq)]
3450+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
34373451
pub struct kvm_vcpu_events {
34383452
pub exception: kvm_vcpu_events__bindgen_ty_1,
34393453
pub interrupt: kvm_vcpu_events__bindgen_ty_2,
@@ -3448,6 +3462,7 @@ pub struct kvm_vcpu_events {
34483462
}
34493463
#[repr(C)]
34503464
#[derive(Debug, Default, Copy, Clone, PartialEq)]
3465+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
34513466
pub struct kvm_vcpu_events__bindgen_ty_1 {
34523467
pub injected: __u8,
34533468
pub nr: __u8,
@@ -3523,6 +3538,7 @@ fn bindgen_test_layout_kvm_vcpu_events__bindgen_ty_1() {
35233538
}
35243539
#[repr(C)]
35253540
#[derive(Debug, Default, Copy, Clone, PartialEq)]
3541+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
35263542
pub struct kvm_vcpu_events__bindgen_ty_2 {
35273543
pub injected: __u8,
35283544
pub nr: __u8,
@@ -3587,6 +3603,7 @@ fn bindgen_test_layout_kvm_vcpu_events__bindgen_ty_2() {
35873603
}
35883604
#[repr(C)]
35893605
#[derive(Debug, Default, Copy, Clone, PartialEq)]
3606+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
35903607
pub struct kvm_vcpu_events__bindgen_ty_3 {
35913608
pub injected: __u8,
35923609
pub pending: __u8,
@@ -3651,6 +3668,7 @@ fn bindgen_test_layout_kvm_vcpu_events__bindgen_ty_3() {
36513668
}
36523669
#[repr(C)]
36533670
#[derive(Debug, Default, Copy, Clone, PartialEq)]
3671+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
36543672
pub struct kvm_vcpu_events__bindgen_ty_4 {
36553673
pub smm: __u8,
36563674
pub pending: __u8,
@@ -3715,6 +3733,7 @@ fn bindgen_test_layout_kvm_vcpu_events__bindgen_ty_4() {
37153733
}
37163734
#[repr(C)]
37173735
#[derive(Debug, Default, Copy, Clone, PartialEq)]
3736+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
37183737
pub struct kvm_vcpu_events__bindgen_ty_5 {
37193738
pub pending: __u8,
37203739
}
@@ -3861,6 +3880,7 @@ fn bindgen_test_layout_kvm_vcpu_events() {
38613880
}
38623881
#[repr(C)]
38633882
#[derive(Debug, Default, Copy, Clone, PartialEq)]
3883+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
38643884
pub struct kvm_debugregs {
38653885
pub db: [__u64; 4usize],
38663886
pub dr6: __u64,
@@ -3939,6 +3959,30 @@ pub struct kvm_xsave {
39393959
pub region: [__u32; 1024usize],
39403960
pub extra: __IncompleteArrayField<__u32>,
39413961
}
3962+
3963+
#[cfg(feature = "with-serde")]
3964+
impl serde::Serialize for kvm_xsave {
3965+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
3966+
where
3967+
S: serde::Serializer,
3968+
{
3969+
self.region.as_ref().serialize(serializer)
3970+
}
3971+
}
3972+
3973+
#[cfg(feature = "with-serde")]
3974+
impl<'de> serde::Deserialize<'de> for kvm_xsave {
3975+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
3976+
where
3977+
D: serde::Deserializer<'de>,
3978+
{
3979+
let mut xsave = kvm_xsave::default();
3980+
let region = Vec::<__u32>::deserialize(deserializer)?;
3981+
xsave.region.copy_from_slice(&region[..]);
3982+
Ok(xsave)
3983+
}
3984+
}
3985+
39423986
#[test]
39433987
fn bindgen_test_layout_kvm_xsave() {
39443988
const UNINIT: ::std::mem::MaybeUninit<kvm_xsave> = ::std::mem::MaybeUninit::uninit();
@@ -3985,6 +4029,7 @@ impl Default for kvm_xsave {
39854029
}
39864030
#[repr(C)]
39874031
#[derive(Debug, Default, Copy, Clone, PartialEq)]
4032+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
39884033
pub struct kvm_xcr {
39894034
pub xcr: __u32,
39904035
pub reserved: __u32,
@@ -4037,6 +4082,7 @@ fn bindgen_test_layout_kvm_xcr() {
40374082
}
40384083
#[repr(C)]
40394084
#[derive(Debug, Default, Copy, Clone, PartialEq)]
4085+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
40404086
pub struct kvm_xcrs {
40414087
pub nr_xcrs: __u32,
40424088
pub flags: __u32,
@@ -5032,6 +5078,7 @@ impl ::std::fmt::Debug for kvm_irq_level {
50325078
}
50335079
#[repr(C)]
50345080
#[derive(Copy, Clone)]
5081+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
50355082
pub struct kvm_irqchip {
50365083
pub chip_id: __u32,
50375084
pub pad: __u32,
@@ -5044,6 +5091,29 @@ pub union kvm_irqchip__bindgen_ty_1 {
50445091
pub pic: kvm_pic_state,
50455092
pub ioapic: kvm_ioapic_state,
50465093
}
5094+
5095+
#[cfg(feature = "with-serde")]
5096+
impl serde::Serialize for kvm_irqchip__bindgen_ty_1 {
5097+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
5098+
where
5099+
S: serde::Serializer,
5100+
{
5101+
// SAFETY: Safe because this is a POD struct and we're only using the read result to
5102+
// serialize the underlying bytes.
5103+
unsafe { self.dummy.serialize(serializer) }
5104+
}
5105+
}
5106+
#[cfg(feature = "with-serde")]
5107+
impl<'de> serde::Deserialize<'de> for kvm_irqchip__bindgen_ty_1 {
5108+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
5109+
where
5110+
D: serde::Deserializer<'de>,
5111+
{
5112+
let dummy = serde_big_array::BigArray::deserialize(deserializer)?;
5113+
assert_eq!(std::mem::size_of_val(&dummy), std::mem::size_of::<Self>());
5114+
Ok(Self { dummy })
5115+
}
5116+
}
50475117
#[test]
50485118
fn bindgen_test_layout_kvm_irqchip__bindgen_ty_1() {
50495119
const UNINIT: ::std::mem::MaybeUninit<kvm_irqchip__bindgen_ty_1> =
@@ -9167,6 +9237,7 @@ fn bindgen_test_layout_kvm_vapic_addr() {
91679237
}
91689238
#[repr(C)]
91699239
#[derive(Debug, Default, Copy, Clone, PartialEq)]
9240+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
91709241
pub struct kvm_mp_state {
91719242
pub mp_state: __u32,
91729243
}
@@ -11404,6 +11475,7 @@ fn bindgen_test_layout_kvm_irqfd() {
1140411475
}
1140511476
#[repr(C)]
1140611477
#[derive(Debug, Default, Copy, Clone, PartialEq)]
11478+
#[cfg_attr(feature = "with-serde", derive(serde::Deserialize, serde::Serialize))]
1140711479
pub struct kvm_clock_data {
1140811480
pub clock: __u64,
1140911481
pub flags: __u32,

0 commit comments

Comments
 (0)