mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
virtio-devices, block_util: Automatically serialized packed structs
With current serde_derive it is possible to #[derive(Serialize)] on packed structures if they implement Copy. This allows the removal of the manual equivalent code. Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
@@ -11,7 +11,6 @@ use crate::seccomp_filters::{get_seccomp_filter, Thread};
|
||||
use crate::VirtioInterrupt;
|
||||
use libc::EFD_NONBLOCK;
|
||||
use seccomp::{SeccompAction, SeccompFilter};
|
||||
use serde::ser::{Serialize, SerializeStruct, Serializer};
|
||||
use std::cmp;
|
||||
use std::collections::VecDeque;
|
||||
use std::io;
|
||||
@@ -41,7 +40,7 @@ const CONFIG_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 4;
|
||||
//Console size feature bit
|
||||
const VIRTIO_CONSOLE_F_SIZE: u64 = 0;
|
||||
|
||||
#[derive(Copy, Clone, Debug, Default, Deserialize)]
|
||||
#[derive(Copy, Clone, Debug, Default, Deserialize, Serialize)]
|
||||
#[repr(C, packed)]
|
||||
pub struct VirtioConsoleConfig {
|
||||
cols: u16,
|
||||
@@ -50,30 +49,6 @@ pub struct VirtioConsoleConfig {
|
||||
emerg_wr: u32,
|
||||
}
|
||||
|
||||
// We must explicitly implement Serialize since the structure is packed and
|
||||
// it's unsafe to borrow from a packed structure. And by default, if we derive
|
||||
// Serialize from serde, it will borrow the values from the structure.
|
||||
// That's why this implementation copies each field separately before it
|
||||
// serializes the entire structure field by field.
|
||||
impl Serialize for VirtioConsoleConfig {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let cols = self.cols;
|
||||
let rows = self.rows;
|
||||
let max_nr_ports = self.max_nr_ports;
|
||||
let emerg_wr = self.emerg_wr;
|
||||
|
||||
let mut virtio_console_config = serializer.serialize_struct("VirtioConsoleConfig", 12)?;
|
||||
virtio_console_config.serialize_field("cols", &cols)?;
|
||||
virtio_console_config.serialize_field("rows", &rows)?;
|
||||
virtio_console_config.serialize_field("max_nr_ports", &max_nr_ports)?;
|
||||
virtio_console_config.serialize_field("emerg_wr", &emerg_wr)?;
|
||||
virtio_console_config.end()
|
||||
}
|
||||
}
|
||||
|
||||
// Safe because it only has data and has no implicit padding.
|
||||
unsafe impl ByteValued for VirtioConsoleConfig {}
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ use super::{
|
||||
EPOLL_HELPER_EVENT_LAST,
|
||||
};
|
||||
use net_util::MacAddr;
|
||||
use serde::ser::{Serialize, SerializeStruct, Serializer};
|
||||
use std::os::unix::io::{AsRawFd, RawFd};
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::{Arc, Barrier};
|
||||
@@ -25,7 +24,7 @@ const QUEUE_SIZE: usize = 256;
|
||||
const CTRL_QUEUE_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 1;
|
||||
|
||||
#[repr(C, packed)]
|
||||
#[derive(Copy, Clone, Debug, Default, Deserialize)]
|
||||
#[derive(Copy, Clone, Debug, Default, Deserialize, Serialize)]
|
||||
pub struct VirtioNetConfig {
|
||||
pub mac: [u8; 6],
|
||||
pub status: u16,
|
||||
@@ -35,34 +34,6 @@ pub struct VirtioNetConfig {
|
||||
pub duplex: u8,
|
||||
}
|
||||
|
||||
// We must explicitly implement Serialize since the structure is packed and
|
||||
// it's unsafe to borrow from a packed structure. And by default, if we derive
|
||||
// Serialize from serde, it will borrow the values from the structure.
|
||||
// That's why this implementation copies each field separately before it
|
||||
// serializes the entire structure field by field.
|
||||
impl Serialize for VirtioNetConfig {
|
||||
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mac = self.mac;
|
||||
let status = self.status;
|
||||
let max_virtqueue_pairs = self.max_virtqueue_pairs;
|
||||
let mtu = self.mtu;
|
||||
let speed = self.speed;
|
||||
let duplex = self.duplex;
|
||||
|
||||
let mut virtio_net_config = serializer.serialize_struct("VirtioNetConfig", 17)?;
|
||||
virtio_net_config.serialize_field("mac", &mac)?;
|
||||
virtio_net_config.serialize_field("status", &status)?;
|
||||
virtio_net_config.serialize_field("max_virtqueue_pairs", &max_virtqueue_pairs)?;
|
||||
virtio_net_config.serialize_field("mtu", &mtu)?;
|
||||
virtio_net_config.serialize_field("speed", &speed)?;
|
||||
virtio_net_config.serialize_field("duplex", &duplex)?;
|
||||
virtio_net_config.end()
|
||||
}
|
||||
}
|
||||
|
||||
// Safe because it only has data and has no implicit padding.
|
||||
unsafe impl ByteValued for VirtioNetConfig {}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user