From 91e26015237b9e5754996b34bbe13f77e5408aa8 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Tue, 7 Feb 2023 17:22:58 +0000 Subject: [PATCH] vfio_user: Improve flags for DMA_MAP command Replace the use of an enum with a bitfield representation which means that is now possible to logical OR flags together. Signed-off-by: Rob Bradford --- Cargo.lock | 1 + vfio_user/Cargo.toml | 1 + vfio_user/src/lib.rs | 23 ++++++++--------------- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c365eba26..9d64ab6b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1384,6 +1384,7 @@ dependencies = [ name = "vfio_user" version = "0.1.0" dependencies = [ + "bitflags", "libc", "log", "serde", diff --git a/vfio_user/Cargo.toml b/vfio_user/Cargo.toml index 3d0c5b5c3..b3818469d 100644 --- a/vfio_user/Cargo.toml +++ b/vfio_user/Cargo.toml @@ -5,6 +5,7 @@ authors = ["The Cloud Hypervisor Authors"] edition = "2021" [dependencies] +bitflags = "1.3.2" libc = "0.2.139" log = "0.4.17" serde = { version = "1.0.151", features = ["rc"] } diff --git a/vfio_user/src/lib.rs b/vfio_user/src/lib.rs index 4c064cdaa..f3b9a78ad 100644 --- a/vfio_user/src/lib.rs +++ b/vfio_user/src/lib.rs @@ -3,6 +3,7 @@ // SPDX-License-Identifier: Apache-2.0 // +use bitflags::bitflags; use std::ffi::CString; use std::fs::File; use std::io::{IoSlice, Read, Write}; @@ -90,19 +91,11 @@ const fn default_migration_capabilities() -> MigrationCapabilities { MigrationCapabilities { pgsize: 4096 } } -#[repr(u32)] -#[derive(Clone, Copy, Debug)] -#[allow(dead_code)] -enum DmaMapFlags { - Unknown = 0, - ReadOnly = 1, - WriteOnly = 2, - ReadWrite = 3, -} - -impl Default for DmaMapFlags { - fn default() -> Self { - Self::Unknown +bitflags! { + struct DmaMapFlags: u32 { + const READ_ONLY = 1 << 0; + const WRITE_ONLY = 1 << 1; + const READ_WRITE = Self::READ_ONLY.bits | Self::WRITE_ONLY.bits; } } @@ -111,7 +104,7 @@ impl Default for DmaMapFlags { struct DmaMap { header: Header, argsz: u32, - flags: DmaMapFlags, + flags: u32, offset: u64, address: u64, size: u64, @@ -367,7 +360,7 @@ impl Client { ..Default::default() }, argsz: (size_of::() - size_of::
()) as u32, - flags: DmaMapFlags::ReadWrite, + flags: DmaMapFlags::READ_WRITE.bits, offset, address, size,