TargetResponseHelper.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.NextR2NSN;
  94. response.BufferOffset = offset + request.DataSegmentLength; // where we left off
  95. response.DesiredDataTransferLength = Math.Min((uint)connection.TargetMaxRecvDataSegmentLength, totalLength - response.BufferOffset);
  96. transfer.NextR2NSN++;
  97. responseList.Add(response);
  98. return responseList;
  99. }
  100. }
  101. internal static List<ISCSIPDU> GetSCSICommandResponse(SCSICommandPDU command, SCSITarget target, SessionParameters session, ConnectionParameters connection)
  102. {
  103. string connectionIdentifier = ConnectionState.GetConnectionIdentifier(session, connection);
  104. byte[] scsiResponse;
  105. SCSIStatusCodeName status = target.ExecuteCommand(command.CommandDescriptorBlock, command.LUN, command.Data, out scsiResponse);
  106. return PrepareSCSICommandResponse(command, status, scsiResponse, connection);
  107. }
  108. internal static List<ISCSIPDU> PrepareSCSICommandResponse(SCSICommandPDU command, SCSIStatusCodeName status, byte[] scsiResponse, ConnectionParameters connection)
  109. {
  110. List<ISCSIPDU> responseList = new List<ISCSIPDU>();
  111. if (!command.Read || status != SCSIStatusCodeName.Good)
  112. {
  113. // RFC 3720: if the command is completed with an error, then the response and sense data MUST be sent in a SCSI Response PDU
  114. SCSIResponsePDU response = new SCSIResponsePDU();
  115. response.InitiatorTaskTag = command.InitiatorTaskTag;
  116. response.Status = status;
  117. response.Data = scsiResponse;
  118. if (command.Read)
  119. {
  120. EnforceExpectedDataTransferLength(response, command.ExpectedDataTransferLength);
  121. }
  122. responseList.Add(response);
  123. }
  124. else if (scsiResponse.Length <= connection.InitiatorMaxRecvDataSegmentLength)
  125. {
  126. SCSIDataInPDU response = new SCSIDataInPDU();
  127. response.InitiatorTaskTag = command.InitiatorTaskTag;
  128. response.Status = status;
  129. response.StatusPresent = true;
  130. response.Final = true;
  131. response.Data = scsiResponse;
  132. EnforceExpectedDataTransferLength(response, command.ExpectedDataTransferLength);
  133. responseList.Add(response);
  134. }
  135. else // we have to split the response to multiple Data-In PDUs
  136. {
  137. int bytesLeftToSend = scsiResponse.Length;
  138. uint dataSN = 0;
  139. while (bytesLeftToSend > 0)
  140. {
  141. int dataSegmentLength = Math.Min(connection.InitiatorMaxRecvDataSegmentLength, bytesLeftToSend);
  142. int dataOffset = scsiResponse.Length - bytesLeftToSend;
  143. SCSIDataInPDU response = new SCSIDataInPDU();
  144. response.InitiatorTaskTag = command.InitiatorTaskTag;
  145. if (bytesLeftToSend == dataSegmentLength)
  146. {
  147. // last Data-In PDU
  148. response.Status = status;
  149. response.StatusPresent = true;
  150. response.Final = true;
  151. }
  152. response.BufferOffset = (uint)dataOffset;
  153. response.DataSN = dataSN;
  154. dataSN++;
  155. response.Data = new byte[dataSegmentLength];
  156. Array.Copy(scsiResponse, dataOffset, response.Data, 0, dataSegmentLength);
  157. responseList.Add(response);
  158. bytesLeftToSend -= dataSegmentLength;
  159. }
  160. }
  161. return responseList;
  162. }
  163. public static void EnforceExpectedDataTransferLength(SCSIResponsePDU response, uint expectedDataTransferLength)
  164. {
  165. if (response.Data.Length > expectedDataTransferLength)
  166. {
  167. response.ResidualOverflow = true;
  168. response.ResidualCount = (uint)(response.Data.Length - expectedDataTransferLength);
  169. response.Data = ByteReader.ReadBytes(response.Data, 0, (int)expectedDataTransferLength);
  170. }
  171. else if (response.Data.Length < expectedDataTransferLength)
  172. {
  173. response.ResidualUnderflow = true;
  174. response.ResidualCount = (uint)(expectedDataTransferLength - response.Data.Length);
  175. }
  176. }
  177. public static void EnforceExpectedDataTransferLength(SCSIDataInPDU response, uint expectedDataTransferLength)
  178. {
  179. if (response.Data.Length > expectedDataTransferLength)
  180. {
  181. response.ResidualOverflow = true;
  182. response.ResidualCount = (uint)(response.Data.Length - expectedDataTransferLength);
  183. response.Data = ByteReader.ReadBytes(response.Data, 0, (int)expectedDataTransferLength);
  184. }
  185. else if (response.Data.Length < expectedDataTransferLength)
  186. {
  187. response.ResidualUnderflow = true;
  188. response.ResidualCount = (uint)(expectedDataTransferLength - response.Data.Length);
  189. }
  190. }
  191. }
  192. }