Skip to content

Commit 6c2c4bc

Browse files
author
Sebastien Boeuf
committed
virtio-queue: Add helpers for accessing queue
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 c5ec512 commit 6c2c4bc

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-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/queue_guard.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ where
138138
self.state.avail_idx(self.mem.deref(), order)
139139
}
140140

141+
/// Read the `idx` field from the used ring.
142+
pub fn used_idx(&self, order: Ordering) -> Result<Wrapping<u16>, Error> {
143+
self.state.used_idx(self.mem.deref(), order)
144+
}
145+
141146
/// Put a used descriptor head into the used ring.
142147
pub fn add_used(&mut self, head_index: u16, len: u32) -> Result<(), Error> {
143148
self.state.add_used(self.mem.deref(), head_index, len)
@@ -172,11 +177,21 @@ where
172177
self.state.next_avail()
173178
}
174179

180+
/// Return the index of the next entry in the used ring.
181+
pub fn next_used(&self) -> u16 {
182+
self.state.next_used()
183+
}
184+
175185
/// Set the index of the next entry in the available ring.
176186
pub fn set_next_avail(&mut self, next_avail: u16) {
177187
self.state.set_next_avail(next_avail);
178188
}
179189

190+
/// Set the index of the next entry in the used ring.
191+
pub fn set_next_used(&mut self, next_used: u16) {
192+
self.state.set_next_used(next_used);
193+
}
194+
180195
/// Get a consuming iterator over all available descriptor chain heads offered by the driver.
181196
pub fn iter(&mut self) -> Result<AvailIter<'_, M>, Error> {
182197
self.state.deref_mut().iter(self.mem.clone())

crates/virtio-queue/src/state.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,17 @@ 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
316+
.used_ring
317+
.checked_add(2)
318+
.ok_or(Error::AddressOverflow)?;
319+
320+
mem.load(addr, order)
321+
.map(Wrapping)
322+
.map_err(Error::GuestMemory)
323+
}
324+
314325
fn add_used<M: GuestMemory>(
315326
&mut self,
316327
mem: &M,
@@ -415,7 +426,15 @@ impl QueueStateT for QueueState {
415426
self.next_avail.0
416427
}
417428

429+
fn next_used(&self) -> u16 {
430+
self.next_used.0
431+
}
432+
418433
fn set_next_avail(&mut self, next_avail: u16) {
419434
self.next_avail = Wrapping(next_avail);
420435
}
436+
437+
fn set_next_used(&mut self, next_used: u16) {
438+
self.next_used = Wrapping(next_used);
439+
}
421440
}

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)