Skip to content

Commit 52b278c

Browse files
committed
Use desc/split and remove descriptor.rs
Signed-off-by: Wenyu Huang <[email protected]>
1 parent e98df19 commit 52b278c

File tree

9 files changed

+233
-439
lines changed

9 files changed

+233
-439
lines changed

virtio-blk/src/request.rs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use virtio_bindings::bindings::virtio_blk::{
3232
VIRTIO_BLK_T_OUT, VIRTIO_BLK_T_WRITE_ZEROES,
3333
};
3434

35-
use virtio_queue::{Descriptor, DescriptorChain};
35+
use virtio_queue::{desc::{split::Descriptor as SplitDescriptor, Descriptor}, DescriptorChain};
3636
use vm_memory::{ByteValued, Bytes, GuestAddress, GuestMemory, GuestMemoryError};
3737

3838
/// Block request parsing errors.
@@ -162,6 +162,7 @@ impl Request {
162162
where
163163
M: GuestMemory + ?Sized,
164164
{
165+
let desc = SplitDescriptor::from(desc);
165166
// The status MUST always be writable.
166167
if !desc.is_write_only() {
167168
return Err(Error::UnexpectedReadOnlyDescriptor);
@@ -183,6 +184,7 @@ impl Request {
183184

184185
// Checks that a descriptor meets the minimal requirements for a valid data descriptor.
185186
fn check_data_desc(desc: Descriptor, request_type: RequestType) -> Result<()> {
187+
let desc = SplitDescriptor::from(desc);
186188
// We do this check only for the device-readable buffers, as opposed to
187189
// also check that the device doesn't want to read a device-writable buffer
188190
// because this one is not a MUST (the device MAY do that for debugging or
@@ -234,14 +236,14 @@ impl Request {
234236
let mut desc = desc_chain.next().ok_or(Error::DescriptorChainTooShort)?;
235237

236238
while desc.has_next() {
237-
Request::check_data_desc(desc, request.request_type)?;
239+
Request::check_data_desc(Descriptor::from(desc), request.request_type)?;
238240

239241
request.data.push((desc.addr(), desc.len()));
240242
desc = desc_chain.next().ok_or(Error::DescriptorChainTooShort)?;
241243
}
242244
let status_desc = desc;
243245

244-
Request::check_status_desc(desc_chain.memory(), status_desc)?;
246+
Request::check_status_desc(desc_chain.memory(), Descriptor::from(status_desc))?;
245247

246248
request.status_addr = status_desc.addr();
247249
Ok(request)
@@ -297,9 +299,9 @@ mod tests {
297299
// The `build_desc_chain` function will populate the `NEXT` related flags and field.
298300
let v = [
299301
// A device-writable request header descriptor.
300-
Descriptor::new(0x10_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
301-
Descriptor::new(0x20_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
302-
Descriptor::new(0x30_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
302+
Descriptor::from(SplitDescriptor::new(0x10_0000, 0x100, VRING_DESC_F_WRITE as u16, 0)),
303+
Descriptor::from(SplitDescriptor::new(0x20_0000, 0x100, VRING_DESC_F_WRITE as u16, 0)),
304+
Descriptor::from(SplitDescriptor::new(0x30_0000, 0x100, VRING_DESC_F_WRITE as u16, 0)),
303305
];
304306
// Create a queue of max 16 descriptors and a descriptor chain based on the array above.
305307
let queue = MockSplitQueue::new(&mem, 16);
@@ -319,10 +321,10 @@ mod tests {
319321
);
320322

321323
let v = [
322-
Descriptor::new(0x10_0000, 0x100, 0, 0),
323-
Descriptor::new(0x20_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
324+
Descriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
325+
Descriptor::from(SplitDescriptor::new(0x20_0000, 0x100, VRING_DESC_F_WRITE as u16, 0)),
324326
// A device-readable request status descriptor.
325-
Descriptor::new(0x30_0000, 0x100, 0, 0),
327+
Descriptor::from(SplitDescriptor::new(0x30_0000, 0x100, 0, 0)),
326328
];
327329
let mut chain = queue.build_desc_chain(&v[..3]).unwrap();
328330

@@ -333,10 +335,10 @@ mod tests {
333335
);
334336

335337
let v = [
336-
Descriptor::new(0x10_0000, 0x100, 0, 0),
337-
Descriptor::new(0x20_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
338+
Descriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
339+
Descriptor::from(SplitDescriptor::new(0x20_0000, 0x100, VRING_DESC_F_WRITE as u16, 0)),
338340
// Status descriptor with len = 0.
339-
Descriptor::new(0x30_0000, 0x0, VRING_DESC_F_WRITE as u16, 0),
341+
Descriptor::from(SplitDescriptor::new(0x30_0000, 0x0, VRING_DESC_F_WRITE as u16, 0)),
340342
];
341343
let mut chain = queue.build_desc_chain(&v[..3]).unwrap();
342344
assert_eq!(
@@ -345,9 +347,9 @@ mod tests {
345347
);
346348

347349
let v = [
348-
Descriptor::new(0x10_0000, 0x100, 0, 0),
349-
Descriptor::new(0x20_0000, 0x100, 0, 0),
350-
Descriptor::new(0x30_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
350+
Descriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
351+
Descriptor::from(SplitDescriptor::new(0x20_0000, 0x100, 0, 0)),
352+
Descriptor::from(SplitDescriptor::new(0x30_0000, 0x100, VRING_DESC_F_WRITE as u16, 0)),
351353
];
352354
let mut chain = queue.build_desc_chain(&v[..3]).unwrap();
353355

@@ -377,10 +379,10 @@ mod tests {
377379

378380
// Invalid status address.
379381
let v = [
380-
Descriptor::new(0x10_0000, 0x100, 0, 0),
381-
Descriptor::new(0x20_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
382-
Descriptor::new(0x30_0000, 0x200, VRING_DESC_F_WRITE as u16, 0),
383-
Descriptor::new(0x1100_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
382+
Descriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
383+
Descriptor::from(SplitDescriptor::new(0x20_0000, 0x100, VRING_DESC_F_WRITE as u16, 0)),
384+
Descriptor::from(SplitDescriptor::new(0x30_0000, 0x200, VRING_DESC_F_WRITE as u16, 0)),
385+
Descriptor::from(SplitDescriptor::new(0x1100_0000, 0x100, VRING_DESC_F_WRITE as u16, 0)),
384386
];
385387
let req_header = RequestHeader {
386388
request_type: VIRTIO_BLK_T_OUT,
@@ -402,10 +404,10 @@ mod tests {
402404

403405
// Valid descriptor chain for OUT.
404406
let v = [
405-
Descriptor::new(0x10_0000, 0x100, 0, 0),
406-
Descriptor::new(0x20_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
407-
Descriptor::new(0x30_0000, 0x200, VRING_DESC_F_WRITE as u16, 0),
408-
Descriptor::new(0x40_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
407+
Descriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
408+
Descriptor::from(SplitDescriptor::new(0x20_0000, 0x100, VRING_DESC_F_WRITE as u16, 0)),
409+
Descriptor::from(SplitDescriptor::new(0x30_0000, 0x200, VRING_DESC_F_WRITE as u16, 0)),
410+
Descriptor::from(SplitDescriptor::new(0x40_0000, 0x100, VRING_DESC_F_WRITE as u16, 0)),
409411
];
410412
let req_header = RequestHeader {
411413
request_type: VIRTIO_BLK_T_OUT,
@@ -447,8 +449,8 @@ mod tests {
447449

448450
// Valid descriptor chain for FLUSH.
449451
let v = [
450-
Descriptor::new(0x10_0000, 0x100, 0, 0),
451-
Descriptor::new(0x40_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
452+
Descriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
453+
Descriptor::from(SplitDescriptor::new(0x40_0000, 0x100, VRING_DESC_F_WRITE as u16, 0)),
452454
];
453455
let req_header = RequestHeader {
454456
request_type: VIRTIO_BLK_T_FLUSH,

virtio-console/src/console.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ mod tests {
283283
use super::*;
284284
use virtio_bindings::bindings::virtio_ring::VRING_DESC_F_WRITE;
285285
use virtio_queue::mock::MockSplitQueue;
286-
use virtio_queue::Descriptor;
286+
use virtio_queue::desc::{split::Descriptor as SplitDescriptor, Descriptor};
287287
use vm_memory::{GuestAddress, GuestMemoryMmap};
288288

289289
impl PartialEq for Error {
@@ -352,8 +352,8 @@ mod tests {
352352

353353
// One descriptor is write only
354354
let v = [
355-
Descriptor::new(0x1000, INPUT_SIZE, 0, 0),
356-
Descriptor::new(0x2000, INPUT_SIZE, VRING_DESC_F_WRITE as u16, 0),
355+
Descriptor::from(SplitDescriptor::new(0x1000, INPUT_SIZE, 0, 0)),
356+
Descriptor::from(SplitDescriptor::new(0x2000, INPUT_SIZE, VRING_DESC_F_WRITE as u16, 0)),
357357
];
358358

359359
let queue = MockSplitQueue::new(&mem, 16);
@@ -366,8 +366,8 @@ mod tests {
366366

367367
// Descriptor is outside of the memory bounds
368368
let v = [
369-
Descriptor::new(0x0001_0000, INPUT_SIZE, 0, 0),
370-
Descriptor::new(0x0002_0000, INPUT_SIZE, 0, 0),
369+
Descriptor::from(SplitDescriptor::new(0x0001_0000, INPUT_SIZE, 0, 0)),
370+
Descriptor::from(SplitDescriptor::new(0x0002_0000, INPUT_SIZE, 0, 0)),
371371
];
372372
let mut chain = queue.build_desc_chain(&v[..2]).unwrap();
373373
assert_eq!(
@@ -379,8 +379,8 @@ mod tests {
379379

380380
// Test normal functionality.
381381
let v = [
382-
Descriptor::new(0x3000, INPUT_SIZE, 0, 0),
383-
Descriptor::new(0x4000, INPUT_SIZE, 0, 0),
382+
Descriptor::from(SplitDescriptor::new(0x3000, INPUT_SIZE, 0, 0)),
383+
Descriptor::from(SplitDescriptor::new(0x4000, INPUT_SIZE, 0, 0)),
384384
];
385385
let mut chain = queue.build_desc_chain(&v[..2]).unwrap();
386386
mem.write_slice(
@@ -419,8 +419,8 @@ mod tests {
419419

420420
// One descriptor is read only
421421
let v = [
422-
Descriptor::new(0x1000, 0x10, VRING_DESC_F_WRITE as u16, 0),
423-
Descriptor::new(0x2000, INPUT_SIZE, 0, 0),
422+
Descriptor::from(SplitDescriptor::new(0x1000, 0x10, VRING_DESC_F_WRITE as u16, 0)),
423+
Descriptor::from(SplitDescriptor::new(0x2000, INPUT_SIZE, 0, 0)),
424424
];
425425

426426
let queue = MockSplitQueue::new(&mem, 16);
@@ -433,8 +433,8 @@ mod tests {
433433

434434
// Descriptor is out of memory bounds
435435
let v = [
436-
Descriptor::new(0x0001_0000, INPUT_SIZE, VRING_DESC_F_WRITE as u16, 0),
437-
Descriptor::new(0x0002_0000, INPUT_SIZE, VRING_DESC_F_WRITE as u16, 0),
436+
Descriptor::from(SplitDescriptor::new(0x0001_0000, INPUT_SIZE, VRING_DESC_F_WRITE as u16, 0)),
437+
Descriptor::from(SplitDescriptor::new(0x0002_0000, INPUT_SIZE, VRING_DESC_F_WRITE as u16, 0)),
438438
];
439439

440440
let queue = MockSplitQueue::new(&mem, 16);
@@ -455,8 +455,8 @@ mod tests {
455455
.enqueue_data(&mut vec![INPUT_VALUE * 2; INPUT_SIZE as usize])
456456
.unwrap();
457457
let v = [
458-
Descriptor::new(0x3000, INPUT_SIZE, VRING_DESC_F_WRITE as u16, 0),
459-
Descriptor::new(0x4000, INPUT_SIZE, VRING_DESC_F_WRITE as u16, 0),
458+
Descriptor::from(SplitDescriptor::new(0x3000, INPUT_SIZE, VRING_DESC_F_WRITE as u16, 0)),
459+
Descriptor::from(SplitDescriptor::new(0x4000, INPUT_SIZE, VRING_DESC_F_WRITE as u16, 0)),
460460
];
461461

462462
let queue = MockSplitQueue::new(&mem, 16);
@@ -477,12 +477,12 @@ mod tests {
477477
console
478478
.enqueue_data(&mut vec![INPUT_VALUE; 2 * INPUT_SIZE as usize])
479479
.unwrap();
480-
let v = [Descriptor::new(
480+
let v = [Descriptor::from(SplitDescriptor::new(
481481
0x5000,
482482
INPUT_SIZE,
483483
VRING_DESC_F_WRITE as u16,
484484
0,
485-
)];
485+
))];
486486

487487
let queue = MockSplitQueue::new(&mem, 16);
488488
let mut chain = queue.build_desc_chain(&v[..1]).unwrap();
@@ -497,12 +497,12 @@ mod tests {
497497

498498
assert!(!console.is_input_buffer_empty());
499499

500-
let v = [Descriptor::new(
500+
let v = [Descriptor::from(SplitDescriptor::new(
501501
0x6000,
502502
INPUT_SIZE,
503503
VRING_DESC_F_WRITE as u16,
504504
0,
505-
)];
505+
))];
506506
let mut chain = queue.build_desc_chain(&v[..1]).unwrap();
507507

508508
assert_eq!(
@@ -516,12 +516,12 @@ mod tests {
516516
assert!(console.is_input_buffer_empty());
517517

518518
// Input buffer is empty.
519-
let v = [Descriptor::new(
519+
let v = [Descriptor::from(SplitDescriptor::new(
520520
0x7000,
521521
INPUT_SIZE,
522522
VRING_DESC_F_WRITE as u16,
523523
0,
524-
)];
524+
))];
525525
let mut chain = queue.build_desc_chain(&v[..1]).unwrap();
526526

527527
assert_eq!(console.process_receiveq_chain(&mut chain).unwrap(), 0);

0 commit comments

Comments
 (0)