From 9c031a98405871b5629956018a02d9b8a97fec2a Mon Sep 17 00:00:00 2001 From: Wang Ningyuan Date: Thu, 7 Sep 2023 16:16:31 +0900 Subject: [PATCH] vm_control: Factor net tap removal to vm_control This CL factors out do_net_remove to vm_control from main.rs. do_net_remove sends a VmRequest to remove a tap device. Test= presubmit tests. Bug=b:294777126 Change-Id: I82e2741a044b1144f04424deacc905fbe079560c Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4846021 Reviewed-by: Daniel Verkamp Reviewed-by: Alexandre Courbot Commit-Queue: Ningyuan Wang --- src/main.rs | 10 ++++++---- vm_control/src/client.rs | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 64c24675a1..8eb3d980b5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,6 +59,8 @@ use vm_control::client::do_gpu_display_remove; use vm_control::client::do_modify_battery; #[cfg(feature = "pci-hotplug")] use vm_control::client::do_net_add; +#[cfg(feature = "pci-hotplug")] +use vm_control::client::do_net_remove; use vm_control::client::do_swap_status; use vm_control::client::do_usb_attach; use vm_control::client::do_usb_detach; @@ -74,8 +76,6 @@ use vm_control::BalloonControlCommand; use vm_control::DiskControlCommand; use vm_control::HotPlugDeviceInfo; use vm_control::HotPlugDeviceType; -#[cfg(feature = "pci-hotplug")] -use vm_control::NetControlCommand; use vm_control::RestoreCommand; use vm_control::SnapshotCommand; use vm_control::SwapCommand; @@ -324,8 +324,10 @@ fn modify_virtio_net(cmd: cmdline::VirtioNetCommand) -> std::result::Result<(), info!("Tap device {} plugged to PCI bus {}", &c.tap_name, bus_num); } cmdline::VirtioNetSubCommand::RemoveTap(c) => { - let request = VmRequest::HotPlugNetCommand(NetControlCommand::RemoveTap(c.bus)); - vms_request(&request, c.socket_path)?; + do_net_remove(c.bus, &c.socket_path).map_err(|e| { + error!("Tap device remove failed: {:?}", &e); + })?; + info!("Tap device removed from PCI bus {}", &c.bus); } }; diff --git a/vm_control/src/client.rs b/vm_control/src/client.rs index 0df053199f..a7fa42fa64 100644 --- a/vm_control/src/client.rs +++ b/vm_control/src/client.rs @@ -73,6 +73,20 @@ pub fn do_net_add + std::fmt::Debug>( } } +#[cfg(feature = "pci-hotplug")] +/// Send a `VmRequest` for removing hotplugged PCI device that expects `VmResponse::Ok` +pub fn do_net_remove + std::fmt::Debug>( + bus_num: u8, + socket_path: T, +) -> AnyHowResult<()> { + let request = VmRequest::HotPlugNetCommand(NetControlCommand::RemoveTap(bus_num)); + let response = handle_request(&request, socket_path).map_err(|()| anyhow!("socket error: "))?; + match response { + VmResponse::Ok => Ok(()), + e => Err(anyhow!("Unexpected response: {:#}", e)), + } +} + pub fn do_usb_attach + std::fmt::Debug>( socket_path: T, dev_path: &Path,