Files
seaweedfs/seaweed-block/core/frontend/nvme/protocol.go
T
wzcheng99 a73909ec21 T2 Batch 11a: NVMe admin dispatch + Identify + queue routing + CNTLID
Per v3-phase-15-t2-batch-11-port-plan.md §7 11a scope (LOCKED
2026-04-22). Port-model port of V2 identify.go + queue model +
controller-id registry. Unlocks Go-harness admin Identify path
+ existing IO path; Linux nvme connect still gated on 11b
Property/Features/KeepAlive/AER work.

core/frontend/nvme/protocol.go
  Added admin opcode constants: adminGetLogPage, adminIdentify,
  adminAbort, adminSetFeatures, adminGetFeatures, adminAsyncEvent,
  adminKeepAlive. Fabric types: fcPropertySet, fcPropertyGet,
  fcDisconnect. CNS codes for Identify. identifySize constant.
  VS version constant (nvmeVersion13 only — declared and used;
  unused nvmeVersion14 removed per port-model discipline).

core/frontend/nvme/target.go
  New adminController struct + Target-level CNTLID registry:
    allocAdminController(subNQN, hostNQN, volumeID) *adminController
    lookupAdminController(id) *adminController
    releaseAdminController(id)
  CNTLIDs allocated monotonically from 1 (0 reserved); released
  on admin-session close.

core/frontend/nvme/session.go
  Session now carries qid / qidSet / ctrl / target references.
  newSession signature extended to take *Target.

  handleCapsuleCmd refactored per port plan §3.4 R5:
    - Fabric commands bypass the qid gate (they establish it)
    - Post-Connect: route by s.qid via adminDispatch / ioDispatch
    - Admin queue accepts admin opcodes only (Invalid Opcode for IO)
    - IO queue accepts IO opcodes only (Invalid Opcode for admin)

  handleFabric split into handleAdminConnect + handleIOConnect:
    - QID decoded from CDW10[31:16]
    - Admin Connect: CNTLID must be 0xFFFF or 0x0000 (request new);
      allocate + register + reply with CNTLID in DW0[15:0]
    - IO Connect: cite existing CNTLID; validate exists + SubNQN
      matches; pin ctrl pointer. HostNQN NOT cross-checked (V2
      parity + Linux spec-compat) — T8 owns auth.
    - Double-Connect on same session rejected (qidSet gate)

  close() releases admin CNTLID from Target registry. IO sessions
  outliving admin hold their pinned ctrl pointer; new IO Connects
  citing a released CNTLID fail registry lookup with
  ConnectInvalidHost.

core/frontend/nvme/identify.go (NEW — port-model port of V2)
  Four CNS builders per NVMe 1.3 §5.15.2:
    - Identify Controller (4 KiB)
    - Identify Namespace (4 KiB)
    - Active NS List
    - NS Descriptor List (NGUID designator NIDT=0x02)

  11 deep-read corrections applied (port plan §3.5 D1-D11):
    D1  Serial derived from VolumeID via sha256 (R1 symmetry with
        Batch 10.5 follow-up #2 VPD 0x80); no "SWF00001" stub
    D2  CMIC / ANACAP / ANAGRPMAX / NANAGRPID / NS ANAGRPID = 0
        (ANA log is 11c-conditional)
    D3  ONCS (WriteZeros + DSM) = 0 (not implemented)
    D4  NSFEAT (thin-prov/Trim) = 0
    D5  DLFEAT (deallocate) = 0
    D6  AERL = 0 (one pending-AER slot; finding #2)
    D7  OFCS (Disconnect) = 0 (not in Batch 11 scope)
    D8  SubNQN NUL-terminated (not space-padded) at offset 768
    D9  LBAF[0] LBADS = 9 for 512B (bits.TrailingZeros32)
    D10 IOCCSZ = (64 + MaxH2CDataLength) / 16 = 2052
    D11 VS = NVMe 1.3; MNAN = 1

  NGUID derivation (R1 / port plan §3.3 N1):
    NGUID[16] = sha256(SubsysNQN + "\x00" + VolumeID)[:16]
    EUI-64[8] = NGUID[:8]
  Deterministic + per-volume unique + per-subsystem unique.
  QA A-tier pins determinism in a separate file.

core/frontend/nvme/client_support_test.go
  Rewrote nvmeClient to open admin (QID=0) + IO (QID=1) TCP
  connection pair. Admin Connect allocates CNTLID, IO Connect
  cites it. writeCmd/readCmd route through ioConn (back-compat
  with ckpt 7 tests). adminIdentify(cns, nsid) added. Existing
  L1 and L2-harness tests now exercise real multi-queue Connect.

core/frontend/nvme/identify_test.go (NEW)
  20 sw-owned tests:
    - D1-D11 field pins on Identify Controller (10 tests)
    - Identify Namespace fields (4 tests: size, NSFEAT+DLFEAT,
      ANAGRPID, LBAF, NGUID/EUI)
    - Active NS List + NS Descriptor List (2 tests)
    - Queue routing enforcement (2 tests: admin-rejects-IO,
      IO-rejects-admin)
    - CNTLID lifecycle (2 tests: unknown rejected with
      ConnectInvalidHost 0x82; preset rejected with
      ConnectInvalidParameters 0x80)
    - End-to-end (1 test: admin Identify all 4 CNS + IO Write/Read)

core/frontend/iscsi/t2_v2port_scsi_inquiry_test.go (QA-authored)
  QA closed the Batch 10.5 follow-up #2 N1 symmetry pin via
  TestT2V2Port_SCSI_InquiryVPD80_SerialDerivationDeterministic
  (4 sub-tests: same-VolumeID determinism, different-VolumeID
  collision, hex-shape, operator-override bypass).

core/frontend/nvme/t2_v2port_support_test.go (QA-authored, NEW)
  Skeleton shared utilities for Batch 11 A-tier tests per
  v3-phase-15-t2-batch-11-test-skeleton.md. Pre-authored to
  slot in when 11a lands; compiles today.

Tests green (no regressions):
  core/frontend/nvme:  51 tests (was 28 pre-11a; +23 including
                       QA skeleton support helpers)
  core/frontend/iscsi: 106 tests (was 104; +2 QA VPD 0x80
                       symmetric pin)
  All other packages:  unchanged.
  Full regression: frontend 17.6s / iscsi 31.2s / nvme 31.2s

Stop-rule audit:
  - In-allowlist per decision §5.2: Identify (all 4 CNS) ✓
  - QA finding #1 (11a includes A10 + A10.5) ✓
  - QA finding #3 (ANA fields zero in 11a/11b) ✓
  - §6 #4 advertised≡implemented: OACS/OAES/ONCS/CMIC/ANACAP
    /KAS/OFCS all zero aligned with un-ported admin commands ✓
  - §6 #7 no V2 adapter.go / write_retry.go / bufpool.go ported ✓
  - No core/authority / core/adapter imports in nvme package ✓
  - No AssignmentInfo / authority-minting literals ✓
  - Stale R/W semantics unchanged (IO path unmodified) ✓
  - N1 symmetry: Serial derives per VolumeID (ckpt 10.5 lesson) ✓

Sign tier per port plan §7: T2B-NVMe-provisional remains.
11a acceptance met (Go-harness Identify + IO round-trip green).
Linux nvme connect still gated on 11b (Property+Features+KATO).

Pre-commit self-review applied 2 fixes:
  I1: Removed unused nvmeVersion14 constant (dead code)
  I2: Removed sw-added HostNQN cross-check in handleIOConnect
      (V2 doesn't do it; Linux-compat safer)

Known items flagged for 11b or later:
  K1: IO sessions outliving admin use pinned ctrl pointer; new
      IO Connects fail. Documented in §3.1 A10.5 note.
  K2: connected + qidSet tracked separately (dispatch gate vs
      re-Connect gate)
  K3: NSID=0xFFFFFFFF (broadcast) mapped to NSID=1 internally
  K5: adminDispatch ctx parameter unused until 11b

QA-owned A-tier tests pending (per port plan §4):
  - TestT2V2Port_NVMe_IdentifyNamespaceDescriptor_Deterministic
    (4 pins: R1 a-d)
  - TestT2V2Port_NVMe_IdentifyAdvertisedFeatures_AreServed
    (walks advertised bits; §6 stop-rule #4 enforcement)
  - TestT2V2Port_NVMe_NoAuthorityOrStorageBleed_AdminSurface
    (AST boundary guard extended to identify.go)
2026-04-22 10:41:30 -07:00

389 lines
10 KiB
Go

package nvme
// NVMe/TCP wire protocol primitives (NVMe-oF spec §3, NVMe TCP
// Transport spec §3). Adapted from
// weed/storage/blockvol/nvme/protocol.go — wire format is
// byte-for-byte the V2 shape so a future merge or shared client
// is straightforward.
import (
"encoding/binary"
"fmt"
)
// ---------- PDU type codes ----------
const (
pduICReq uint8 = 0x0 // Initialization Connection Request
pduICResp uint8 = 0x1 // Initialization Connection Response
pduH2CTermReq uint8 = 0x2 // Host-to-Controller Termination Request
pduC2HTermReq uint8 = 0x3 // Controller-to-Host Termination Request
pduCapsuleCmd uint8 = 0x4 // NVMe Capsule Command
pduCapsuleResp uint8 = 0x5 // NVMe Capsule Response
pduH2CData uint8 = 0x6 // Host-to-Controller Data Transfer
pduC2HData uint8 = 0x7 // Controller-to-Host Data Transfer
pduR2T uint8 = 0x9 // Ready-to-Transfer
)
// ---------- Admin / Fabric command opcodes ----------
const (
// NVM admin opcode subset. Batch 11a introduces Identify
// (0x06). SetFeatures / GetFeatures / KeepAlive / AsyncEventRequest
// land with Batch 11b per port plan §7.
adminGetLogPage uint8 = 0x02
adminIdentify uint8 = 0x06
adminAbort uint8 = 0x08
adminSetFeatures uint8 = 0x09
adminGetFeatures uint8 = 0x0A
adminAsyncEvent uint8 = 0x0C
adminKeepAlive uint8 = 0x18
adminFabric uint8 = 0x7F // Fabric-specific commands
)
// Fabric command types (FCType).
const (
fcPropertySet uint8 = 0x00
fcConnect uint8 = 0x01
fcPropertyGet uint8 = 0x04
fcDisconnect uint8 = 0x08
)
// Identify CNS values (Batch 11a).
const (
cnsIdentifyNamespace uint8 = 0x00
cnsIdentifyController uint8 = 0x01
cnsActiveNSList uint8 = 0x02
cnsNSDescriptorList uint8 = 0x03
)
// Misc constants used by Identify builders. Only the versions
// we actually advertise are declared here; adding dead versions
// violates port-model discipline (port plan §5 stop-rule #4
// "Advertised ≡ implemented").
const (
identifySize = 4096 // Identify response size (fixed)
nvmeVersion13 = 0x00010300 // NVMe 1.3 — what 11a's VS register advertises (D11)
)
// ---------- Sizes ----------
const (
commonHeaderSize = 8
maxHeaderSize = 128
capsuleCmdSize = 64
capsuleRespSize = 16
c2hDataHdrSize = 16
h2cDataHdrSize = 16
r2tHdrSize = 16
icBodySize = 120
connectDataSize = 1024
c2hFlagLast uint8 = 0x04 // C2HData last-PDU flag
)
// ---------- CommonHeader (8 bytes) ----------
// CommonHeader is the 8-byte preamble of every NVMe/TCP PDU.
type CommonHeader struct {
Type uint8
Flags uint8
HeaderLength uint8
DataOffset uint8
DataLength uint32
}
func (h *CommonHeader) Marshal(buf []byte) {
buf[0] = h.Type
buf[1] = h.Flags
buf[2] = h.HeaderLength
buf[3] = h.DataOffset
binary.LittleEndian.PutUint32(buf[4:], h.DataLength)
}
func (h *CommonHeader) Unmarshal(buf []byte) {
h.Type = buf[0]
h.Flags = buf[1]
h.HeaderLength = buf[2]
h.DataOffset = buf[3]
h.DataLength = binary.LittleEndian.Uint32(buf[4:])
}
func (h *CommonHeader) String() string {
return fmt.Sprintf("PDU{type=0x%x hlen=%d doff=%d dlen=%d}",
h.Type, h.HeaderLength, h.DataOffset, h.DataLength)
}
// PDU is implemented by every PDU-specific header.
type PDU interface {
Marshal([]byte)
Unmarshal([]byte)
}
// ---------- ICRequest / ICResponse (120-byte body) ----------
type ICRequest struct {
PDUFormatVersion uint16
PDUDataAlignment uint8
PDUDataDigest uint8
PDUMaxR2T uint32
}
func (r *ICRequest) Marshal(buf []byte) {
for i := range buf[:icBodySize] {
buf[i] = 0
}
binary.LittleEndian.PutUint16(buf[0:], r.PDUFormatVersion)
buf[2] = r.PDUDataAlignment
buf[3] = r.PDUDataDigest
binary.LittleEndian.PutUint32(buf[4:], r.PDUMaxR2T)
}
func (r *ICRequest) Unmarshal(buf []byte) {
r.PDUFormatVersion = binary.LittleEndian.Uint16(buf[0:])
r.PDUDataAlignment = buf[2]
r.PDUDataDigest = buf[3]
r.PDUMaxR2T = binary.LittleEndian.Uint32(buf[4:])
}
type ICResponse struct {
PDUFormatVersion uint16
PDUDataAlignment uint8
PDUDataDigest uint8
MaxH2CDataLength uint32
}
func (r *ICResponse) Marshal(buf []byte) {
for i := range buf[:icBodySize] {
buf[i] = 0
}
binary.LittleEndian.PutUint16(buf[0:], r.PDUFormatVersion)
buf[2] = r.PDUDataAlignment
buf[3] = r.PDUDataDigest
binary.LittleEndian.PutUint32(buf[4:], r.MaxH2CDataLength)
}
func (r *ICResponse) Unmarshal(buf []byte) {
r.PDUFormatVersion = binary.LittleEndian.Uint16(buf[0:])
r.PDUDataAlignment = buf[2]
r.PDUDataDigest = buf[3]
r.MaxH2CDataLength = binary.LittleEndian.Uint32(buf[4:])
}
// ---------- CapsuleCommand (64-byte specific header) ----------
type CapsuleCommand struct {
OpCode uint8
PRP uint8
CID uint16
FCType uint8 // Fabric command type (only when OpCode=0x7F)
NSID uint32
DPTR [16]byte
D10 uint32
D11 uint32
D12 uint32
D13 uint32
D14 uint32
D15 uint32
}
// Lba returns the starting LBA from D10:D11.
func (c *CapsuleCommand) Lba() uint64 { return uint64(c.D11)<<32 | uint64(c.D10) }
// LbaLength returns NLB (D12 low 16 bits, zero-based on wire,
// converted to one-based here for ergonomic use).
func (c *CapsuleCommand) LbaLength() uint32 { return c.D12&0xFFFF + 1 }
func (c *CapsuleCommand) Marshal(buf []byte) {
for i := range buf[:capsuleCmdSize] {
buf[i] = 0
}
buf[0] = c.OpCode
buf[1] = c.PRP
binary.LittleEndian.PutUint16(buf[2:], c.CID)
if c.OpCode == adminFabric {
buf[4] = c.FCType
} else {
binary.LittleEndian.PutUint32(buf[4:], c.NSID)
}
copy(buf[24:40], c.DPTR[:])
binary.LittleEndian.PutUint32(buf[40:], c.D10)
binary.LittleEndian.PutUint32(buf[44:], c.D11)
binary.LittleEndian.PutUint32(buf[48:], c.D12)
binary.LittleEndian.PutUint32(buf[52:], c.D13)
binary.LittleEndian.PutUint32(buf[56:], c.D14)
binary.LittleEndian.PutUint32(buf[60:], c.D15)
}
func (c *CapsuleCommand) Unmarshal(buf []byte) {
c.OpCode = buf[0]
c.PRP = buf[1]
c.CID = binary.LittleEndian.Uint16(buf[2:])
c.FCType = buf[4]
c.NSID = binary.LittleEndian.Uint32(buf[4:])
copy(c.DPTR[:], buf[24:40])
c.D10 = binary.LittleEndian.Uint32(buf[40:])
c.D11 = binary.LittleEndian.Uint32(buf[44:])
c.D12 = binary.LittleEndian.Uint32(buf[48:])
c.D13 = binary.LittleEndian.Uint32(buf[52:])
c.D14 = binary.LittleEndian.Uint32(buf[56:])
c.D15 = binary.LittleEndian.Uint32(buf[60:])
}
// ---------- CapsuleResponse (16-byte specific header / NVMe CQE) ----------
type CapsuleResponse struct {
DW0 uint32
DW1 uint32
SQHD uint16
QueueID uint16
CID uint16
Status uint16
}
func (r *CapsuleResponse) Marshal(buf []byte) {
binary.LittleEndian.PutUint32(buf[0:], r.DW0)
binary.LittleEndian.PutUint32(buf[4:], r.DW1)
binary.LittleEndian.PutUint16(buf[8:], r.SQHD)
binary.LittleEndian.PutUint16(buf[10:], r.QueueID)
binary.LittleEndian.PutUint16(buf[12:], r.CID)
binary.LittleEndian.PutUint16(buf[14:], r.Status)
}
func (r *CapsuleResponse) Unmarshal(buf []byte) {
r.DW0 = binary.LittleEndian.Uint32(buf[0:])
r.DW1 = binary.LittleEndian.Uint32(buf[4:])
r.SQHD = binary.LittleEndian.Uint16(buf[8:])
r.QueueID = binary.LittleEndian.Uint16(buf[10:])
r.CID = binary.LittleEndian.Uint16(buf[12:])
r.Status = binary.LittleEndian.Uint16(buf[14:])
}
// ---------- C2HDataHeader / H2CDataHeader / R2THeader (16 bytes each) ----------
type C2HDataHeader struct {
CCCID uint16
DATAO uint32
DATAL uint32
}
func (h *C2HDataHeader) Marshal(buf []byte) {
for i := range buf[:c2hDataHdrSize] {
buf[i] = 0
}
binary.LittleEndian.PutUint16(buf[0:], h.CCCID)
binary.LittleEndian.PutUint32(buf[4:], h.DATAO)
binary.LittleEndian.PutUint32(buf[8:], h.DATAL)
}
func (h *C2HDataHeader) Unmarshal(buf []byte) {
h.CCCID = binary.LittleEndian.Uint16(buf[0:])
h.DATAO = binary.LittleEndian.Uint32(buf[4:])
h.DATAL = binary.LittleEndian.Uint32(buf[8:])
}
type R2THeader struct {
CCCID uint16
TAG uint16
DATAO uint32
DATAL uint32
}
func (h *R2THeader) Marshal(buf []byte) {
for i := range buf[:r2tHdrSize] {
buf[i] = 0
}
binary.LittleEndian.PutUint16(buf[0:], h.CCCID)
binary.LittleEndian.PutUint16(buf[2:], h.TAG)
binary.LittleEndian.PutUint32(buf[4:], h.DATAO)
binary.LittleEndian.PutUint32(buf[8:], h.DATAL)
}
func (h *R2THeader) Unmarshal(buf []byte) {
h.CCCID = binary.LittleEndian.Uint16(buf[0:])
h.TAG = binary.LittleEndian.Uint16(buf[2:])
h.DATAO = binary.LittleEndian.Uint32(buf[4:])
h.DATAL = binary.LittleEndian.Uint32(buf[8:])
}
type H2CDataHeader struct {
CCCID uint16
TAG uint16
DATAO uint32
DATAL uint32
}
func (h *H2CDataHeader) Marshal(buf []byte) {
for i := range buf[:h2cDataHdrSize] {
buf[i] = 0
}
binary.LittleEndian.PutUint16(buf[0:], h.CCCID)
binary.LittleEndian.PutUint16(buf[2:], h.TAG)
binary.LittleEndian.PutUint32(buf[4:], h.DATAO)
binary.LittleEndian.PutUint32(buf[8:], h.DATAL)
}
func (h *H2CDataHeader) Unmarshal(buf []byte) {
h.CCCID = binary.LittleEndian.Uint16(buf[0:])
h.TAG = binary.LittleEndian.Uint16(buf[2:])
h.DATAO = binary.LittleEndian.Uint32(buf[4:])
h.DATAL = binary.LittleEndian.Uint32(buf[8:])
}
// ---------- ConnectData (1024 bytes, payload of Fabric Connect) ----------
type ConnectData struct {
HostID [16]byte
CNTLID uint16
SubNQN string
HostNQN string
}
func (d *ConnectData) Marshal(buf []byte) {
for i := range buf[:connectDataSize] {
buf[i] = 0
}
copy(buf[0:16], d.HostID[:])
binary.LittleEndian.PutUint16(buf[16:], d.CNTLID)
copyNQN(buf[256:512], d.SubNQN)
copyNQN(buf[512:768], d.HostNQN)
}
func (d *ConnectData) Unmarshal(buf []byte) {
copy(d.HostID[:], buf[0:16])
d.CNTLID = binary.LittleEndian.Uint16(buf[16:])
d.SubNQN = extractNQN(buf[256:512])
d.HostNQN = extractNQN(buf[512:768])
}
func copyNQN(dst []byte, s string) {
n := copy(dst, s)
if n < len(dst) {
dst[n] = 0
}
}
func extractNQN(buf []byte) string {
for i, b := range buf {
if b == 0 {
return string(buf[:i])
}
}
return string(buf)
}
// ---------- Status word encoding helpers ----------
// MakeStatusField packs SCT + SC + DNR into the 16-bit CQE
// status field. The Phase Tag bit (bit 0) is NOT set here —
// that's a queue-level concern handled by the caller. T2's
// minimal session keeps Phase Tag at 0 throughout.
func MakeStatusField(sct, sc uint8, dnr bool) uint16 {
w := uint16(sct&0x07)<<9 | uint16(sc)<<1
if dnr {
w |= 1 << 15
}
return w
}