From bb54c5e169927ee2fffbba8b5aec79c9700c15ae Mon Sep 17 00:00:00 2001 From: Daniel Verkamp Date: Tue, 13 Dec 2022 13:45:02 -0800 Subject: [PATCH] virtio_sys: add virtio_fs bindings Replace the hand-written bindings in the virtio-fs device with automatically generated bindings from bindgen. BUG=None TEST=./virtio_sys/bindgen.sh Change-Id: Ia11a2d97f3546515196a41764107e7aaece8ad82 Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4103907 Reviewed-by: Alexandre Courbot Commit-Queue: Daniel Verkamp --- devices/src/virtio/fs/mod.rs | 20 ++------------ devices/src/virtio/vhost/user/device/fs.rs | 2 +- devices/src/virtio/vhost/user/vmm/fs.rs | 2 +- virtio_sys/bindgen.sh | 18 ++++++++++++ virtio_sys/src/lib.rs | 1 + virtio_sys/src/virtio_fs.rs | 32 ++++++++++++++++++++++ 6 files changed, 56 insertions(+), 19 deletions(-) create mode 100644 virtio_sys/src/virtio_fs.rs diff --git a/devices/src/virtio/fs/mod.rs b/devices/src/virtio/fs/mod.rs index 3497579e94..f634f4fe5d 100644 --- a/devices/src/virtio/fs/mod.rs +++ b/devices/src/virtio/fs/mod.rs @@ -22,6 +22,8 @@ use remain::sorted; use resources::Alloc; use sync::Mutex; use thiserror::Error; +use virtio_sys::virtio_fs::virtio_fs_config; +use virtio_sys::virtio_fs::VIRTIO_FS_SHMCAP_ID_CACHE; use vm_control::FsMappingRequest; use vm_control::VmResponse; use vm_memory::GuestMemory; @@ -59,25 +61,9 @@ const FS_BAR_NUM: u8 = 4; const FS_BAR_OFFSET: u64 = 0; const FS_BAR_SIZE: u64 = 1 << 33; -/// Defined in kernel/include/uapi/linux/virtio_fs.h. -const VIRTIO_FS_SHMCAP_ID_CACHE: u8 = 0; - /// The maximum allowable length of the tag used to identify a specific virtio-fs device. pub const FS_MAX_TAG_LEN: usize = 36; -/// kernel/include/uapi/linux/virtio_fs.h -#[repr(C, packed)] -#[derive(Clone, Copy)] -pub struct virtio_fs_config { - /// Filesystem name (UTF-8, not NUL-terminated, padded with NULs) - pub tag: [u8; FS_MAX_TAG_LEN], - /// Number of request queues - pub num_request_queues: Le32, -} - -// Safe because all members are plain old data and any value is valid. -unsafe impl DataInit for virtio_fs_config {} - /// Errors that may occur during the creation or operation of an Fs device. #[sorted] #[derive(Error, Debug)] @@ -353,7 +339,7 @@ impl VirtioDevice for Fs { FS_BAR_NUM, FS_BAR_OFFSET, FS_BAR_SIZE, - VIRTIO_FS_SHMCAP_ID_CACHE, + VIRTIO_FS_SHMCAP_ID_CACHE as u8, ))] } } diff --git a/devices/src/virtio/vhost/user/device/fs.rs b/devices/src/virtio/vhost/user/device/fs.rs index ac268332d1..30409f9870 100644 --- a/devices/src/virtio/vhost/user/device/fs.rs +++ b/devices/src/virtio/vhost/user/device/fs.rs @@ -27,6 +27,7 @@ use futures::future::Abortable; use hypervisor::ProtectionType; use sync::Mutex; pub use sys::start_device as run_fs_device; +use virtio_sys::virtio_fs::virtio_fs_config; use vm_memory::GuestMemory; use vmm_vhost::message::VhostUserProtocolFeatures; use vmm_vhost::message::VhostUserVirtioFeatures; @@ -35,7 +36,6 @@ use crate::virtio; use crate::virtio::copy_config; use crate::virtio::fs::passthrough::PassthroughFs; use crate::virtio::fs::process_fs_queue; -use crate::virtio::fs::virtio_fs_config; use crate::virtio::fs::FS_MAX_TAG_LEN; use crate::virtio::vhost::user::device::handler::sys::Doorbell; use crate::virtio::vhost::user::device::handler::VhostUserBackend; diff --git a/devices/src/virtio/vhost/user/vmm/fs.rs b/devices/src/virtio/vhost/user/vmm/fs.rs index dcb72630b9..2d59a9f80d 100644 --- a/devices/src/virtio/vhost/user/vmm/fs.rs +++ b/devices/src/virtio/vhost/user/vmm/fs.rs @@ -4,9 +4,9 @@ use data_model::DataInit; use data_model::Le32; +use virtio_sys::virtio_fs::virtio_fs_config; use vmm_vhost::message::VhostUserProtocolFeatures; -use crate::virtio::fs::virtio_fs_config; use crate::virtio::fs::FS_MAX_TAG_LEN; use crate::virtio::fs::QUEUE_SIZE; use crate::virtio::vhost::user::vmm::Connection; diff --git a/virtio_sys/bindgen.sh b/virtio_sys/bindgen.sh index 736debd903..d956885186 100755 --- a/virtio_sys/bindgen.sh +++ b/virtio_sys/bindgen.sh @@ -35,6 +35,24 @@ bindgen_generate \ | replace_linux_int_types \ > virtio_sys/src/virtio_config.rs +VIRTIO_FS_EXTRA="// Added by virtio_sys/bindgen.sh +use data_model::DataInit; +use data_model::Le32; + +// Safe because all members are plain old data and any value is valid. +unsafe impl DataInit for virtio_fs_config {}" + +bindgen_generate \ + --raw-line "${VIRTIO_FS_EXTRA}" \ + --allowlist-var='VIRTIO_FS_.*' \ + --allowlist-type='virtio_fs_.*' \ + "${BINDGEN_LINUX_X86_HEADERS}/include/linux/virtio_fs.h" \ + -- \ + -isystem "${BINDGEN_LINUX_X86_HEADERS}/include" \ + | replace_linux_int_types \ + | replace_linux_endian_types \ + > virtio_sys/src/virtio_fs.rs + VIRTIO_IDS_EXTRAS=" //! This file defines virtio device IDs. IDs with large values (counting down //! from 63) are nonstandard and not defined by the virtio specification. diff --git a/virtio_sys/src/lib.rs b/virtio_sys/src/lib.rs index 5d349c689b..346fd00861 100644 --- a/virtio_sys/src/lib.rs +++ b/virtio_sys/src/lib.rs @@ -15,6 +15,7 @@ use base::ioctl_iowr_nr; pub mod vhost; pub mod virtio_config; +pub mod virtio_fs; pub mod virtio_ids; pub mod virtio_mmio; pub mod virtio_net; diff --git a/virtio_sys/src/virtio_fs.rs b/virtio_sys/src/virtio_fs.rs new file mode 100644 index 0000000000..221a93abfb --- /dev/null +++ b/virtio_sys/src/virtio_fs.rs @@ -0,0 +1,32 @@ +/* automatically generated by tools/bindgen-all-the-things */ + +#![allow(clippy::missing_safety_doc)] +#![allow(clippy::upper_case_acronyms)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] +#![allow(dead_code)] + +// Added by virtio_sys/bindgen.sh +use data_model::DataInit; +use data_model::Le32; + +// Safe because all members are plain old data and any value is valid. +unsafe impl DataInit for virtio_fs_config {} + +pub const VIRTIO_FS_SHMCAP_ID_CACHE: u32 = 0; +#[repr(C, packed)] +#[derive(Debug, Copy, Clone)] +pub struct virtio_fs_config { + pub tag: [u8; 36usize], + pub num_request_queues: Le32, +} +impl Default for virtio_fs_config { + fn default() -> Self { + let mut s = ::std::mem::MaybeUninit::::uninit(); + unsafe { + ::std::ptr::write_bytes(s.as_mut_ptr(), 0, 1); + s.assume_init() + } + } +}