Skip to content

Commit d949c0b

Browse files
MatiasVarastefano-garzarella
authored andcommitted
virtio_queue: Add descriptors_utils.rs
This commit adds descriptors_utils.rs from virtiofsd. This module provides the Reader/Writer classes to iterate over descriptors. Devices shall rely on this interface instead of manipulate descriptors. The version in this commit removes read_to_at() and write_from_at() that are required only for virtiofsd. To instantiate these helpers, user shall use the reader()/writer() functions defined in the context of DescriptorChain. Co-developed-by: Stefano Garzarella <sgarzare@redhat.com> Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> Signed-off-by: Matias Ezequiel Vara Larsen <mvaralar@redhat.com>
1 parent 0e90efe commit d949c0b

File tree

3 files changed

+921
-3
lines changed

3 files changed

+921
-3
lines changed

virtio-queue/src/chain.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ use std::fmt::{self, Debug};
1414
use std::mem::size_of;
1515
use std::ops::Deref;
1616

17-
use vm_memory::{Address, Bytes, GuestAddress, GuestMemory};
17+
use vm_memory::bitmap::{BitmapSlice, WithBitmapSlice};
18+
use vm_memory::{Address, Bytes, GuestAddress, GuestMemory, GuestMemoryRegion};
1819

19-
use crate::{Descriptor, Error};
20+
use crate::{Descriptor, Error, Reader, Writer};
2021
use virtio_bindings::bindings::virtio_ring::VRING_DESC_ALIGN_SIZE;
2122

2223
/// A virtio descriptor chain.
@@ -88,6 +89,24 @@ where
8889
}
8990
}
9091

92+
/// Return a new instance of Writer
93+
pub fn writer<'a, B: BitmapSlice>(self, mem: &'a M::Target) -> Result<Writer<'a, B>, Error>
94+
where
95+
M::Target: Sized,
96+
<<M::Target as GuestMemory>::R as GuestMemoryRegion>::B: WithBitmapSlice<'a, S = B>,
97+
{
98+
Writer::new(mem, self).map_err(|_| Error::InvalidChain)
99+
}
100+
101+
/// Return a new instance of Reader
102+
pub fn reader<'a, B: BitmapSlice>(self, mem: &'a M::Target) -> Result<Reader<'a, B>, Error>
103+
where
104+
M::Target: Sized,
105+
<<M::Target as GuestMemory>::R as GuestMemoryRegion>::B: WithBitmapSlice<'a, S = B>,
106+
{
107+
Reader::new(mem, self).map_err(|_| Error::InvalidChain)
108+
}
109+
91110
/// Return an iterator that only yields the writable descriptors in the chain.
92111
pub fn writable(self) -> DescriptorChainRwIter<M> {
93112
DescriptorChainRwIter {

0 commit comments

Comments
 (0)