ISCSISession.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. internal class ISCSISession
  13. {
  14. public int MaxConnections = DefaultParameters.Session.MaxConnections;
  15. public bool InitialR2T = DefaultParameters.Session.InitialR2T;
  16. public bool ImmediateData = DefaultParameters.Session.ImmediateData;
  17. public int MaxBurstLength = DefaultParameters.Session.MaxBurstLength;
  18. public int FirstBurstLength = DefaultParameters.Session.FirstBurstLength;
  19. public int DefaultTime2Wait = DefaultParameters.Session.DefaultTime2Wait;
  20. public int DefaultTime2Retain = DefaultParameters.Session.DefaultTime2Retain;
  21. public int MaxOutstandingR2T = DefaultParameters.Session.MaxOutstandingR2T;
  22. public bool DataPDUInOrder = DefaultParameters.Session.DataPDUInOrder;
  23. public bool DataSequenceInOrder = DefaultParameters.Session.DataSequenceInOrder;
  24. public int ErrorRecoveryLevel = DefaultParameters.Session.ErrorRecoveryLevel;
  25. public uint CommandQueueSize = ISCSIServer.DefaultCommandQueueSize;
  26. public readonly ulong ISID; // Initiator Session ID
  27. public readonly ushort TSIH; // Target Session Identifying Handle
  28. public bool IsDiscovery; // Indicate whether this is a discovery session
  29. public bool IsFullFeaturePhase; // Indicate whether login has been completed
  30. public bool CommandNumberingStarted;
  31. public uint ExpCmdSN;
  32. public ISCSITarget Target; // Across all connections within a session, an initiator sees one and the same target.
  33. public List<uint> CommandsInTransfer = new List<uint>();
  34. public List<SCSICommandPDU> DelayedCommands = new List<SCSICommandPDU>();
  35. /// <summary>
  36. /// Target Transfer Tag:
  37. /// There are no protocol specific requirements with regard to the value of these tags,
  38. /// but it is assumed that together with the LUN, they will enable the target to associate data with an R2T.
  39. /// </summary>
  40. private uint m_nextTransferTag = 0;
  41. private object m_transferTagLock = new object();
  42. public ISCSISession(ulong isid, ushort tsih)
  43. {
  44. ISID = isid;
  45. TSIH = tsih;
  46. }
  47. public uint GetNextTransferTag()
  48. {
  49. lock (m_transferTagLock)
  50. {
  51. uint transferTag = m_nextTransferTag;
  52. m_nextTransferTag++;
  53. return transferTag;
  54. }
  55. }
  56. public bool IsPrecedingCommandPending(uint cmdSN)
  57. {
  58. foreach (uint entry in CommandsInTransfer)
  59. {
  60. if (IsFirstCmdSNPreceding(entry, cmdSN))
  61. {
  62. return true;
  63. }
  64. }
  65. return false;
  66. }
  67. public List<SCSICommandPDU> GetDelayedCommandsReadyForExecution()
  68. {
  69. List<SCSICommandPDU> result = new List<SCSICommandPDU>();
  70. if (CommandsInTransfer.Count == 0)
  71. {
  72. result.AddRange(DelayedCommands);
  73. DelayedCommands.Clear();
  74. return result;
  75. }
  76. // We find the earliest CmdSN of the commands in transfer
  77. uint earliestCmdSN = CommandsInTransfer[0];
  78. for(int index = 1; index < CommandsInTransfer.Count; index++)
  79. {
  80. if (IsFirstCmdSNPreceding(CommandsInTransfer[index], earliestCmdSN))
  81. {
  82. earliestCmdSN = CommandsInTransfer[index];
  83. }
  84. }
  85. // Any command that is preceding minCmdSN should be executed
  86. for(int index = 0; index < DelayedCommands.Count; index++)
  87. {
  88. SCSICommandPDU delayedCommand = DelayedCommands[index];
  89. if (IsFirstCmdSNPreceding(delayedCommand.CmdSN, earliestCmdSN))
  90. {
  91. result.Add(delayedCommand);
  92. DelayedCommands.RemoveAt(index);
  93. index--;
  94. }
  95. }
  96. return result;
  97. }
  98. public string SessionIdentifier
  99. {
  100. get
  101. {
  102. return String.Format("ISID={0},TSIH={1}", ISID.ToString("x"), TSIH.ToString("x"));
  103. }
  104. }
  105. /// <summary>
  106. /// Returns true if cmdSN1 should be executed before cmdSN2
  107. /// </summary>
  108. public static bool IsFirstCmdSNPreceding(uint cmdSN1, uint cmdSN2)
  109. {
  110. // 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.
  111. const uint commandWindow = 2 ^ 31 - 1;
  112. if (cmdSN2 >= commandWindow)
  113. {
  114. if ((cmdSN1 > cmdSN2 - commandWindow) && (cmdSN1 < cmdSN2))
  115. {
  116. return true;
  117. }
  118. }
  119. else
  120. {
  121. if ((cmdSN1 > cmdSN2 - commandWindow) || (cmdSN1 < cmdSN2))
  122. {
  123. return true;
  124. }
  125. }
  126. return false;
  127. }
  128. }
  129. }