From 4fe7347fb9088b3ef86f26f464e7c1439981c3e1 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Thu, 11 Jun 2020 18:50:25 +0200 Subject: [PATCH] vmm: Manually implement Serialize for PciDeviceInfo In order to provide a more comprehensive b/d/f to the user, the serialization of PciDeviceInfo is implemented manually to control the formatting. Signed-off-by: Sebastien Boeuf --- vmm/src/lib.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/vmm/src/lib.rs b/vmm/src/lib.rs index 7d6c95a20..689b3f836 100644 --- a/vmm/src/lib.rs +++ b/vmm/src/lib.rs @@ -29,6 +29,7 @@ use crate::seccomp_filters::{get_seccomp_filter, Thread}; use crate::vm::{Error as VmError, Vm, VmState}; use libc::EFD_NONBLOCK; use seccomp::{SeccompFilter, SeccompLevel}; +use serde::ser::{Serialize, SerializeStruct, Serializer}; use std::fs::File; use std::io; use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; @@ -177,12 +178,34 @@ impl AsRawFd for EpollContext { } } -#[derive(Deserialize, Serialize)] pub struct PciDeviceInfo { pub id: String, pub bdf: u32, } +impl Serialize for PciDeviceInfo { + fn serialize(&self, serializer: S) -> std::result::Result + where + S: Serializer, + { + // Transform the PCI b/d/f into a standardized string. + let segment = (self.bdf >> 16) & 0xffff; + let bus = (self.bdf >> 8) & 0xff; + let device = (self.bdf >> 3) & 0x1f; + let function = self.bdf & 0x7; + let bdf_str = format!( + "{:04x}:{:02x}:{:02x}.{:01x}", + segment, bus, device, function + ); + + // Serialize the structure. + let mut state = serializer.serialize_struct("PciDeviceInfo", 2)?; + state.serialize_field("id", &self.id)?; + state.serialize_field("bdf", &bdf_str)?; + state.end() + } +} + pub fn start_vmm_thread( vmm_version: String, http_path: &str,