Skip to content

Commit

Permalink
virtio_queue: Add descriptors_utils.rs
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
MatiasVara authored and stefano-garzarella committed Feb 8, 2024
1 parent 0e90efe commit d949c0b
Show file tree
Hide file tree
Showing 3 changed files with 921 additions and 3 deletions.
23 changes: 21 additions & 2 deletions virtio-queue/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ use std::fmt::{self, Debug};
use std::mem::size_of;
use std::ops::Deref;

use vm_memory::{Address, Bytes, GuestAddress, GuestMemory};
use vm_memory::bitmap::{BitmapSlice, WithBitmapSlice};
use vm_memory::{Address, Bytes, GuestAddress, GuestMemory, GuestMemoryRegion};

use crate::{Descriptor, Error};
use crate::{Descriptor, Error, Reader, Writer};
use virtio_bindings::bindings::virtio_ring::VRING_DESC_ALIGN_SIZE;

/// A virtio descriptor chain.
Expand Down Expand Up @@ -88,6 +89,24 @@ where
}
}

/// Return a new instance of Writer
pub fn writer<'a, B: BitmapSlice>(self, mem: &'a M::Target) -> Result<Writer<'a, B>, Error>
where
M::Target: Sized,
<<M::Target as GuestMemory>::R as GuestMemoryRegion>::B: WithBitmapSlice<'a, S = B>,
{
Writer::new(mem, self).map_err(|_| Error::InvalidChain)
}

/// Return a new instance of Reader
pub fn reader<'a, B: BitmapSlice>(self, mem: &'a M::Target) -> Result<Reader<'a, B>, Error>
where
M::Target: Sized,
<<M::Target as GuestMemory>::R as GuestMemoryRegion>::B: WithBitmapSlice<'a, S = B>,
{
Reader::new(mem, self).map_err(|_| Error::InvalidChain)
}

/// Return an iterator that only yields the writable descriptors in the chain.
pub fn writable(self) -> DescriptorChainRwIter<M> {
DescriptorChainRwIter {
Expand Down
Loading

0 comments on commit d949c0b

Please sign in to comment.