ISCSIClient.cs 19 KB

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