Skip to content

nvme: populate inline NGUID and derive per-subsystem SN - #3678

Open
jeffbromberger wants to merge 8 commits into
microsoft:mainfrom
jeffbromberger:fix/nvme-inline-nguid-and-derived-sn
Open

jeffbromberger wants to merge 8 commits into
microsoft:mainfrom
jeffbromberger:fix/nvme-inline-nguid-and-derived-sn

Conversation

@jeffbromberger

@jeffbromberger jeffbromberger commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fix two NVMe identity-reporting gaps:

  1. Populate the inline NGUID in Identify Namespace when the backing disk
    provides a suitable stable ID, and keep it consistent with CNS=03h.
  2. Derive Identify Controller Serial Number from the configured subsystem ID
    instead of reporting the same hard-coded value for every controller.

Namespace NGUID

Previously, Namespace::identify left the inline NGUID field at offsets
104..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:

  • Copies it into the CNS=00h inline NGUID field.
  • Emits the same bytes in the CNS=03h NIDT=NSGUID descriptor.
  • Leaves EUI64 zero.

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), rendered
as 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 main and the namespace conflict was
resolved while preserving current read-only namespace behavior.

  • cargo test -p nvme --lib: 40 passed.
  • Strict 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:

  • Inline NGUID copied from a nonzero backing disk ID.
  • Matching CNS=03h NGUID descriptor.
  • Missing and all-zero backing IDs leave NGUID absent.
  • Exact serial-number format for a known subsystem GUID.
  • Stable serial number for the same subsystem and distinct values for different
    subsystem IDs.
  • Full Identify Controller command path returning the derived serial number.

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>
@jeffbromberger
jeffbromberger requested review from a team as code owners June 5, 2026 22:35
Copilot AI review requested due to automatic review settings June 5, 2026 22:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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_id via 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 sha2 dependency.

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.

jeffbromberger and others added 2 commits June 5, 2026 17:53
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>
Copilot AI review requested due to automatic review settings June 5, 2026 23:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 5 changed files in this pull request and generated 3 comments.

@github-actions

github-actions Bot commented Jun 6, 2026

Copy link
Copy Markdown

jeffbromberger and others added 2 commits June 8, 2026 11:31
Copilot AI review requested due to automatic review settings June 30, 2026 13:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 5 changed files in this pull request and generated no new comments.

@github-actions

Copy link
Copy Markdown

@jeffbromberger

Copy link
Copy Markdown
Contributor Author

Merged latest main to clear the flaky x64-windows-amd-snp CVM failure (already fixed in main per #3846). Fresh CI is running on the merge commit. Ready for review -- thanks!

@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown

@jeffbromberger

Copy link
Copy Markdown
Contributor Author

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.

@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown

@benhillis

Copy link
Copy Markdown
Member

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.

@jstarks John Starks (jstarks) self-assigned this Oct 14, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 02787e20-8e70-4f92-8124-63ecdbb4e322
Copilot AI balanced review requested due to automatic review settings November 24, 2026 20:29
@jeffbromberger

Copy link
Copy Markdown
Contributor Author

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 cargo check -p openvmm. It is ready for review; fresh CI is running.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 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
Copilot AI balanced review requested due to automatic review settings November 25, 2026 03:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 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

Comment on lines +423 to +427
let buf = identify_buf_for(5, Some([0u8; 16]));
assert_eq!(
&buf[NGUID_OFFSET..NGUID_OFFSET + NGUID_LEN],
&[0u8; NGUID_LEN]
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants