Skip to content

Commit a32c2a3

Browse files
author
Sebastien Boeuf
committed
virtio-queue: Add helpers for accessing queue information
These helpers are meant to help crate's consumers getting and setting information about the queue. Signed-off-by: Sebastien Boeuf <[email protected]>
1 parent 26fcb56 commit a32c2a3

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

crates/virtio-queue/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ pub trait QueueStateT: for<'a> QueueStateGuard<'a> {
154154
/// Read the `idx` field from the available ring.
155155
fn avail_idx<M: GuestMemory>(&self, mem: &M, order: Ordering) -> Result<Wrapping<u16>, Error>;
156156

157+
/// Read the `idx` field from the used ring.
158+
fn used_idx<M: GuestMemory>(&self, mem: &M, order: Ordering) -> Result<Wrapping<u16>, Error>;
159+
157160
/// Put a used descriptor head into the used ring.
158161
fn add_used<M: GuestMemory>(&mut self, mem: &M, head_index: u16, len: u32)
159162
-> Result<(), Error>;
@@ -179,6 +182,12 @@ pub trait QueueStateT: for<'a> QueueStateGuard<'a> {
179182
/// Return the index of the next entry in the available ring.
180183
fn next_avail(&self) -> u16;
181184

185+
/// Return the index for the next descriptor in the used ring.
186+
fn next_used(&self) -> u16;
187+
182188
/// Set the index of the next entry in the available ring.
183189
fn set_next_avail(&mut self, next_avail: u16);
190+
191+
/// Set the index for the next descriptor in the used ring.
192+
fn set_next_used(&mut self, next_used: u16);
184193
}

crates/virtio-queue/src/queue.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ impl<M: GuestAddressSpace, S: QueueStateT> Queue<M, S> {
197197
self.state.avail_idx(self.mem.memory().deref(), order)
198198
}
199199

200+
/// Reads the `idx` field from the used ring.
201+
///
202+
/// # Arguments
203+
/// * `order` - the memory ordering used to access the `idx` field from memory.
204+
pub fn used_idx(&self, order: Ordering) -> Result<Wrapping<u16>, Error> {
205+
self.state.used_idx(self.mem.memory().deref(), order)
206+
}
207+
200208
/// Put a used descriptor head into the used ring.
201209
///
202210
/// # Arguments
@@ -236,13 +244,26 @@ impl<M: GuestAddressSpace, S: QueueStateT> Queue<M, S> {
236244
self.state.next_avail()
237245
}
238246

247+
/// Returns the index for the next descriptor in the used ring.
248+
pub fn next_used(&self) -> u16 {
249+
self.state.next_used()
250+
}
251+
239252
/// Set the index of the next entry in the available ring.
240253
///
241254
/// # Arguments
242255
/// * `next_avail` - the index of the next available ring entry.
243256
pub fn set_next_avail(&mut self, next_avail: u16) {
244257
self.state.set_next_avail(next_avail);
245258
}
259+
260+
/// Sets the index for the next descriptor in the used ring.
261+
///
262+
/// # Arguments
263+
/// * `next_used` - the index of the next used ring entry.
264+
pub fn set_next_used(&mut self, next_used: u16) {
265+
self.state.set_next_used(next_used);
266+
}
246267
}
247268

248269
impl<M: GuestAddressSpace> Queue<M, QueueState> {

crates/virtio-queue/src/state.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,14 @@ impl QueueStateT for QueueState {
311311
.map_err(Error::GuestMemory)
312312
}
313313

314+
fn used_idx<M: GuestMemory>(&self, mem: &M, order: Ordering) -> Result<Wrapping<u16>, Error> {
315+
let addr = self.used_ring.unchecked_add(2);
316+
317+
mem.load(addr, order)
318+
.map(Wrapping)
319+
.map_err(Error::GuestMemory)
320+
}
321+
314322
fn add_used<M: GuestMemory>(
315323
&mut self,
316324
mem: &M,
@@ -415,7 +423,15 @@ impl QueueStateT for QueueState {
415423
self.next_avail.0
416424
}
417425

426+
fn next_used(&self) -> u16 {
427+
self.next_used.0
428+
}
429+
418430
fn set_next_avail(&mut self, next_avail: u16) {
419431
self.next_avail = Wrapping(next_avail);
420432
}
433+
434+
fn set_next_used(&mut self, next_used: u16) {
435+
self.next_used = Wrapping(next_used);
436+
}
421437
}

crates/virtio-queue/src/state_sync.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ impl QueueStateT for QueueStateSync {
107107
self.lock_state().avail_idx(mem, order)
108108
}
109109

110+
fn used_idx<M: GuestMemory>(&self, mem: &M, order: Ordering) -> Result<Wrapping<u16>, Error> {
111+
self.lock_state().used_idx(mem, order)
112+
}
113+
110114
fn add_used<M: GuestMemory>(
111115
&mut self,
112116
mem: &M,
@@ -132,9 +136,17 @@ impl QueueStateT for QueueStateSync {
132136
self.lock_state().next_avail()
133137
}
134138

139+
fn next_used(&self) -> u16 {
140+
self.lock_state().next_used()
141+
}
142+
135143
fn set_next_avail(&mut self, next_avail: u16) {
136144
self.lock_state().set_next_avail(next_avail);
137145
}
146+
147+
fn set_next_used(&mut self, next_used: u16) {
148+
self.lock_state().set_next_used(next_used);
149+
}
138150
}
139151

140152
#[cfg(test)]

0 commit comments

Comments
 (0)