From b2abead65b80667ccf02ab910e860ee138d9c425 Mon Sep 17 00:00:00 2001 From: Wei Liu Date: Sat, 27 Jun 2020 15:56:43 +0000 Subject: [PATCH] vmm: interrupt: provide and use extension trait RoutingEntryExt This trait contains a function which produces a interrupt routing entry. Implement that trait for KvmRoutingEntry and rewrite the update function. Signed-off-by: Wei Liu --- vmm/src/interrupt.rs | 63 ++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/vmm/src/interrupt.rs b/vmm/src/interrupt.rs index 84dac1538..6e4cb4494 100644 --- a/vmm/src/interrupt.rs +++ b/vmm/src/interrupt.rs @@ -116,6 +116,38 @@ struct MsiInterruptGroup { irq_routes: HashMap, } +trait RoutingEntryExt { + fn make_entry(gsi: u32, config: &InterruptSourceConfig) -> Result>; +} + +impl RoutingEntryExt for KvmRoutingEntry { + fn make_entry(gsi: u32, config: &InterruptSourceConfig) -> Result> { + if let InterruptSourceConfig::MsiIrq(cfg) = &config { + let mut kvm_route = kvm_irq_routing_entry { + gsi, + type_: KVM_IRQ_ROUTING_MSI, + ..Default::default() + }; + + kvm_route.u.msi.address_lo = cfg.low_addr; + kvm_route.u.msi.address_hi = cfg.high_addr; + kvm_route.u.msi.data = cfg.data; + + let kvm_entry = KvmRoutingEntry { + route: kvm_route, + masked: false, + }; + + return Ok(Box::new(kvm_entry)); + } + + Err(io::Error::new( + io::ErrorKind::Other, + "Interrupt config type not supported", + )) + } +} + impl MsiInterruptGroup { fn new( vm_fd: Arc, @@ -202,32 +234,11 @@ impl InterruptSourceGroup for KvmMsiInterruptGroup { fn update(&self, index: InterruptIndex, config: InterruptSourceConfig) -> Result<()> { if let Some(route) = self.irq_routes.get(&index) { - if let InterruptSourceConfig::MsiIrq(cfg) = &config { - let mut kvm_route = kvm_irq_routing_entry { - gsi: route.gsi, - type_: KVM_IRQ_ROUTING_MSI, - ..Default::default() - }; - - kvm_route.u.msi.address_lo = cfg.low_addr; - kvm_route.u.msi.address_hi = cfg.high_addr; - kvm_route.u.msi.data = cfg.data; - - let kvm_entry = KvmRoutingEntry { - route: kvm_route, - masked: false, - }; - - self.gsi_msi_routes - .lock() - .unwrap() - .insert(route.gsi, kvm_entry); - } else { - return Err(io::Error::new( - io::ErrorKind::Other, - "Interrupt config type not supported", - )); - } + let entry = KvmRoutingEntry::make_entry(route.gsi, &config)?; + self.gsi_msi_routes + .lock() + .unwrap() + .insert(route.gsi, *entry); return self.set_gsi_routes(); }