TargetResponseHelper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. internal class TargetResponseHelper
  18. {
  19. internal static List<ReadyToTransferPDU> GetReadyToTransferPDUs(SCSICommandPDU command, ISCSISession 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 R2Ts:
  31. uint bytesLeft = command.ExpectedDataTransferLength - command.DataSegmentLength;
  32. uint nextOffset = command.DataSegmentLength;
  33. int totalR2Ts = (int)Math.Ceiling((double)bytesLeft / connection.TargetMaxRecvDataSegmentLength);
  34. int outgoingR2Ts = Math.Min(session.MaxOutstandingR2T, totalR2Ts);
  35. for (uint index = 0; index < outgoingR2Ts; index++)
  36. {
  37. ReadyToTransferPDU response = new ReadyToTransferPDU();
  38. response.InitiatorTaskTag = command.InitiatorTaskTag;
  39. response.R2TSN = index; // R2Ts are sequenced per command and must start with 0 for each new command;
  40. response.TargetTransferTag = transferTag;
  41. response.BufferOffset = nextOffset;
  42. response.DesiredDataTransferLength = Math.Min((uint)connection.TargetMaxRecvDataSegmentLength, command.ExpectedDataTransferLength - response.BufferOffset);
  43. responseList.Add(response);
  44. nextOffset += (uint)connection.TargetMaxRecvDataSegmentLength;
  45. }
  46. connection.AddTransfer(transferTag, command, (uint)outgoingR2Ts, nextOffset, (uint)totalR2Ts);
  47. session.CommandsInTransfer.Add(command.CmdSN);
  48. return responseList;
  49. }
  50. if (session.IsPrecedingCommandPending(command.CmdSN))
  51. {
  52. session.DelayedCommands.Add(command);
  53. }
  54. else
  55. {
  56. commandsToExecute.Add(command);
  57. }
  58. return responseList;
  59. }
  60. internal static List<ReadyToTransferPDU> GetReadyToTransferPDUs(SCSIDataOutPDU request, ISCSISession session, ConnectionParameters connection, out List<SCSICommandPDU> commandsToExecute)
  61. {
  62. List<ReadyToTransferPDU> responseList = new List<ReadyToTransferPDU>();
  63. commandsToExecute = new List<SCSICommandPDU>();
  64. string connectionIdentifier = ConnectionState.GetConnectionIdentifier(session, connection);
  65. TransferEntry transfer = connection.GetTransferEntry(request.TargetTransferTag);
  66. if (transfer == null)
  67. {
  68. throw new InvalidTargetTransferTagException(request.TargetTransferTag);
  69. }
  70. uint offset = request.BufferOffset;
  71. uint totalLength = (uint)transfer.Command.ExpectedDataTransferLength;
  72. // Store segment (we only execute the command after receiving all of its data)
  73. Array.Copy(request.Data, 0, transfer.Command.Data, offset, request.DataSegmentLength);
  74. if (offset + request.DataSegmentLength == totalLength)
  75. {
  76. // Last Data-out PDU
  77. if (session.IsPrecedingCommandPending(transfer.Command.CmdSN))
  78. {
  79. session.DelayedCommands.Add(transfer.Command);
  80. }
  81. else
  82. {
  83. commandsToExecute.Add(transfer.Command);
  84. connection.RemoveTransfer(request.TargetTransferTag);
  85. session.CommandsInTransfer.Remove(transfer.Command.CmdSN);
  86. // Check if delayed commands are ready to be executed
  87. List<SCSICommandPDU> pendingCommands = session.GetDelayedCommandsReadyForExecution();
  88. foreach (SCSICommandPDU pendingCommand in pendingCommands)
  89. {
  90. commandsToExecute.Add(pendingCommand);
  91. }
  92. }
  93. return responseList;
  94. }
  95. else
  96. {
  97. // RFC 3720: An R2T MAY be answered with one or more SCSI Data-Out PDUs with a matching Target Transfer Tag.
  98. // If an R2T is answered with a single Data-Out PDU, the Buffer Offset in the Data PDU MUST be the same as the one specified
  99. // by the R2T, and the data length of the Data PDU MUST be the same as the Desired Data Transfer Length specified in the R2T.
  100. // If the R2T is answered with a sequence of Data PDUs, the Buffer Offset and Length MUST be within
  101. // the range of those specified by R2T, and the last PDU MUST have the F bit set to 1.
  102. // An R2T is considered outstanding until the last data PDU is transferred.
  103. if (request.Final)
  104. {
  105. // We already sent as many R2T as we could, we will only send R2T if any remained.
  106. if (transfer.NextR2TSN < transfer.TotalR2Ts)
  107. {
  108. // Send R2T
  109. ReadyToTransferPDU response = new ReadyToTransferPDU();
  110. response.InitiatorTaskTag = request.InitiatorTaskTag;
  111. response.TargetTransferTag = request.TargetTransferTag;
  112. response.R2TSN = transfer.NextR2TSN;
  113. response.BufferOffset = transfer.NextOffset + request.DataSegmentLength; // where we left off
  114. response.DesiredDataTransferLength = Math.Min((uint)connection.TargetMaxRecvDataSegmentLength, totalLength - response.BufferOffset);
  115. responseList.Add(response);
  116. transfer.NextR2TSN++;
  117. transfer.NextOffset += (uint)connection.TargetMaxRecvDataSegmentLength;
  118. }
  119. }
  120. return responseList;
  121. }
  122. }
  123. internal static List<ISCSIPDU> PrepareSCSICommandResponse(SCSICommandPDU command, SCSIStatusCodeName status, byte[] scsiResponse, ConnectionParameters connection)
  124. {
  125. List<ISCSIPDU> responseList = new List<ISCSIPDU>();
  126. if (!command.Read || status != SCSIStatusCodeName.Good)
  127. {
  128. // RFC 3720: if the command is completed with an error, then the response and sense data MUST be sent in a SCSI Response PDU
  129. SCSIResponsePDU response = new SCSIResponsePDU();
  130. response.InitiatorTaskTag = command.InitiatorTaskTag;
  131. response.Status = status;
  132. response.Data = scsiResponse;
  133. if (command.Read)
  134. {
  135. EnforceExpectedDataTransferLength(response, command.ExpectedDataTransferLength);
  136. }
  137. responseList.Add(response);
  138. }
  139. else if (scsiResponse.Length <= connection.InitiatorMaxRecvDataSegmentLength)
  140. {
  141. SCSIDataInPDU response = new SCSIDataInPDU();
  142. response.InitiatorTaskTag = command.InitiatorTaskTag;
  143. response.Status = status;
  144. response.StatusPresent = true;
  145. response.Final = true;
  146. response.Data = scsiResponse;
  147. EnforceExpectedDataTransferLength(response, command.ExpectedDataTransferLength);
  148. responseList.Add(response);
  149. }
  150. else // we have to split the response to multiple Data-In PDUs
  151. {
  152. int bytesLeftToSend = scsiResponse.Length;
  153. uint dataSN = 0;
  154. while (bytesLeftToSend > 0)
  155. {
  156. int dataSegmentLength = Math.Min(connection.InitiatorMaxRecvDataSegmentLength, bytesLeftToSend);
  157. int dataOffset = scsiResponse.Length - bytesLeftToSend;
  158. SCSIDataInPDU response = new SCSIDataInPDU();
  159. response.InitiatorTaskTag = command.InitiatorTaskTag;
  160. if (bytesLeftToSend == dataSegmentLength)
  161. {
  162. // last Data-In PDU
  163. response.Status = status;
  164. response.StatusPresent = true;
  165. response.Final = true;
  166. }
  167. response.BufferOffset = (uint)dataOffset;
  168. response.DataSN = dataSN;
  169. dataSN++;
  170. response.Data = new byte[dataSegmentLength];
  171. Array.Copy(scsiResponse, dataOffset, response.Data, 0, dataSegmentLength);
  172. responseList.Add(response);
  173. bytesLeftToSend -= dataSegmentLength;
  174. }
  175. }
  176. return responseList;
  177. }
  178. public static void EnforceExpectedDataTransferLength(SCSIResponsePDU response, uint expectedDataTransferLength)
  179. {
  180. if (response.Data.Length > expectedDataTransferLength)
  181. {
  182. response.ResidualOverflow = true;
  183. response.ResidualCount = (uint)(response.Data.Length - expectedDataTransferLength);
  184. response.Data = ByteReader.ReadBytes(response.Data, 0, (int)expectedDataTransferLength);
  185. }
  186. else if (response.Data.Length < expectedDataTransferLength)
  187. {
  188. response.ResidualUnderflow = true;
  189. response.ResidualCount = (uint)(expectedDataTransferLength - response.Data.Length);
  190. }
  191. }
  192. public static void EnforceExpectedDataTransferLength(SCSIDataInPDU response, uint expectedDataTransferLength)
  193. {
  194. if (response.Data.Length > expectedDataTransferLength)
  195. {
  196. response.ResidualOverflow = true;
  197. response.ResidualCount = (uint)(response.Data.Length - expectedDataTransferLength);
  198. response.Data = ByteReader.ReadBytes(response.Data, 0, (int)expectedDataTransferLength);
  199. }
  200. else if (response.Data.Length < expectedDataTransferLength)
  201. {
  202. response.ResidualUnderflow = true;
  203. response.ResidualCount = (uint)(expectedDataTransferLength - response.Data.Length);
  204. }
  205. }
  206. }
  207. }