TargetResponseHelper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 = StateObject.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 = StateObject.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. ushort LUN = (ushort)request.LUN;
  68. Disk disk = target.Disks[LUN];
  69. uint offset = request.BufferOffset;
  70. uint totalLength = (uint)transfer.Command.ExpectedDataTransferLength;
  71. // Store segment (we only execute the command after receiving all of its data)
  72. Array.Copy(request.Data, 0, transfer.Command.Data, offset, request.DataSegmentLength);
  73. ISCSIServer.Log(String.Format("[{0}][GetSCSIDataOutResponsePDU] Buffer offset: {1}, Total length: {2}", connectionIdentifier, offset, totalLength));
  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. ISCSIServer.Log("[{0}] Queuing Command: CmdSN: {1}", connectionIdentifier, pendingCommand.CmdSN);
  91. commandsToExecute.Add(pendingCommand);
  92. }
  93. }
  94. return responseList;
  95. }
  96. else
  97. {
  98. // Send R2T
  99. ReadyToTransferPDU response = new ReadyToTransferPDU();
  100. response.InitiatorTaskTag = request.InitiatorTaskTag;
  101. response.TargetTransferTag = request.TargetTransferTag;
  102. response.R2TSN = transfer.NextR2NSN;
  103. response.BufferOffset = offset + request.DataSegmentLength; // where we left off
  104. response.DesiredDataTransferLength = Math.Min((uint)connection.TargetMaxRecvDataSegmentLength, totalLength - response.BufferOffset);
  105. transfer.NextR2NSN++;
  106. responseList.Add(response);
  107. return responseList;
  108. }
  109. }
  110. internal static List<ISCSIPDU> GetSCSICommandResponse(SCSICommandPDU command, ISCSITarget target, SessionParameters session, ConnectionParameters connection)
  111. {
  112. List<ISCSIPDU> responseList = new List<ISCSIPDU>();
  113. string connectionIdentifier = StateObject.GetConnectionIdentifier(session, connection);
  114. ISCSIServer.Log("[{0}] Executing Command: CmdSN: {1}", connectionIdentifier, command.CmdSN);
  115. byte[] scsiResponse;
  116. SCSIStatusCodeName status = target.ExecuteCommand(command.CommandDescriptorBlock, command.LUN, command.Data, out scsiResponse);
  117. if (!command.Read || status != SCSIStatusCodeName.Good)
  118. {
  119. // RFC 3720: if the command is completed with an error, then the response and sense data MUST be sent in a SCSI Response PDU
  120. SCSIResponsePDU response = new SCSIResponsePDU();
  121. response.InitiatorTaskTag = command.InitiatorTaskTag;
  122. response.Status = status;
  123. response.Data = scsiResponse;
  124. if (command.Read)
  125. {
  126. EnforceExpectedDataTransferLength(response, command.ExpectedDataTransferLength);
  127. }
  128. responseList.Add(response);
  129. }
  130. else if (scsiResponse.Length <= connection.InitiatorMaxRecvDataSegmentLength)
  131. {
  132. SCSIDataInPDU response = new SCSIDataInPDU();
  133. response.InitiatorTaskTag = command.InitiatorTaskTag;
  134. response.Status = status;
  135. response.StatusPresent = true;
  136. response.Final = true;
  137. response.Data = scsiResponse;
  138. EnforceExpectedDataTransferLength(response, command.ExpectedDataTransferLength);
  139. responseList.Add(response);
  140. }
  141. else // we have to split the response to multiple Data-In PDUs
  142. {
  143. int bytesLeftToSend = scsiResponse.Length;
  144. uint dataSN = 0;
  145. while (bytesLeftToSend > 0)
  146. {
  147. int dataSegmentLength = Math.Min(connection.InitiatorMaxRecvDataSegmentLength, bytesLeftToSend);
  148. int dataOffset = scsiResponse.Length - bytesLeftToSend;
  149. SCSIDataInPDU response = new SCSIDataInPDU();
  150. response.InitiatorTaskTag = command.InitiatorTaskTag;
  151. if (bytesLeftToSend == dataSegmentLength)
  152. {
  153. // last Data-In PDU
  154. response.Status = status;
  155. response.StatusPresent = true;
  156. response.Final = true;
  157. }
  158. response.BufferOffset = (uint)dataOffset;
  159. response.DataSN = dataSN;
  160. dataSN++;
  161. response.Data = new byte[dataSegmentLength];
  162. Array.Copy(scsiResponse, dataOffset, response.Data, 0, dataSegmentLength);
  163. responseList.Add(response);
  164. bytesLeftToSend -= dataSegmentLength;
  165. }
  166. }
  167. return responseList;
  168. }
  169. public static void EnforceExpectedDataTransferLength(SCSIResponsePDU response, uint expectedDataTransferLength)
  170. {
  171. if (response.Data.Length > expectedDataTransferLength)
  172. {
  173. response.ResidualOverflow = true;
  174. response.ResidualCount = (uint)(response.Data.Length - expectedDataTransferLength);
  175. response.Data = ByteReader.ReadBytes(response.Data, 0, (int)expectedDataTransferLength);
  176. }
  177. else if (response.Data.Length < expectedDataTransferLength)
  178. {
  179. response.ResidualUnderflow = true;
  180. response.ResidualCount = (uint)(expectedDataTransferLength - response.Data.Length);
  181. }
  182. }
  183. public static void EnforceExpectedDataTransferLength(SCSIDataInPDU response, uint expectedDataTransferLength)
  184. {
  185. if (response.Data.Length > expectedDataTransferLength)
  186. {
  187. response.ResidualOverflow = true;
  188. response.ResidualCount = (uint)(response.Data.Length - expectedDataTransferLength);
  189. response.Data = ByteReader.ReadBytes(response.Data, 0, (int)expectedDataTransferLength);
  190. }
  191. else if (response.Data.Length < expectedDataTransferLength)
  192. {
  193. response.ResidualUnderflow = true;
  194. response.ResidualCount = (uint)(expectedDataTransferLength - response.Data.Length);
  195. }
  196. }
  197. }
  198. }