TargetResponseHelper.cs 11 KB

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