/* Copyright (C) 2012-2016 Tal Aloni . All rights reserved. * * You can redistribute this program and/or modify it under the terms of * the GNU Lesser Public License as published by the Free Software Foundation, * either version 3 of the License, or (at your option) any later version. */ using System; using System.Collections.Generic; using System.Text; namespace ISCSI.Server { public class SessionParameters { public const int DefaultMaxConnections = 1; public const bool DefaultInitialR2T = true; public const bool DefaultImmediateData = true; public const int DefaultMaxBurstLength = 262144; public const int DefaultFirstBurstLength = 65536; public const int DefaultDefaultTime2Wait = 2; public const int DefaultDefaultTime2Retain = 20; public const int DefaultMaxOutstandingR2T = 1; public const bool DefaultDataPDUInOrder = true; public const bool DefaultDataSequenceInOrder = true; public const int DefaultErrorRecoveryLevel = 0; public static uint DefaultCommandQueueSize = 64; /// /// The maximum number of connections per session. /// public int MaxConnections = DefaultMaxConnections; /// /// Allow the initiator to start sending data to a target as if it has received an initial R2T /// public bool InitialR2T = DefaultInitialR2T; public bool ImmediateData = DefaultImmediateData; /// /// The total of all the DataSegmentLength of all PDUs in a sequence MUST not exceed MaxBurstLength. /// Maximum SCSI data payload in bytes in a Data-In or a solicited Data-Out iSCSI sequence (i.e. that belongs to a single command). /// Irrelevant to the target in general, the initiator instructs us using ExpectedDataTransferLength. /// public int MaxBurstLength = DefaultMaxBurstLength; /// /// The total of all the DataSegmentLength of all PDUs in a sequence MUST not exceed FirstBurstLength for unsolicited data. /// Maximum amount in bytes of unsolicited [SCSI] data an iSCSI initiator may send to the target during the execution of a single SCSI command. /// Irrelevant to the target in general, irrelevant when (InitialR2T = Yes and) ImmediateData = No. /// public int FirstBurstLength = DefaultFirstBurstLength; /// /// minimum time, in seconds, to wait before attempting an explicit/implicit logout after connection termination / reset. /// public int DefaultTime2Wait = DefaultDefaultTime2Wait; /// /// maximum time, in seconds after an initial wait (Time2Wait), before which an active task reassignment /// is still possible after an unexpected connection termination or a connection reset. /// public int DefaultTime2Retain = DefaultDefaultTime2Retain; public int MaxOutstandingR2T = DefaultMaxOutstandingR2T; public bool DataPDUInOrder = DefaultDataPDUInOrder; public bool DataSequenceInOrder = DefaultDataSequenceInOrder; public int ErrorRecoveryLevel = DefaultErrorRecoveryLevel; /// /// - CommandQueueSize = 0 means the initiator can send one command at a time (because MaxCmdSN = ExpCmdSN + CommandQueueSize), /// (in this case there won't be any queue following the currently processed command). /// - Over a low-latency connection, most of the gain comes from increasing the queue size from 0 to 1 /// - CmdSN is session wide, so CommandQueueSize is a session parameter. /// public uint CommandQueueSize = DefaultCommandQueueSize; public ulong ISID; // Initiator Session ID public ushort TSIH; // Target Session Identifying Handle public bool IsDiscovery; // Indicate whether this is a discovery session public bool IsFullFeaturePhase; // Indicate whether login has been completed public bool CommandNumberingStarted; public uint ExpCmdSN; public List CommandsInTransfer = new List(); public List DelayedCommands = new List(); /// /// Target Transfer Tag: /// There are no protocol specific requirements with regard to the value of these tags, /// but it is assumed that together with the LUN, they will enable the target to associate data with an R2T. /// private static uint m_nextTransferTag; public uint GetNextTransferTag() { uint transferTag = m_nextTransferTag; m_nextTransferTag++; return transferTag; } public bool IsPrecedingCommandPending(uint cmdSN) { foreach (uint entry in CommandsInTransfer) { if (IsFirstCmdSNPreceding(entry, cmdSN)) { return true; } } return false; } public List GetDelayedCommandsReadyForExecution() { List result = new List(); if (CommandsInTransfer.Count == 0) { result.AddRange(DelayedCommands); DelayedCommands.Clear(); return result; } // We find the earliest CmdSN of the commands in transfer uint earliestCmdSN = CommandsInTransfer[0]; for(int index = 1; index < CommandsInTransfer.Count; index++) { if (IsFirstCmdSNPreceding(CommandsInTransfer[index], earliestCmdSN)) { earliestCmdSN = CommandsInTransfer[index]; } } // Any command that is preceding minCmdSN should be executed for(int index = 0; index < DelayedCommands.Count; index++) { SCSICommandPDU delayedCommand = DelayedCommands[index]; if (IsFirstCmdSNPreceding(delayedCommand.CmdSN, earliestCmdSN)) { result.Add(delayedCommand); DelayedCommands.RemoveAt(index); index--; } } return result; } /// /// Returns true if cmdSN1 should be executed before cmdSN2 /// public static bool IsFirstCmdSNPreceding(uint cmdSN1, uint cmdSN2) { // The iSCSI protocol is designed to avoid having old, retried command instances appear in a valid command window after a command sequence number wrap around. const uint commandWindow = 2 ^ 31 - 1; if (cmdSN2 >= commandWindow) { if ((cmdSN1 > cmdSN2 - commandWindow) && (cmdSN1 < cmdSN2)) { return true; } } else { if ((cmdSN1 > cmdSN2 - commandWindow) || (cmdSN1 < cmdSN2)) { return true; } } return false; } } }