Skip to content

Commit a2bc456

Browse files
stefano-garzarellajiangliu
authored andcommitted
Add VhostIotlbBackend trait
This new VhostIotlbBackend trait will be implemented by the backends that support IOTLB messages. Add also VhostIotlbMsg struct and related enums to handle IOTLB messages properly. Signed-off-by: Stefano Garzarella <[email protected]>
1 parent 1906686 commit a2bc456

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

src/backend.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,96 @@ pub struct VhostUserDirtyLogRegion {
8585
pub mmap_handle: RawFd,
8686
}
8787

88+
/// Vhost memory access permission (VHOST_ACCESS_* mapping)
89+
#[repr(u8)]
90+
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
91+
pub enum VhostAccess {
92+
/// No access.
93+
No = 0,
94+
/// Read-Only access.
95+
ReadOnly = 1,
96+
/// Write-Only access.
97+
WriteOnly = 2,
98+
/// Read and Write access.
99+
ReadWrite = 3,
100+
}
101+
102+
impl Default for VhostAccess {
103+
fn default() -> Self {
104+
VhostAccess::No
105+
}
106+
}
107+
108+
/// Vhost IOTLB message type (VHOST_IOTLB_* mapping)
109+
#[repr(u8)]
110+
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
111+
pub enum VhostIotlbType {
112+
/// Empty message (not valid).
113+
Empty = 0,
114+
/// I/O virtual address mapping is missing or invalidated.
115+
Miss = 1,
116+
/// Update the I/O virtual address mapping.
117+
Update = 2,
118+
/// Invalidate the I/O virtual address mapping.
119+
Invalidate = 3,
120+
/// Access failed to an I/O virtual address.
121+
AccessFail = 4,
122+
/// Batch of multiple `Update` messages begins.
123+
BatchBegin = 5,
124+
/// Batch of multiple `Update` messages ends.
125+
BatchEnd = 6,
126+
}
127+
128+
impl Default for VhostIotlbType {
129+
fn default() -> Self {
130+
VhostIotlbType::Empty
131+
}
132+
}
133+
134+
/// Vhost IOTLB message structure.
135+
#[derive(Default, Clone, Copy)]
136+
pub struct VhostIotlbMsg {
137+
/// I/O virtual address.
138+
pub iova: u64,
139+
/// Size of the I/O mapping.
140+
pub size: u64,
141+
/// Virtual address in the current process.
142+
pub userspace_addr: u64,
143+
/// Access permissions.
144+
pub perm: VhostAccess,
145+
/// Type of the message.
146+
pub msg_type: VhostIotlbType,
147+
}
148+
149+
/// Vhost IOTLB message parser.
150+
pub trait VhostIotlbMsgParser {
151+
/// Parse the IOTLB message and fill a VhostIotlbMsg.
152+
///
153+
/// # Arguments
154+
/// * `msg` - IOTLB message parsed.
155+
fn parse(&self, msg: &mut VhostIotlbMsg) -> Result<()>;
156+
}
157+
158+
/// An interface for IOTLB messages support for vhost-based backend
159+
pub trait VhostIotlbBackend: std::marker::Sized {
160+
/// Send an IOTLB message to the vhost-based backend.
161+
///
162+
/// # Arguments
163+
/// * `msg` - IOTLB message to send.
164+
fn send_iotlb_msg(&self, msg: &VhostIotlbMsg) -> Result<()>;
165+
166+
/// Parse a buffer received from the vhost-based backend and fill a VhostIotlbMsg.
167+
///
168+
/// # Arguments
169+
/// * `buffer` - Buffer containing the raw data received from the vhost-based backend.
170+
/// * `msg` - IOTLB message parsed.
171+
fn parse_iotlb_msg<T: Sized + VhostIotlbMsgParser>(
172+
&self,
173+
buffer: &T,
174+
msg: &mut VhostIotlbMsg,
175+
) -> Result<()>;
176+
}
177+
88178
/// An interface for setting up vhost-based backend drivers with interior mutability.
89179
///
90180
/// Vhost devices are subset of virtio devices, which improve virtio device's performance by

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ pub enum Error {
5656
InvalidGuestMemory,
5757
/// Invalid guest memory region.
5858
InvalidGuestMemoryRegion,
59+
/// Invalid IOTLB message.
60+
InvalidIotlbMsg,
5961
/// Invalid queue.
6062
InvalidQueue,
6163
/// Invalid descriptor table address.
@@ -85,6 +87,7 @@ impl std::fmt::Display for Error {
8587
Error::InvalidOperation => write!(f, "invalid vhost operations"),
8688
Error::InvalidGuestMemory => write!(f, "invalid guest memory object"),
8789
Error::InvalidGuestMemoryRegion => write!(f, "invalid guest memory region"),
90+
Error::InvalidIotlbMsg => write!(f, "invalid IOTLB message"),
8891
Error::InvalidQueue => write!(f, "invalid virtqueue"),
8992
Error::DescriptorTableAddress => {
9093
write!(f, "invalid virtqueue descriptor table address")
@@ -137,6 +140,10 @@ mod tests {
137140
format!("{}", Error::InvalidGuestMemoryRegion),
138141
"invalid guest memory region"
139142
);
143+
assert_eq!(
144+
format!("{}", Error::InvalidIotlbMsg),
145+
"invalid IOTLB message"
146+
);
140147
assert_eq!(format!("{}", Error::InvalidQueue), "invalid virtqueue");
141148
assert_eq!(
142149
format!("{}", Error::DescriptorTableAddress),

0 commit comments

Comments
 (0)