mirror of
https://chromium.googlesource.com/crosvm/crosvm
synced 2025-02-06 10:32:10 +00:00
crosvm: argh-ify vfio command
Usage: crosvm vfio <command> [<args>] add/remove host vfio pci device into guest Options: --help display usage information Commands: add ADD remove REMOVE Change-Id: I79d0b5205b1ae8d880b26cf12d61a2f70bb73756 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3680295 Reviewed-by: Dennis Kempin <denniskempin@google.com> Commit-Queue: Anton Romanov <romanton@google.com> Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
parent
9140f8d41c
commit
d19b575fd1
2 changed files with 57 additions and 38 deletions
|
@ -277,10 +277,44 @@ pub struct UsbCommand {
|
|||
/// Show package version.
|
||||
pub struct VersionCommand {}
|
||||
|
||||
#[generate_catchall_args]
|
||||
#[derive(FromArgs)]
|
||||
#[argh(subcommand, name = "add")]
|
||||
/// ADD
|
||||
pub struct VfioAddSubCommand {
|
||||
#[argh(positional)]
|
||||
/// path to host's vfio sysfs
|
||||
pub vfio_path: PathBuf,
|
||||
#[argh(positional, arg_name = "VM_SOCKET")]
|
||||
/// VM Socket path
|
||||
pub socket_path: String,
|
||||
}
|
||||
|
||||
#[derive(FromArgs)]
|
||||
#[argh(subcommand, name = "remove")]
|
||||
/// REMOVE
|
||||
pub struct VfioRemoveSubCommand {
|
||||
#[argh(positional)]
|
||||
/// path to host's vfio sysfs
|
||||
pub vfio_path: PathBuf,
|
||||
#[argh(positional, arg_name = "VM_SOCKET")]
|
||||
/// VM Socket path
|
||||
pub socket_path: String,
|
||||
}
|
||||
|
||||
#[derive(FromArgs)]
|
||||
#[argh(subcommand)]
|
||||
pub enum VfioSubCommand {
|
||||
Add(VfioAddSubCommand),
|
||||
Remove(VfioRemoveSubCommand),
|
||||
}
|
||||
|
||||
#[derive(FromArgs)]
|
||||
#[argh(subcommand, name = "vfio")]
|
||||
/// add/remove host vfio pci device into guest
|
||||
pub struct VfioCrosvmCommand {}
|
||||
pub struct VfioCrosvmCommand {
|
||||
#[argh(subcommand)]
|
||||
pub command: VfioSubCommand,
|
||||
}
|
||||
|
||||
#[derive(FromArgs)]
|
||||
#[argh(subcommand, name = "device")]
|
||||
|
|
57
src/main.rs
57
src/main.rs
|
@ -2779,44 +2779,29 @@ fn modify_battery(cmd: crosvm::BatteryCommand) -> std::result::Result<(), ()> {
|
|||
}
|
||||
|
||||
fn modify_vfio(cmd: crosvm::VfioCrosvmCommand) -> std::result::Result<(), ()> {
|
||||
if cmd.args.len() < 3 {
|
||||
print_help(
|
||||
"crosvm vfio",
|
||||
"[add | remove host_vfio_sysfs] VM_SOCKET...",
|
||||
&[],
|
||||
);
|
||||
return Err(());
|
||||
}
|
||||
|
||||
let mut args = cmd.args.into_iter();
|
||||
// This unwrap will not panic because of the above length check.
|
||||
let command = args.next().unwrap();
|
||||
let path_str = args.next().unwrap();
|
||||
let vfio_path = PathBuf::from(&path_str);
|
||||
if !vfio_path.exists() || !vfio_path.is_dir() {
|
||||
error!("Invalid host sysfs path: {}", path_str);
|
||||
return Err(());
|
||||
}
|
||||
|
||||
let socket_path = args.next().unwrap();
|
||||
let socket_path = Path::new(&socket_path);
|
||||
|
||||
let add = match command.as_ref() {
|
||||
"add" => true,
|
||||
"remove" => false,
|
||||
other => {
|
||||
error!("Invalid vfio command {}", other);
|
||||
return Err(());
|
||||
let (request, socket_path, vfio_path) = match cmd.command {
|
||||
crosvm::VfioSubCommand::Add(c) => {
|
||||
let request = VmRequest::VfioCommand {
|
||||
vfio_path: c.vfio_path.clone(),
|
||||
add: true,
|
||||
hp_interrupt: true,
|
||||
};
|
||||
(request, c.socket_path, c.vfio_path)
|
||||
}
|
||||
crosvm::VfioSubCommand::Remove(c) => {
|
||||
let request = VmRequest::VfioCommand {
|
||||
vfio_path: c.vfio_path.clone(),
|
||||
add: true,
|
||||
hp_interrupt: true,
|
||||
};
|
||||
(request, c.socket_path, c.vfio_path)
|
||||
}
|
||||
};
|
||||
|
||||
let hp_interrupt = true;
|
||||
let request = VmRequest::VfioCommand {
|
||||
vfio_path,
|
||||
add,
|
||||
hp_interrupt,
|
||||
};
|
||||
handle_request(&request, socket_path)?;
|
||||
if !vfio_path.exists() || !vfio_path.is_dir() {
|
||||
error!("Invalid host sysfs path: {:?}", vfio_path);
|
||||
return Err(());
|
||||
}
|
||||
handle_request(&request, Path::new(&socket_path))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue