Skip to content

Commit

Permalink
block: add StdIoBackend abstraction
Browse files Browse the repository at this point in the history
This commit proposes a separate abstraction for block device
request execution. The reason behind this is the fact that
there can be multiple ways to execute these requests,
e.g. asynchronous dispatch of requests and result collection.
Such complex executors can be implemented as separate components
which coexist with the current proposal.

Signed-off-by: Laura Loghin <[email protected]>
Suggested-by: Alexandru Agache <[email protected]>
  • Loading branch information
lauralt committed Dec 9, 2020
1 parent 949639d commit e849cd7
Show file tree
Hide file tree
Showing 4 changed files with 484 additions and 4 deletions.
2 changes: 1 addition & 1 deletion coverage_config_x86_64.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"coverage_score": 91.4,
"coverage_score": 87.7,
"exclude_path": "",
"crate_features": ""
}
2 changes: 2 additions & 0 deletions src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@

/// Contains block request parsing abstraction.
pub mod request;
/// Contains block request execution abstraction.
pub mod stdio_executor;
31 changes: 28 additions & 3 deletions src/block/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ use vm_memory::{
ByteValued, Bytes, GuestAddress, GuestAddressSpace, GuestMemory, GuestMemoryError,
};

const VIRTIO_BLK_T_IN: u32 = 0;
const VIRTIO_BLK_T_OUT: u32 = 1;
const VIRTIO_BLK_T_FLUSH: u32 = 4;
/// Read request.
pub const VIRTIO_BLK_T_IN: u32 = 0;
/// Write request.
pub const VIRTIO_BLK_T_OUT: u32 = 1;
/// Flush request.
pub const VIRTIO_BLK_T_FLUSH: u32 = 4;

/// Block request parsing errors.
#[derive(Debug)]
Expand Down Expand Up @@ -141,6 +144,11 @@ impl Request {
self.status_addr
}

/// Returns the total length of request data.
pub fn total_data_len(&self) -> u32 {
self.data.iter().map(|&x| x.1).sum()
}

// Checks that a descriptor meets the minimal requirements for a valid status descriptor.
fn check_status_desc<M: GuestMemory>(mem: &M, desc: Descriptor) -> Result<()> {
// The status MUST always be writable.
Expand Down Expand Up @@ -264,6 +272,22 @@ mod tests {
}
}

impl Request {
pub fn new(
request_type: RequestType,
data: Vec<(GuestAddress, u32)>,
sector: u64,
status_addr: GuestAddress,
) -> Self {
Request {
request_type,
data,
sector,
status_addr,
}
}
}

// Helper method that writes a descriptor chain to a `GuestMemoryMmap` object and returns
// the associated `DescriptorChain` object. `descs` represents a slice of `Descriptor` objects
// which are used to populate the chain. This method ensures the next flags and values are
Expand Down Expand Up @@ -474,6 +498,7 @@ mod tests {
};
assert_eq!(request, expected_request);
assert_eq!(request.status_addr(), GuestAddress(0x40_0000));
assert_eq!(request.total_data_len(), 0x100 + 0x200);

// Request header with unsupported request type.
let req_header = RequestHeader {
Expand Down
Loading

0 comments on commit e849cd7

Please sign in to comment.