SessionParameters.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* Copyright (C) 2012-2016 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. namespace ISCSI.Server
  11. {
  12. public class SessionParameters
  13. {
  14. public const int DefaultMaxConnections = 1;
  15. public const bool DefaultInitialR2T = true;
  16. public const bool DefaultImmediateData = true;
  17. public const int DefaultMaxBurstLength = 262144;
  18. public const int DefaultFirstBurstLength = 65536;
  19. public const int DefaultDefaultTime2Wait = 2;
  20. public const int DefaultDefaultTime2Retain = 20;
  21. public const int DefaultMaxOutstandingR2T = 1;
  22. public const bool DefaultDataPDUInOrder = true;
  23. public const bool DefaultDataSequenceInOrder = true;
  24. public const int DefaultErrorRecoveryLevel = 0;
  25. public static uint DefaultCommandQueueSize = 64;
  26. /// <summary>
  27. /// The maximum number of connections per session.
  28. /// </summary>
  29. public int MaxConnections = DefaultMaxConnections;
  30. /// <summary>
  31. /// Allow the initiator to start sending data to a target as if it has received an initial R2T
  32. /// </summary>
  33. public bool InitialR2T = DefaultInitialR2T;
  34. public bool ImmediateData = DefaultImmediateData;
  35. /// <summary>
  36. /// The total of all the DataSegmentLength of all PDUs in a sequence MUST not exceed MaxBurstLength.
  37. /// 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).
  38. /// Irrelevant to the target in general, the initiator instructs us using ExpectedDataTransferLength.
  39. /// </summary>
  40. public int MaxBurstLength = DefaultMaxBurstLength;
  41. /// <summary>
  42. /// The total of all the DataSegmentLength of all PDUs in a sequence MUST not exceed FirstBurstLength for unsolicited data.
  43. /// Maximum amount in bytes of unsolicited [SCSI] data an iSCSI initiator may send to the target during the execution of a single SCSI command.
  44. /// Irrelevant to the target in general, irrelevant when (InitialR2T = Yes and) ImmediateData = No.
  45. /// </summary>
  46. public int FirstBurstLength = DefaultFirstBurstLength;
  47. /// <summary>
  48. /// minimum time, in seconds, to wait before attempting an explicit/implicit logout after connection termination / reset.
  49. /// </summary>
  50. public int DefaultTime2Wait = DefaultDefaultTime2Wait;
  51. /// <summary>
  52. /// maximum time, in seconds after an initial wait (Time2Wait), before which an active task reassignment
  53. /// is still possible after an unexpected connection termination or a connection reset.
  54. /// </summary>
  55. public int DefaultTime2Retain = DefaultDefaultTime2Retain;
  56. public int MaxOutstandingR2T = DefaultMaxOutstandingR2T;
  57. public bool DataPDUInOrder = DefaultDataPDUInOrder;
  58. public bool DataSequenceInOrder = DefaultDataSequenceInOrder;
  59. public int ErrorRecoveryLevel = DefaultErrorRecoveryLevel;
  60. /// <summary>
  61. /// - CommandQueueSize = 0 means the initiator can send one command at a time (because MaxCmdSN = ExpCmdSN + CommandQueueSize),
  62. /// (in this case there won't be any queue following the currently processed command).
  63. /// - Over a low-latency connection, most of the gain comes from increasing the queue size from 0 to 1
  64. /// - CmdSN is session wide, so CommandQueueSize is a session parameter.
  65. /// </summary>
  66. public uint CommandQueueSize = DefaultCommandQueueSize;
  67. public ulong ISID; // Initiator Session ID
  68. public ushort TSIH; // Target Session Identifying Handle
  69. public bool IsDiscovery; // Indicate whether this is a discovery session
  70. public bool IsFullFeaturePhase; // Indicate whether login has been completed
  71. public bool CommandNumberingStarted;
  72. public uint ExpCmdSN;
  73. public List<uint> CommandsInTransfer = new List<uint>();
  74. public List<SCSICommandPDU> DelayedCommands = new List<SCSICommandPDU>();
  75. /// <summary>
  76. /// Target Transfer Tag:
  77. /// There are no protocol specific requirements with regard to the value of these tags,
  78. /// but it is assumed that together with the LUN, they will enable the target to associate data with an R2T.
  79. /// </summary>
  80. private static uint m_nextTransferTag;
  81. public uint GetNextTransferTag()
  82. {
  83. uint transferTag = m_nextTransferTag;
  84. m_nextTransferTag++;
  85. return transferTag;
  86. }
  87. public bool IsPrecedingCommandPending(uint cmdSN)
  88. {
  89. foreach (uint entry in CommandsInTransfer)
  90. {
  91. if (IsFirstCmdSNPreceding(entry, cmdSN))
  92. {
  93. return true;
  94. }
  95. }
  96. return false;
  97. }
  98. public List<SCSICommandPDU> GetDelayedCommandsReadyForExecution()
  99. {
  100. List<SCSICommandPDU> result = new List<SCSICommandPDU>();
  101. if (CommandsInTransfer.Count == 0)
  102. {
  103. result.AddRange(DelayedCommands);
  104. DelayedCommands.Clear();
  105. return result;
  106. }
  107. // We find the earliest CmdSN of the commands in transfer
  108. uint earliestCmdSN = CommandsInTransfer[0];
  109. for(int index = 1; index < CommandsInTransfer.Count; index++)
  110. {
  111. if (IsFirstCmdSNPreceding(CommandsInTransfer[index], earliestCmdSN))
  112. {
  113. earliestCmdSN = CommandsInTransfer[index];
  114. }
  115. }
  116. // Any command that is preceding minCmdSN should be executed
  117. for(int index = 0; index < DelayedCommands.Count; index++)
  118. {
  119. SCSICommandPDU delayedCommand = DelayedCommands[index];
  120. if (IsFirstCmdSNPreceding(delayedCommand.CmdSN, earliestCmdSN))
  121. {
  122. result.Add(delayedCommand);
  123. DelayedCommands.RemoveAt(index);
  124. index--;
  125. }
  126. }
  127. return result;
  128. }
  129. /// <summary>
  130. /// Returns true if cmdSN1 should be executed before cmdSN2
  131. /// </summary>
  132. public static bool IsFirstCmdSNPreceding(uint cmdSN1, uint cmdSN2)
  133. {
  134. // 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.
  135. const uint commandWindow = 2 ^ 31 - 1;
  136. if (cmdSN2 >= commandWindow)
  137. {
  138. if ((cmdSN1 > cmdSN2 - commandWindow) && (cmdSN1 < cmdSN2))
  139. {
  140. return true;
  141. }
  142. }
  143. else
  144. {
  145. if ((cmdSN1 > cmdSN2 - commandWindow) || (cmdSN1 < cmdSN2))
  146. {
  147. return true;
  148. }
  149. }
  150. return false;
  151. }
  152. }
  153. }