Skip to content

[sp-sim] Simulate host flash hashing #8584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sp-sim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ omicron-common.workspace = true
oxide-tokio-rt.workspace = true
serde.workspace = true
serde_cbor.workspace = true
sha2.workspace = true
sha3.workspace = true
slog.workspace = true
slog-dtrace.workspace = true
Expand Down
11 changes: 4 additions & 7 deletions sp-sim/src/gimlet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1596,18 +1596,15 @@ impl SpHandler for Handler {
_addr: u32,
_buf: &mut [u8],
) -> Result<(), SpError> {
// TODO we should simulate this; will need it for OS update testing.
Err(SpError::RequestUnsupportedForSp)
}

fn start_host_flash_hash(&mut self, _slot: u16) -> Result<(), SpError> {
// TODO we should simulate this; will need it for OS update testing.
Err(SpError::RequestUnsupportedForSp)
fn start_host_flash_hash(&mut self, slot: u16) -> Result<(), SpError> {
self.update_state.start_host_flash_hash(slot)
}

fn get_host_flash_hash(&mut self, _slot: u16) -> Result<[u8; 32], SpError> {
// TODO we should simulate this; will need it for OS update testing.
Err(SpError::RequestUnsupportedForSp)
fn get_host_flash_hash(&mut self, slot: u16) -> Result<[u8; 32], SpError> {
self.update_state.get_host_flash_hash(slot)
}
}

Expand Down
98 changes: 98 additions & 0 deletions sp-sim/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use std::collections::BTreeMap;
use std::io::Cursor;
use std::mem;
use std::time::Duration;
use std::time::Instant;

use crate::SIM_GIMLET_BOARD;
use crate::SIM_ROT_BOARD;
Expand All @@ -13,6 +15,7 @@ use crate::SIM_SIDECAR_BOARD;
use crate::helpers::rot_slot_id_from_u16;
use crate::helpers::rot_slot_id_to_u16;
use gateway_messages::Fwid;
use gateway_messages::HfError;
use gateway_messages::RotSlotId;
use gateway_messages::RotStateV3;
use gateway_messages::SpComponent;
Expand All @@ -21,9 +24,14 @@ use gateway_messages::UpdateChunk;
use gateway_messages::UpdateId;
use gateway_messages::UpdateInProgressStatus;
use hubtools::RawHubrisImage;
use sha2::Sha256;
use sha3::Digest;
use sha3::Sha3_256;

// How long do we take to hash host flash? Real SPs take a handful of seconds;
// we'll pick something similar.
const TIME_TO_HASH_HOST_PHASE_1: Duration = Duration::from_secs(5);

pub(crate) struct SimSpUpdate {
/// tracks the state of any ongoing simulated update
///
Expand All @@ -38,6 +46,8 @@ pub(crate) struct SimSpUpdate {
/// data from the last completed phase1 update for each slot (exposed for
/// testing)
last_host_phase1_update_data: BTreeMap<u16, Box<[u8]>>,
/// state of hashing each of the host phase1 slots
phase1_hash_state: BTreeMap<u16, HostFlashHashState>,

/// records whether a change to the stage0 "active slot" has been requested
pending_stage0_update: bool,
Expand Down Expand Up @@ -177,6 +187,7 @@ impl SimSpUpdate {
last_sp_update_data: None,
last_rot_update_data: None,
last_host_phase1_update_data: BTreeMap::new(),
phase1_hash_state: BTreeMap::new(),

pending_stage0_update: false,

Expand Down Expand Up @@ -303,6 +314,13 @@ impl SimSpUpdate {
std::io::Write::write_all(data, chunk_data)
.map_err(|_| SpError::UpdateIsTooLarge)?;

// If we're writing to the host flash, invalidate the cached
// hash of this slot.
if *component == SpComponent::HOST_CPU_BOOT_FLASH {
self.phase1_hash_state
.insert(*slot, HostFlashHashState::HashInvalidated);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is consistent with the real SP, but wanted to check. If we start up and have this sequence of requests:

  • get_host_flash_hash -> will return HfError::HashUncalculated
  • we do not get a request to start_host_flash_hash
  • we perform an update, writing the target slot
  • after the update, we get another get_host_flash_hash request -> this will now return HfError::RecalculateHash, because the act of writing sets our state to HashInvalidated

I think this is completely reasonable but wanted to double check it matches; any write will cause future gets to return HfError::RecalculateHash, even if we'd never calculated it a first time, right?

}

if data.position() == data.get_ref().len() as u64 {
let mut stolen = Cursor::new(Box::default());
mem::swap(data, &mut stolen);
Expand Down Expand Up @@ -452,6 +470,78 @@ impl SimSpUpdate {
self.last_host_phase1_update_data.get(&slot).cloned()
}

pub(crate) fn start_host_flash_hash(
&mut self,
slot: u16,
) -> Result<(), SpError> {
match self
.phase1_hash_state
.entry(slot)
.or_insert(HostFlashHashState::NeverHashed)
{
// No current hash; record our start time so we can emulate hashing
// taking a few seconds.
state @ (HostFlashHashState::NeverHashed
| HostFlashHashState::HashInvalidated) => {
*state = HostFlashHashState::HashStarted(Instant::now());
Ok(())
}
// Already hashed; this is a no-op.
HostFlashHashState::Hashed(_) => Ok(()),
// Still hashing; check and see if it's done. This is either an
// error (if we're still hashing) or a no-op (if we're done).
HostFlashHashState::HashStarted(started) => {
let started = *started;
self.finalize_host_flash_hash_if_sufficient_time_elapsed(
slot, started,
)?;
Comment on lines +493 to +497
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we get a start_host_flash_hash() request while we're still hashing from a previous start request, this will return HfError::HashInProgress. I think the SP returns a generic HashError in this case, but maybe it should also return HashInProgress?

Ok(())
}
}
}

pub(crate) fn get_host_flash_hash(
&mut self,
slot: u16,
) -> Result<[u8; 32], SpError> {
match self
.phase1_hash_state
.entry(slot)
.or_insert(HostFlashHashState::NeverHashed)
{
HostFlashHashState::NeverHashed => {
Err(SpError::Hf(HfError::HashUncalculated))
}
HostFlashHashState::HashStarted(started) => {
let started = *started;
self.finalize_host_flash_hash_if_sufficient_time_elapsed(
slot, started,
)
}
HostFlashHashState::Hashed(hash) => Ok(*hash),
HostFlashHashState::HashInvalidated => {
Err(SpError::Hf(HfError::RecalculateHash))
}
}
}

fn finalize_host_flash_hash_if_sufficient_time_elapsed(
&mut self,
slot: u16,
started: Instant,
) -> Result<[u8; 32], SpError> {
if started.elapsed() < TIME_TO_HASH_HOST_PHASE_1 {
return Err(SpError::Hf(HfError::HashInProgress));
}

let data = self.last_host_phase1_update_data(slot);
let data = data.as_deref().unwrap_or(&[]);
let hash = Sha256::digest(&data).into();
self.phase1_hash_state.insert(slot, HostFlashHashState::Hashed(hash));

Ok(hash)
}

pub(crate) fn get_component_caboose_value(
&mut self,
component: SpComponent,
Expand Down Expand Up @@ -663,3 +753,11 @@ fn fake_fwid_compute(data: &[u8]) -> Fwid {
digest.update(data);
Fwid::Sha3_256(digest.finalize().into())
}

#[derive(Debug, Clone, Copy)]
enum HostFlashHashState {
NeverHashed,
HashStarted(Instant),
Hashed([u8; 32]),
HashInvalidated,
}
Loading