TargetResponseHelper.cs 12 KB

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