Skip to content

Commit d059319

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

File tree

9 files changed

+468
-452
lines changed

9 files changed

+468
-452
lines changed

virtio-blk/src/request.rs

Lines changed: 100 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ 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::{
36+
desc::{split::Descriptor as SplitDescriptor, Descriptor},
37+
DescriptorChain,
38+
};
3639
use vm_memory::{ByteValued, Bytes, GuestAddress, GuestMemory, GuestMemoryError};
3740

3841
/// Block request parsing errors.
@@ -162,6 +165,7 @@ impl Request {
162165
where
163166
M: GuestMemory + ?Sized,
164167
{
168+
let desc = SplitDescriptor::from(desc);
165169
// The status MUST always be writable.
166170
if !desc.is_write_only() {
167171
return Err(Error::UnexpectedReadOnlyDescriptor);
@@ -183,6 +187,7 @@ impl Request {
183187

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

236241
while desc.has_next() {
237-
Request::check_data_desc(desc, request.request_type)?;
242+
Request::check_data_desc(Descriptor::from(desc), request.request_type)?;
238243

239244
request.data.push((desc.addr(), desc.len()));
240245
desc = desc_chain.next().ok_or(Error::DescriptorChainTooShort)?;
241246
}
242247
let status_desc = desc;
243248

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

246251
request.status_addr = status_desc.addr();
247252
Ok(request)
@@ -297,9 +302,24 @@ mod tests {
297302
// The `build_desc_chain` function will populate the `NEXT` related flags and field.
298303
let v = [
299304
// 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),
305+
Descriptor::from(SplitDescriptor::new(
306+
0x10_0000,
307+
0x100,
308+
VRING_DESC_F_WRITE as u16,
309+
0,
310+
)),
311+
Descriptor::from(SplitDescriptor::new(
312+
0x20_0000,
313+
0x100,
314+
VRING_DESC_F_WRITE as u16,
315+
0,
316+
)),
317+
Descriptor::from(SplitDescriptor::new(
318+
0x30_0000,
319+
0x100,
320+
VRING_DESC_F_WRITE as u16,
321+
0,
322+
)),
303323
];
304324
// Create a queue of max 16 descriptors and a descriptor chain based on the array above.
305325
let queue = MockSplitQueue::new(&mem, 16);
@@ -319,10 +339,15 @@ mod tests {
319339
);
320340

321341
let v = [
322-
Descriptor::new(0x10_0000, 0x100, 0, 0),
323-
Descriptor::new(0x20_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
342+
Descriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
343+
Descriptor::from(SplitDescriptor::new(
344+
0x20_0000,
345+
0x100,
346+
VRING_DESC_F_WRITE as u16,
347+
0,
348+
)),
324349
// A device-readable request status descriptor.
325-
Descriptor::new(0x30_0000, 0x100, 0, 0),
350+
Descriptor::from(SplitDescriptor::new(0x30_0000, 0x100, 0, 0)),
326351
];
327352
let mut chain = queue.build_desc_chain(&v[..3]).unwrap();
328353

@@ -333,10 +358,20 @@ mod tests {
333358
);
334359

335360
let v = [
336-
Descriptor::new(0x10_0000, 0x100, 0, 0),
337-
Descriptor::new(0x20_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
361+
Descriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
362+
Descriptor::from(SplitDescriptor::new(
363+
0x20_0000,
364+
0x100,
365+
VRING_DESC_F_WRITE as u16,
366+
0,
367+
)),
338368
// Status descriptor with len = 0.
339-
Descriptor::new(0x30_0000, 0x0, VRING_DESC_F_WRITE as u16, 0),
369+
Descriptor::from(SplitDescriptor::new(
370+
0x30_0000,
371+
0x0,
372+
VRING_DESC_F_WRITE as u16,
373+
0,
374+
)),
340375
];
341376
let mut chain = queue.build_desc_chain(&v[..3]).unwrap();
342377
assert_eq!(
@@ -345,9 +380,14 @@ mod tests {
345380
);
346381

347382
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),
383+
Descriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
384+
Descriptor::from(SplitDescriptor::new(0x20_0000, 0x100, 0, 0)),
385+
Descriptor::from(SplitDescriptor::new(
386+
0x30_0000,
387+
0x100,
388+
VRING_DESC_F_WRITE as u16,
389+
0,
390+
)),
351391
];
352392
let mut chain = queue.build_desc_chain(&v[..3]).unwrap();
353393

@@ -377,10 +417,25 @@ mod tests {
377417

378418
// Invalid status address.
379419
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),
420+
Descriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
421+
Descriptor::from(SplitDescriptor::new(
422+
0x20_0000,
423+
0x100,
424+
VRING_DESC_F_WRITE as u16,
425+
0,
426+
)),
427+
Descriptor::from(SplitDescriptor::new(
428+
0x30_0000,
429+
0x200,
430+
VRING_DESC_F_WRITE as u16,
431+
0,
432+
)),
433+
Descriptor::from(SplitDescriptor::new(
434+
0x1100_0000,
435+
0x100,
436+
VRING_DESC_F_WRITE as u16,
437+
0,
438+
)),
384439
];
385440
let req_header = RequestHeader {
386441
request_type: VIRTIO_BLK_T_OUT,
@@ -402,10 +457,25 @@ mod tests {
402457

403458
// Valid descriptor chain for OUT.
404459
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),
460+
Descriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
461+
Descriptor::from(SplitDescriptor::new(
462+
0x20_0000,
463+
0x100,
464+
VRING_DESC_F_WRITE as u16,
465+
0,
466+
)),
467+
Descriptor::from(SplitDescriptor::new(
468+
0x30_0000,
469+
0x200,
470+
VRING_DESC_F_WRITE as u16,
471+
0,
472+
)),
473+
Descriptor::from(SplitDescriptor::new(
474+
0x40_0000,
475+
0x100,
476+
VRING_DESC_F_WRITE as u16,
477+
0,
478+
)),
409479
];
410480
let req_header = RequestHeader {
411481
request_type: VIRTIO_BLK_T_OUT,
@@ -447,8 +517,13 @@ mod tests {
447517

448518
// Valid descriptor chain for FLUSH.
449519
let v = [
450-
Descriptor::new(0x10_0000, 0x100, 0, 0),
451-
Descriptor::new(0x40_0000, 0x100, VRING_DESC_F_WRITE as u16, 0),
520+
Descriptor::from(SplitDescriptor::new(0x10_0000, 0x100, 0, 0)),
521+
Descriptor::from(SplitDescriptor::new(
522+
0x40_0000,
523+
0x100,
524+
VRING_DESC_F_WRITE as u16,
525+
0,
526+
)),
452527
];
453528
let req_header = RequestHeader {
454529
request_type: VIRTIO_BLK_T_FLUSH,

virtio-console/src/console.rs

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

289289
impl PartialEq for Error {
@@ -352,8 +352,13 @@ 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(
357+
0x2000,
358+
INPUT_SIZE,
359+
VRING_DESC_F_WRITE as u16,
360+
0,
361+
)),
357362
];
358363

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

367372
// Descriptor is outside of the memory bounds
368373
let v = [
369-
Descriptor::new(0x0001_0000, INPUT_SIZE, 0, 0),
370-
Descriptor::new(0x0002_0000, INPUT_SIZE, 0, 0),
374+
Descriptor::from(SplitDescriptor::new(0x0001_0000, INPUT_SIZE, 0, 0)),
375+
Descriptor::from(SplitDescriptor::new(0x0002_0000, INPUT_SIZE, 0, 0)),
371376
];
372377
let mut chain = queue.build_desc_chain(&v[..2]).unwrap();
373378
assert_eq!(
@@ -379,8 +384,8 @@ mod tests {
379384

380385
// Test normal functionality.
381386
let v = [
382-
Descriptor::new(0x3000, INPUT_SIZE, 0, 0),
383-
Descriptor::new(0x4000, INPUT_SIZE, 0, 0),
387+
Descriptor::from(SplitDescriptor::new(0x3000, INPUT_SIZE, 0, 0)),
388+
Descriptor::from(SplitDescriptor::new(0x4000, INPUT_SIZE, 0, 0)),
384389
];
385390
let mut chain = queue.build_desc_chain(&v[..2]).unwrap();
386391
mem.write_slice(
@@ -419,8 +424,13 @@ mod tests {
419424

420425
// One descriptor is read only
421426
let v = [
422-
Descriptor::new(0x1000, 0x10, VRING_DESC_F_WRITE as u16, 0),
423-
Descriptor::new(0x2000, INPUT_SIZE, 0, 0),
427+
Descriptor::from(SplitDescriptor::new(
428+
0x1000,
429+
0x10,
430+
VRING_DESC_F_WRITE as u16,
431+
0,
432+
)),
433+
Descriptor::from(SplitDescriptor::new(0x2000, INPUT_SIZE, 0, 0)),
424434
];
425435

426436
let queue = MockSplitQueue::new(&mem, 16);
@@ -433,8 +443,18 @@ mod tests {
433443

434444
// Descriptor is out of memory bounds
435445
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),
446+
Descriptor::from(SplitDescriptor::new(
447+
0x0001_0000,
448+
INPUT_SIZE,
449+
VRING_DESC_F_WRITE as u16,
450+
0,
451+
)),
452+
Descriptor::from(SplitDescriptor::new(
453+
0x0002_0000,
454+
INPUT_SIZE,
455+
VRING_DESC_F_WRITE as u16,
456+
0,
457+
)),
438458
];
439459

440460
let queue = MockSplitQueue::new(&mem, 16);
@@ -455,8 +475,18 @@ mod tests {
455475
.enqueue_data(&mut vec![INPUT_VALUE * 2; INPUT_SIZE as usize])
456476
.unwrap();
457477
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),
478+
Descriptor::from(SplitDescriptor::new(
479+
0x3000,
480+
INPUT_SIZE,
481+
VRING_DESC_F_WRITE as u16,
482+
0,
483+
)),
484+
Descriptor::from(SplitDescriptor::new(
485+
0x4000,
486+
INPUT_SIZE,
487+
VRING_DESC_F_WRITE as u16,
488+
0,
489+
)),
460490
];
461491

462492
let queue = MockSplitQueue::new(&mem, 16);
@@ -477,12 +507,12 @@ mod tests {
477507
console
478508
.enqueue_data(&mut vec![INPUT_VALUE; 2 * INPUT_SIZE as usize])
479509
.unwrap();
480-
let v = [Descriptor::new(
510+
let v = [Descriptor::from(SplitDescriptor::new(
481511
0x5000,
482512
INPUT_SIZE,
483513
VRING_DESC_F_WRITE as u16,
484514
0,
485-
)];
515+
))];
486516

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

498528
assert!(!console.is_input_buffer_empty());
499529

500-
let v = [Descriptor::new(
530+
let v = [Descriptor::from(SplitDescriptor::new(
501531
0x6000,
502532
INPUT_SIZE,
503533
VRING_DESC_F_WRITE as u16,
504534
0,
505-
)];
535+
))];
506536
let mut chain = queue.build_desc_chain(&v[..1]).unwrap();
507537

508538
assert_eq!(
@@ -516,12 +546,12 @@ mod tests {
516546
assert!(console.is_input_buffer_empty());
517547

518548
// Input buffer is empty.
519-
let v = [Descriptor::new(
549+
let v = [Descriptor::from(SplitDescriptor::new(
520550
0x7000,
521551
INPUT_SIZE,
522552
VRING_DESC_F_WRITE as u16,
523553
0,
524-
)];
554+
))];
525555
let mut chain = queue.build_desc_chain(&v[..1]).unwrap();
526556

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

0 commit comments

Comments
 (0)