Skip to content

Commit 1d91776

Browse files
committed
refactor(virtio): add id method to the VirtioDevice trait
Add a trait method to VirtioDevice to get a unique device id. This can be used in default imps of some other methods to print current device id in case of an error/warning etc. Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
1 parent b2d7a16 commit 1d91776

File tree

18 files changed

+70
-39
lines changed

18 files changed

+70
-39
lines changed

src/vmm/src/builder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use crate::device_manager::{
3131
};
3232
use crate::devices::virtio::balloon::Balloon;
3333
use crate::devices::virtio::block::device::Block;
34+
use crate::devices::virtio::device::VirtioDevice;
3435
use crate::devices::virtio::mem::{VIRTIO_MEM_DEFAULT_SLOT_SIZE_MIB, VirtioMem};
3536
use crate::devices::virtio::net::Net;
3637
use crate::devices::virtio::pmem::device::Pmem;
@@ -682,7 +683,7 @@ fn attach_net_devices<'a, I: Iterator<Item = &'a Arc<Mutex<Net>>> + Debug>(
682683
event_manager: &mut EventManager,
683684
) -> Result<(), StartMicrovmError> {
684685
for net_device in net_devices {
685-
let id = net_device.lock().expect("Poisoned lock").id().clone();
686+
let id = net_device.lock().expect("Poisoned lock").id().to_string();
686687
event_manager.add_subscriber(net_device.clone());
687688
// The device mutex mustn't be locked here otherwise it will deadlock.
688689
device_manager.attach_virtio_device(vm, id, net_device.clone(), cmdline, false)?;

src/vmm/src/device_manager/mmio.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,10 @@ pub(crate) mod tests {
494494
impl VirtioDevice for DummyDevice {
495495
impl_device_type!(VirtioDeviceType::Net);
496496

497+
fn id(&self) -> &str {
498+
"dummy"
499+
}
500+
497501
fn avail_features(&self) -> u64 {
498502
0
499503
}

src/vmm/src/devices/virtio/balloon/device.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -672,11 +672,6 @@ impl Balloon {
672672
Ok(())
673673
}
674674

675-
/// Provides the ID of this balloon device.
676-
pub fn id(&self) -> &str {
677-
BALLOON_DEV_ID
678-
}
679-
680675
fn trigger_stats_update(&mut self) -> Result<(), BalloonError> {
681676
// The communication is driven by the device by using the buffer
682677
// and sending a used buffer notification
@@ -868,6 +863,10 @@ impl Balloon {
868863
impl VirtioDevice for Balloon {
869864
impl_device_type!(VirtioDeviceType::Balloon);
870865

866+
fn id(&self) -> &str {
867+
BALLOON_DEV_ID
868+
}
869+
871870
fn avail_features(&self) -> u64 {
872871
self.avail_features
873872
}

src/vmm/src/devices/virtio/block/device.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,6 @@ impl Block {
9595
}
9696
}
9797

98-
pub fn id(&self) -> &str {
99-
match self {
100-
Self::Virtio(b) => &b.id,
101-
Self::VhostUser(b) => &b.id,
102-
}
103-
}
104-
10598
pub fn root_device(&self) -> bool {
10699
match self {
107100
Self::Virtio(b) => b.root_device,
@@ -134,6 +127,13 @@ impl Block {
134127
impl VirtioDevice for Block {
135128
impl_device_type!(VirtioDeviceType::Block);
136129

130+
fn id(&self) -> &str {
131+
match self {
132+
Self::Virtio(b) => b.id(),
133+
Self::VhostUser(b) => b.id(),
134+
}
135+
}
136+
137137
fn avail_features(&self) -> u64 {
138138
match self {
139139
Self::Virtio(b) => b.avail_features,

src/vmm/src/devices/virtio/block/vhost_user/device.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,10 @@ impl<T: VhostUserHandleBackend> VhostUserBlockImpl<T> {
290290
impl<T: VhostUserHandleBackend + Send + 'static> VirtioDevice for VhostUserBlockImpl<T> {
291291
impl_device_type!(VirtioDeviceType::Block);
292292

293+
fn id(&self) -> &str {
294+
&self.id
295+
}
296+
293297
fn avail_features(&self) -> u64 {
294298
self.avail_features
295299
}

src/vmm/src/devices/virtio/block/virtio/device.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,10 @@ impl VirtioBlock {
585585
impl VirtioDevice for VirtioBlock {
586586
impl_device_type!(VirtioDeviceType::Block);
587587

588+
fn id(&self) -> &str {
589+
&self.id
590+
}
591+
588592
fn avail_features(&self) -> u64 {
589593
self.avail_features
590594
}

src/vmm/src/devices/virtio/device.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ pub trait VirtioDevice: AsAny + Send {
102102
/// It should be the same as returned by Self::const_device_type().
103103
fn device_type(&self) -> VirtioDeviceType;
104104

105+
/// Returns unique device id
106+
fn id(&self) -> &str;
107+
105108
/// Returns the device queues.
106109
fn queues(&self) -> &[Queue];
107110

@@ -222,6 +225,10 @@ pub(crate) mod tests {
222225
impl VirtioDevice for MockVirtioDevice {
223226
impl_device_type!(VirtioDeviceType::Net);
224227

228+
fn id(&self) -> &str {
229+
"mock"
230+
}
231+
225232
fn avail_features(&self) -> u64 {
226233
self.avail_features
227234
}

src/vmm/src/devices/virtio/mem/device.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,6 @@ impl VirtioMem {
169169
})
170170
}
171171

172-
pub fn id(&self) -> &str {
173-
VIRTIO_MEM_DEV_ID
174-
}
175-
176172
pub fn guest_address(&self) -> GuestAddress {
177173
GuestAddress(self.config.addr)
178174
}
@@ -603,6 +599,10 @@ impl VirtioMem {
603599
impl VirtioDevice for VirtioMem {
604600
impl_device_type!(VirtioDeviceType::Mem);
605601

602+
fn id(&self) -> &str {
603+
VIRTIO_MEM_DEV_ID
604+
}
605+
606606
fn queues(&self) -> &[Queue] {
607607
&self.queues
608608
}

src/vmm/src/devices/virtio/net/device.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,6 @@ impl Net {
343343
Self::new_with_tap(id, tap, guest_mac, rx_rate_limiter, tx_rate_limiter)
344344
}
345345

346-
/// Provides the ID of this net device.
347-
pub fn id(&self) -> &String {
348-
&self.id
349-
}
350-
351346
/// Provides the MAC of this net device.
352347
pub fn guest_mac(&self) -> Option<&MacAddr> {
353348
self.guest_mac.as_ref()
@@ -962,6 +957,10 @@ impl Net {
962957
impl VirtioDevice for Net {
963958
impl_device_type!(VirtioDeviceType::Net);
964959

960+
fn id(&self) -> &str {
961+
&self.id
962+
}
963+
965964
fn avail_features(&self) -> u64 {
966965
self.avail_features
967966
}

src/vmm/src/devices/virtio/net/persist.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl Persist<'_> for Net {
7474

7575
fn save(&self) -> Self::State {
7676
NetState {
77-
id: self.id().clone(),
77+
id: self.id.clone(),
7878
tap_if_name: self.iface_name(),
7979
rx_rate_limiter_state: self.rx_rate_limiter.save(),
8080
tx_rate_limiter_state: self.tx_rate_limiter.save(),

0 commit comments

Comments
 (0)