TargetResponseHelper.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.IO;
  10. using System.Text;
  11. using System.Runtime.InteropServices;
  12. using DiskAccessLibrary;
  13. using SCSI;
  14. using Utilities;
  15. namespace ISCSI.Server
  16. {
  17. public class TargetResponseHelper
  18. {
  19. internal static List<ReadyToTransferPDU> GetReadyToTransferPDUs(SCSICommandPDU command, SCSITarget target, SessionParameters session, ConnectionParameters connection, out List<SCSICommandPDU> commandsToExecute)
  20. {
  21. // We return either SCSIResponsePDU or List<SCSIDataInPDU>
  22. List<ReadyToTransferPDU> responseList = new List<ReadyToTransferPDU>();
  23. commandsToExecute = new List<SCSICommandPDU>();
  24. string connectionIdentifier = ConnectionState.GetConnectionIdentifier(session, connection);
  25. if (command.Write && command.DataSegmentLength < command.ExpectedDataTransferLength)
  26. {
  27. uint transferTag = session.GetNextTransferTag();
  28. // Create buffer for next segments (we only execute the command after receiving all of its data)
  29. Array.Resize<byte>(ref command.Data, (int)command.ExpectedDataTransferLength);
  30. // Send R2T
  31. ReadyToTransferPDU response = new ReadyToTransferPDU();
  32. response.InitiatorTaskTag = command.InitiatorTaskTag;
  33. response.R2TSN = 0; // R2Ts are sequenced per command and must start with 0 for each new command;
  34. response.TargetTransferTag = transferTag;
  35. response.BufferOffset = command.DataSegmentLength;
  36. response.DesiredDataTransferLength = Math.Min((uint)connection.TargetMaxRecvDataSegmentLength, command.ExpectedDataTransferLength - response.BufferOffset);
  37. connection.AddTransfer(transferTag, command, 1);
  38. session.CommandsInTransfer.Add(command.CmdSN);
  39. responseList.Add(response);
  40. return responseList;
  41. }
  42. if (session.IsPrecedingCommandPending(command.CmdSN))
  43. {
  44. session.DelayedCommands.Add(command);
  45. }
  46. else
  47. {
  48. commandsToExecute.Add(command);
  49. }
  50. return responseList;
  51. }
  52. internal static List<ReadyToTransferPDU> GetReadyToTransferPDUs(SCSIDataOutPDU request, SCSITarget target, SessionParameters session, ConnectionParameters connection, out List<SCSICommandPDU> commandsToExecute)
  53. {
  54. List<ReadyToTransferPDU> responseList = new List<ReadyToTransferPDU>();
  55. commandsToExecute = new List<SCSICommandPDU>();
  56. string connectionIdentifier = ConnectionState.GetConnectionIdentifier(session, connection);
  57. TransferEntry transfer = connection.GetTransferEntry(request.TargetTransferTag);
  58. if (transfer == null)
  59. {
  60. throw new InvalidTargetTransferTagException(request.TargetTransferTag);
  61. }
  62. uint offset = request.BufferOffset;
  63. uint totalLength = (uint)transfer.Command.ExpectedDataTransferLength;
  64. // Store segment (we only execute the command after receiving all of its data)
  65. Array.Copy(request.Data, 0, transfer.Command.Data, offset, request.DataSegmentLength);
  66. if (offset + request.DataSegmentLength == totalLength)
  67. {
  68. // Last Data-out PDU
  69. if (session.IsPrecedingCommandPending(transfer.Command.CmdSN))
  70. {
  71. session.DelayedCommands.Add(transfer.Command);
  72. }
  73. else
  74. {
  75. commandsToExecute.Add(transfer.Command);
  76. connection.RemoveTransfer(request.TargetTransferTag);
  77. session.CommandsInTransfer.Remove(transfer.Command.CmdSN);
  78. // Check if delayed commands are ready to be executed
  79. List<SCSICommandPDU> pendingCommands = session.GetDelayedCommandsReadyForExecution();
  80. foreach (SCSICommandPDU pendingCommand in pendingCommands)
  81. {
  82. commandsToExecute.Add(pendingCommand);
  83. }
  84. }
  85. return responseList;
  86. }
  87. else
  88. {
  89. // Send R2T
  90. ReadyToTransferPDU response = new ReadyToTransferPDU();
  91. response.InitiatorTaskTag = request.InitiatorTaskTag;
  92. response.TargetTransferTag = request.TargetTransferTag;
  93. response.R2TSN = transfer.NextR2TSN;
  94. response.BufferOffset = offset + request.DataSegmentLength; // where we left off
  95. response.DesiredDataTransferLength = Math.Min((uint)connection.TargetMaxRecvDataSegmentLength, totalLength - response.BufferOffset);
  96. transfer.NextR2TSN++;
  97. responseList.Add(response);
  98. return responseList;
  99. }
  100. }
  101. internal static List<ISCSIPDU> PrepareSCSICommandResponse(SCSICommandPDU command, SCSIStatusCodeName status, byte[] scsiResponse, ConnectionParameters connection)
  102. {
  103. List<ISCSIPDU> responseList = new List<ISCSIPDU>();
  104. if (!command.Read || status != SCSIStatusCodeName.Good)
  105. {
  106. // RFC 3720: if the command is completed with an error, then the response and sense data MUST be sent in a SCSI Response PDU
  107. SCSIResponsePDU response = new SCSIResponsePDU();
  108. response.InitiatorTaskTag = command.InitiatorTaskTag;
  109. response.Status = status;
  110. response.Data = scsiResponse;
  111. if (command.Read)
  112. {
  113. EnforceExpectedDataTransferLength(response, command.ExpectedDataTransferLength);
  114. }
  115. responseList.Add(response);
  116. }
  117. else if (scsiResponse.Length <= connection.InitiatorMaxRecvDataSegmentLength)
  118. {
  119. SCSIDataInPDU response = new SCSIDataInPDU();
  120. response.InitiatorTaskTag = command.InitiatorTaskTag;
  121. response.Status = status;
  122. response.StatusPresent = true;
  123. response.Final = true;
  124. response.Data = scsiResponse;
  125. EnforceExpectedDataTransferLength(response, command.ExpectedDataTransferLength);
  126. responseList.Add(response);
  127. }
  128. else // we have to split the response to multiple Data-In PDUs
  129. {
  130. int bytesLeftToSend = scsiResponse.Length;
  131. uint dataSN = 0;
  132. while (bytesLeftToSend > 0)
  133. {
  134. int dataSegmentLength = Math.Min(connection.InitiatorMaxRecvDataSegmentLength, bytesLeftToSend);
  135. int dataOffset = scsiResponse.Length - bytesLeftToSend;
  136. SCSIDataInPDU response = new SCSIDataInPDU();
  137. response.InitiatorTaskTag = command.InitiatorTaskTag;
  138. if (bytesLeftToSend == dataSegmentLength)
  139. {
  140. // last Data-In PDU
  141. response.Status = status;
  142. response.StatusPresent = true;
  143. response.Final = true;
  144. }
  145. response.BufferOffset = (uint)dataOffset;
  146. response.DataSN = dataSN;
  147. dataSN++;
  148. response.Data = new byte[dataSegmentLength];
  149. Array.Copy(scsiResponse, dataOffset, response.Data, 0, dataSegmentLength);
  150. responseList.Add(response);
  151. bytesLeftToSend -= dataSegmentLength;
  152. }
  153. }
  154. return responseList;
  155. }
  156. public static void EnforceExpectedDataTransferLength(SCSIResponsePDU response, uint expectedDataTransferLength)
  157. {
  158. if (response.Data.Length > expectedDataTransferLength)
  159. {
  160. response.ResidualOverflow = true;
  161. response.ResidualCount = (uint)(response.Data.Length - expectedDataTransferLength);
  162. response.Data = ByteReader.ReadBytes(response.Data, 0, (int)expectedDataTransferLength);
  163. }
  164. else if (response.Data.Length < expectedDataTransferLength)
  165. {
  166. response.ResidualUnderflow = true;
  167. response.ResidualCount = (uint)(expectedDataTransferLength - response.Data.Length);
  168. }
  169. }
  170. public static void EnforceExpectedDataTransferLength(SCSIDataInPDU response, uint expectedDataTransferLength)
  171. {
  172. if (response.Data.Length > expectedDataTransferLength)
  173. {
  174. response.ResidualOverflow = true;
  175. response.ResidualCount = (uint)(response.Data.Length - expectedDataTransferLength);
  176. response.Data = ByteReader.ReadBytes(response.Data, 0, (int)expectedDataTransferLength);
  177. }
  178. else if (response.Data.Length < expectedDataTransferLength)
  179. {
  180. response.ResidualUnderflow = true;
  181. response.ResidualCount = (uint)(expectedDataTransferLength - response.Data.Length);
  182. }
  183. }
  184. }
  185. }