Skip to content

Commit a752c47

Browse files
committed
tests: switch to typed ring accessors, remove manual endianness conversions
Refactors all test and mock code to use the new typed accessor helpers on SplitQueueRing. Removes manual calls to to_le/from_le at call sites, and eliminates direct use of .idx(), .ring(), and similar raw field accessors. Signed-off-by: Nelson Wong <[email protected]>
1 parent 18980df commit a752c47

File tree

3 files changed

+18
-23
lines changed

3 files changed

+18
-23
lines changed

virtio-queue/src/desc/split.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ use virtio_bindings::bindings::virtio_ring::{
3737
/// # let desc = RawDescriptor::from(SplitDescriptor::new(0x2000, 0x1000, VRING_DESC_F_WRITE as u16, 0));
3838
/// # vq.desc_table().store(1, desc);
3939
/// #
40-
/// # vq.avail().ring().ref_at(0).unwrap().store(u16::to_le(0));
41-
/// # vq.avail().idx().store(u16::to_le(1));
40+
/// # vq.avail().store_ring_entry(0, 0).unwrap();
41+
/// # vq.avail().store_idx(1);
4242
/// # q
4343
/// # }
4444
/// let m = &GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0), 0x10000)]).unwrap();

virtio-queue/src/mock.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ impl<'a, M: GuestMemory, T: RingAccess> SplitQueueRing<'a, M, T> {
192192
.checked_add(self.ring.len as GuestUsize)
193193
.unwrap()
194194
}
195-
195+
196196
/// Load the value of the `flags` field.
197197
pub fn load_flags(&self) -> u16 {
198198
u16::from_le(self.flags.load())
@@ -215,16 +215,12 @@ impl<'a, M: GuestMemory, T: RingAccess> SplitQueueRing<'a, M, T> {
215215

216216
/// Load a ring entry at `index`.
217217
pub fn load_ring_entry(&self, index: usize) -> Result<T, MockError> {
218-
self.ring
219-
.ref_at(index)
220-
.map(|r| T::from_le(r.load()))
218+
self.ring.ref_at(index).map(|r| T::from_le(r.load()))
221219
}
222220

223221
/// Store a ring entry at `index`.
224222
pub fn store_ring_entry(&self, index: usize, val: T) -> Result<(), MockError> {
225-
self.ring
226-
.ref_at(index)
227-
.map(|r| r.store(val.to_le()))
223+
self.ring.ref_at(index).map(|r| r.store(val.to_le()))
228224
}
229225

230226
/// Load the value of the event field.
@@ -236,7 +232,6 @@ impl<'a, M: GuestMemory, T: RingAccess> SplitQueueRing<'a, M, T> {
236232
pub fn store_event(&self, val: u16) {
237233
self.event.store(u16::to_le(val))
238234
}
239-
240235
}
241236

242237
/// The available ring is used by the driver to offer buffers to the device.

virtio-queue/src/queue.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -871,19 +871,19 @@ mod tests {
871871
let mut q: Queue = vq.create_queue().unwrap();
872872

873873
assert_eq!(q.used_idx(mem, Ordering::Acquire).unwrap(), Wrapping(0));
874-
assert_eq!(u16::from_le(vq.used().idx().load()), 0);
874+
assert_eq!(vq.used().load_idx(), 0);
875875

876876
// index too large
877877
assert!(q.add_used(mem, 16, 0x1000).is_err());
878-
assert_eq!(u16::from_le(vq.used().idx().load()), 0);
878+
assert_eq!(vq.used().load_idx(), 0);
879879

880880
// should be ok
881881
q.add_used(mem, 1, 0x1000).unwrap();
882882
assert_eq!(q.next_used, Wrapping(1));
883883
assert_eq!(q.used_idx(mem, Ordering::Acquire).unwrap(), Wrapping(1));
884-
assert_eq!(u16::from_le(vq.used().idx().load()), 1);
884+
assert_eq!(vq.used().load_idx(), 1);
885885

886-
let x = vq.used().ring().ref_at(0).unwrap().load();
886+
let x = vq.used().load_ring_entry(0).unwrap();
887887
assert_eq!(x.id(), 1);
888888
assert_eq!(x.len(), 0x1000);
889889
}
@@ -1075,7 +1075,7 @@ mod tests {
10751075
// Update the index of the chain that can be consumed to not be the last one.
10761076
// This enables us to consume chains in multiple iterations as opposed to consuming
10771077
// all the driver written chains at once.
1078-
vq.avail().idx().store(u16::to_le(2));
1078+
vq.avail().store_idx(2);
10791079
// No descriptor chains are consumed at this point.
10801080
assert_eq!(q.next_avail(), 0);
10811081

@@ -1108,7 +1108,7 @@ mod tests {
11081108
assert_eq!(q.next_avail(), 2);
11091109
assert_eq!(q.next_used(), 2);
11101110
// Let the device know it can consume one more chain.
1111-
vq.avail().idx().store(u16::to_le(3));
1111+
vq.avail().store_idx(3);
11121112
i = 0;
11131113

11141114
loop {
@@ -1132,7 +1132,7 @@ mod tests {
11321132
// ring. Ideally this should be done on a separate thread.
11331133
// Because of this update, the loop should be iterated again to consume the new
11341134
// available descriptor chains.
1135-
vq.avail().idx().store(u16::to_le(4));
1135+
vq.avail().store_idx(4);
11361136
if !q.enable_notification(mem).unwrap() {
11371137
break;
11381138
}
@@ -1144,7 +1144,7 @@ mod tests {
11441144

11451145
// Set an `idx` that is bigger than the number of entries added in the ring.
11461146
// This is an allowed scenario, but the indexes of the chain will have unexpected values.
1147-
vq.avail().idx().store(u16::to_le(7));
1147+
vq.avail().store_idx(7);
11481148
loop {
11491149
q.disable_notification(mem).unwrap();
11501150

@@ -1199,7 +1199,7 @@ mod tests {
11991199

12001200
vq.add_desc_chains(&descs, 0).unwrap();
12011201
// Let the device know it can consume chains with the index < 2.
1202-
vq.avail().idx().store(u16::to_le(3));
1202+
vq.avail().store_idx(3);
12031203
// No descriptor chains are consumed at this point.
12041204
assert_eq!(q.next_avail(), 0);
12051205
assert_eq!(q.next_used(), 0);
@@ -1232,7 +1232,7 @@ mod tests {
12321232

12331233
// Decrement `idx` which should be forbidden. We don't enforce this thing, but we should
12341234
// test that we don't panic in case the driver decrements it.
1235-
vq.avail().idx().store(u16::to_le(1));
1235+
vq.avail().store_idx(1);
12361236
// Invalid available ring index
12371237
assert!(q.iter(mem).is_err());
12381238
}
@@ -1269,16 +1269,16 @@ mod tests {
12691269
// When the number of chains exposed by the driver is equal to or less than the queue
12701270
// size, the available ring index is valid and constructs an iterator successfully.
12711271
let avail_idx = Wrapping(q.next_avail()) + Wrapping(queue_size);
1272-
vq.avail().idx().store(u16::to_le(avail_idx.0));
1272+
vq.avail().store_idx(avail_idx.0);
12731273
assert!(q.iter(mem).is_ok());
12741274
let avail_idx = Wrapping(q.next_avail()) + Wrapping(queue_size - 1);
1275-
vq.avail().idx().store(u16::to_le(avail_idx.0));
1275+
vq.avail().store_idx(avail_idx.0);
12761276
assert!(q.iter(mem).is_ok());
12771277

12781278
// When the number of chains exposed by the driver is larger than the queue size, the
12791279
// available ring index is invalid and produces an error from constructing an iterator.
12801280
let avail_idx = Wrapping(q.next_avail()) + Wrapping(queue_size + 1);
1281-
vq.avail().idx().store(u16::to_le(avail_idx.0));
1281+
vq.avail().store_idx(avail_idx.0);
12821282
assert!(q.iter(mem).is_err());
12831283
}
12841284

0 commit comments

Comments
 (0)