ISCSISession.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 ulong ISID; // Initiator Session ID
  27. public 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 static uint m_nextTransferTag;
  41. public uint GetNextTransferTag()
  42. {
  43. uint transferTag = m_nextTransferTag;
  44. m_nextTransferTag++;
  45. return transferTag;
  46. }
  47. public bool IsPrecedingCommandPending(uint cmdSN)
  48. {
  49. foreach (uint entry in CommandsInTransfer)
  50. {
  51. if (IsFirstCmdSNPreceding(entry, cmdSN))
  52. {
  53. return true;
  54. }
  55. }
  56. return false;
  57. }
  58. public List<SCSICommandPDU> GetDelayedCommandsReadyForExecution()
  59. {
  60. List<SCSICommandPDU> result = new List<SCSICommandPDU>();
  61. if (CommandsInTransfer.Count == 0)
  62. {
  63. result.AddRange(DelayedCommands);
  64. DelayedCommands.Clear();
  65. return result;
  66. }
  67. // We find the earliest CmdSN of the commands in transfer
  68. uint earliestCmdSN = CommandsInTransfer[0];
  69. for(int index = 1; index < CommandsInTransfer.Count; index++)
  70. {
  71. if (IsFirstCmdSNPreceding(CommandsInTransfer[index], earliestCmdSN))
  72. {
  73. earliestCmdSN = CommandsInTransfer[index];
  74. }
  75. }
  76. // Any command that is preceding minCmdSN should be executed
  77. for(int index = 0; index < DelayedCommands.Count; index++)
  78. {
  79. SCSICommandPDU delayedCommand = DelayedCommands[index];
  80. if (IsFirstCmdSNPreceding(delayedCommand.CmdSN, earliestCmdSN))
  81. {
  82. result.Add(delayedCommand);
  83. DelayedCommands.RemoveAt(index);
  84. index--;
  85. }
  86. }
  87. return result;
  88. }
  89. /// <summary>
  90. /// Returns true if cmdSN1 should be executed before cmdSN2
  91. /// </summary>
  92. public static bool IsFirstCmdSNPreceding(uint cmdSN1, uint cmdSN2)
  93. {
  94. // 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.
  95. const uint commandWindow = 2 ^ 31 - 1;
  96. if (cmdSN2 >= commandWindow)
  97. {
  98. if ((cmdSN1 > cmdSN2 - commandWindow) && (cmdSN1 < cmdSN2))
  99. {
  100. return true;
  101. }
  102. }
  103. else
  104. {
  105. if ((cmdSN1 > cmdSN2 - commandWindow) || (cmdSN1 < cmdSN2))
  106. {
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
  112. }
  113. }