ISCSISession.cs 5.7 KB

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