|
1 |
| -mod enums; |
2 |
| -mod ioctls; |
3 |
| -mod structs; |
4 |
| -mod vmm_data; |
| 1 | +use std::fs::{File, OpenOptions}; |
| 2 | +use std::io::{Error, ErrorKind, Result}; |
| 3 | +use std::os::fd::*; |
| 4 | +use std::os::unix::fs::OpenOptionsExt; |
| 5 | +use std::path::PathBuf; |
5 | 6 |
|
6 |
| -pub use enums::*; |
7 |
| -pub use ioctls::*; |
8 |
| -pub use structs::*; |
9 |
| -pub use vmm_data::*; |
10 |
| - |
11 |
| -pub const VM_MAXCPU: usize = 32; |
| 7 | +pub use bhyve_api_sys::*; |
12 | 8 |
|
13 | 9 | pub const VMM_PATH_PREFIX: &str = "/dev/vmm";
|
14 | 10 | pub const VMM_CTL_PATH: &str = "/dev/vmmctl";
|
15 | 11 |
|
16 |
| -/// This is the VMM interface version against which bhyve_api expects to operate |
17 |
| -/// against. All constants and structs defined by the crate are done so in |
18 |
| -/// terms of that specific version. |
19 |
| -pub const VMM_CURRENT_INTERFACE_VERSION: u32 = 8; |
| 12 | +pub struct VmmCtlFd(File); |
| 13 | +impl VmmCtlFd { |
| 14 | + pub fn open() -> Result<Self> { |
| 15 | + let ctl = OpenOptions::new() |
| 16 | + .write(true) |
| 17 | + .custom_flags(libc::O_EXCL) |
| 18 | + .open(VMM_CTL_PATH)?; |
| 19 | + Ok(Self(ctl)) |
| 20 | + } |
| 21 | + |
| 22 | + /// Issue ioctl against open vmmctl handle |
| 23 | + /// |
| 24 | + /// # Safety |
| 25 | + /// |
| 26 | + /// Caller is charged with providing `data` argument which is adequate for |
| 27 | + /// any copyin/copyout actions which may occur as part of the ioctl |
| 28 | + /// processing. |
| 29 | + pub unsafe fn ioctl<T>(&self, cmd: i32, data: *mut T) -> Result<i32> { |
| 30 | + ioctl(self.as_raw_fd(), cmd, data as *mut libc::c_void) |
| 31 | + } |
| 32 | + pub fn ioctl_usize(&self, cmd: i32, data: usize) -> Result<i32> { |
| 33 | + if !Self::ioctl_usize_safe(cmd) { |
| 34 | + return Err(Error::new( |
| 35 | + ErrorKind::InvalidInput, |
| 36 | + "unsafe cmd provided", |
| 37 | + )); |
| 38 | + } |
| 39 | + // Safety: Since we are explicitly filtering for vmm ioctls which will |
| 40 | + // not assume the data argument is a pointer for copyin/copyout, we can |
| 41 | + // dismiss those dangers. The caller is assumed to be cognizant of |
| 42 | + // other potential side effects. |
| 43 | + unsafe { ioctl(self.as_raw_fd(), cmd, data as *mut libc::c_void) } |
| 44 | + } |
| 45 | + |
| 46 | + /// Check VMM ioctl command against those known to not require any |
| 47 | + /// copyin/copyout to function. |
| 48 | + const fn ioctl_usize_safe(cmd: i32) -> bool { |
| 49 | + matches!(cmd, ioctls::VMM_INTERFACE_VERSION,) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +impl AsRawFd for VmmCtlFd { |
| 54 | + fn as_raw_fd(&self) -> RawFd { |
| 55 | + self.0.as_raw_fd() |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +pub struct VmmFd(File); |
| 60 | +impl VmmFd { |
| 61 | + pub fn open(name: &str) -> Result<Self> { |
| 62 | + let mut vmpath = PathBuf::from(VMM_PATH_PREFIX); |
| 63 | + vmpath.push(name); |
20 | 64 |
|
21 |
| -use std::io::Result; |
| 65 | + let fp = OpenOptions::new().write(true).read(true).open(vmpath)?; |
| 66 | + Ok(Self(fp)) |
| 67 | + } |
| 68 | + |
| 69 | + /// Create new instance from raw `File` resource |
| 70 | + /// |
| 71 | + /// # Safety |
| 72 | + /// |
| 73 | + /// Caller is expected to provide `File` resource which which is a valid vmm |
| 74 | + /// resource. (Or alternatively, is not to make any vmm-related ioctls, if |
| 75 | + /// this instance was created for unit-testing purposes.) |
| 76 | + pub unsafe fn new_raw(fp: File) -> Self { |
| 77 | + Self(fp) |
| 78 | + } |
| 79 | + |
| 80 | + /// Issue ioctl against open vmm instance |
| 81 | + /// |
| 82 | + /// # Safety |
| 83 | + /// |
| 84 | + /// Caller is charged with providing `data` argument which is adequate for |
| 85 | + /// any copyin/copyout actions which may occur as part of the ioctl |
| 86 | + /// processing. |
| 87 | + pub unsafe fn ioctl<T>(&self, cmd: i32, data: *mut T) -> Result<i32> { |
| 88 | + ioctl(self.as_raw_fd(), cmd, data as *mut libc::c_void) |
| 89 | + } |
| 90 | + pub fn ioctl_usize(&self, cmd: i32, data: usize) -> Result<i32> { |
| 91 | + if !Self::ioctl_usize_safe(cmd) { |
| 92 | + return Err(Error::new( |
| 93 | + ErrorKind::InvalidInput, |
| 94 | + "unsafe cmd provided", |
| 95 | + )); |
| 96 | + } |
| 97 | + // Safety: Since we are explicitly filtering for vmm ioctls which will |
| 98 | + // not assume the data argument is a pointer for copyin/copyout, we can |
| 99 | + // dismiss those dangers. The caller is assumed to be cognizant of |
| 100 | + // other potential side effects. |
| 101 | + unsafe { ioctl(self.as_raw_fd(), cmd, data as *mut libc::c_void) } |
| 102 | + } |
| 103 | + |
| 104 | + /// Check VMM ioctl command against those known to not require any |
| 105 | + /// copyin/copyout to function. |
| 106 | + const fn ioctl_usize_safe(cmd: i32) -> bool { |
| 107 | + matches!( |
| 108 | + cmd, |
| 109 | + ioctls::VM_PAUSE |
| 110 | + | ioctls::VM_RESUME |
| 111 | + | ioctls::VM_DESTROY_SELF |
| 112 | + | ioctls::VM_SET_AUTODESTRUCT, |
| 113 | + ) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +impl AsRawFd for VmmFd { |
| 118 | + fn as_raw_fd(&self) -> RawFd { |
| 119 | + self.0.as_raw_fd() |
| 120 | + } |
| 121 | +} |
22 | 122 |
|
23 | 123 | /// Check that bhyve kernel VMM component matches version underlying interfaces
|
24 | 124 | /// defined in bhyve-api. Can use a user-provided version (via `version` arg)
|
25 | 125 | /// or the current one known to bhyve_api.
|
26 |
| -#[cfg(target_os = "illumos")] |
27 | 126 | pub fn check_version(version: Option<u32>) -> Result<bool> {
|
28 |
| - use std::fs::OpenOptions; |
29 |
| - use std::io::Error; |
30 |
| - use std::os::unix::fs::OpenOptionsExt; |
31 |
| - use std::os::unix::io::AsRawFd; |
32 |
| - |
33 |
| - let ctl = OpenOptions::new() |
34 |
| - .write(true) |
35 |
| - .custom_flags(libc::O_EXCL) |
36 |
| - .open(VMM_CTL_PATH)?; |
37 |
| - let ctlfd = ctl.as_raw_fd(); |
38 |
| - let res = unsafe { |
39 |
| - libc::ioctl( |
40 |
| - ctlfd, |
41 |
| - VMM_INTERFACE_VERSION, |
| 127 | + let ctl = VmmCtlFd::open()?; |
| 128 | + let current_vers = unsafe { |
| 129 | + ctl.ioctl( |
| 130 | + ioctls::VMM_INTERFACE_VERSION, |
42 | 131 | std::ptr::null_mut() as *mut libc::c_void,
|
43 | 132 | )
|
44 |
| - }; |
45 |
| - if res < 0 { |
46 |
| - return Err(Error::last_os_error()); |
47 |
| - } |
| 133 | + }?; |
48 | 134 |
|
49 |
| - let check_against = version.unwrap_or(VMM_CURRENT_INTERFACE_VERSION); |
50 |
| - Ok(check_against == res as u32) |
| 135 | + Ok(current_vers as u32 == version.unwrap_or(VMM_CURRENT_INTERFACE_VERSION)) |
| 136 | +} |
| 137 | + |
| 138 | +#[cfg(target_os = "illumos")] |
| 139 | +unsafe fn ioctl(fd: RawFd, cmd: i32, data: *mut libc::c_void) -> Result<i32> { |
| 140 | + match libc::ioctl(fd, cmd, data) { |
| 141 | + -1 => Err(Error::last_os_error()), |
| 142 | + other => Ok(other), |
| 143 | + } |
51 | 144 | }
|
52 | 145 |
|
53 | 146 | #[cfg(not(target_os = "illumos"))]
|
54 |
| -pub fn check_version(_version: Option<u32>) -> Result<bool> { |
55 |
| - Ok(false) |
| 147 | +unsafe fn ioctl( |
| 148 | + _fd: RawFd, |
| 149 | + _cmd: i32, |
| 150 | + _data: *mut libc::c_void, |
| 151 | +) -> Result<i32> { |
| 152 | + Err(Error::new(ErrorKind::Other, "illumos required")) |
56 | 153 | }
|
0 commit comments