ISCSIClient.cs 20 KB

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