Skip to content

Commit

Permalink
dbs-virtio-devices: Clear resources when removing virtio devices
Browse files Browse the repository at this point in the history
This PR implements functionaility of clearing resources for virtio-net
and virtio-vsock devices.

For virtio-net devices, the removed resource is its event hanlder.

For virtio-vsock devices, the removed resources are its event handler
and a socket on the host.

Fixes: openanolis#273

Signed-off-by: Zizheng Bian <[email protected]>
Signed-off-by: Xuewei Niu <[email protected]>
  • Loading branch information
justxuewei authored and studychao committed Jul 7, 2023
1 parent 284accc commit 7ce416f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
14 changes: 13 additions & 1 deletion crates/dbs-virtio-devices/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use dbs_utils::metric::{IncMetric, SharedIncMetric};
use dbs_utils::net::{net_gen, MacAddr, Tap, MAC_ADDR_LEN};
use dbs_utils::rate_limiter::{BucketUpdate, RateLimiter, TokenType};
use libc;
use log::{debug, error, info, trace};
use log::{debug, error, info, trace, warn};
use serde::Serialize;
use virtio_bindings::bindings::virtio_net::*;
use virtio_queue::{QueueOwnedT, QueueSync, QueueT};
Expand Down Expand Up @@ -861,6 +861,18 @@ where
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}

fn remove(&mut self) {
let subscriber_id = self.subscriber_id.take();
if let Some(subscriber_id) = subscriber_id {
match self.device_info.remove_event_handler(subscriber_id) {
Ok(_) => debug!("virtio-net: removed subscriber_id {:?}", subscriber_id),
Err(err) => warn!("virtio-net: failed to remove event handler: {:?}", err),
};
} else {
self.tap.take();
}
}
}

#[cfg(test)]
Expand Down
6 changes: 6 additions & 0 deletions crates/dbs-virtio-devices/src/vsock/backend/unix_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ impl VsockBackend for VsockUnixStreamBackend {
}
}

impl Drop for VsockUnixStreamBackend {
fn drop(&mut self) {
std::fs::remove_file(&self.host_sock_path).ok();
}
}

#[cfg(test)]
mod tests {
use std::fs;
Expand Down
14 changes: 14 additions & 0 deletions crates/dbs-virtio-devices/src/vsock/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use std::sync::Arc;

use dbs_device::resources::ResourceConstraint;
use dbs_utils::epoll_manager::{EpollManager, SubscriberId};
use log::debug;
use log::trace;
use log::warn;
use virtio_queue::QueueT;
use vm_memory::GuestAddressSpace;
use vm_memory::GuestMemoryRegion;
Expand Down Expand Up @@ -187,6 +189,18 @@ where
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}

fn remove(&mut self) {
let subscriber_id = self.subscriber_id.take();
if let Some(subscriber_id) = subscriber_id {
match self.device_info.remove_event_handler(subscriber_id) {
Ok(_) => debug!("virtio-vsock: removed subscriber_id {:?}", subscriber_id),
Err(err) => warn!("virtio-vsock: failed to remove event handler: {:?}", err),
};
} else {
self.muxer.take();
}
}
}

#[cfg(test)]
Expand Down

0 comments on commit 7ce416f

Please sign in to comment.