Repository navigation
nvme: populate inline NGUID and derive per-subsystem SN - #3678
jeffbromberger wants to merge 8 commits into
Conversation
Two independent correctness gaps in how the emulated NVMe controller surfaces identifiers to the host. * Inline NGUID in Identify Namespace was always zero. `Namespace::identify` (CNS=00h) never populated the inline NGUID field at offsets 104..120; the 16 bytes were left at their `FromZeros::new_zeroed()` default. Hosts that read NGUID from the inline data structure -- rather than from the descriptor in the Identification Descriptor List (CNS=03h) -- got zeros and fell through to weaker unique-id construction. The descriptor- list path was a little better: it populated the NID from `disk.disk_id()` when present, but emitted a NIDT=2 (NGUID) descriptor with a zero NID when `disk.disk_id()` was `None`, violating NVMe Base section 4.7.1.2 / Figure 326's normative `the controller shall not report a Namespace Identification Descriptor with a value of type 2h` when NGUID is zero. The fix caches a single 16-byte NGUID on `Namespace` at construction. The value is the backing `disk.disk_id()` when available; otherwise it is synthesized as the first 16 bytes of SHA-256(`subsystem_id` || `nsid` little-endian), giving every namespace a stable, non-zero NGUID that is unique within its subsystem and deterministic across reboots. Both the CNS=00h inline field and the CNS=03h NIDT=2 (NGUID) descriptor now report the same cached value, so the two views are byte-for-byte consistent. EUI64 is left as zero -- there is no general way to derive a unique 8-byte EUI from the backing disk. * Identify Controller Serial Number was a hard-coded literal. The SN field was always `SN: 000001` (space-padded) regardless of the controller's identity, collapsing the host's view of subsystem identity. NVMe defines SN as a per-NVM-subsystem field (Base section 4.7.1 / Identify Controller Figure 328): controllers that share a subsystem share an SN, and CNTLID distinguishes them. The fix derives the 20-byte SN as the first 10 bytes of SHA-256(`subsystem_id`) rendered as 20 lowercase ASCII hex characters. Hashing before truncating diffuses the entropy of the full 16-byte `subsystem_id` across every output byte (the same principle that makes Git short hashes reliable identifiers), so the truncated SN retains ~80 bits of effective entropy regardless of where the entropy in `subsystem_id` was concentrated. Controllers that share a `subsystem_id` (e.g. future multi-controller-per-subsystem emulation) will report the same SN by construction, matching the spec semantics. Nine unit tests pin both behaviors: NGUID populated from disk_id, NGUID synthesized when no disk_id is available, descriptor-list / inline NGUID byte-equality in both paths, synthesize_nguid determinism + intra/inter-subsystem distinctness, derive_serial_number format pin, derive_serial_number distinctness across different subsystem_ids, derive_serial_number using the full subsystem_id (not just a prefix), plus an integration test that issues IDENTIFY_CONTROLLER through the real controller pipeline and verifies the SN byte sequence in the response. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR improves NVMe identity reporting by deriving stable identifiers from the configured subsystem GUID, replacing hard-coded values and ensuring namespaces always expose a usable NGUID.
Changes:
- Derive Identify Controller Serial Number (SN) from
subsystem_idvia SHA-256 (stable, per-subsystem). - Synthesize and cache per-namespace NGUID when the backing disk doesn’t provide one; populate both inline NGUID and the CNS=03h descriptor consistently.
- Add unit/integration tests to pin formats and prevent regressions; add
sha2dependency.
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| vm/devices/storage/nvme/src/workers/admin.rs | Adds SN derivation helper and wires SN to subsystem_id; adds unit tests. |
| vm/devices/storage/nvme/src/tests/controller_tests.rs | Adds test helper to control subsystem_id and an end-to-end IDENTIFY_CONTROLLER SN regression test. |
| vm/devices/storage/nvme/src/namespace.rs | Adds NGUID caching + synthesized fallback; ensures inline NGUID and descriptor list match; adds unit tests. |
| vm/devices/storage/nvme/Cargo.toml | Adds sha2 workspace dependency for hashing-based derivations. |
Address review comments on PR microsoft#3678: * Treat `Some([0u8; 16])` from `disk.disk_id()` the same as `None`: fall through to `synthesize_nguid` rather than caching and reporting an all-zero NGUID. An all-zero NGUID is indistinguishable from "no identifier" to a spec-following host, and would violate the rule against a NIDT=NSGUID descriptor with a zero NID. Added regression test `test_identify_namespace_treats_zero_disk_id_as_absent`. * Reword comments that said `NIDT=2 (NGUID)` to say `NIDT=NSGUID (NGUID in the NVMe spec)` instead. `NSGUID` is the existing variant name in `NamespaceIdentifierType::NSGUID`; the parenthetical clarifies it is the spec's NGUID (distinct from NUUID/IEEE) for readers working from the spec rather than the codebase. * Cache the derived Serial Number on `AdminHandler` at construction (`derive_serial_number` is a pure function of `subsystem_id`, which is fixed for the controller's lifetime). Avoids re-running SHA-256 on every IDENTIFY_CONTROLLER response. No behavioral change beyond the zero-disk_id correctness fix. All 21 nvme + nvme_spec unit tests pass (1 new + 20 unchanged). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…-and-derived-sn # Conflicts: # vm/devices/storage/nvme/src/tests/controller_tests.rs
|
Merged latest main to clear the flaky |
|
Closing and reopening to re-trigger CI. The x64-windows-intel-tdx job failed with exit code 127 (CI harness command-not-found), an infrastructure error unrelated to this NVMe change -- all other jobs on the merge commit passed. |
|
jeffbromberger, this PR looks close but has been inactive for a while. Is it ready for review or merge, or is anything still pending? Thanks. |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 02787e20-8e70-4f92-8124-63ecdbb4e322
|
Ben Hillis (@benhillis) Refreshed this PR onto current main in 811389d and resolved the namespace refactor conflict while preserving the new read-only namespace behavior. The final diff remains limited to the intended NGUID/SN changes. Local validation passed: 41 NVMe unit tests, strict NVMe clippy, repository formatting/lints/fuzzer/Flowey verification, and |
There was a problem hiding this comment.
🟡 Changes recommended
The NGUID fallback assigns the same persistent identifier to different raw disks at the same NSID across VMs and after replacement, risking guest misidentification of disks.
1 open issue
3 resolved since last review
Review effort: Balanced
Report NGUID only when the backing disk supplies a nonzero stable ID. Leave the inline field zero and omit the CNS=03h descriptor otherwise, avoiding attachment-slot identities that survive backing replacement. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 02787e20-8e70-4f92-8124-63ecdbb4e322
There was a problem hiding this comment.
🟡 Changes recommended
The tests do not catch removal of the all-zero-ID filter; an explicit assertion that an all-zero NGUID produces an entirely zero descriptor buffer is still needed.
1 open issue
1 resolved since last review
Review effort: Balanced
| let buf = identify_buf_for(5, Some([0u8; 16])); | ||
| assert_eq!( | ||
| &buf[NGUID_OFFSET..NGUID_OFFSET + NGUID_LEN], | ||
| &[0u8; NGUID_LEN] | ||
| ); |



Summary
Fix two NVMe identity-reporting gaps:
provides a suitable stable ID, and keep it consistent with CNS=03h.
instead of reporting the same hard-coded value for every controller.
Namespace NGUID
Previously,
Namespace::identifyleft the inline NGUID field at offsets104..120 zero even when
disk.disk_id()supplied a stable 16-byte identity.CNS=03h separately attempted to expose the disk ID, so the two identify paths
could disagree.
The namespace now caches a nonzero backing
disk_id()at construction and:If the backing disk does not provide a suitable persistent identity, or returns
an all-zero ID, inline NGUID remains zero and CNS=03h emits no NGUID descriptor.
The controller/NSID attachment slot is deliberately not used to invent media
identity because it can be reused for a different backing disk or across VMs.
Controller Serial Number
Identify Controller previously reported the same literal serial number for
every controller.
The serial number is now the first 10 bytes of
SHA-256(subsystem_id), renderedas 20 lowercase ASCII hexadecimal characters. Controllers in the same NVM
subsystem report the same serial number, while different subsystem IDs produce
different values. Hashing the complete GUID before truncation avoids depending
on entropy being concentrated in a particular subset of GUID bytes.
Validation
The branch was refreshed onto current
mainand the namespace conflict wasresolved while preserving current read-only namespace behavior.
cargo test -p nvme --lib: 40 passed.cargo clippy -p nvme --all-targets --no-deps -- -D warnings.cargo xtask fmt --fix, including repository lints, fuzzer verification,and Flowey regeneration verification.
cargo check -p openvmm.Focused tests cover:
subsystem IDs.