TargetResponseHelper.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. responseList.Add(response);
  38. return responseList;
  39. }
  40. commandsToExecute.Add(command);
  41. return responseList;
  42. }
  43. internal static List<ISCSIPDU> GetReadyToTransferPDUs(SCSIDataOutPDU request, ISCSITarget target, SessionParameters session, ConnectionParameters connection, out List<SCSICommandPDU> commandsToExecute)
  44. {
  45. List<ISCSIPDU> responseList = new List<ISCSIPDU>();
  46. commandsToExecute = new List<SCSICommandPDU>();
  47. string connectionIdentifier = StateObject.GetConnectionIdentifier(session, connection);
  48. TransferEntry transfer = connection.GetTransferEntry(request.TargetTransferTag);
  49. if (transfer == null)
  50. {
  51. ISCSIServer.Log("[{0}][GetSCSIDataOutResponsePDU] Invalid TargetTransferTag {1}", connectionIdentifier, request.TargetTransferTag);
  52. RejectPDU reject = new RejectPDU();
  53. reject.InitiatorTaskTag = request.InitiatorTaskTag;
  54. reject.Reason = RejectReason.InvalidPDUField;
  55. reject.Data = ByteReader.ReadBytes(request.GetBytes(), 0, 48);
  56. responseList.Add(reject);
  57. return responseList;
  58. }
  59. ushort LUN = (ushort)request.LUN;
  60. Disk disk = target.Disks[LUN];
  61. uint offset = request.BufferOffset;
  62. uint totalLength = (uint)transfer.Command.ExpectedDataTransferLength;
  63. // Store segment (we only execute the command after receiving all of its data)
  64. Array.Copy(request.Data, 0, transfer.Command.Data, offset, request.DataSegmentLength);
  65. ISCSIServer.Log(String.Format("[{0}][GetSCSIDataOutResponsePDU] Buffer offset: {1}, Total length: {2}", connectionIdentifier, offset, totalLength));
  66. if (offset + request.DataSegmentLength == totalLength)
  67. {
  68. // Last Data-out PDU
  69. commandsToExecute.Add(transfer.Command);
  70. return responseList;
  71. }
  72. else
  73. {
  74. // Send R2T
  75. ReadyToTransferPDU response = new ReadyToTransferPDU();
  76. response.InitiatorTaskTag = request.InitiatorTaskTag;
  77. response.TargetTransferTag = request.TargetTransferTag;
  78. response.R2TSN = transfer.NextR2NSN;
  79. response.BufferOffset = offset + request.DataSegmentLength; // where we left off
  80. response.DesiredDataTransferLength = Math.Min((uint)connection.TargetMaxRecvDataSegmentLength, totalLength - response.BufferOffset);
  81. transfer.NextR2NSN++;
  82. responseList.Add(response);
  83. return responseList;
  84. }
  85. }
  86. internal static List<ISCSIPDU> GetSCSICommandResponse(SCSICommandPDU command, ISCSITarget target, SessionParameters session, ConnectionParameters connection)
  87. {
  88. List<ISCSIPDU> responseList = new List<ISCSIPDU>();
  89. string connectionIdentifier = StateObject.GetConnectionIdentifier(session, connection);
  90. ISCSIServer.Log("[{0}] Executing Command: CmdSN: {1}", connectionIdentifier, command.CmdSN);
  91. byte[] scsiResponse;
  92. SCSIStatusCodeName status = target.ExecuteCommand(command.CommandDescriptorBlock, command.LUN, command.Data, out scsiResponse);
  93. if (!command.Read || status != SCSIStatusCodeName.Good)
  94. {
  95. // RFC 3720: if the command is completed with an error, then the response and sense data MUST be sent in a SCSI Response PDU
  96. SCSIResponsePDU response = new SCSIResponsePDU();
  97. response.InitiatorTaskTag = command.InitiatorTaskTag;
  98. response.Status = status;
  99. response.Data = scsiResponse;
  100. if (command.Read)
  101. {
  102. EnforceExpectedDataTransferLength(response, command.ExpectedDataTransferLength);
  103. }
  104. responseList.Add(response);
  105. }
  106. else if (scsiResponse.Length <= connection.InitiatorMaxRecvDataSegmentLength)
  107. {
  108. SCSIDataInPDU response = new SCSIDataInPDU();
  109. response.InitiatorTaskTag = command.InitiatorTaskTag;
  110. response.Status = status;
  111. response.StatusPresent = true;
  112. response.Final = true;
  113. response.Data = scsiResponse;
  114. EnforceExpectedDataTransferLength(response, command.ExpectedDataTransferLength);
  115. responseList.Add(response);
  116. }
  117. else // we have to split the response to multiple Data-In PDUs
  118. {
  119. int bytesLeftToSend = scsiResponse.Length;
  120. uint dataSN = 0;
  121. while (bytesLeftToSend > 0)
  122. {
  123. int dataSegmentLength = Math.Min(connection.InitiatorMaxRecvDataSegmentLength, bytesLeftToSend);
  124. int dataOffset = scsiResponse.Length - bytesLeftToSend;
  125. SCSIDataInPDU response = new SCSIDataInPDU();
  126. response.InitiatorTaskTag = command.InitiatorTaskTag;
  127. if (bytesLeftToSend == dataSegmentLength)
  128. {
  129. // last Data-In PDU
  130. response.Status = status;
  131. response.StatusPresent = true;
  132. response.Final = true;
  133. }
  134. response.BufferOffset = (uint)dataOffset;
  135. response.DataSN = dataSN;
  136. dataSN++;
  137. response.Data = new byte[dataSegmentLength];
  138. Array.Copy(scsiResponse, dataOffset, response.Data, 0, dataSegmentLength);
  139. responseList.Add(response);
  140. bytesLeftToSend -= dataSegmentLength;
  141. }
  142. }
  143. return responseList;
  144. }
  145. public static void EnforceExpectedDataTransferLength(SCSIResponsePDU response, uint expectedDataTransferLength)
  146. {
  147. if (response.Data.Length > expectedDataTransferLength)
  148. {
  149. response.ResidualOverflow = true;
  150. response.ResidualCount = (uint)(response.Data.Length - expectedDataTransferLength);
  151. response.Data = ByteReader.ReadBytes(response.Data, 0, (int)expectedDataTransferLength);
  152. }
  153. else if (response.Data.Length < expectedDataTransferLength)
  154. {
  155. response.ResidualUnderflow = true;
  156. response.ResidualCount = (uint)(expectedDataTransferLength - response.Data.Length);
  157. }
  158. }
  159. public static void EnforceExpectedDataTransferLength(SCSIDataInPDU response, uint expectedDataTransferLength)
  160. {
  161. if (response.Data.Length > expectedDataTransferLength)
  162. {
  163. response.ResidualOverflow = true;
  164. response.ResidualCount = (uint)(response.Data.Length - expectedDataTransferLength);
  165. response.Data = ByteReader.ReadBytes(response.Data, 0, (int)expectedDataTransferLength);
  166. }
  167. else if (response.Data.Length < expectedDataTransferLength)
  168. {
  169. response.ResidualUnderflow = true;
  170. response.ResidualCount = (uint)(expectedDataTransferLength - response.Data.Length);
  171. }
  172. }
  173. }
  174. }