ISCSIServer.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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.Net;
  11. using System.Net.Sockets;
  12. using System.Text;
  13. using System.Threading;
  14. using Utilities;
  15. namespace ISCSI.Server
  16. {
  17. /// <summary>
  18. /// an iSCSI server that can serve multiple iSCSI targets
  19. /// </summary>
  20. public partial class ISCSIServer
  21. {
  22. public const int DefaultPort = 3260;
  23. private IPEndPoint m_listenerEP;
  24. private List<ISCSITarget> m_targets;
  25. private Socket m_listenerSocket;
  26. private bool m_listening;
  27. private SessionManager m_sessionManager = new SessionManager();
  28. private ConnectionManager m_connectionManager = new ConnectionManager();
  29. public event EventHandler<LogEntry> OnLogEntry;
  30. public ISCSIServer(List<ISCSITarget> targets) : this(targets, DefaultPort)
  31. { }
  32. /// <summary>
  33. /// Server needs to be started with Start()
  34. /// </summary>
  35. /// <param name="port">The port on which the iSCSI server will listen</param>
  36. public ISCSIServer(List<ISCSITarget> targets, int port) : this(targets, new IPEndPoint(IPAddress.Any, port))
  37. {
  38. }
  39. /// <summary>
  40. /// Server needs to be started with Start()
  41. /// </summary>
  42. /// <param name="listenerEP">The endpoint on which the iSCSI server will listen</param>
  43. public ISCSIServer(List<ISCSITarget> targets, IPEndPoint listenerEP)
  44. {
  45. m_listenerEP = listenerEP;
  46. m_targets = targets;
  47. }
  48. public void Start()
  49. {
  50. if (!m_listening)
  51. {
  52. Log(Severity.Information, "Starting Server");
  53. m_listening = true;
  54. m_listenerSocket = new Socket(m_listenerEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  55. m_listenerSocket.Bind(m_listenerEP);
  56. m_listenerSocket.Listen(1000);
  57. m_listenerSocket.BeginAccept(ConnectRequestCallback, m_listenerSocket);
  58. }
  59. }
  60. // This method accepts new connections
  61. private void ConnectRequestCallback(IAsyncResult ar)
  62. {
  63. Socket listenerSocket = (Socket)ar.AsyncState;
  64. Socket clientSocket;
  65. try
  66. {
  67. clientSocket = listenerSocket.EndAccept(ar);
  68. }
  69. catch (ObjectDisposedException)
  70. {
  71. return;
  72. }
  73. catch (SocketException)
  74. {
  75. return;
  76. }
  77. Log(Severity.Information, "New connection has been accepted");
  78. ConnectionState state = new ConnectionState();
  79. state.ConnectionParameters.InitiatorEndPoint = clientSocket.RemoteEndPoint as IPEndPoint;
  80. state.ReceiveBuffer = new byte[ConnectionState.ReceiveBufferSize];
  81. // Disable the Nagle Algorithm for this tcp socket:
  82. clientSocket.NoDelay = true;
  83. state.ClientSocket = clientSocket;
  84. Thread senderThread = new Thread(delegate()
  85. {
  86. ProcessSendQueue(state);
  87. });
  88. senderThread.IsBackground = true;
  89. senderThread.Start();
  90. try
  91. {
  92. clientSocket.BeginReceive(state.ReceiveBuffer, 0, ConnectionState.ReceiveBufferSize, 0, ReceiveCallback, state);
  93. }
  94. catch (ObjectDisposedException)
  95. {
  96. Log(Severity.Debug, "[OnConnectRequest] BeginReceive ObjectDisposedException");
  97. }
  98. catch (SocketException ex)
  99. {
  100. Log(Severity.Debug, "[OnConnectRequest] BeginReceive SocketException: {0}", ex.Message);
  101. }
  102. m_listenerSocket.BeginAccept(ConnectRequestCallback, m_listenerSocket);
  103. }
  104. public void Stop()
  105. {
  106. Log(Severity.Information, "Stopping Server");
  107. m_listening = false;
  108. SocketUtils.ReleaseSocket(m_listenerSocket);
  109. }
  110. private void ReceiveCallback(IAsyncResult result)
  111. {
  112. if (!m_listening)
  113. {
  114. return;
  115. }
  116. ConnectionState state = (ConnectionState)result.AsyncState;
  117. Socket clientSocket = state.ClientSocket;
  118. if (!clientSocket.Connected)
  119. {
  120. return;
  121. }
  122. int numberOfBytesReceived;
  123. try
  124. {
  125. numberOfBytesReceived = clientSocket.EndReceive(result);
  126. }
  127. catch (ObjectDisposedException)
  128. {
  129. Log(Severity.Debug, "[ReceiveCallback] EndReceive ObjectDisposedException");
  130. return;
  131. }
  132. catch (SocketException ex)
  133. {
  134. Log(Severity.Debug, "[ReceiveCallback] EndReceive SocketException: {0}", ex.Message);
  135. return;
  136. }
  137. if (numberOfBytesReceived == 0)
  138. {
  139. // The other side has closed the connection
  140. clientSocket.Close();
  141. Log(Severity.Verbose, "The initiator has closed the connection");
  142. // Wait for pending I/O to complete.
  143. state.RunningSCSICommands.WaitUntilZero();
  144. state.SendQueue.Stop();
  145. m_connectionManager.RemoveConnection(state);
  146. return;
  147. }
  148. byte[] currentBuffer = ByteReader.ReadBytes(state.ReceiveBuffer, 0, numberOfBytesReceived);
  149. ProcessCurrentBuffer(currentBuffer, state);
  150. try
  151. {
  152. clientSocket.BeginReceive(state.ReceiveBuffer, 0, ConnectionState.ReceiveBufferSize, 0, ReceiveCallback, state);
  153. }
  154. catch (ObjectDisposedException)
  155. {
  156. Log(Severity.Debug, "[ReceiveCallback] BeginReceive ObjectDisposedException");
  157. }
  158. catch (SocketException ex)
  159. {
  160. Log(Severity.Debug, "[ReceiveCallback] BeginReceive SocketException: {0}", ex.Message);
  161. }
  162. }
  163. private void ProcessCurrentBuffer(byte[] currentBuffer, ConnectionState state)
  164. {
  165. Socket clientSocket = state.ClientSocket;
  166. if (state.ConnectionBuffer.Length == 0)
  167. {
  168. state.ConnectionBuffer = currentBuffer;
  169. }
  170. else
  171. {
  172. state.ConnectionBuffer = ByteUtils.Concatenate(state.ConnectionBuffer, currentBuffer);
  173. }
  174. // we now have all PDU bytes received so far in state.ConnectionBuffer
  175. int bytesLeftInBuffer = state.ConnectionBuffer.Length;
  176. while (bytesLeftInBuffer >= 8)
  177. {
  178. int bufferOffset = state.ConnectionBuffer.Length - bytesLeftInBuffer;
  179. int pduLength = ISCSIPDU.GetPDULength(state.ConnectionBuffer, bufferOffset);
  180. if (pduLength > bytesLeftInBuffer)
  181. {
  182. Log(Severity.Debug, "[{0}][ProcessCurrentBuffer] Bytes left in receive buffer: {1}", state.ConnectionIdentifier, bytesLeftInBuffer);
  183. break;
  184. }
  185. else
  186. {
  187. byte[] pduBytes = ByteReader.ReadBytes(state.ConnectionBuffer, bufferOffset, pduLength);
  188. bytesLeftInBuffer -= pduLength;
  189. ISCSIPDU pdu = null;
  190. try
  191. {
  192. pdu = ISCSIPDU.GetPDU(pduBytes);
  193. }
  194. catch (Exception ex)
  195. {
  196. Log(Severity.Error, "[{0}] Failed to read PDU (Exception: {1})", state.ConnectionIdentifier, ex.Message);
  197. RejectPDU reject = new RejectPDU();
  198. reject.Reason = RejectReason.InvalidPDUField;
  199. reject.Data = ByteReader.ReadBytes(pduBytes, 0, 48);
  200. state.SendQueue.Enqueue(reject);
  201. }
  202. if (pdu != null)
  203. {
  204. if (pdu.GetType() == typeof(ISCSIPDU))
  205. {
  206. Log(Severity.Error, "[{0}][ProcessCurrentBuffer] Unsupported PDU (0x{1})", state.ConnectionIdentifier, pdu.OpCode.ToString("X"));
  207. // Unsupported PDU
  208. RejectPDU reject = new RejectPDU();
  209. reject.InitiatorTaskTag = pdu.InitiatorTaskTag;
  210. reject.Reason = RejectReason.CommandNotSupported;
  211. reject.Data = ByteReader.ReadBytes(pduBytes, 0, 48);
  212. state.SendQueue.Enqueue(reject);
  213. }
  214. else
  215. {
  216. bool valid = ValidateCommandNumbering(pdu, state);
  217. if (valid)
  218. {
  219. ProcessPDU(pdu, state);
  220. }
  221. else
  222. {
  223. // We ignore this PDU
  224. Log(Severity.Warning, "[{0}] Ignoring PDU with CmdSN outside of expected range", state.ConnectionIdentifier);
  225. }
  226. }
  227. }
  228. if (!clientSocket.Connected)
  229. {
  230. // Do not continue to process the buffer if the other side closed the connection
  231. if (bytesLeftInBuffer > 0)
  232. {
  233. Log(Severity.Debug, "[{0}] Buffer processing aborted, bytes left in receive buffer: {1}", state.ConnectionIdentifier, bytesLeftInBuffer);
  234. }
  235. return;
  236. }
  237. }
  238. }
  239. if (bytesLeftInBuffer > 0)
  240. {
  241. state.ConnectionBuffer = ByteReader.ReadBytes(state.ConnectionBuffer, state.ConnectionBuffer.Length - bytesLeftInBuffer, bytesLeftInBuffer);
  242. }
  243. else
  244. {
  245. state.ConnectionBuffer = new byte[0];
  246. }
  247. }
  248. private void ProcessSendQueue(ConnectionState state)
  249. {
  250. while (true)
  251. {
  252. Log(Severity.Trace, "Entering ProcessSendQueue");
  253. ISCSIPDU response;
  254. bool stopped = !state.SendQueue.TryDequeue(out response);
  255. if (stopped)
  256. {
  257. return;
  258. }
  259. Socket clientSocket = state.ClientSocket;
  260. PDUHelper.SetStatSN(response, state.ConnectionParameters.StatSN);
  261. PDUHelper.SetExpCmdSN(response, state.Session.ExpCmdSN, state.Session.ExpCmdSN + state.Session.CommandQueueSize);
  262. if (response is SCSIResponsePDU ||
  263. response is LoginResponsePDU ||
  264. response is TextResponsePDU ||
  265. (response is SCSIDataInPDU && ((SCSIDataInPDU)response).StatusPresent) ||
  266. response is RejectPDU)
  267. {
  268. state.ConnectionParameters.StatSN++;
  269. }
  270. try
  271. {
  272. clientSocket.Send(response.GetBytes());
  273. Log(Severity.Verbose, "[{0}] Sent response to initator, Operation: {1}, Size: {2}", state.ConnectionIdentifier, response.OpCode, response.Length);
  274. if (response is LogoutResponsePDU)
  275. {
  276. clientSocket.Close(); // We can close the connection now
  277. Log(Severity.Trace, "Leaving ProcessSendQueue");
  278. return;
  279. }
  280. else if (response is LoginResponsePDU)
  281. {
  282. if (((LoginResponsePDU)response).Status == LoginResponseStatusName.InvalidDuringLogon)
  283. {
  284. clientSocket.Close(); // We can close the connection now
  285. Log(Severity.Trace, "Leaving ProcessSendQueue");
  286. return;
  287. }
  288. }
  289. }
  290. catch (SocketException ex)
  291. {
  292. Log(Severity.Verbose, "[{0}] Failed to send response to initator. Operation: {1}, Size: {2}, SocketException: {3}", state.ConnectionIdentifier, response.OpCode, response.Length, ex.Message);
  293. Log(Severity.Trace, "Leaving ProcessSendQueue");
  294. return;
  295. }
  296. catch (ObjectDisposedException)
  297. {
  298. Log(Severity.Verbose, "[{0}] Failed to send response to initator. Operation: {1}, Size: {2}. ObjectDisposedException", state.ConnectionIdentifier, response.OpCode, response.Length);
  299. Log(Severity.Trace, "Leaving ProcessSendQueue");
  300. return;
  301. }
  302. }
  303. }
  304. public void Log(Severity severity, string message)
  305. {
  306. // To be thread-safe we must capture the delegate reference first
  307. EventHandler<LogEntry> handler = OnLogEntry;
  308. if (handler != null)
  309. {
  310. handler(this, new LogEntry(DateTime.Now, severity, "iSCSI Server", message));
  311. }
  312. }
  313. public void Log(Severity severity, string message, params object[] args)
  314. {
  315. Log(severity, String.Format(message, args));
  316. }
  317. }
  318. }