123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528 |
- /* Copyright (C) 2012-2016 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
- *
- * You can redistribute this program and/or modify it under the terms of
- * the GNU Lesser Public License as published by the Free Software Foundation,
- * either version 3 of the License, or (at your option) any later version.
- */
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using Utilities;
- namespace ISCSI.Server
- {
- public delegate ushort GetNextTSIH();
- public class ISCSIServer // Server may serve more than one target
- {
- public const int DefaultPort = 3260;
- // Offered Session Parameters:
- public static bool OfferedInitialR2T = true;
- public static bool OfferedImmediateData = true;
- public static int OfferedMaxBurstLength = SessionParameters.DefaultMaxBurstLength;
- public static int OfferedFirstBurstLength = SessionParameters.DefaultFirstBurstLength;
- public static int OfferedDefaultTime2Wait = 0;
- public static int OfferedDefaultTime2Retain = 20;
- public static int OfferedMaxOutstandingR2T = 1;
- public static bool OfferedDataPDUInOrder = true;
- public static bool OfferedDataSequenceInOrder = true;
- public static int OfferedErrorRecoveryLevel = 0;
- public static int OfferedMaxConnections = 1;
- private List<ISCSITarget> m_targets;
- private int m_port;
- private ushort m_nextTSIH = 1; // Next Target Session Identifying Handle
- private Socket m_listenerSocket;
- private bool m_listening;
- private static object m_activeConnectionsLock = new object();
- private static List<StateObject> m_activeConnections = new List<StateObject>();
- public static object m_logSyncLock = new object();
- private static FileStream m_logFile;
-
- public ISCSIServer(List<ISCSITarget> targets) : this(targets, DefaultPort)
- { }
- public ISCSIServer(List<ISCSITarget> targets, int port) : this(targets, port, String.Empty)
- { }
- /// <summary>
- /// Server needs to be started with Start()
- /// </summary>
- public ISCSIServer(List<ISCSITarget> targets, int port, string logFilePath)
- {
- m_port = port;
- m_targets = targets;
- if (logFilePath != String.Empty)
- {
- try
- {
- // We must avoid using buffered writes, using it will negatively affect the performance and reliability.
- // Note: once the file system write buffer is filled, Windows may delay any (buffer-dependent) pending write operations, which will create a deadlock.
- m_logFile = new FileStream(logFilePath, FileMode.Append, FileAccess.Write, FileShare.Read, 0x1000, FileOptions.WriteThrough);
- }
- catch
- {
- Console.WriteLine("Cannot open log file");
- }
- }
- }
- public void Start()
- {
- if (!m_listening)
- {
- m_listening = true;
- m_listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- m_listenerSocket.Bind(new IPEndPoint(IPAddress.Any, m_port));
- m_listenerSocket.Listen(1000);
- m_listenerSocket.BeginAccept(ConnectRequestCallback, m_listenerSocket);
- }
- }
- // This method Accepts new connections
- private void ConnectRequestCallback(IAsyncResult ar)
- {
- Socket listenerSocket = (Socket)ar.AsyncState;
- Socket clientSocket;
- try
- {
- clientSocket = listenerSocket.EndAccept(ar);
- }
- catch (ObjectDisposedException)
- {
- return;
- }
- catch (SocketException)
- {
- return;
- }
- Log("[OnConnectRequest] New connection has been accepted");
- StateObject state = new StateObject();
- state.ReceiveBuffer = new byte[StateObject.ReceiveBufferSize];
- // Disable the Nagle Algorithm for this tcp socket:
- clientSocket.NoDelay = true;
- state.ClientSocket = clientSocket;
- try
- {
- clientSocket.BeginReceive(state.ReceiveBuffer, 0, StateObject.ReceiveBufferSize, 0, ReceiveCallback, state);
- }
- catch (ObjectDisposedException)
- {
- Log("[OnConnectRequest] BeginReceive ObjectDisposedException");
- }
- catch (SocketException ex)
- {
- Log("[OnConnectRequest] BeginReceive SocketException: " + ex.Message);
- }
- m_listenerSocket.BeginAccept(ConnectRequestCallback, m_listenerSocket);
- }
- public void Stop()
- {
- m_listening = false;
- SocketUtils.ReleaseSocket(m_listenerSocket);
- if (m_logFile != null)
- {
- m_logFile.Close();
- m_logFile = null;
- }
- }
- private void ReceiveCallback(IAsyncResult result)
- {
- if (!m_listening)
- {
- return;
- }
- StateObject state = (StateObject)result.AsyncState;
- Socket clientSocket = state.ClientSocket;
- int numberOfBytesReceived;
- try
- {
- numberOfBytesReceived = clientSocket.EndReceive(result);
- }
- catch (ObjectDisposedException)
- {
- Log("[ReceiveCallback] EndReceive ObjectDisposedException");
- return;
- }
- catch (SocketException ex)
- {
- Log("[ReceiveCallback] EndReceive SocketException: " + ex.Message);
- return;
- }
- if (numberOfBytesReceived == 0)
- {
- // The other side has closed the connection
- clientSocket.Close();
- Log("[ReceiveCallback] The initiator has closed the connection");
- lock (m_activeConnectionsLock)
- {
- int connectionIndex = GetStateObjectIndex(m_activeConnections, state.SessionParameters.ISID, state.SessionParameters.TSIH, state.ConnectionParameters.CID);
- if (connectionIndex >= 0)
- {
- lock (m_activeConnections[connectionIndex].WriteLock)
- {
- // Wait for pending I/O to complete.
- }
- m_activeConnections.RemoveAt(connectionIndex);
- }
- }
- return;
- }
- Log("[{0}][ReceiveCallback] Received {1} bytes", state.ConnectionIdentifier, numberOfBytesReceived);
- byte[] currentBuffer = ByteReader.ReadBytes(state.ReceiveBuffer, 0, numberOfBytesReceived);
- ProcessCurrentBuffer(currentBuffer, state);
- if (clientSocket.Connected)
- {
- try
- {
- clientSocket.BeginReceive(state.ReceiveBuffer, 0, StateObject.ReceiveBufferSize, 0, ReceiveCallback, state);
- }
- catch (ObjectDisposedException)
- {
- Log("[ReceiveCallback] BeginReceive ObjectDisposedException");
- }
- catch (SocketException ex)
- {
- Log("[ReceiveCallback] BeginReceive SocketException: " + ex.Message);
- }
- }
- }
- public void ProcessCurrentBuffer(byte[] currentBuffer, StateObject state)
- {
- Socket clientSocket = state.ClientSocket;
- if (state.ConnectionBuffer.Length == 0)
- {
- state.ConnectionBuffer = currentBuffer;
- }
- else
- {
- state.ConnectionBuffer = ByteUtils.Concatenate(state.ConnectionBuffer, currentBuffer);
- }
- // we now have all PDU bytes received so far in state.ConnectionBuffer
- int bytesLeftInBuffer = state.ConnectionBuffer.Length;
- while (bytesLeftInBuffer >= 8)
- {
- int bufferOffset = state.ConnectionBuffer.Length - bytesLeftInBuffer;
- int pduLength = ISCSIPDU.GetPDULength(state.ConnectionBuffer, bufferOffset);
- if (pduLength > bytesLeftInBuffer)
- {
- Log("[{0}][ProcessCurrentBuffer] Bytes left in receive buffer: {1}", state.ConnectionIdentifier, bytesLeftInBuffer);
- break;
- }
- else
- {
- Log("[{0}][ProcessCurrentBuffer] PDU is being processed, Length: {1}", state.ConnectionIdentifier, pduLength);
- byte[] pduBytes = ByteReader.ReadBytes(state.ConnectionBuffer, bufferOffset, pduLength);
- bytesLeftInBuffer -= pduLength;
- ISCSIPDU pdu = null;
- try
- {
- pdu = ISCSIPDU.GetPDU(pduBytes);
- }
- catch (UnsupportedSCSICommandException)
- {
- pdu = new SCSICommandPDU(pduBytes, false);
- }
- catch (Exception ex)
- {
- Log("[{0}][ProcessCurrentBuffer] Failed to read PDU (Exception: {1})", state.ConnectionIdentifier, ex.Message);
- RejectPDU reject = new RejectPDU();
- reject.Reason = RejectReason.InvalidPDUField;
- reject.Data = ByteReader.ReadBytes(pduBytes, 0, 48);
- TrySendPDU(state, reject);
- }
- if (pdu != null)
- {
- if (pdu.GetType() == typeof(ISCSIPDU))
- {
- Log("[{0}][ProcessCurrentBuffer] Unsupported PDU (0x{1})", state.ConnectionIdentifier, pdu.OpCode.ToString("X"));
- // Unsupported PDU
- RejectPDU reject = new RejectPDU();
- reject.Reason = RejectReason.CommandNotSupported;
- reject.Data = ByteReader.ReadBytes(pduBytes, 0, 48);
- TrySendPDU(state, reject);
- }
- else
- {
- ProcessPDU(pdu, state);
- }
- }
-
- if (!clientSocket.Connected)
- {
- // Do not continue to process the buffer if the other side closed the connection
- Log("[{0}][ProcessCurrentBuffer] Buffer processing aborted, bytes left in receive buffer: {1}", state.ConnectionIdentifier, bytesLeftInBuffer);
- return;
- }
- }
- }
- if (bytesLeftInBuffer > 0)
- {
- state.ConnectionBuffer = ByteReader.ReadBytes(state.ConnectionBuffer, state.ConnectionBuffer.Length - bytesLeftInBuffer, bytesLeftInBuffer);
- }
- else
- {
- state.ConnectionBuffer = new byte[0];
- }
- }
- public void ProcessPDU(ISCSIPDU pdu, StateObject state)
- {
- Socket clientSocket = state.ClientSocket;
- Log("[{0}][ProcessPDU] Received PDU from initiator, Operation: {1}, Size: {2}", state.ConnectionIdentifier, (ISCSIOpCodeName)pdu.OpCode, pdu.Length);
-
- uint? cmdSN = PDUHelper.GetCmdSN(pdu);
- // RFC 3720: On any connection, the iSCSI initiator MUST send the commands in increasing order of CmdSN,
- // except for commands that are retransmitted due to digest error recovery and connection recovery.
- if (cmdSN.HasValue)
- {
- if (state.SessionParameters.CommandNumberingStarted)
- {
- if (cmdSN != state.SessionParameters.ExpCmdSN)
- {
- // We ignore this PDU
- return;
- }
- }
- else
- {
- state.SessionParameters.ExpCmdSN = cmdSN.Value;
- state.SessionParameters.CommandNumberingStarted = true;
- }
- if (pdu is LogoutRequestPDU || pdu is TextRequestPDU || pdu is SCSICommandPDU || pdu is RejectPDU)
- {
- if (!pdu.ImmediateDelivery)
- {
- state.SessionParameters.ExpCmdSN++;
- }
- }
- }
- if (pdu is NOPOutPDU)
- {
- NOPOutPDU request = (NOPOutPDU)pdu;
- if (request.InitiatorTaskTag != 0xFFFFFFFF)
- {
- NOPInPDU response = ServerResponseHelper.GetNOPResponsePDU(request);
- TrySendPDU(state, response);
- }
- }
- else if (pdu is TextRequestPDU)
- {
- TextRequestPDU request = (TextRequestPDU)pdu;
- TextResponsePDU response = ServerResponseHelper.GetTextResponsePDU(request, m_targets);
- TrySendPDU(state, response);
- }
- else if (pdu is LoginRequestPDU)
- {
- LoginRequestPDU request = (LoginRequestPDU)pdu;
- Log("[{0}][ReceiveCallback] Login Request, current stage: {1}, next stage: {2}, parameters: {3}", state.ConnectionIdentifier, request.CurrentStage, request.NextStage, KeyValuePairUtils.ToString(request.LoginParameters));
- if (request.TSIH != 0)
- {
- // RFC 3720: A Login Request with a non-zero TSIH and a CID equal to that of an existing
- // connection implies a logout of the connection followed by a Login
- lock (m_activeConnectionsLock)
- {
- int existingConnectionIndex = GetStateObjectIndex(m_activeConnections, request.ISID, request.TSIH, request.CID);
- if (existingConnectionIndex >= 0)
- {
- // Perform implicit logout
- Log("[{0}][ProcessPDU] Initiating implicit logout", state.ConnectionIdentifier);
- SocketUtils.ReleaseSocket(m_activeConnections[existingConnectionIndex].ClientSocket);
- lock (m_activeConnections[existingConnectionIndex].Target.WriteLock)
- {
- // Wait for pending I/O to complete.
- }
- m_activeConnections.RemoveAt(existingConnectionIndex);
- Log("[{0}][ProcessPDU] Implicit logout completed", state.ConnectionIdentifier);
- }
- }
- }
- LoginResponsePDU response = ServerResponseHelper.GetLoginResponsePDU(request, m_targets, state.SessionParameters, state.ConnectionParameters, ref state.Target, GetNextTSIH);
- if (state.Target != null)
- {
- if (request.LoginParameters.ContainsKey("TargetMaxRecvDataSegmentLength"))
- {
- state.ConnectionParameters.InitiatorMaxRecvDataSegmentLength = Convert.ToInt32(request.LoginParameters.ValueOf("TargetMaxRecvDataSegmentLength"));
- Log("[{0}][ProcessPDU] Initiator's TargetMaxRecvDataSegmentLength: {1}", state.ConnectionIdentifier, state.ConnectionParameters.InitiatorMaxRecvDataSegmentLength);
- }
- state.SessionParameters.ISID = request.ISID;
- state.ConnectionParameters.CID = request.CID;
- if (response.NextStage == 3)
- {
- m_activeConnections.Add(state);
- }
- }
- Log("[{0}][ReceiveCallback] Login Response parameters: {1}", state.ConnectionIdentifier, KeyValuePairUtils.ToString(response.LoginParameters));
- TrySendPDU(state, response);
- }
- else if (pdu is LogoutRequestPDU)
- {
- lock (m_activeConnectionsLock)
- {
- int connectionIndex = GetStateObjectIndex(m_activeConnections, state.SessionParameters.ISID, state.SessionParameters.TSIH, state.ConnectionParameters.CID);
- if (connectionIndex >= 0)
- {
- lock (m_activeConnections[connectionIndex].Target.WriteLock)
- {
- // Wait for pending I/O to complete.
- }
- m_activeConnections.RemoveAt(connectionIndex);
- }
- }
- LogoutRequestPDU request = (LogoutRequestPDU)pdu;
- LogoutResponsePDU response = ServerResponseHelper.GetLogoutResponsePDU(request);
- TrySendPDU(state, response);
- clientSocket.Close(); // We can close the connection now
- }
- else // Target commands
- {
- if (state.SessionParameters.IsDiscovery || state.Target == null)
- {
- Log("[{0}][ProcessPDU] Unknown or improper command during discovery session, OpCode: 0x{1}", state.ConnectionIdentifier, pdu.OpCode.ToString("x"));
- }
- else // Logged in to target
- {
- if (pdu is SCSIDataOutPDU)
- {
- SCSIDataOutPDU request = (SCSIDataOutPDU)pdu;
- ISCSIServer.Log("[{0}][ProcessPDU] SCSIDataOutPDU: Target transfer tag: {1}, LUN: {2}, Buffer offset: {3}, Data segment length: {4}, DataSN: {5}, Final: {6}", state.ConnectionIdentifier, request.TargetTransferTag, (ushort)request.LUN, request.BufferOffset, request.DataSegmentLength, request.DataSN, request.Final);
- ISCSIPDU response = TargetResponseHelper.GetSCSIDataOutResponsePDU(request, state.Target, state.SessionParameters, state.ConnectionParameters);
- TrySendPDU(state, response);
- }
- else if (pdu is SCSICommandPDU)
- {
- SCSICommandPDU command = (SCSICommandPDU)pdu;
- if (command.CommandDescriptorBlock == null)
- {
- Log("[{0}][ProcessPDU] Unsupported SCSI Command (0x{1})", state.ConnectionIdentifier, command.OpCodeSpecific[12].ToString("X"));
- SCSIResponsePDU response = new SCSIResponsePDU();
- response.InitiatorTaskTag = command.InitiatorTaskTag;
- response.Status = SCSIStatusCodeName.CheckCondition;
- response.Data = SCSITarget.FormatSenseData(SenseDataParameter.GetIllegalRequestUnsupportedCommandCodeSenseData());
- TrySendPDU(state, response);
- }
- else
- {
- ISCSIServer.Log("[{0}][ProcessPDU] SCSICommandPDU: CmdSN: {1}, SCSI command: {2}, LUN: {3}, Data segment length: {4}, Expected Data Transfer Length: {5}, Final: {6}", state.ConnectionIdentifier, command.CmdSN, (SCSIOpCodeName)command.CommandDescriptorBlock.OpCode, (ushort)command.LUN, command.DataSegmentLength, command.ExpectedDataTransferLength, command.Final);
- List<ISCSIPDU> scsiResponseList = TargetResponseHelper.GetSCSIResponsePDU(command, state.Target, state.SessionParameters, state.ConnectionParameters);
- foreach (ISCSIPDU response in scsiResponseList)
- {
- TrySendPDU(state, response);
- if (!clientSocket.Connected)
- {
- return;
- }
- }
- }
- }
- else
- {
- Log("[{0}][ProcessPDU] Unknown or improper command, OpCode: 0x{1}", state.ConnectionIdentifier, pdu.OpCode.ToString("x"));
- }
- }
- }
- }
- private static int GetStateObjectIndex(List<StateObject> stateObjects, ulong isid, ushort tsih, ushort cid)
- {
- for (int index = 0; index < stateObjects.Count; index++)
- {
- if (stateObjects[index].SessionParameters.ISID == isid &&
- stateObjects[index].SessionParameters.TSIH == tsih &&
- stateObjects[index].ConnectionParameters.CID == cid)
- {
- return index;
- }
- }
- return -1;
- }
- public static void TrySendPDU(StateObject state, ISCSIPDU response)
- {
- Socket clientSocket = state.ClientSocket;
- try
- {
- PDUHelper.SetStatSN(response, state.ConnectionParameters.StatSN);
- PDUHelper.SetExpCmdSN(response, state.SessionParameters.ExpCmdSN, state.SessionParameters.ExpCmdSN + state.SessionParameters.CommandQueueSize);
- if (response is SCSIResponsePDU || (response is SCSIDataInPDU && ((SCSIDataInPDU)response).StatusPresent))
- {
- state.ConnectionParameters.StatSN++;
- }
- clientSocket.Send(response.GetBytes());
- Log("[{0}][TrySendPDU] Sent response to initator, Operation: {1}, Size: {2}", state.ConnectionIdentifier, response.OpCode, response.Length);
- }
- catch (SocketException ex)
- {
- Log("[{0}][TrySendPDU] Failed to send response to initator (Operation: {1}, Size: {2}), SocketException: {3}", state.ConnectionIdentifier, response.OpCode, response.Length, ex.Message);
- }
- catch (ObjectDisposedException)
- {
- }
- }
- public ushort GetNextTSIH()
- {
- // The iSCSI Target selects a non-zero value for the TSIH at
- // session creation (when an initiator presents a 0 value at Login).
- // After being selected, the same TSIH value MUST be used whenever the
- // initiator or target refers to the session and a TSIH is required
- ushort nextTSIH = m_nextTSIH;
- m_nextTSIH++;
- if (m_nextTSIH == 0)
- {
- m_nextTSIH++;
- }
- return nextTSIH;
- }
- public static void Log(string message)
- {
- if (m_logFile != null)
- {
- lock (m_logSyncLock)
- {
- StreamWriter writer = new StreamWriter(m_logFile);
- string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ");
- writer.WriteLine(timestamp + message);
- writer.Flush();
- }
- }
- }
- public static void Log(string message, params object[] args)
- {
- Log(String.Format(message, args));
- }
- }
- }
|