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.

Signed-off-by: Matias Ezequiel Vara Larsen <mvaralar@redhat.com>
  • Loading branch information
MatiasVara committed Jan 5, 2024
1 parent 4758e4f commit f142662
Show file tree
Hide file tree
Showing 3 changed files with 919 additions and 3 deletions.
20 changes: 18 additions & 2 deletions virtio-queue/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use std::fmt::{self, Debug};
use std::mem::size_of;
use std::ops::Deref;

use vm_memory::{Address, Bytes, GuestAddress, GuestMemory};
use vm_memory::{Address, Bytes, GuestAddress, GuestMemory, GuestMemoryMmap};

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 +88,22 @@ where
}
}

/// Return a new instance of Writer
pub fn writer(self, mem: &GuestMemoryMmap) -> Result<Writer<'_>, Error>
where
M::Target: Sized,
{
Writer::new(mem, self).map_err(|_| Error::InvalidChain)
}

/// Return a new instance of Reader
pub fn reader(self, mem: &GuestMemoryMmap) -> Result<Reader<'_>, Error>
where
M::Target: Sized,
{
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 f142662

Please sign in to comment.