Skip to content

Commit 54e257e

Browse files
committed
refactor: use VirtioInterrupt in VirtIO devices
VirtIO devices assume they're operating under an MMIO transport and as a consequence they use IrqTrigger as interrupts. Switch that to using VirtioInterrupt for all VirtIO device objects. Only assume a VirtioInterrupt is an IrqTrigger in MMIO specific code. Signed-off-by: Babis Chalios <[email protected]>
1 parent 2ee9583 commit 54e257e

File tree

15 files changed

+215
-163
lines changed

15 files changed

+215
-163
lines changed

src/vmm/src/device_manager/mmio.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ impl MMIODeviceManager {
505505
.unwrap();
506506
if vsock.is_activated() {
507507
info!("kick vsock {id}.");
508-
vsock.signal_used_queue().unwrap();
508+
vsock.signal_used_queue(0).unwrap();
509509
}
510510
}
511511
TYPE_RNG => {
@@ -534,6 +534,7 @@ mod tests {
534534
use crate::devices::virtio::ActivateError;
535535
use crate::devices::virtio::device::VirtioDevice;
536536
use crate::devices::virtio::queue::Queue;
537+
use crate::devices::virtio::transport::VirtioInterrupt;
537538
use crate::devices::virtio::transport::mmio::IrqTrigger;
538539
use crate::test_utils::multi_region_mem_raw;
539540
use crate::vstate::kvm::Kvm;
@@ -620,7 +621,7 @@ mod tests {
620621
&self.queue_evts
621622
}
622623

623-
fn interrupt_trigger(&self) -> Arc<IrqTrigger> {
624+
fn interrupt_trigger(&self) -> Arc<dyn VirtioInterrupt> {
624625
self.interrupt_trigger
625626
.as_ref()
626627
.expect("Device is not activated")
@@ -645,7 +646,7 @@ mod tests {
645646
fn activate(
646647
&mut self,
647648
_: GuestMemoryMmap,
648-
_: Arc<IrqTrigger>,
649+
_: Arc<dyn VirtioInterrupt>,
649650
) -> Result<(), ActivateError> {
650651
Ok(())
651652
}

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

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use super::{
2525
};
2626
use crate::devices::virtio::balloon::BalloonError;
2727
use crate::devices::virtio::generated::virtio_config::VIRTIO_F_VERSION_1;
28-
use crate::devices::virtio::transport::mmio::{IrqTrigger, IrqType};
28+
use crate::devices::virtio::transport::{VirtioInterrupt, VirtioInterruptType};
2929
use crate::logger::IncMetric;
3030
use crate::utils::u64_to_usize;
3131
use crate::vstate::memory::{Address, ByteValued, Bytes, GuestAddress, GuestMemoryMmap};
@@ -339,7 +339,7 @@ impl Balloon {
339339
}
340340

341341
if needs_interrupt {
342-
self.signal_used_queue()?;
342+
self.signal_used_queue(INFLATE_INDEX)?;
343343
}
344344

345345
Ok(())
@@ -357,7 +357,7 @@ impl Balloon {
357357
}
358358

359359
if needs_interrupt {
360-
self.signal_used_queue()
360+
self.signal_used_queue(DEFLATE_INDEX)
361361
} else {
362362
Ok(())
363363
}
@@ -401,9 +401,9 @@ impl Balloon {
401401
Ok(())
402402
}
403403

404-
pub(crate) fn signal_used_queue(&self) -> Result<(), BalloonError> {
404+
pub(crate) fn signal_used_queue(&self, qidx: usize) -> Result<(), BalloonError> {
405405
self.interrupt_trigger()
406-
.trigger_irq(IrqType::Vring)
406+
.trigger(VirtioInterruptType::Queue(qidx as u16))
407407
.map_err(|err| {
408408
METRICS.event_fails.inc();
409409
BalloonError::InterruptError(err)
@@ -428,7 +428,7 @@ impl Balloon {
428428
self.queues[STATS_INDEX]
429429
.add_used(index, 0)
430430
.map_err(BalloonError::Queue)?;
431-
self.signal_used_queue()
431+
self.signal_used_queue(STATS_INDEX)
432432
} else {
433433
error!("Failed to update balloon stats, missing descriptor.");
434434
Ok(())
@@ -440,7 +440,7 @@ impl Balloon {
440440
if self.is_activated() {
441441
self.config_space.num_pages = mib_to_pages(amount_mib)?;
442442
self.interrupt_trigger()
443-
.trigger_irq(IrqType::Config)
443+
.trigger(VirtioInterruptType::Config)
444444
.map_err(BalloonError::InterruptError)
445445
} else {
446446
Err(BalloonError::DeviceNotActive)
@@ -551,7 +551,7 @@ impl VirtioDevice for Balloon {
551551
&self.queue_evts
552552
}
553553

554-
fn interrupt_trigger(&self) -> Arc<IrqTrigger> {
554+
fn interrupt_trigger(&self) -> Arc<dyn VirtioInterrupt> {
555555
self.device_state
556556
.active_state()
557557
.expect("Device is not activated")
@@ -585,7 +585,7 @@ impl VirtioDevice for Balloon {
585585
fn activate(
586586
&mut self,
587587
mem: GuestMemoryMmap,
588-
interrupt: Arc<IrqTrigger>,
588+
interrupt: Arc<dyn VirtioInterrupt>,
589589
) -> Result<(), ActivateError> {
590590
for q in self.queues.iter_mut() {
591591
q.initialize(&mem)
@@ -621,7 +621,7 @@ pub(crate) mod tests {
621621
check_request_completion, invoke_handler_for_queue_event, set_request,
622622
};
623623
use crate::devices::virtio::queue::{VIRTQ_DESC_F_NEXT, VIRTQ_DESC_F_WRITE};
624-
use crate::devices::virtio::test_utils::{VirtQueue, default_mem};
624+
use crate::devices::virtio::test_utils::{VirtQueue, default_interrupt, default_mem};
625625
use crate::test_utils::single_region_mem;
626626
use crate::vstate::memory::GuestAddress;
627627

@@ -798,11 +798,10 @@ pub(crate) mod tests {
798798
fn test_invalid_request() {
799799
let mut balloon = Balloon::new(0, true, 0, false).unwrap();
800800
let mem = default_mem();
801-
let interrupt = Arc::new(IrqTrigger::new().unwrap());
802801
// Only initialize the inflate queue to demonstrate invalid request handling.
803802
let infq = VirtQueue::new(GuestAddress(0), &mem, 16);
804803
balloon.set_queue(INFLATE_INDEX, infq.create_queue());
805-
balloon.activate(mem.clone(), interrupt).unwrap();
804+
balloon.activate(mem.clone(), default_interrupt()).unwrap();
806805

807806
// Fill the second page with non-zero bytes.
808807
for i in 0..0x1000 {
@@ -858,10 +857,9 @@ pub(crate) mod tests {
858857
fn test_inflate() {
859858
let mut balloon = Balloon::new(0, true, 0, false).unwrap();
860859
let mem = default_mem();
861-
let interrupt = Arc::new(IrqTrigger::new().unwrap());
862860
let infq = VirtQueue::new(GuestAddress(0), &mem, 16);
863861
balloon.set_queue(INFLATE_INDEX, infq.create_queue());
864-
balloon.activate(mem.clone(), interrupt).unwrap();
862+
balloon.activate(mem.clone(), default_interrupt()).unwrap();
865863

866864
// Fill the third page with non-zero bytes.
867865
for i in 0..0x1000 {
@@ -929,10 +927,9 @@ pub(crate) mod tests {
929927
fn test_deflate() {
930928
let mut balloon = Balloon::new(0, true, 0, false).unwrap();
931929
let mem = default_mem();
932-
let interrupt = Arc::new(IrqTrigger::new().unwrap());
933930
let defq = VirtQueue::new(GuestAddress(0), &mem, 16);
934931
balloon.set_queue(DEFLATE_INDEX, defq.create_queue());
935-
balloon.activate(mem.clone(), interrupt).unwrap();
932+
balloon.activate(mem.clone(), default_interrupt()).unwrap();
936933

937934
let page_addr = 0x10;
938935

@@ -978,10 +975,9 @@ pub(crate) mod tests {
978975
fn test_stats() {
979976
let mut balloon = Balloon::new(0, true, 1, false).unwrap();
980977
let mem = default_mem();
981-
let interrupt = Arc::new(IrqTrigger::new().unwrap());
982978
let statsq = VirtQueue::new(GuestAddress(0), &mem, 16);
983979
balloon.set_queue(STATS_INDEX, statsq.create_queue());
984-
balloon.activate(mem.clone(), interrupt).unwrap();
980+
balloon.activate(mem.clone(), default_interrupt()).unwrap();
985981

986982
let page_addr = 0x100;
987983

@@ -1057,7 +1053,11 @@ pub(crate) mod tests {
10571053
assert!(balloon.stats_desc_index.is_some());
10581054
balloon.process_stats_timer_event().unwrap();
10591055
assert!(balloon.stats_desc_index.is_none());
1060-
assert!(balloon.interrupt_trigger().has_pending_irq(IrqType::Vring));
1056+
assert!(
1057+
balloon
1058+
.interrupt_trigger()
1059+
.has_pending_interrupt(VirtioInterruptType::Queue(STATS_INDEX as u16))
1060+
);
10611061
});
10621062
}
10631063
}
@@ -1066,23 +1066,21 @@ pub(crate) mod tests {
10661066
fn test_process_balloon_queues() {
10671067
let mut balloon = Balloon::new(0x10, true, 0, false).unwrap();
10681068
let mem = default_mem();
1069-
let interrupt = Arc::new(IrqTrigger::new().unwrap());
10701069
let infq = VirtQueue::new(GuestAddress(0), &mem, 16);
10711070
let defq = VirtQueue::new(GuestAddress(0), &mem, 16);
10721071

10731072
balloon.set_queue(INFLATE_INDEX, infq.create_queue());
10741073
balloon.set_queue(DEFLATE_INDEX, defq.create_queue());
10751074

1076-
balloon.activate(mem, interrupt).unwrap();
1075+
balloon.activate(mem, default_interrupt()).unwrap();
10771076
balloon.process_virtio_queues()
10781077
}
10791078

10801079
#[test]
10811080
fn test_update_stats_interval() {
10821081
let mut balloon = Balloon::new(0, true, 0, false).unwrap();
10831082
let mem = default_mem();
1084-
let interrupt = Arc::new(IrqTrigger::new().unwrap());
1085-
balloon.activate(mem, interrupt).unwrap();
1083+
balloon.activate(mem, default_interrupt()).unwrap();
10861084
assert_eq!(
10871085
format!("{:?}", balloon.update_stats_polling_interval(1)),
10881086
"Err(StatisticsStateChange)"
@@ -1091,8 +1089,7 @@ pub(crate) mod tests {
10911089

10921090
let mut balloon = Balloon::new(0, true, 1, false).unwrap();
10931091
let mem = default_mem();
1094-
let interrupt = Arc::new(IrqTrigger::new().unwrap());
1095-
balloon.activate(mem, interrupt).unwrap();
1092+
balloon.activate(mem, default_interrupt()).unwrap();
10961093
assert_eq!(
10971094
format!("{:?}", balloon.update_stats_polling_interval(0)),
10981095
"Err(StatisticsStateChange)"
@@ -1113,7 +1110,7 @@ pub(crate) mod tests {
11131110
let mut balloon = Balloon::new(0, true, 0, false).unwrap();
11141111
// Switch the state to active.
11151112
balloon.device_state =
1116-
DeviceState::Activated((single_region_mem(0x1), Arc::new(IrqTrigger::new().unwrap())));
1113+
DeviceState::Activated((single_region_mem(0x1), default_interrupt()));
11171114

11181115
assert_eq!(balloon.num_pages(), 0);
11191116
assert_eq!(balloon.actual_pages(), 0);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::devices::virtio::{balloon::BALLOON_NUM_QUEUES, balloon::Balloon};
1010
#[cfg(test)]
1111
pub fn invoke_handler_for_queue_event(b: &mut Balloon, queue_index: usize) {
1212
use crate::devices::virtio::balloon::{DEFLATE_INDEX, INFLATE_INDEX, STATS_INDEX};
13-
use crate::devices::virtio::transport::mmio::IrqType;
13+
use crate::devices::virtio::transport::VirtioInterruptType;
1414

1515
assert!(queue_index < BALLOON_NUM_QUEUES);
1616
// Trigger the queue event.
@@ -24,7 +24,7 @@ pub fn invoke_handler_for_queue_event(b: &mut Balloon, queue_index: usize) {
2424
};
2525
// Validate the queue operation finished successfully.
2626
let (_, interrupt) = b.device_state.active_state().unwrap();
27-
assert!(interrupt.has_pending_irq(IrqType::Vring));
27+
assert!(interrupt.has_pending_interrupt(VirtioInterruptType::Queue(queue_index as u16)));
2828
}
2929

3030
pub fn set_request(queue: &VirtQueue, idx: u16, addr: u64, len: u32, flags: u16) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use super::vhost_user::device::{VhostUserBlock, VhostUserBlockConfig};
1212
use super::virtio::device::{VirtioBlock, VirtioBlockConfig};
1313
use crate::devices::virtio::device::VirtioDevice;
1414
use crate::devices::virtio::queue::Queue;
15-
use crate::devices::virtio::transport::mmio::IrqTrigger;
15+
use crate::devices::virtio::transport::VirtioInterrupt;
1616
use crate::devices::virtio::{ActivateError, TYPE_BLOCK};
1717
use crate::rate_limiter::BucketUpdate;
1818
use crate::snapshot::Persist;
@@ -176,7 +176,7 @@ impl VirtioDevice for Block {
176176
}
177177
}
178178

179-
fn interrupt_trigger(&self) -> Arc<IrqTrigger> {
179+
fn interrupt_trigger(&self) -> Arc<dyn VirtioInterrupt> {
180180
match self {
181181
Self::Virtio(b) => {
182182
b.device_state
@@ -210,7 +210,7 @@ impl VirtioDevice for Block {
210210
fn activate(
211211
&mut self,
212212
mem: GuestMemoryMmap,
213-
interrupt: Arc<IrqTrigger>,
213+
interrupt: Arc<dyn VirtioInterrupt>,
214214
) -> Result<(), ActivateError> {
215215
match self {
216216
Self::Virtio(b) => b.activate(mem, interrupt),

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ use crate::devices::virtio::generated::virtio_blk::{VIRTIO_BLK_F_FLUSH, VIRTIO_B
1919
use crate::devices::virtio::generated::virtio_config::VIRTIO_F_VERSION_1;
2020
use crate::devices::virtio::generated::virtio_ring::VIRTIO_RING_F_EVENT_IDX;
2121
use crate::devices::virtio::queue::Queue;
22-
use crate::devices::virtio::transport::mmio::{IrqTrigger, IrqType};
22+
use crate::devices::virtio::transport::VirtioInterrupt;
23+
use crate::devices::virtio::transport::VirtioInterruptType;
2324
use crate::devices::virtio::vhost_user::{VhostUserHandleBackend, VhostUserHandleImpl};
2425
use crate::devices::virtio::vhost_user_metrics::{
2526
VhostUserDeviceMetrics, VhostUserMetricsPerDevice,
@@ -272,7 +273,7 @@ impl<T: VhostUserHandleBackend> VhostUserBlockImpl<T> {
272273
.map_err(VhostUserBlockError::Vhost)?;
273274
self.config_space = new_config_space;
274275
interrupt
275-
.trigger_irq(IrqType::Config)
276+
.trigger(VirtioInterruptType::Config)
276277
.map_err(VhostUserBlockError::IrqTrigger)?;
277278

278279
let delta_us = get_time_us(ClockType::Monotonic) - start_time;
@@ -311,7 +312,7 @@ impl<T: VhostUserHandleBackend + Send + 'static> VirtioDevice for VhostUserBlock
311312
&self.queue_evts
312313
}
313314

314-
fn interrupt_trigger(&self) -> Arc<IrqTrigger> {
315+
fn interrupt_trigger(&self) -> Arc<dyn VirtioInterrupt> {
315316
self.device_state
316317
.active_state()
317318
.expect("Device is not initialized")
@@ -337,7 +338,7 @@ impl<T: VhostUserHandleBackend + Send + 'static> VirtioDevice for VhostUserBlock
337338
fn activate(
338339
&mut self,
339340
mem: GuestMemoryMmap,
340-
interrupt: Arc<IrqTrigger>,
341+
interrupt: Arc<dyn VirtioInterrupt>,
341342
) -> Result<(), ActivateError> {
342343
for q in self.queues.iter_mut() {
343344
q.initialize(&mem)
@@ -353,7 +354,7 @@ impl<T: VhostUserHandleBackend + Send + 'static> VirtioDevice for VhostUserBlock
353354
self.vu_handle.setup_backend(
354355
&mem,
355356
&[(0, &self.queues[0], &self.queue_evts[0])],
356-
&interrupt,
357+
interrupt.clone(),
357358
)
358359
})
359360
.map_err(|err| {
@@ -383,7 +384,7 @@ mod tests {
383384

384385
use super::*;
385386
use crate::devices::virtio::block::virtio::device::FileEngineType;
386-
use crate::devices::virtio::test_utils::default_mem;
387+
use crate::devices::virtio::test_utils::{default_interrupt, default_mem};
387388
use crate::devices::virtio::transport::mmio::VIRTIO_MMIO_INT_CONFIG;
388389
use crate::devices::virtio::vhost_user::tests::create_mem;
389390
use crate::test_utils::create_tmp_socket;
@@ -660,8 +661,7 @@ mod tests {
660661
assert_eq!(vhost_block.config_space, vec![0x69, 0x69, 0x69]);
661662

662663
// Testing [`config_update`]
663-
vhost_block.device_state =
664-
DeviceState::Activated((default_mem(), Arc::new(IrqTrigger::new().unwrap())));
664+
vhost_block.device_state = DeviceState::Activated((default_mem(), default_interrupt()));
665665
vhost_block.config_space = vec![];
666666
vhost_block.config_update().unwrap();
667667
assert_eq!(vhost_block.config_space, vec![0x69, 0x69, 0x69]);
@@ -791,10 +791,11 @@ mod tests {
791791
file.set_len(region_size as u64).unwrap();
792792
let regions = vec![(GuestAddress(0x0), region_size)];
793793
let guest_memory = create_mem(file, &regions);
794-
let interrupt = Arc::new(IrqTrigger::new().unwrap());
795794

796795
// During actiavion of the device features, memory and queues should be set and activated.
797-
vhost_block.activate(guest_memory, interrupt).unwrap();
796+
vhost_block
797+
.activate(guest_memory, default_interrupt())
798+
.unwrap();
798799
assert!(unsafe { *vhost_block.vu_handle.vu.features_are_set.get() });
799800
assert!(unsafe { *vhost_block.vu_handle.vu.memory_is_set.get() });
800801
assert!(unsafe { *vhost_block.vu_handle.vu.vring_enabled.get() });

0 commit comments

Comments
 (0)