Skip to content

Commit

Permalink
Make crutest use BlockIO trait instead of a Guest (#1452)
Browse files Browse the repository at this point in the history
This enables future work where we plan to use a Volume in crutest
instead of a Guest.

Update crutest to use <T: BlockIO> instead of Guest.
This involved adding a few more methods to the BlockIO trait, moving a
few guest specific methods over to BlockIO, and adding support for these
new methods in places where they did not have them.

This should result in no differences at run time as we are not doing
anything different than before.

I wanted this change first to reduce some of the boiler plate noise in
future PRs.
  • Loading branch information
leftwo authored Sep 13, 2024
1 parent 558a6d9 commit a551f24
Show file tree
Hide file tree
Showing 11 changed files with 394 additions and 100 deletions.
10 changes: 7 additions & 3 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ pub enum CrucibleError {
#[error("Offset past end of extent")]
OffsetInvalid,

#[error("Upstairs is already active")]
UpstairsAlreadyActive,
#[error("Upstairs activation is in progress")]
UpstairsActivateInProgress,

#[error("Upstairs is deactivating")]
UpstairsDeactivating,
Expand Down Expand Up @@ -133,6 +133,9 @@ pub enum CrucibleError {
#[error("Generation number is too low: {0}")]
GenerationNumberTooLow(String),

#[error("Active with different generation number")]
GenerationNumberInvalid,

#[error("No longer active")]
NoLongerActive,

Expand Down Expand Up @@ -389,6 +392,7 @@ impl From<CrucibleError> for dropshot::HttpError {
| CrucibleError::Disconnect
| CrucibleError::EncryptionError(_)
| CrucibleError::GenerationNumberTooLow(_)
| CrucibleError::GenerationNumberInvalid
| CrucibleError::GenericError(_)
| CrucibleError::HashMismatch
| CrucibleError::InvalidExtent
Expand All @@ -403,7 +407,7 @@ impl From<CrucibleError> for dropshot::HttpError {
| CrucibleError::RwLockError(_)
| CrucibleError::SendError(_)
| CrucibleError::SubvolumeSizeMismatch
| CrucibleError::UpstairsAlreadyActive
| CrucibleError::UpstairsActivateInProgress
| CrucibleError::UpstairsDeactivating
| CrucibleError::UuidMismatch
| CrucibleError::MissingContextSlot(..)
Expand Down
36 changes: 18 additions & 18 deletions crutest/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ enum CliCommand {
* After verify, we truncate the data to 10 fields and return that so
* the cli server can send it back to the client for display.
*/
async fn cli_read(
guest: &Arc<Guest>,
async fn cli_read<T: BlockIO>(
guest: &T,
ri: &mut RegionInfo,
block_index: usize,
size: usize,
Expand Down Expand Up @@ -264,8 +264,8 @@ async fn cli_read(
/*
* A wrapper around write that just picks a random offset.
*/
async fn rand_write(
guest: &Arc<Guest>,
async fn rand_write<T: BlockIO>(
guest: &T,
ri: &mut RegionInfo,
) -> Result<(), CrucibleError> {
/*
Expand All @@ -290,8 +290,8 @@ async fn rand_write(
* Data is generated based on the value in the internal write counter.
* Update the internal write counter so we have something to compare to.
*/
async fn cli_write(
guest: &Arc<Guest>,
async fn cli_write<T: BlockIO>(
guest: &T,
ri: &mut RegionInfo,
block_index: usize,
size: usize,
Expand Down Expand Up @@ -334,8 +334,8 @@ async fn cli_write(
* If we do believe the block is written to, then we don't update our
* internal counter and we don't expect our write to change the contents.
*/
async fn cli_write_unwritten(
guest: &Arc<Guest>,
async fn cli_write_unwritten<T: BlockIO>(
guest: &T,
ri: &mut RegionInfo,
block_index: usize,
) -> Result<(), CrucibleError> {
Expand Down Expand Up @@ -744,8 +744,8 @@ pub async fn start_cli_client(attach: SocketAddr) -> Result<()> {
/**
* Process a CLI command from the client, we are the server side.
*/
async fn process_cli_command(
guest: &Arc<Guest>,
async fn process_cli_command<T: BlockIO + Send + Sync + 'static>(
guest: &Arc<T>,
fw: &mut FramedWrite<tokio::net::tcp::OwnedWriteHalf, CliEncoder>,
cmd: protocol::CliMessage,
ri: &mut RegionInfo,
Expand Down Expand Up @@ -945,7 +945,7 @@ async fn process_cli_command(
let block_max = ri.total_blocks - size + 1;
let offset = rng.gen_range(0..block_max);

let res = cli_read(guest, ri, offset, size).await;
let res = cli_read(guest.as_ref(), ri, offset, size).await;
fw.send(CliMessage::ReadResponse(offset, res)).await
}
}
Expand All @@ -956,7 +956,7 @@ async fn process_cli_command(
)))
.await
} else {
match rand_write(guest, ri).await {
match rand_write(guest.as_ref(), ri).await {
Ok(_) => fw.send(CliMessage::DoneOk).await,
Err(e) => fw.send(CliMessage::Error(e)).await,
}
Expand All @@ -969,7 +969,7 @@ async fn process_cli_command(
)))
.await
} else {
let res = cli_read(guest, ri, offset, len).await;
let res = cli_read(guest.as_ref(), ri, offset, len).await;
fw.send(CliMessage::ReadResponse(offset, res)).await
}
}
Expand All @@ -988,7 +988,7 @@ async fn process_cli_command(
)))
.await
} else {
match cli_write(guest, ri, offset, len).await {
match cli_write(guest.as_ref(), ri, offset, len).await {
Ok(_) => fw.send(CliMessage::DoneOk).await,
Err(e) => fw.send(CliMessage::Error(e)).await,
}
Expand All @@ -1001,7 +1001,7 @@ async fn process_cli_command(
)))
.await
} else {
match cli_write_unwritten(guest, ri, offset).await {
match cli_write_unwritten(guest.as_ref(), ri, offset).await {
Ok(_) => fw.send(CliMessage::DoneOk).await,
Err(e) => fw.send(CliMessage::Error(e)).await,
}
Expand Down Expand Up @@ -1040,14 +1040,14 @@ async fn process_cli_command(
/*
* Server for a crucible client CLI.
* This opens a network port and listens for commands from the cli_client.
* When it receives one, it translates it into the crucible Guest command
* When it receives one, it translates it into the crucible BlockIO command
* and passes it on to the Upstairs.
* State is kept here.
* No checking is done.
* Wait here if you want.
*/
pub async fn start_cli_server(
guest: &Arc<Guest>,
pub async fn start_cli_server<T: BlockIO + Send + Sync + 'static>(
guest: &Arc<T>,
address: IpAddr,
port: u16,
verify_input: Option<PathBuf>,
Expand Down
Loading

0 comments on commit a551f24

Please sign in to comment.