mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
vm-virtio: Create new VirtioTransport trait to abstract ioeventfds
In order to group together some functions that can be shared across virtio transport layers, this commit introduces a new trait called VirtioTransport. The first function of this trait being ioeventfds() as it is needed from both virtio-mmio and virtio-pci devices, represented by MmioDevice and VirtioPciDevice structures respectively. Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
committed by
Samuel Ortiz
parent
3fa5df4161
commit
5694ac2b1e
@@ -8,6 +8,7 @@ use std::sync::{Arc, RwLock};
|
||||
use byteorder::{ByteOrder, LittleEndian};
|
||||
use libc::EFD_NONBLOCK;
|
||||
|
||||
use crate::transport::{VirtioTransport, NOTIFY_REG_OFFSET};
|
||||
use crate::{
|
||||
Queue, VirtioDevice, VirtioInterrupt, VirtioInterruptType, DEVICE_ACKNOWLEDGE, DEVICE_DRIVER,
|
||||
DEVICE_DRIVER_OK, DEVICE_FAILED, DEVICE_FEATURES_OK, DEVICE_INIT,
|
||||
@@ -86,7 +87,7 @@ impl MmioDevice {
|
||||
/// Gets the list of queue events that must be triggered whenever the VM writes to
|
||||
/// `virtio::NOTIFY_REG_OFFSET` past the MMIO base. Each event must be triggered when the
|
||||
/// value being written equals the index of the event in this list.
|
||||
pub fn queue_evts(&self) -> &[EventFd] {
|
||||
fn queue_evts(&self) -> &[EventFd] {
|
||||
self.queue_evts.as_slice()
|
||||
}
|
||||
|
||||
@@ -140,6 +141,16 @@ impl MmioDevice {
|
||||
}
|
||||
}
|
||||
|
||||
impl VirtioTransport for MmioDevice {
|
||||
fn ioeventfds(&self, base_addr: u64) -> Vec<(&EventFd, u64)> {
|
||||
let notify_base = base_addr + u64::from(NOTIFY_REG_OFFSET);
|
||||
self.queue_evts()
|
||||
.iter()
|
||||
.map(|event| (event, notify_base))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl BusDevice for MmioDevice {
|
||||
fn read(&mut self, _base: u64, offset: u64, data: &mut [u8]) {
|
||||
match offset {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use vmm_sys_util::eventfd::EventFd;
|
||||
#[cfg(feature = "pci_support")]
|
||||
mod pci_common_config;
|
||||
#[cfg(feature = "pci_support")]
|
||||
@@ -17,3 +18,7 @@ mod mmio;
|
||||
pub use mmio::MmioDevice;
|
||||
#[cfg(feature = "mmio_support")]
|
||||
pub const NOTIFY_REG_OFFSET: u32 = 0x50;
|
||||
|
||||
pub trait VirtioTransport {
|
||||
fn ioeventfds(&self, base_addr: u64) -> Vec<(&EventFd, u64)>;
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ use vm_memory::{Address, ByteValued, GuestAddress, GuestMemoryMmap, GuestUsize,
|
||||
use vmm_sys_util::{errno::Result, eventfd::EventFd};
|
||||
|
||||
use super::VirtioPciCommonConfig;
|
||||
use crate::transport::VirtioTransport;
|
||||
use crate::{
|
||||
Queue, VirtioDevice, VirtioDeviceType, VirtioInterrupt, VirtioInterruptType,
|
||||
VirtioIommuRemapping, DEVICE_ACKNOWLEDGE, DEVICE_DRIVER, DEVICE_DRIVER_OK, DEVICE_FAILED,
|
||||
@@ -338,7 +339,7 @@ impl VirtioPciDevice {
|
||||
/// Gets the list of queue events that must be triggered whenever the VM writes to
|
||||
/// `virtio::NOTIFY_REG_OFFSET` past the MMIO base. Each event must be triggered when the
|
||||
/// value being written equals the index of the event in this list.
|
||||
pub fn queue_evts(&self) -> &[EventFd] {
|
||||
fn queue_evts(&self) -> &[EventFd] {
|
||||
self.queue_evts.as_slice()
|
||||
}
|
||||
|
||||
@@ -366,21 +367,6 @@ impl VirtioPciDevice {
|
||||
self.configuration.get_bar_addr(self.settings_bar as usize)
|
||||
}
|
||||
|
||||
pub fn ioeventfds(&self, bar_addr: u64) -> Vec<(&EventFd, u64, u64)> {
|
||||
let notify_base = bar_addr + NOTIFICATION_BAR_OFFSET;
|
||||
self.queue_evts()
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, event)| {
|
||||
(
|
||||
event,
|
||||
notify_base + i as u64 * u64::from(NOTIFY_OFF_MULTIPLIER),
|
||||
i as u64,
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn add_pci_capabilities(
|
||||
&mut self,
|
||||
settings_bar: u8,
|
||||
@@ -452,6 +438,22 @@ impl VirtioPciDevice {
|
||||
}
|
||||
}
|
||||
|
||||
impl VirtioTransport for VirtioPciDevice {
|
||||
fn ioeventfds(&self, base_addr: u64) -> Vec<(&EventFd, u64)> {
|
||||
let notify_base = base_addr + NOTIFICATION_BAR_OFFSET;
|
||||
self.queue_evts()
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, event)| {
|
||||
(
|
||||
event,
|
||||
notify_base + i as u64 * u64::from(NOTIFY_OFF_MULTIPLIER),
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl PciDevice for VirtioPciDevice {
|
||||
fn assign_pin_irq(
|
||||
&mut self,
|
||||
|
||||
Reference in New Issue
Block a user