ISCSIClient.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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.Diagnostics;
  10. using System.IO;
  11. using System.Net;
  12. using System.Net.Sockets;
  13. using System.Text;
  14. using System.Threading;
  15. using Utilities;
  16. namespace ISCSI.Client
  17. {
  18. public class ISCSIClient
  19. {
  20. // Offered Session Parameters:
  21. public static bool OfferedInitialR2T = true;
  22. public static bool OfferedImmediateData = true;
  23. public static int OfferedMaxBurstLength = SessionParameters.DefaultMaxBurstLength;
  24. public static int OfferedFirstBurstLength = SessionParameters.DefaultFirstBurstLength;
  25. public static int OfferedDefaultTime2Wait = 0;
  26. public static int OfferedDefaultTime2Retain = 20;
  27. public static int OfferedMaxOutstandingR2T = 1;
  28. public static bool OfferedDataPDUInOrder = true;
  29. public static bool OfferedDataSequenceInOrder = true;
  30. public static int OfferedErrorRecoveryLevel = 0;
  31. public static int OfferedMaxConnections = 1;
  32. private SessionParameters m_session = new SessionParameters();
  33. private ConnectionParameters m_connection = new ConnectionParameters();
  34. private string m_initiatorName;
  35. private IPAddress m_targetAddress;
  36. private int m_targetPort;
  37. private bool m_isConnected;
  38. private Socket m_clientSocket;
  39. private IAsyncResult m_currentAsyncResult;
  40. private object m_incomingQueueLock = new object();
  41. private List<ISCSIPDU> m_incomingQueue = new List<ISCSIPDU>();
  42. private EventWaitHandle m_incomingQueueEventHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
  43. public static object m_logSyncLock = new object();
  44. private static FileStream m_logFile = null;
  45. public ISCSIClient(string initiatorName)
  46. {
  47. m_initiatorName = initiatorName;
  48. }
  49. public bool Connect(IPAddress targetAddress, int targetPort)
  50. {
  51. m_targetAddress = targetAddress;
  52. m_targetPort = targetPort;
  53. if (!m_isConnected)
  54. {
  55. m_clientSocket = new Socket(m_targetAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  56. try
  57. {
  58. m_clientSocket.Connect(m_targetAddress, m_targetPort);
  59. }
  60. catch (SocketException)
  61. {
  62. return false;
  63. }
  64. ConnectionState state = new ConnectionState();
  65. m_currentAsyncResult = m_clientSocket.BeginReceive(state.ReceiveBuffer, 0, state.ReceiveBuffer.Length, SocketFlags.None, new AsyncCallback(OnClientSocketReceive), state);
  66. m_isConnected = true;
  67. }
  68. return m_isConnected;
  69. }
  70. public void Disconnect()
  71. {
  72. if (m_isConnected)
  73. {
  74. m_clientSocket.Disconnect(false);
  75. m_isConnected = false;
  76. }
  77. }
  78. /// <param name="targetName">Set to null for discovery session</param>
  79. public bool Login(string targetName)
  80. {
  81. if (!m_isConnected)
  82. {
  83. throw new InvalidOperationException("iSCSI client is not connected");
  84. }
  85. m_session.ISID = ClientHelper.GetRandomISID();
  86. m_connection.CID = m_session.GetNextCID();
  87. // p.s. It's possible to perform a single stage login (stage 1 to stage 3, tested against Microsoft iSCSI Target v3.1)
  88. LoginRequestPDU request = ClientHelper.GetFirstStageLoginRequest(m_initiatorName, targetName, m_session, m_connection);
  89. SendPDU(request);
  90. LoginResponsePDU response = WaitForPDU(request.InitiatorTaskTag) as LoginResponsePDU;
  91. if (response != null && response.Status == LoginResponseStatusName.Success)
  92. {
  93. // Status numbering starts with the Login response to the first Login request of the connection
  94. m_connection.StatusNumberingStarted = true;
  95. m_connection.ExpStatSN = response.StatSN + 1;
  96. request = ClientHelper.GetSecondStageLoginRequest(response, m_session, m_connection, targetName == null);
  97. SendPDU(request);
  98. response = WaitForPDU(request.InitiatorTaskTag) as LoginResponsePDU;
  99. if (response != null && response.Status == LoginResponseStatusName.Success)
  100. {
  101. ClientHelper.UpdateOperationalParameters(response.LoginParameters, m_session, m_connection);
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. public bool Logout()
  108. {
  109. if (!m_isConnected)
  110. {
  111. throw new InvalidOperationException("iSCSI client is not connected");
  112. }
  113. LogoutRequestPDU request = ClientHelper.GetLogoutRequest(m_session, m_connection);
  114. SendPDU(request);
  115. LogoutResponsePDU response = WaitForPDU(request.InitiatorTaskTag) as LogoutResponsePDU;
  116. return (response != null && response.Response == LogoutResponse.ClosedSuccessfully);
  117. }
  118. public List<string> ListTargets()
  119. {
  120. if (!m_isConnected)
  121. {
  122. throw new InvalidOperationException("iSCSI client is not connected");
  123. }
  124. TextRequestPDU request = ClientHelper.GetSendTargetsRequest(m_session, m_connection);
  125. SendPDU(request);
  126. TextResponsePDU response = WaitForPDU(request.InitiatorTaskTag) as TextResponsePDU;
  127. if (response != null && response.Final)
  128. {
  129. KeyValuePairList<string, string> entries = KeyValuePairUtils.GetKeyValuePairList(response.Text);
  130. List<string> result = new List<string>();
  131. foreach(KeyValuePair<string, string> entry in entries)
  132. {
  133. if (entry.Key == "TargetName")
  134. {
  135. result.Add(entry.Value);
  136. }
  137. }
  138. return result;
  139. }
  140. return null;
  141. }
  142. public List<ushort> GetLUNList()
  143. {
  144. if (!m_isConnected)
  145. {
  146. throw new InvalidOperationException("iSCSI client is not connected");
  147. }
  148. SCSICommandPDU reportLUNs = ClientHelper.GetReportLUNsCommand(m_session, m_connection, ReportLUNsParameter.MinimumAllocationLength);
  149. SendPDU(reportLUNs);
  150. SCSIDataInPDU data = WaitForPDU(reportLUNs.InitiatorTaskTag) as SCSIDataInPDU;
  151. if (data != null && data.StatusPresent && data.Status == SCSIStatusCodeName.Good)
  152. {
  153. uint requiredAllocationLength = ReportLUNsParameter.GetRequiredAllocationLength(data.Data);
  154. if (requiredAllocationLength > ReportLUNsParameter.MinimumAllocationLength)
  155. {
  156. reportLUNs = ClientHelper.GetReportLUNsCommand(m_session, m_connection, requiredAllocationLength);
  157. m_clientSocket.Send(reportLUNs.GetBytes());
  158. data = WaitForPDU(reportLUNs.InitiatorTaskTag) as SCSIDataInPDU;
  159. if (data == null || !data.StatusPresent || data.Status != SCSIStatusCodeName.Good)
  160. {
  161. return null;
  162. }
  163. }
  164. ReportLUNsParameter parameter = new ReportLUNsParameter(data.Data);
  165. List<ushort> result = new List<ushort>();
  166. foreach(LUNStructure lun in parameter.LUNList)
  167. {
  168. if (lun.IsSingleLevelLUN)
  169. {
  170. result.Add(lun);
  171. }
  172. }
  173. return result;
  174. }
  175. return null;
  176. }
  177. /// <returns>Capacity in bytes</returns>
  178. public ulong ReadCapacity(ushort LUN, out int bytesPerSector)
  179. {
  180. if (!m_isConnected)
  181. {
  182. throw new InvalidOperationException("iSCSI client is not connected");
  183. }
  184. SCSICommandPDU readCapacity = ClientHelper.GetReadCapacity10Command(m_session, m_connection, LUN);
  185. SendPDU(readCapacity);
  186. // SCSIResponsePDU with CheckCondition could be returned in case of an error
  187. SCSIDataInPDU data = WaitForPDU(readCapacity.InitiatorTaskTag) as SCSIDataInPDU;
  188. if (data != null && data.StatusPresent && data.Status == SCSIStatusCodeName.Good)
  189. {
  190. ReadCapacity10Parameter capacity = new ReadCapacity10Parameter(data.Data);
  191. if (capacity.ReturnedLBA != 0xFFFFFFFF)
  192. {
  193. bytesPerSector = (int)capacity.BlockLengthInBytes;
  194. return (ulong)(capacity.ReturnedLBA + 1) * capacity.BlockLengthInBytes;
  195. }
  196. readCapacity = ClientHelper.GetReadCapacity16Command(m_session, m_connection, LUN);
  197. m_clientSocket.Send(readCapacity.GetBytes());
  198. data = WaitForPDU(readCapacity.InitiatorTaskTag) as SCSIDataInPDU;
  199. if (data != null && data.StatusPresent && data.Status == SCSIStatusCodeName.Good)
  200. {
  201. ReadCapacity16Parameter capacity16 = new ReadCapacity16Parameter(data.Data);
  202. bytesPerSector = (int)capacity16.BlockLengthInBytes;
  203. return (ulong)(capacity16.ReturnedLBA + 1) * capacity16.BlockLengthInBytes;
  204. }
  205. }
  206. bytesPerSector = 0;
  207. return 0;
  208. }
  209. public byte[] Read(ushort LUN, ulong sectorIndex, uint sectorCount, int bytesPerSector)
  210. {
  211. if (!m_isConnected)
  212. {
  213. throw new InvalidOperationException("iSCSI client is not connected");
  214. }
  215. SCSICommandPDU readCommand = ClientHelper.GetRead16Command(m_session, m_connection, LUN, sectorIndex, sectorCount, bytesPerSector);
  216. SendPDU(readCommand);
  217. // RFC 3720: Data payload is associated with a specific SCSI command through the Initiator Task Tag
  218. SCSIDataInPDU data = WaitForPDU(readCommand.InitiatorTaskTag) as SCSIDataInPDU;
  219. byte[] result = new byte[sectorCount * bytesPerSector];
  220. while (data != null)
  221. {
  222. Array.Copy(data.Data, 0, result, data.BufferOffset, data.DataSegmentLength);
  223. if (data.StatusPresent)
  224. {
  225. break;
  226. }
  227. data = WaitForPDU(readCommand.InitiatorTaskTag) as SCSIDataInPDU;
  228. }
  229. if (data != null && data.Status == SCSIStatusCodeName.Good)
  230. {
  231. return result;
  232. }
  233. else
  234. {
  235. return null;
  236. }
  237. }
  238. public bool Write(ushort LUN, ulong sectorIndex, byte[] data, int bytesPerSector)
  239. {
  240. if (!m_isConnected)
  241. {
  242. throw new InvalidOperationException("iSCSI client is not connected");
  243. }
  244. SCSICommandPDU writeCommand = ClientHelper.GetWrite16Command(m_session, m_connection, LUN, sectorIndex, data, bytesPerSector);
  245. SendPDU(writeCommand);
  246. ISCSIPDU response = WaitForPDU(writeCommand.InitiatorTaskTag);
  247. while (response is ReadyToTransferPDU)
  248. {
  249. List<SCSIDataOutPDU> requestedData = ClientHelper.GetWriteData(m_session, m_connection, LUN, sectorIndex, data, bytesPerSector, (ReadyToTransferPDU)response);
  250. foreach (SCSIDataOutPDU dataOut in requestedData)
  251. {
  252. SendPDU(dataOut);
  253. }
  254. response = WaitForPDU(writeCommand.InitiatorTaskTag);
  255. }
  256. if (response is SCSIResponsePDU)
  257. {
  258. if (((SCSIResponsePDU)response).Status == SCSIStatusCodeName.Good)
  259. {
  260. return true;
  261. }
  262. }
  263. return false;
  264. }
  265. public bool PingTarget()
  266. {
  267. if (!m_isConnected)
  268. {
  269. throw new InvalidOperationException("iSCSI client is not connected");
  270. }
  271. NOPOutPDU request = ClientHelper.GetPingRequest(m_session, m_connection);
  272. SendPDU(request);
  273. NOPInPDU response = WaitForPDU(request.InitiatorTaskTag) as NOPInPDU;
  274. return response != null;
  275. }
  276. private void OnClientSocketReceive(IAsyncResult ar)
  277. {
  278. if (ar != m_currentAsyncResult)
  279. {
  280. // We ignore calls for old sockets which we no longer use
  281. // See: http://rajputyh.blogspot.co.il/2010/04/solve-exception-message-iasyncresult.html
  282. return;
  283. }
  284. ConnectionState state = (ConnectionState)ar.AsyncState;
  285. if (!m_clientSocket.Connected)
  286. {
  287. return;
  288. }
  289. int numberOfBytesReceived = 0;
  290. try
  291. {
  292. numberOfBytesReceived = m_clientSocket.EndReceive(ar);
  293. }
  294. catch (ObjectDisposedException)
  295. {
  296. Log("[ReceiveCallback] EndReceive ObjectDisposedException");
  297. return;
  298. }
  299. catch (SocketException ex)
  300. {
  301. Log("[ReceiveCallback] EndReceive SocketException: " + ex.Message);
  302. return;
  303. }
  304. if (numberOfBytesReceived == 0)
  305. {
  306. m_isConnected = false;
  307. }
  308. else
  309. {
  310. byte[] currentBuffer = ByteReader.ReadBytes(state.ReceiveBuffer, 0, numberOfBytesReceived);
  311. ProcessCurrentBuffer(currentBuffer, state);
  312. try
  313. {
  314. m_currentAsyncResult = m_clientSocket.BeginReceive(state.ReceiveBuffer, 0, state.ReceiveBuffer.Length, SocketFlags.None, new AsyncCallback(OnClientSocketReceive), state);
  315. }
  316. catch (ObjectDisposedException)
  317. {
  318. m_isConnected = false;
  319. Log("[ReceiveCallback] BeginReceive ObjectDisposedException");
  320. }
  321. catch (SocketException ex)
  322. {
  323. m_isConnected = false;
  324. Log("[ReceiveCallback] BeginReceive SocketException: " + ex.Message);
  325. }
  326. }
  327. }
  328. public void ProcessCurrentBuffer(byte[] currentBuffer, ConnectionState state)
  329. {
  330. if (state.ConnectionBuffer.Length == 0)
  331. {
  332. state.ConnectionBuffer = currentBuffer;
  333. }
  334. else
  335. {
  336. state.ConnectionBuffer = ByteUtils.Concatenate(state.ConnectionBuffer, currentBuffer);
  337. }
  338. // we now have all PDU bytes received so far in state.ConnectionBuffer
  339. int bytesLeftInBuffer = state.ConnectionBuffer.Length;
  340. while (bytesLeftInBuffer >= 8)
  341. {
  342. int bufferOffset = state.ConnectionBuffer.Length - bytesLeftInBuffer;
  343. int pduLength = ISCSIPDU.GetPDULength(state.ConnectionBuffer, bufferOffset);
  344. if (pduLength > bytesLeftInBuffer)
  345. {
  346. break;
  347. }
  348. else
  349. {
  350. byte[] pduBytes = ByteReader.ReadBytes(state.ConnectionBuffer, bufferOffset, pduLength);
  351. bytesLeftInBuffer -= pduLength;
  352. ISCSIPDU pdu = null;
  353. try
  354. {
  355. pdu = ISCSIPDU.GetPDU(pduBytes);
  356. }
  357. catch (UnsupportedSCSICommandException)
  358. {
  359. throw;
  360. }
  361. catch (Exception)
  362. {
  363. throw;
  364. }
  365. if (pdu.GetType() == typeof(ISCSIPDU))
  366. {
  367. /*
  368. Log("[{0}][ProcessCurrentBuffer] Unsupported PDU (0x{1})", state.Connection.Identifier, pdu.OpCode.ToString("X"));
  369. // Unsupported PDU
  370. RejectPDU reject = new RejectPDU();
  371. reject.Reason = RejectReason.CommandNotSupported;
  372. reject.StatSN = state.Connection.StatSN;
  373. reject.ExpCmdSN = state.Connection.ExpCmdSN;
  374. reject.MaxCmdSN = state.Connection.ExpCmdSN + ISCSIServer.CommandQueueSize;
  375. reject.Data = ByteReader.ReadBytes(pduBytes, 0, 48);
  376. // StatSN is advanced after a Reject
  377. state.Connection.StatSN++;
  378. TrySendPDU(state, reject);*/
  379. throw new Exception("Unsupported");
  380. }
  381. else
  382. {
  383. ProcessPDU(pdu, state);
  384. }
  385. }
  386. }
  387. if (bytesLeftInBuffer > 0)
  388. {
  389. state.ConnectionBuffer = ByteReader.ReadBytes(state.ConnectionBuffer, state.ConnectionBuffer.Length - bytesLeftInBuffer, bytesLeftInBuffer);
  390. }
  391. else
  392. {
  393. state.ConnectionBuffer = new byte[0];
  394. }
  395. }
  396. public void ProcessPDU(ISCSIPDU pdu, ConnectionState state)
  397. {
  398. if (pdu is NOPInPDU)
  399. {
  400. if (((NOPInPDU)pdu).TargetTransferTag != 0xFFFFFFFF)
  401. {
  402. // Send NOP-OUT
  403. NOPOutPDU response = ClientHelper.GetPingResponse((NOPInPDU)pdu, m_session, m_connection);
  404. SendPDU(response);
  405. return;
  406. }
  407. }
  408. if (m_connection.StatusNumberingStarted)
  409. {
  410. uint? responseStatSN = PDUHelper.GetStatSN(pdu);
  411. if (m_connection.ExpStatSN == responseStatSN)
  412. {
  413. m_connection.ExpStatSN++;
  414. }
  415. }
  416. lock (m_incomingQueueLock)
  417. {
  418. m_incomingQueue.Add(pdu);
  419. m_incomingQueueEventHandle.Set();
  420. }
  421. }
  422. public ISCSIPDU WaitForPDU(uint initiatorTaskTag)
  423. {
  424. const int TimeOut = 5000;
  425. Stopwatch stopwatch = new Stopwatch();
  426. stopwatch.Start();
  427. while (stopwatch.ElapsedMilliseconds < TimeOut)
  428. {
  429. lock (m_incomingQueueLock)
  430. {
  431. for (int index = 0; index < m_incomingQueue.Count; index++)
  432. {
  433. ISCSIPDU pdu = m_incomingQueue[index];
  434. if (pdu.InitiatorTaskTag == initiatorTaskTag)
  435. {
  436. m_incomingQueue.RemoveAt(index);
  437. return pdu;
  438. }
  439. }
  440. }
  441. m_incomingQueueEventHandle.WaitOne(100);
  442. }
  443. return null;
  444. }
  445. public void SendPDU(ISCSIPDU request)
  446. {
  447. try
  448. {
  449. if (m_connection.StatusNumberingStarted)
  450. {
  451. PDUHelper.SetExpStatSN(request, m_connection.ExpStatSN);
  452. }
  453. m_clientSocket.Send(request.GetBytes());
  454. Log("[{0}][SendPDU] Sent request to target, Operation: {1}, Size: {2}", this.ConnectionIdentifier, (ISCSIOpCodeName)request.OpCode, request.Length);
  455. }
  456. catch (SocketException ex)
  457. {
  458. Log("[{0}][SendPDU] Failed to send PDU to target (Operation: {1}, Size: {2}), SocketException: {3}", this.ConnectionIdentifier, (ISCSIOpCodeName)request.OpCode, request.Length, ex.Message);
  459. m_isConnected = false;
  460. }
  461. catch (ObjectDisposedException)
  462. {
  463. m_isConnected = false;
  464. }
  465. }
  466. public string ConnectionIdentifier
  467. {
  468. get
  469. {
  470. return String.Format("ISID={0},TSIH={1},CID={2}", m_session.ISID.ToString("x"), m_session.TSIH.ToString("x"), m_connection.CID.ToString("x"));
  471. }
  472. }
  473. public bool IsConnected
  474. {
  475. get
  476. {
  477. return m_isConnected;
  478. }
  479. }
  480. public static void Log(string message, params object[] args)
  481. {
  482. Log(String.Format(message, args));
  483. }
  484. public static void Log(string message)
  485. {
  486. if (m_logFile != null)
  487. {
  488. lock (m_logSyncLock)
  489. {
  490. StreamWriter writer = new StreamWriter(m_logFile);
  491. string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ");
  492. writer.WriteLine(timestamp + message);
  493. writer.Flush();
  494. }
  495. }
  496. }
  497. }
  498. }