SessionParameters.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 static uint DefaultCommandQueueSize = 64;
  15. public int MaxConnections = DefaultParameters.Session.MaxConnections;
  16. public bool InitialR2T = DefaultParameters.Session.InitialR2T;
  17. public bool ImmediateData = DefaultParameters.Session.ImmediateData;
  18. public int MaxBurstLength = DefaultParameters.Session.MaxBurstLength;
  19. public int FirstBurstLength = DefaultParameters.Session.FirstBurstLength;
  20. public int DefaultTime2Wait = DefaultParameters.Session.DefaultTime2Wait;
  21. public int DefaultTime2Retain = DefaultParameters.Session.DefaultTime2Retain;
  22. public int MaxOutstandingR2T = DefaultParameters.Session.MaxOutstandingR2T;
  23. public bool DataPDUInOrder = DefaultParameters.Session.DataPDUInOrder;
  24. public bool DataSequenceInOrder = DefaultParameters.Session.DataSequenceInOrder;
  25. public int ErrorRecoveryLevel = DefaultParameters.Session.ErrorRecoveryLevel;
  26. /// <summary>
  27. /// - CommandQueueSize = 0 means the initiator can send one command at a time (because MaxCmdSN = ExpCmdSN + CommandQueueSize),
  28. /// (in this case there won't be any queue following the currently processed command).
  29. /// - Over a low-latency connection, most of the gain comes from increasing the queue size from 0 to 1
  30. /// - CmdSN is session wide, so CommandQueueSize is a session parameter.
  31. /// </summary>
  32. public uint CommandQueueSize = DefaultCommandQueueSize;
  33. public ulong ISID; // Initiator Session ID
  34. public ushort TSIH; // Target Session Identifying Handle
  35. public bool IsDiscovery; // Indicate whether this is a discovery session
  36. public bool IsFullFeaturePhase; // Indicate whether login has been completed
  37. public bool CommandNumberingStarted;
  38. public uint ExpCmdSN;
  39. public List<uint> CommandsInTransfer = new List<uint>();
  40. public List<SCSICommandPDU> DelayedCommands = new List<SCSICommandPDU>();
  41. /// <summary>
  42. /// Target Transfer Tag:
  43. /// There are no protocol specific requirements with regard to the value of these tags,
  44. /// but it is assumed that together with the LUN, they will enable the target to associate data with an R2T.
  45. /// </summary>
  46. private static uint m_nextTransferTag;
  47. public uint GetNextTransferTag()
  48. {
  49. uint transferTag = m_nextTransferTag;
  50. m_nextTransferTag++;
  51. return transferTag;
  52. }
  53. public bool IsPrecedingCommandPending(uint cmdSN)
  54. {
  55. foreach (uint entry in CommandsInTransfer)
  56. {
  57. if (IsFirstCmdSNPreceding(entry, cmdSN))
  58. {
  59. return true;
  60. }
  61. }
  62. return false;
  63. }
  64. public List<SCSICommandPDU> GetDelayedCommandsReadyForExecution()
  65. {
  66. List<SCSICommandPDU> result = new List<SCSICommandPDU>();
  67. if (CommandsInTransfer.Count == 0)
  68. {
  69. result.AddRange(DelayedCommands);
  70. DelayedCommands.Clear();
  71. return result;
  72. }
  73. // We find the earliest CmdSN of the commands in transfer
  74. uint earliestCmdSN = CommandsInTransfer[0];
  75. for(int index = 1; index < CommandsInTransfer.Count; index++)
  76. {
  77. if (IsFirstCmdSNPreceding(CommandsInTransfer[index], earliestCmdSN))
  78. {
  79. earliestCmdSN = CommandsInTransfer[index];
  80. }
  81. }
  82. // Any command that is preceding minCmdSN should be executed
  83. for(int index = 0; index < DelayedCommands.Count; index++)
  84. {
  85. SCSICommandPDU delayedCommand = DelayedCommands[index];
  86. if (IsFirstCmdSNPreceding(delayedCommand.CmdSN, earliestCmdSN))
  87. {
  88. result.Add(delayedCommand);
  89. DelayedCommands.RemoveAt(index);
  90. index--;
  91. }
  92. }
  93. return result;
  94. }
  95. /// <summary>
  96. /// Returns true if cmdSN1 should be executed before cmdSN2
  97. /// </summary>
  98. public static bool IsFirstCmdSNPreceding(uint cmdSN1, uint cmdSN2)
  99. {
  100. // 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.
  101. const uint commandWindow = 2 ^ 31 - 1;
  102. if (cmdSN2 >= commandWindow)
  103. {
  104. if ((cmdSN1 > cmdSN2 - commandWindow) && (cmdSN1 < cmdSN2))
  105. {
  106. return true;
  107. }
  108. }
  109. else
  110. {
  111. if ((cmdSN1 > cmdSN2 - commandWindow) || (cmdSN1 < cmdSN2))
  112. {
  113. return true;
  114. }
  115. }
  116. return false;
  117. }
  118. }
  119. }