Skip to content

Commit 388a575

Browse files
committed
interrupt: introduce traits to manage interrupt sources
Introduce traits InterruptManager and Interrupt to manage interrupt sources for virtual devices. Signed-off-by: Liu Jiang <[email protected]> Signed-off-by: Bin Zha <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 3daea68 commit 388a575

File tree

3 files changed

+379
-0
lines changed

3 files changed

+379
-0
lines changed

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ repository = "https://github.com/rust-vmm/vm-device"
66
license = "Apache-2.0"
77

88
[dependencies]
9+
vmm-sys-util = "~0"
10+
11+
[dev-dependencies]
12+
matches = ">=0"

src/interrupt/mod.rs

+369
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,369 @@
1+
// Copyright (C) 2019-2020 Alibaba Cloud and Red Hat, Inc..
2+
// All rights reserved.
3+
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
//! Traits and Structs to manage interrupt sources for devices.
7+
//!
8+
//! In system programming, an interrupt is a signal to the processor emitted by hardware or
9+
//! software indicating an event that needs immediate attention. An interrupt alerts the processor
10+
//! to a high-priority condition requiring the interruption of the current code the processor is
11+
//! executing. The processor responds by suspending its current activities, saving its state, and
12+
//! executing a function called an interrupt handler (or an interrupt service routine, ISR) to deal
13+
//! with the event. This interruption is temporary, and, after the interrupt handler finishes,
14+
//! unless handling the interrupt has emitted a fatal error, the processor resumes normal
15+
//! activities.
16+
//!
17+
//! Hardware interrupts are used by devices to communicate that they require attention from the
18+
//! operating system, or a bare-metal program running on the CPU if there are no OSes. The act of
19+
//! initiating a hardware interrupt is referred to as an interrupt request (IRQ). Different devices
20+
//! are usually associated with different interrupts using a unique value associated with each
21+
//! interrupt. This makes it possible to know which hardware device caused which interrupts.
22+
//! These interrupt values are often called IRQ lines, or just interrupt lines.
23+
//!
24+
//! Nowadays, IRQ lines is not the only mechanism to deliver device interrupts to processors.
25+
//! MSI [(Message Signaled Interrupt)](https://en.wikipedia.org/wiki/Message_Signaled_Interrupts)
26+
//! is another commonly used alternative in-band method of signaling an interrupt, using special
27+
//! in-band messages to replace traditional out-of-band assertion of dedicated interrupt lines.
28+
//! While more complex to implement in a device, message signaled interrupts have some significant
29+
//! advantages over pin-based out-of-band interrupt signaling. Message signaled interrupts are
30+
//! supported in PCI bus since its version 2.2, and in later available PCI Express bus. Some non-PCI
31+
//! architectures also use message signaled interrupts.
32+
//!
33+
//! While IRQ is a term commonly used by Operating Systems when dealing with hardware
34+
//! interrupts, the IRQ numbers managed by OSes are independent of the ones managed by VMM.
35+
//! For simplicity sake, the term `Interrupt Source` is used instead of IRQ to represent both pin-based
36+
//! interrupts and MSI interrupts.
37+
//!
38+
//! A device may support multiple types of interrupts, and each type of interrupt may support one
39+
//! or multiple interrupt sources. For example, a PCI device may support:
40+
//! * Legacy Irq: exactly one interrupt source.
41+
//! * PCI MSI Irq: 1,2,4,8,16,32 interrupt sources.
42+
//! * PCI MSIx Irq: 2^n(n=0-11) interrupt sources.
43+
//!
44+
//! A distinct Interrupt Source Identifier (ISID) will be assigned to each interrupt source.
45+
//! An ID allocator will be used to allocate and free Interrupt Source Identifiers for devices.
46+
//! To decouple the vm-device crate from the ID allocator, the vm-device crate doesn't take the
47+
//! responsibility to allocate/free Interrupt Source IDs but only makes use of assigned IDs.
48+
49+
use std::fmt::{self, Display};
50+
use std::io;
51+
use std::ops::Deref;
52+
use std::ops::Index;
53+
use vmm_sys_util::eventfd::EventFd;
54+
55+
/// Errors associated with handling interrupts
56+
#[derive(Debug)]
57+
pub enum Error {
58+
/// Operation not supported for this interrupt.
59+
OperationNotSupported,
60+
61+
/// The specified configuration is not valid.
62+
InvalidConfiguration,
63+
64+
/// The interrupt was not enabled.
65+
InterruptNotEnabled,
66+
67+
/// Generic IO error,
68+
IOError(io::Error),
69+
}
70+
71+
/// Reuse std::io::Result to simplify interoperability among crates.
72+
pub type Result<T> = std::result::Result<T, Error>;
73+
74+
impl std::error::Error for Error {}
75+
76+
impl Display for Error {
77+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
78+
write!(f, "Interrupt error: ")?;
79+
match self {
80+
Error::OperationNotSupported => write!(f, "operation not supported"),
81+
Error::InvalidConfiguration => write!(f, "invalid configuration"),
82+
Error::InterruptNotEnabled => write!(f, "the interrupt was not enabled"),
83+
Error::IOError(error) => write!(f, "{}", error),
84+
}
85+
}
86+
}
87+
88+
/// Data type to store an interrupt source identifier.
89+
pub type InterruptIndex = u32;
90+
91+
pub trait Interrupt {
92+
/// Type of configuration information for interrupt source.
93+
type C;
94+
95+
/// Enable generation of interrupts from this interrupt source.
96+
fn enable(&self, config: &Self::C) -> Result<()> {
97+
self.update(config)
98+
}
99+
100+
/// Disable generation of interrupts from this interrupt source.
101+
fn disable(&self) -> Result<()> {
102+
Err(Error::OperationNotSupported)
103+
}
104+
105+
/// Update configuration of the interrupt.
106+
fn update(&self, _config: &Self::C) -> Result<()> {
107+
Err(Error::OperationNotSupported)
108+
}
109+
110+
/// Returns an interrupt notifier from this interrupt.
111+
///
112+
/// An interrupt notifier allows for external components and processes
113+
/// to inject interrupts into a guest, by writing to the file returned
114+
/// by this method.
115+
fn notifier(&self) -> Option<&EventFd> {
116+
None
117+
}
118+
119+
/// Inject an interrupt from this interrupt source into the guest.
120+
fn trigger(&self) -> Result<()>;
121+
}
122+
123+
pub trait MSIInterrupt: Interrupt {
124+
/// Mask the interrupt. Masked interrupts are remembered but
125+
/// not delivered.
126+
fn mask(&self) -> Result<()>;
127+
128+
/// Unmask the interrupt, delivering it if it was pending.
129+
fn unmask(&self) -> Result<()>;
130+
131+
/// Check whether there are pending interrupts.
132+
fn is_pending(&self) -> bool;
133+
}
134+
135+
/// Trait to manage a group of interrupt sources for a device.
136+
///
137+
/// A device may support several types of interrupts, and each type of interrupt may contain one or
138+
/// multiple continuous interrupt sources. For example, a PCI device may concurrently support:
139+
/// * Legacy Irq: exactly one interrupt source.
140+
/// * PCI MSI Irq: 1,2,4,8,16,32 interrupt sources.
141+
/// * PCI MSIx Irq: 2^n(n=0-11) interrupt sources.
142+
///
143+
/// PCI MSI interrupts of a device may not be configured individually, and must configured as a
144+
/// whole block. So all interrupts of the same type of a device are abstracted as an
145+
/// [InterruptSourceGroup](struct.InterruptSourceGroup.html) object, instead of abstracting each
146+
/// interrupt source as a distinct Interrupt.
147+
pub struct InterruptSourceGroup<I: Interrupt> {
148+
vec: Vec<I>,
149+
}
150+
151+
impl<I: Interrupt> InterruptSourceGroup<I> {
152+
/// Create a new interrupt source group from the given interrupts.
153+
pub fn from_interrupts(interrupts: Vec<I>) -> Self {
154+
Self { vec: interrupts }
155+
}
156+
157+
/// Return whether the group manages no interrupts.
158+
pub fn is_empty(&self) -> bool {
159+
self.vec.is_empty()
160+
}
161+
162+
/// Get number of interrupt sources managed by the group.
163+
pub fn len(&self) -> InterruptIndex {
164+
self.vec.len() as u32
165+
}
166+
167+
/// Enable the interrupt sources in the group to generate interrupts.
168+
///
169+
/// The `enable()` should be invoked before invoking other methods to manipulate the
170+
/// `InterruptSourceGroup` object.
171+
pub fn enable(&self, configs: &[I::C]) -> Result<()> {
172+
for (int, config) in self.vec.iter().zip(configs.iter()) {
173+
int.enable(config)?;
174+
}
175+
Ok(())
176+
}
177+
178+
/// Disable the interrupt sources in the group to generate interrupts.
179+
pub fn disable(&self) -> Result<()> {
180+
for int in self.vec.iter() {
181+
int.disable()?;
182+
}
183+
Ok(())
184+
}
185+
186+
/// Return the index-th interrupt in the group, or `None` if the index is out
187+
/// of bounds.
188+
pub fn get(&self, index: InterruptIndex) -> Option<&I> {
189+
self.vec.get(index as usize)
190+
}
191+
}
192+
193+
impl<I: Interrupt> Index<InterruptIndex> for InterruptSourceGroup<I> {
194+
type Output = I;
195+
fn index(&self, index: u32) -> &Self::Output {
196+
&self.vec[index as usize]
197+
}
198+
}
199+
200+
/// Trait to manage interrupt sources for virtual device backends.
201+
///
202+
/// The InterruptManager implementations should protect itself from concurrent accesses internally,
203+
/// so it could be invoked from multi-threaded context.
204+
pub trait InterruptManager {
205+
/// Interrupt type used by these sources.
206+
type I: Interrupt;
207+
208+
/// Type returned by create_group(). It will usually be either a simple reference
209+
/// to an interrupt source group, or a reference-counted wrapper.
210+
type G: Deref<Target = InterruptSourceGroup<Self::I>>;
211+
212+
/// Configuration used to create a group, for example a (base, count) pair
213+
/// or even () if no configuration is needed (such as for PCI legacy interrupts).
214+
type GroupConfig;
215+
216+
/// Create an [InterruptSourceGroup](struct.InterruptSourceGroup.html) object to manage
217+
/// interrupt sources for a virtual device
218+
///
219+
/// An [InterruptSourceGroup](struct.InterruptSourceGroup.html) object manages all interrupt
220+
/// sources of the same type for a virtual device.
221+
///
222+
/// # Arguments
223+
/// * config: The interrupt group configuration
224+
fn create_group(&self, config: Self::GroupConfig) -> Result<Self::G>;
225+
}
226+
227+
#[cfg(test)]
228+
mod tests {
229+
use super::*;
230+
231+
use std::cell::Cell;
232+
use std::rc::Rc;
233+
234+
use matches::assert_matches;
235+
use vmm_sys_util::eventfd::{EventFd, EFD_NONBLOCK};
236+
237+
struct MockInterrupt {
238+
enabled: Cell<bool>,
239+
eventfd: EventFd,
240+
index: InterruptIndex,
241+
}
242+
243+
impl MockInterrupt {
244+
fn enabled(&self) -> bool {
245+
self.enabled.get()
246+
}
247+
}
248+
249+
impl Interrupt for MockInterrupt {
250+
type C = InterruptIndex;
251+
252+
fn enable(&self, config: &Self::C) -> Result<()> {
253+
self.enabled.set(true);
254+
self.update(config)
255+
}
256+
257+
fn disable(&self) -> Result<()> {
258+
self.enabled.set(false);
259+
Ok(())
260+
}
261+
262+
fn update(&self, config: &Self::C) -> Result<()> {
263+
if !self.enabled() {
264+
Err(Error::InterruptNotEnabled)
265+
} else if *config != self.index {
266+
Err(Error::InvalidConfiguration)
267+
} else {
268+
Ok(())
269+
}
270+
}
271+
272+
fn trigger(&self) -> Result<()> {
273+
self.notifier()
274+
.ok_or(Error::InterruptNotEnabled)?
275+
.write(1)
276+
.map_err(Error::IOError)
277+
}
278+
279+
fn notifier(&self) -> Option<&EventFd> {
280+
if !self.enabled() {
281+
None
282+
} else {
283+
Some(&self.eventfd)
284+
}
285+
}
286+
}
287+
288+
struct MockInterruptManager;
289+
impl InterruptManager for MockInterruptManager {
290+
type I = MockInterrupt;
291+
type G = Rc<InterruptSourceGroup<Self::I>>;
292+
type GroupConfig = u32;
293+
294+
fn create_group(
295+
&self,
296+
config: Self::GroupConfig,
297+
) -> Result<Rc<InterruptSourceGroup<Self::I>>> {
298+
let ints: Vec<_> = (0..config)
299+
.map(|index| MockInterrupt {
300+
enabled: Cell::new(false),
301+
eventfd: EventFd::new(EFD_NONBLOCK).unwrap(),
302+
index,
303+
})
304+
.collect();
305+
Ok(Rc::new(InterruptSourceGroup::from_interrupts(ints)))
306+
}
307+
}
308+
309+
#[test]
310+
fn create_group() {
311+
let mgr = MockInterruptManager;
312+
let grp = mgr.create_group(1).unwrap();
313+
assert_eq!(1, grp.len());
314+
}
315+
316+
#[test]
317+
fn enable_succeeds() {
318+
let mgr = MockInterruptManager;
319+
let configs = &vec![0, 1, 2];
320+
let grp = mgr.create_group(3).unwrap();
321+
assert!(grp.enable(configs).is_ok());
322+
}
323+
324+
#[test]
325+
fn enable_fails() {
326+
let mgr = MockInterruptManager;
327+
let configs = &vec![0, 1, 3];
328+
let grp = mgr.create_group(3).unwrap();
329+
assert_matches!(grp.enable(configs), Err(Error::InvalidConfiguration));
330+
}
331+
332+
#[test]
333+
fn disable() {
334+
let mgr = MockInterruptManager;
335+
let configs = &vec![0];
336+
let grp = mgr.create_group(1).unwrap();
337+
assert!(grp[0].notifier().is_none());
338+
assert_matches!(grp[0].trigger(), Err(Error::InterruptNotEnabled));
339+
assert!(grp.enable(configs).is_ok());
340+
assert!(grp[0].notifier().is_some());
341+
assert!(grp.disable().is_ok());
342+
assert!(grp[0].notifier().is_none());
343+
assert_matches!(grp[0].trigger(), Err(Error::InterruptNotEnabled));
344+
}
345+
346+
#[test]
347+
fn notifier_and_trigger() {
348+
let mgr = MockInterruptManager;
349+
let configs = &vec![0];
350+
let grp = mgr.create_group(1).unwrap();
351+
assert!(grp.enable(configs).is_ok());
352+
let eventfd = grp[0].notifier().unwrap();
353+
assert_eq!(
354+
eventfd.read().unwrap_err().kind(),
355+
io::ErrorKind::WouldBlock
356+
);
357+
assert!(grp[0].trigger().is_ok());
358+
assert_eq!(eventfd.read().unwrap(), 1);
359+
}
360+
361+
#[test]
362+
fn get() {
363+
let mgr = MockInterruptManager;
364+
let grp = mgr.create_group(2).unwrap();
365+
assert_eq!(grp.get(0).unwrap().index, 0);
366+
assert_eq!(grp.get(1).unwrap().index, 1);
367+
assert!(grp.get(2).is_none());
368+
}
369+
}

src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33

44
//! rust-vmm device model.
55
6+
extern crate vmm_sys_util;
7+
8+
#[cfg(test)]
9+
extern crate matches;
10+
611
use std::cmp::{Ord, Ordering, PartialOrd};
712

813
pub mod device_manager;
14+
pub mod interrupt;
915
pub mod resources;
1016

1117
// IO Size.

0 commit comments

Comments
 (0)