diff --git a/seaweed-block/core/frontend/iscsi/p1_os_initiator_test.go b/seaweed-block/core/frontend/iscsi/p1_os_initiator_test.go new file mode 100644 index 0000000000..d4e74e5d50 --- /dev/null +++ b/seaweed-block/core/frontend/iscsi/p1_os_initiator_test.go @@ -0,0 +1,146 @@ +package iscsi_test + +import ( + "bytes" + "testing" + + "github.com/seaweedfs/seaweed-block/core/frontend" + "github.com/seaweedfs/seaweed-block/core/frontend/iscsi" + "github.com/seaweedfs/seaweed-block/core/frontend/testback" +) + +// TestP1_ISCSI_R2TDataOut_AllowsPipelinedCommand pins the Linux +// kernel initiator shape observed during mkfs.ext4: while the target +// is collecting R2T-solicited Data-Out for one WRITE, another +// SCSI-Command can arrive on the same connection. That command must be +// queued and processed after the write completes; it must not tear down +// the session with "expected Data-Out, got SCSI-Command". +func TestP1_ISCSI_R2TDataOut_AllowsPipelinedCommand(t *testing.T) { + const ( + blocks = 256 // 128 KiB at 512-byte SCSI blocks. + maxBurst = 64 * 1024 + blockSize = int(iscsi.DefaultBlockSize) + ) + totalBytes := blocks * blockSize + + rec := testback.NewRecordingBackend(frontend.Identity{ + VolumeID: "v1", ReplicaID: "r1", Epoch: 5, EndpointVersion: 3, + }) + prov := testback.NewStaticProvider(rec) + neg := iscsi.DefaultNegotiableConfig() + neg.MaxBurstLength = maxBurst + neg.FirstBurstLength = 0 + tg := iscsi.NewTarget(iscsi.TargetConfig{ + Listen: "127.0.0.1:0", + IQN: "iqn.2026-04.example.v3:v1", + VolumeID: "v1", + Provider: prov, + Negotiation: neg, + }) + addr, err := tg.Start() + if err != nil { + t.Fatalf("target Start: %v", err) + } + defer tg.Close() + + cli := dialAndLogin(t, addr) + defer cli.logout(t) + + payload := make([]byte, totalBytes) + for i := range payload { + payload[i] = byte((i * 29) % 251) + } + + writeITT := cli.itt + writeCmd := &iscsi.PDU{} + writeCmd.SetOpcode(iscsi.OpSCSICmd) + writeCmd.SetOpSpecific1(iscsi.FlagF | iscsi.FlagW) + writeCmd.SetLUN(0) + writeCmd.SetInitiatorTaskTag(writeITT) + cli.itt++ + writeCmd.SetExpectedDataTransferLength(uint32(totalBytes)) + writeCmd.SetCmdSN(cli.cmdSN) + cli.cmdSN++ + writeCmd.SetExpStatSN(cli.statSN + 1) + writeCmd.SetCDB(writeCDB10(0, uint16(blocks))) + if err := iscsi.WritePDU(cli.conn, writeCmd); err != nil { + t.Fatalf("write SCSI WRITE(10): %v", err) + } + + r2t, err := iscsi.ReadPDU(cli.conn) + if err != nil { + t.Fatalf("read first R2T: %v", err) + } + if r2t.Opcode() != iscsi.OpR2T { + t.Fatalf("first response opcode=%s want R2T", iscsi.OpcodeName(r2t.Opcode())) + } + + turITT := cli.itt + tur := &iscsi.PDU{} + tur.SetOpcode(iscsi.OpSCSICmd) + tur.SetOpSpecific1(iscsi.FlagF) + tur.SetLUN(0) + tur.SetInitiatorTaskTag(turITT) + cli.itt++ + tur.SetCmdSN(cli.cmdSN) + cli.cmdSN++ + tur.SetExpStatSN(cli.statSN + 1) + tur.SetCDB([16]byte{iscsi.ScsiTestUnitReady}) + if err := iscsi.WritePDU(cli.conn, tur); err != nil { + t.Fatalf("write pipelined TEST_UNIT_READY: %v", err) + } + + sendDataOut := func(r2t *iscsi.PDU) { + t.Helper() + offset := r2t.BufferOffset() + desired := r2t.DesiredDataLength() + out := &iscsi.PDU{} + out.SetOpcode(iscsi.OpSCSIDataOut) + out.SetOpSpecific1(iscsi.FlagF) + out.SetLUN(0) + out.SetInitiatorTaskTag(writeITT) + out.SetTargetTransferTag(r2t.TargetTransferTag()) + out.SetDataSN(0) + out.SetBufferOffset(offset) + out.SetExpStatSN(cli.statSN + 1) + out.DataSegment = payload[offset : offset+desired] + if err := iscsi.WritePDU(cli.conn, out); err != nil { + t.Fatalf("write Data-Out offset=%d desired=%d: %v", offset, desired, err) + } + } + sendDataOut(r2t) + + var sawWriteResp, sawTURResp bool + for !(sawWriteResp && sawTURResp) { + resp, err := iscsi.ReadPDU(cli.conn) + if err != nil { + t.Fatalf("read response after pipelined command: %v", err) + } + switch resp.Opcode() { + case iscsi.OpR2T: + sendDataOut(resp) + case iscsi.OpSCSIResp: + if resp.SCSIStatusByte() != iscsi.StatusGood { + t.Fatalf("SCSI response ITT=0x%08x status=0x%02x want Good", + resp.InitiatorTaskTag(), resp.SCSIStatusByte()) + } + switch resp.InitiatorTaskTag() { + case writeITT: + sawWriteResp = true + case turITT: + sawTURResp = true + default: + t.Fatalf("unexpected SCSI response ITT=0x%08x", resp.InitiatorTaskTag()) + } + default: + t.Fatalf("unexpected opcode after Data-Out: %s", iscsi.OpcodeName(resp.Opcode())) + } + } + + if rec.WriteCount() != 1 { + t.Fatalf("backend WriteCount=%d want 1", rec.WriteCount()) + } + if got := rec.WriteAt(0).Data; !bytes.Equal(got, payload) { + t.Fatalf("backend write payload mismatch: got %d bytes want %d", len(got), len(payload)) + } +} diff --git a/seaweed-block/core/frontend/iscsi/session.go b/seaweed-block/core/frontend/iscsi/session.go index 8d0d3c591c..a056a29d2b 100644 --- a/seaweed-block/core/frontend/iscsi/session.go +++ b/seaweed-block/core/frontend/iscsi/session.go @@ -34,6 +34,13 @@ const ( SessionClosed ) +// maxPendingQueue bounds commands queued while a write is collecting +// R2T-solicited Data-Out. Linux initiators can pipeline a later SCSI +// command during an earlier write's Data-Out phase; queueing keeps that +// legal shape from tearing down the session, while the bound prevents an +// unbounded memory sink from a hostile initiator. +const maxPendingQueue = 64 + // Session carries one iSCSI session's per-connection state. // Created by the Target on accept; driven by serve(). // @@ -83,6 +90,12 @@ type Session struct { // Session lifetime. closed atomic.Bool + + // pending holds non-Data-Out PDUs read while collectWriteData is + // waiting for R2T-solicited Data-Out. The main loop drains this + // before reading the socket again, preserving serial command + // processing without rejecting initiator pipelining. + pending []*PDU } // Logger is a tiny abstraction so Target callers can pipe logs @@ -178,7 +191,7 @@ func (s *Session) loginPhase() error { func (s *Session) fullFeatureLoop(ctx context.Context) error { for s.state == SessionFullFeature { - pdu, err := ReadPDU(s.conn) + pdu, err := s.nextPDU() if err != nil { if err == io.EOF { return nil @@ -192,6 +205,17 @@ func (s *Session) fullFeatureLoop(ctx context.Context) error { return nil } +func (s *Session) nextPDU() (*PDU, error) { + if len(s.pending) > 0 { + pdu := s.pending[0] + copy(s.pending, s.pending[1:]) + s.pending[len(s.pending)-1] = nil + s.pending = s.pending[:len(s.pending)-1] + return pdu, nil + } + return ReadPDU(s.conn) +} + func (s *Session) dispatch(ctx context.Context, pdu *PDU) error { switch pdu.Opcode() { case OpSCSICmd: @@ -405,7 +429,11 @@ func (s *Session) collectWriteData(req *PDU, edtl uint32) ([]byte, error) { return nil, fmt.Errorf("read Data-Out: %w", err) } if pdu.Opcode() != OpSCSIDataOut { - return nil, fmt.Errorf("expected Data-Out, got %s", OpcodeName(pdu.Opcode())) + if len(s.pending) >= maxPendingQueue { + return nil, fmt.Errorf("pending queue overflow (%d PDUs)", maxPendingQueue) + } + s.pending = append(s.pending, pdu) + continue } if pdu.TargetTransferTag() != ttt { // R2T-solicited Data-Out MUST echo the target's TTT. diff --git a/seaweed-block/internal/docs/iscsi-os-initiator-compat-plan.md b/seaweed-block/internal/docs/iscsi-os-initiator-compat-plan.md index 94d7dee99c..fea54965f3 100644 --- a/seaweed-block/internal/docs/iscsi-os-initiator-compat-plan.md +++ b/seaweed-block/internal/docs/iscsi-os-initiator-compat-plan.md @@ -1,6 +1,6 @@ # iSCSI OS Initiator Compatibility Plan -Status: draft on `fix/iscsi-os-initiator-compat` +Status: P1-A implemented on `fix/iscsi-os-initiator-compat` Owner track: frontend compatibility @@ -168,6 +168,14 @@ Add a failing V3 test where: This test directly captures `expected Data-Out, got SCSI-Command`. +Implementation status: + +```text +TestP1_ISCSI_R2TDataOut_AllowsPipelinedCommand + old behavior: session failed with "expected Data-Out, got SCSI-Command" + fixed behavior: queued command is processed after WRITE completes +``` + ### Slice B: V3 DataOutCollector Extract collector semantics and tests: @@ -230,8 +238,9 @@ Only after WRITE/mkfs is green: ## Recommended Implementation Order -1. Write Slice A red test. -2. Add V3-local pending queue support in current serial session. +1. Write Slice A red test. Done. +2. Add V3-local pending queue support in current serial session. Done for the + single pipelined-command case; overflow test remains. 3. Extract V3-local `DataOutCollector`. 4. Add Data-Out timeout. 5. Run Linux OS initiator mkfs harness. @@ -277,4 +286,3 @@ This track can close when all are true: - mount + write/read byte-equal succeeds, - logout leaves no active iSCSI sessions, - public demo docs no longer need to warn that large OS format is blocked. -