1 // Copyright 2017 The ChromiumOS Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 use std::fs::File; 6 use std::fs::OpenOptions; 7 use std::marker::PhantomData; 8 use std::os::unix::fs::OpenOptionsExt; 9 use std::path::Path; 10 11 use base::ioctl_with_ref; 12 use base::AsRawDescriptor; 13 use base::RawDescriptor; 14 use net_util::TapT; 15 16 use super::ioctl_result; 17 use super::Error; 18 use super::Result; 19 use super::Vhost; 20 21 /// Handle to run VHOST_NET ioctls. 22 /// 23 /// This provides a simple wrapper around a VHOST_NET file descriptor and 24 /// methods that safely run ioctls on that file descriptor. 25 pub struct Net<T> { 26 // descriptor must be dropped first, which will stop and tear down the 27 // vhost-net worker before GuestMemory can potentially be unmapped. 28 descriptor: File, 29 phantom: PhantomData<T>, 30 } 31 32 pub trait NetT<T: TapT>: Vhost + AsRawDescriptor + Send + Sized { 33 /// Create a new NetT instance new(vhost_net_device_path: &Path) -> Result<Self>34 fn new(vhost_net_device_path: &Path) -> Result<Self>; 35 36 /// Set the tap file descriptor that will serve as the VHOST_NET backend. 37 /// This will start the vhost worker for the given queue. 38 /// 39 /// # Arguments 40 /// * `queue_index` - Index of the queue to modify. 41 /// * `descriptor` - Tap interface that will be used as the backend. set_backend(&self, queue_index: usize, descriptor: Option<&T>) -> Result<()>42 fn set_backend(&self, queue_index: usize, descriptor: Option<&T>) -> Result<()>; 43 } 44 45 impl<T> NetT<T> for Net<T> 46 where 47 T: TapT, 48 { 49 /// Opens /dev/vhost-net and holds a file descriptor open for it. 50 /// 51 /// # Arguments 52 /// * `mem` - Guest memory mapping. new(vhost_net_device_path: &Path) -> Result<Net<T>>53 fn new(vhost_net_device_path: &Path) -> Result<Net<T>> { 54 Ok(Net::<T> { 55 descriptor: OpenOptions::new() 56 .read(true) 57 .write(true) 58 .custom_flags(libc::O_CLOEXEC | libc::O_NONBLOCK) 59 .open(vhost_net_device_path) 60 .map_err(Error::VhostOpen)?, 61 phantom: PhantomData, 62 }) 63 } 64 set_backend(&self, queue_index: usize, event: Option<&T>) -> Result<()>65 fn set_backend(&self, queue_index: usize, event: Option<&T>) -> Result<()> { 66 let vring_file = virtio_sys::vhost::vhost_vring_file { 67 index: queue_index as u32, 68 fd: event.map_or(-1, |event| event.as_raw_descriptor()), 69 }; 70 71 // This ioctl is called on a valid vhost_net descriptor and has its 72 // return value checked. 73 let ret = unsafe { 74 ioctl_with_ref( 75 &self.descriptor, 76 virtio_sys::VHOST_NET_SET_BACKEND(), 77 &vring_file, 78 ) 79 }; 80 if ret < 0 { 81 return ioctl_result(); 82 } 83 Ok(()) 84 } 85 } 86 87 impl<T> Vhost for Net<T> {} 88 89 impl<T> AsRawDescriptor for Net<T> { as_raw_descriptor(&self) -> RawDescriptor90 fn as_raw_descriptor(&self) -> RawDescriptor { 91 self.descriptor.as_raw_descriptor() 92 } 93 } 94 95 pub mod fakes { 96 use std::fs::remove_file; 97 use std::fs::OpenOptions; 98 99 use super::*; 100 101 const TMP_FILE: &str = "/tmp/crosvm_vhost_test_file"; 102 103 pub struct FakeNet<T> { 104 descriptor: File, 105 phantom: PhantomData<T>, 106 } 107 108 impl<T> Drop for FakeNet<T> { drop(&mut self)109 fn drop(&mut self) { 110 let _ = remove_file(TMP_FILE); 111 } 112 } 113 114 impl<T> NetT<T> for FakeNet<T> 115 where 116 T: TapT, 117 { new(_vhost_net_device_path: &Path) -> Result<FakeNet<T>>118 fn new(_vhost_net_device_path: &Path) -> Result<FakeNet<T>> { 119 Ok(FakeNet::<T> { 120 descriptor: OpenOptions::new() 121 .read(true) 122 .append(true) 123 .create(true) 124 .open(TMP_FILE) 125 .unwrap(), 126 phantom: PhantomData, 127 }) 128 } 129 set_backend(&self, _queue_index: usize, _fd: Option<&T>) -> Result<()>130 fn set_backend(&self, _queue_index: usize, _fd: Option<&T>) -> Result<()> { 131 Ok(()) 132 } 133 } 134 135 impl<T> Vhost for FakeNet<T> {} 136 137 impl<T> AsRawDescriptor for FakeNet<T> { as_raw_descriptor(&self) -> RawDescriptor138 fn as_raw_descriptor(&self) -> RawDescriptor { 139 self.descriptor.as_raw_descriptor() 140 } 141 } 142 } 143