summary refs log tree commit diff
path: root/vm_control/src/lib.rs
diff options
context:
space:
mode:
authorAlyssa Ross <hi@alyssa.is>2020-03-08 22:41:30 +0000
committerAlyssa Ross <hi@alyssa.is>2020-06-15 09:36:14 +0000
commit2507cc57bc0145eb57305e60f6a7c21f3b4c9192 (patch)
treea6b700b920b6b2733ae8049302c613d3eb93a330 /vm_control/src/lib.rs
parent3d1bc2e0bb5bae7f32a9fa18b5348295facd5ab6 (diff)
downloadcrosvm-2507cc57bc0145eb57305e60f6a7c21f3b4c9192.tar
crosvm-2507cc57bc0145eb57305e60f6a7c21f3b4c9192.tar.gz
crosvm-2507cc57bc0145eb57305e60f6a7c21f3b4c9192.tar.bz2
crosvm-2507cc57bc0145eb57305e60f6a7c21f3b4c9192.tar.lz
crosvm-2507cc57bc0145eb57305e60f6a7c21f3b4c9192.tar.xz
crosvm-2507cc57bc0145eb57305e60f6a7c21f3b4c9192.tar.zst
crosvm-2507cc57bc0145eb57305e60f6a7c21f3b4c9192.zip
vm_control: make MaybeOwnedFd generic
This will allow more easily sending types other than File over
sockets, (e.g., UnixSeqpacket).
Diffstat (limited to 'vm_control/src/lib.rs')
-rw-r--r--vm_control/src/lib.rs37
1 files changed, 25 insertions, 12 deletions
diff --git a/vm_control/src/lib.rs b/vm_control/src/lib.rs
index d4922ab..3d92c8b 100644
--- a/vm_control/src/lib.rs
+++ b/vm_control/src/lib.rs
@@ -26,14 +26,27 @@ use sys_util::{error, Error as SysError, EventFd, GuestAddress, MemoryMapping, M
 
 /// A data structure that either owns or borrows a file descriptor.
 #[derive(Debug)]
-pub enum MaybeOwnedFd {
+pub enum MaybeOwnedFd<Owned> {
     /// Owned by this enum variant, and will be destructed automatically if not moved out.
-    Owned(File),
+    Owned(Owned),
     /// A file descriptor borrwed by this enum.
     Borrowed(RawFd),
 }
 
-impl AsRawFd for MaybeOwnedFd {
+impl<Owned> MaybeOwnedFd<Owned> {
+    pub fn new_borrowed(fd: &dyn AsRawFd) -> Self {
+        Self::Borrowed(fd.as_raw_fd())
+    }
+
+    pub fn owned(self) -> Owned {
+        match self {
+            Self::Owned(owned) => owned,
+            Self::Borrowed(_) => panic!("owned() called on a borrowed MaybeOwnedFd"),
+        }
+    }
+}
+
+impl<Owned: AsRawFd> AsRawFd for MaybeOwnedFd<Owned> {
     fn as_raw_fd(&self) -> RawFd {
         match self {
             MaybeOwnedFd::Owned(f) => f.as_raw_fd(),
@@ -43,7 +56,7 @@ impl AsRawFd for MaybeOwnedFd {
 }
 
 // When sent, it could be owned or borrowed. On the receiver end, it always owned.
-impl MsgOnSocket for MaybeOwnedFd {
+impl<Owned: AsRawFd + FromRawFd + MsgOnSocket> MsgOnSocket for MaybeOwnedFd<Owned> {
     fn uses_fd() -> bool {
         true
     }
@@ -54,7 +67,7 @@ impl MsgOnSocket for MaybeOwnedFd {
         1usize
     }
     unsafe fn read_from_buffer(buffer: &[u8], fds: &[RawFd]) -> MsgResult<(Self, usize)> {
-        let (file, size) = File::read_from_buffer(buffer, fds)?;
+        let (file, size) = Owned::read_from_buffer(buffer, fds)?;
         Ok((MaybeOwnedFd::Owned(file), size))
     }
     fn write_to_buffer(&self, _buffer: &mut [u8], fds: &mut [RawFd]) -> MsgResult<usize> {
@@ -202,7 +215,7 @@ pub enum UsbControlCommand {
         addr: u8,
         vid: u16,
         pid: u16,
-        fd: Option<MaybeOwnedFd>,
+        fd: Option<MaybeOwnedFd<File>>,
     },
     DetachDevice {
         port: u8,
@@ -260,10 +273,10 @@ impl Display for UsbControlResult {
 pub enum VmMemoryRequest {
     /// Register shared memory represented by the given fd into guest address space. The response
     /// variant is `VmResponse::RegisterMemory`.
-    RegisterMemory(MaybeOwnedFd, usize),
+    RegisterMemory(MaybeOwnedFd<File>, usize),
     /// Similiar to `VmMemoryRequest::RegisterMemory`, but doesn't allocate new address space.
     /// Useful for cases where the address space is already allocated (PCI regions).
-    RegisterFdAtPciBarOffset(Alloc, MaybeOwnedFd, usize, u64),
+    RegisterFdAtPciBarOffset(Alloc, MaybeOwnedFd<File>, usize, u64),
     /// Unregister the given memory slot that was previously registereed with `RegisterMemory`.
     UnregisterMemory(u32),
     /// Allocate GPU buffer of a given size/format and register the memory into guest address space.
@@ -275,7 +288,7 @@ pub enum VmMemoryRequest {
     },
     /// Register mmaped memory into kvm's EPT.
     RegisterMmapMemory {
-        fd: MaybeOwnedFd,
+        fd: MaybeOwnedFd<File>,
         size: usize,
         offset: u64,
         gpa: u64,
@@ -369,7 +382,7 @@ pub enum VmMemoryResponse {
     /// The request to allocate and register GPU memory into guest address space was successfully
     /// done at page frame number `pfn` and memory slot number `slot` for buffer with `desc`.
     AllocateAndRegisterGpuMemory {
-        fd: MaybeOwnedFd,
+        fd: MaybeOwnedFd<File>,
         pfn: u64,
         slot: u32,
         desc: GpuMemoryDesc,
@@ -381,7 +394,7 @@ pub enum VmMemoryResponse {
 #[derive(MsgOnSocket, Debug)]
 pub enum VmIrqRequest {
     /// Allocate one gsi, and associate gsi to irqfd with register_irqfd()
-    AllocateOneMsi { irqfd: MaybeOwnedFd },
+    AllocateOneMsi { irqfd: MaybeOwnedFd<File> },
     /// Add one msi route entry into kvm
     AddMsiRoute {
         gsi: u32,
@@ -693,7 +706,7 @@ pub enum VmResponse {
     /// The request to allocate and register GPU memory into guest address space was successfully
     /// done at page frame number `pfn` and memory slot number `slot` for buffer with `desc`.
     AllocateAndRegisterGpuMemory {
-        fd: MaybeOwnedFd,
+        fd: MaybeOwnedFd<File>,
         pfn: u64,
         slot: u32,
         desc: GpuMemoryDesc,