ISCSIClient.cs 21 KB

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