ISCSI.cpp revision cbda1eff859e8ab0e1b154859231a7a7856750cf
/* $Id$ */
/** @file
* iSCSI initiator driver, VD backend.
*/
/*
* Copyright (C) 2006-2011 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_VD_ISCSI
#include <VBox/vd-plugin.h>
#include <iprt/semaphore.h>
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** The maximum number of release log entries per image. */
#define MAX_LOG_REL_ERRORS 1024
/** Default port number to use for iSCSI. */
#define ISCSI_DEFAULT_PORT 3260
/** Converts a number in the range of 0 - 15 into the corresponding hex char. */
/** Converts a hex char into the corresponding number in the range 0-15. */
/* Converts a base64 char into the corresponding number in the range 0-63. */
#define B64_2_NUM(c) ((c >= 'A' && c <= 'Z') ? (c - 'A') : (c >= 'a' && c <= 'z') ? (c - 'a' + 26) : (c >= '0' && c <= '9') ? (c - '0' + 52) : (c == '+') ? 62 : (c == '/') ? 63 : -1)
/** Minimum CHAP_MD5 challenge length in bytes. */
#define CHAP_MD5_CHALLENGE_MIN 16
/** Maximum CHAP_MD5 challenge length in bytes. */
#define CHAP_MD5_CHALLENGE_MAX 24
/**
* SCSI peripheral device type. */
typedef enum SCSIDEVTYPE
{
/** direct-access device. */
SCSI_DEVTYPE_DISK = 0,
/** sequential-access device. */
/** printer device. */
/** processor device. */
/** write-once device. */
/** scanner device. */
/** optical memory device. */
/** medium changer. */
/** communications device. */
/** storage array controller device. */
SCSI_DEVTYPE_RAIDCTL = 0x0c,
/** enclosure services device. */
/** simplified direct-access device. */
/** bridge controller device. */
/** object-based storage device. */
} SCSIDEVTYPE;
/** Mask for extracting the SCSI device type out of the first byte of the INQUIRY response. */
#define SCSI_DEVTYPE_MASK 0x1f
/** Mask to extract the CmdQue bit out of the seventh byte of the INQUIRY response. */
#define SCSI_INQUIRY_CMDQUE_MASK 0x02
/** Maximum PDU payload size we can handle in one piece. Greater or equal than
* s_iscsiConfigDefaultWriteSplit. */
#define ISCSI_DATA_LENGTH_MAX _256K
/** Maximum PDU size we can handle in one piece. */
/** Version of the iSCSI standard which this initiator driver can handle. */
#define ISCSI_MY_VERSION 0
/** Length of ISCSI basic header segment. */
#define ISCSI_BHS_SIZE 48
/** Reserved task tag value. */
#define ISCSI_TASK_TAG_RSVD 0xffffffff
/**
* iSCSI opcodes. */
typedef enum ISCSIOPCODE
{
/** NOP-Out. */
ISCSIOP_NOP_OUT = 0x00000000,
/** SCSI command. */
ISCSIOP_SCSI_CMD = 0x01000000,
/** SCSI task management request. */
ISCSIOP_SCSI_TASKMGMT_REQ = 0x02000000,
/** Login request. */
ISCSIOP_LOGIN_REQ = 0x03000000,
/** Text request. */
ISCSIOP_TEXT_REQ = 0x04000000,
/** SCSI Data-Out. */
ISCSIOP_SCSI_DATA_OUT = 0x05000000,
/** Logout request. */
ISCSIOP_LOGOUT_REQ = 0x06000000,
/** SNACK request. */
ISCSIOP_SNACK_REQ = 0x10000000,
/** NOP-In. */
ISCSIOP_NOP_IN = 0x20000000,
/** SCSI response. */
ISCSIOP_SCSI_RES = 0x21000000,
/** SCSI Task Management response. */
ISCSIOP_SCSI_TASKMGMT_RES = 0x22000000,
/** Login response. */
ISCSIOP_LOGIN_RES = 0x23000000,
/** Text response. */
ISCSIOP_TEXT_RES = 0x24000000,
/** SCSI Data-In. */
ISCSIOP_SCSI_DATA_IN = 0x25000000,
/** Logout response. */
ISCSIOP_LOGOUT_RES = 0x26000000,
/** Ready To Transfer (R2T). */
ISCSIOP_R2T = 0x31000000,
/** Asynchronous message. */
ISCSIOP_ASYN_MSG = 0x32000000,
/** Reject. */
ISCSIOP_REJECT = 0x3f000000
} ISCSIOPCODE;
/** Mask for extracting the iSCSI opcode out of the first header word. */
#define ISCSIOP_MASK 0x3f000000
/** ISCSI BHS word 0: Request should be processed immediately. */
#define ISCSI_IMMEDIATE_DELIVERY_BIT 0x40000000
#define ISCSI_FINAL_BIT 0x00800000
/** ISCSI BHS word 0: Mask for extracting the CSG. */
#define ISCSI_CSG_MASK 0x000c0000
/** ISCSI BHS word 0: Shift offset for extracting the CSG. */
#define ISCSI_CSG_SHIFT 18
/** ISCSI BHS word 0: Mask for extracting the NSG. */
#define ISCSI_NSG_MASK 0x00030000
/** ISCSI BHS word 0: Shift offset for extracting the NSG. */
#define ISCSI_NSG_SHIFT 16
/** ISCSI BHS word 0: task attribute untagged */
#define ISCSI_TASK_ATTR_UNTAGGED 0x00000000
/** ISCSI BHS word 0: task attribute simple */
#define ISCSI_TASK_ATTR_SIMPLE 0x00010000
/** ISCSI BHS word 0: task attribute ordered */
#define ISCSI_TASK_ATTR_ORDERED 0x00020000
/** ISCSI BHS word 0: task attribute head of queue */
#define ISCSI_TASK_ATTR_HOQ 0x00030000
/** ISCSI BHS word 0: task attribute ACA */
#define ISCSI_TASK_ATTR_ACA 0x00040000
/** ISCSI BHS word 0: transit to next login phase. */
#define ISCSI_TRANSIT_BIT 0x00800000
/** ISCSI BHS word 0: continue with login negotiation. */
#define ISCSI_CONTINUE_BIT 0x00400000
/** ISCSI BHS word 0: residual underflow. */
#define ISCSI_RESIDUAL_UNFL_BIT 0x00020000
/** ISCSI BHS word 0: residual overflow. */
#define ISCSI_RESIDUAL_OVFL_BIT 0x00040000
/** ISCSI BHS word 0: Bidirectional read residual underflow. */
#define ISCSI_BI_READ_RESIDUAL_UNFL_BIT 0x00080000
/** ISCSI BHS word 0: Bidirectional read residual overflow. */
#define ISCSI_BI_READ_RESIDUAL_OVFL_BIT 0x00100000
/** ISCSI BHS word 0: SCSI response mask. */
#define ISCSI_SCSI_RESPONSE_MASK 0x0000ff00
/** ISCSI BHS word 0: SCSI status mask. */
#define ISCSI_SCSI_STATUS_MASK 0x000000ff
/** ISCSI BHS word 0: response includes status. */
#define ISCSI_STATUS_BIT 0x00010000
#define ISCSI_SG_SEGMENTS_MAX 4
/** Number of entries in the command table. */
#define ISCSI_CMD_WAITING_ENTRIES 32
/**
* iSCSI login status class. */
typedef enum ISCSILOGINSTATUSCLASS
{
/** Success. */
/** Redirection. */
/** Initiator error. */
/** Target error. */
/**
* iSCSI connection state. */
typedef enum ISCSISTATE
{
/** Not having a connection/session at all. */
/** Currently trying to login. */
/** Normal operation, corresponds roughly to the Full Feature Phase. */
/** Currently trying to logout. */
} ISCSISTATE;
/**
* iSCSI PDU send flags (and maybe more in the future). */
typedef enum ISCSIPDUFLAGS
{
/** No special flags */
ISCSIPDU_DEFAULT = 0,
/** Do not attempt to re-attach to the target if the connection is lost */
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* iSCSI login negotiation parameter
*/
typedef struct ISCSIPARAMETER
{
/** Name of the parameter. */
const char *pszParamName;
/** Value of the parameter. */
const char *pszParamValue;
/** Length of the binary parameter. 0=zero-terminated string. */
/**
* iSCSI Response PDU buffer (scatter).
*/
typedef struct ISCSIRES
{
/** Length of PDU segment. */
/** Pointer to PDU segment. */
void *pvSeg;
} ISCSIRES;
/** Pointer to an iSCSI Response PDU buffer. */
/** Pointer to a const iSCSI Response PDU buffer. */
typedef ISCSIRES const *PCISCSIRES;
/**
* iSCSI Request PDU buffer (gather).
*/
typedef struct ISCSIREQ
{
/** Length of PDU segment in bytes. */
/** Pointer to PDU segment. */
const void *pcvSeg;
} ISCSIREQ;
/** Pointer to an iSCSI Request PDU buffer. */
/** Pointer to a const iSCSI Request PDU buffer. */
typedef ISCSIREQ const *PCISCSIREQ;
/**
* SCSI transfer directions.
*/
typedef enum SCSIXFER
{
SCSIXFER_NONE = 0,
/** Forward declaration. */
typedef struct ISCSIIMAGE *PISCSIIMAGE;
/**
* SCSI request structure.
*/
typedef struct SCSIREQ
{
/** I/O context associated with this request. */
/** Transfer direction. */
/** Length of command block. */
/** Length of Initiator2Target data buffer. */
/** Length of Target2Initiator data buffer. */
/** Length of sense buffer
* This contains the number of sense bytes received upon completion. */
/** Completion status of the command. */
/** The CDB. */
/** The sense buffer. */
/** Status code to return if we got sense data. */
int rcSense;
/** Pointer to the Initiator2Target S/G list. */
/** Number of entries in the I2T S/G list. */
unsigned cI2TSegs;
/** Pointer to the Target2Initiator S/G list. */
/** Number of entries in the T2I S/G list. */
unsigned cT2ISegs;
/** S/G buffer for the target to initiator bits. */
/** Number of retries if the command completes with sense
* data before we return with an error.
*/
unsigned cSenseRetries;
/** The S/G list - variable in size.
* This array holds both the I2T and T2I segments.
* The I2T segments are first and the T2I are second.
*/
typedef enum ISCSICMDTYPE
{
/** Process a SCSI request. */
ISCSICMDTYPE_REQ = 0,
/** Call a function in the I/O thread. */
/** Usual 32bit hack. */
ISCSICMDTYPE_32BIT_HACK = 0x7fffffff
} ISCSICMDTYPE;
/** The command completion function. */
/** Pointer to a command completion function. */
typedef FNISCSICMDCOMPLETED *PFNISCSICMDCOMPLETED;
/** The command execution function. */
/** Pointer to a command execution function. */
typedef FNISCSIEXEC *PFNISCSIEXEC;
/**
* Structure used to complete a synchronous request.
*/
typedef struct ISCSICMDSYNC
{
/** Event semaphore to wakeup the waiting thread. */
/** Status code of the command. */
int rcCmd;
/**
* iSCSI command.
* Used to forward requests to the I/O thread
* if existing.
*/
typedef struct ISCSICMD
{
/** Next one in the list. */
/** Assigned ITT. */
/** Completion callback. */
/** Opaque user data. */
void *pvUser;
/** Command to execute. */
/** Command type dependent data. */
union
{
/** Process a SCSI request. */
struct
{
/** The SCSI request to process. */
} ScsiReq;
/** Call a function in the I/O thread. */
struct
{
/** The method to execute. */
/** User data. */
void *pvUser;
} Exec;
} CmdType;
/**
* Send iSCSI PDU.
* Contains all necessary data to send a PDU.
*/
typedef struct ISCSIPDUTX
{
/** Pointer to the next PDu to send. */
struct ISCSIPDUTX *pNext;
/** The BHS. */
/** Assigned CmdSN for this PDU. */
/** The S/G buffer used for sending. */
/** Number of bytes to send until the PDU completed. */
/** The iSCSI command this PDU belongs to. */
/** Number of segments in the request segments array. */
unsigned cISCSIReq;
/** The request segments - variable in size. */
} ISCSIPDUTX, *PISCSIPDUTX;
/**
* Block driver instance data.
*/
typedef struct ISCSIIMAGE
{
/** Pointer to the filename (location). Not really used. */
const char *pszFilename;
/** Pointer to the initiator name. */
char *pszInitiatorName;
/** Pointer to the target name. */
char *pszTargetName;
/** Pointer to the target address. */
char *pszTargetAddress;
/** Pointer to the user name for authenticating the Initiator. */
char *pszInitiatorUsername;
/** Pointer to the secret for authenticating the Initiator. */
/** Length of the secret for authenticating the Initiator. */
/** Pointer to the user name for authenticating the Target. */
char *pszTargetUsername;
/** Pointer to the secret for authenticating the Initiator. */
/** Length of the secret for authenticating the Initiator. */
/** Limit for iSCSI writes, essentially limiting the amount of data
* written in a single write. This is negotiated with the target, so
* the actual size might be smaller. */
/** Initiator session identifier. */
/** SCSI Logical Unit Number. */
/** Pointer to the per-disk VD interface list. */
/** Pointer to the per-image VD interface list. */
/** Error interface. */
/** Config interface. */
/** I/O interface. */
/** TCP network stack interface. */
/** Image open flags. */
unsigned uOpenFlags;
/** Number of re-login retries when a connection fails. */
/** Sector size on volume. */
/** Size of volume in sectors. */
/** Total volume size in bytes. Easier than multiplying the above values all the time. */
/** Negotiated maximum data length when sending to target. */
/** Negotiated maximum data length when receiving from target. */
/** Current state of the connection/session. */
/** Flag whether the first Login Response PDU has been seen. */
bool FirstRecvPDU;
/** Initiator Task Tag of the last iSCSI request PDU. */
/** Sequence number of the last command. */
/** Sequence number of the next command expected by the target. */
/** Maximum sequence number accepted by the target (determines size of window). */
/** Expected sequence number of next status. */
/** Currently active request. */
/** Segment number of currently active request. */
/** Pointer to receive PDU buffer. (Freed by RT) */
void *pvRecvPDUBuf;
/** Length of receive PDU buffer. */
/** Mutex protecting against concurrent use from several threads. */
/** Pointer to the target hostname. */
char *pszHostname;
/** Port to use on the target host. */
/** Socket handle of the TCP connection. */
/** Timeout for read operations on the TCP connection (in milliseconds). */
/** Flag whether to automatically generate the initiator name. */
bool fAutomaticInitiatorName;
/** Flag whether to use the host IP stack or DevINIP. */
bool fHostIP;
/** Head of request queue */
/** Mutex protecting the request queue from concurrent access. */
/** I/O thread. */
/** Flag whether the thread should be still running. */
volatile bool fRunning;
/* Flag whether the target supports command queuing. */
bool fCmdQueuingSupported;
/** Flag whether extended select is supported. */
bool fExtendedSelectSupported;
/** Padding used for aligning the PDUs. */
/** Socket events to poll for. */
/** Number of bytes to read to complete the current PDU. */
/** Current position in the PDU buffer. */
/** Flag whether we are currently reading the BHS. */
bool fRecvPDUBHS;
/** List of PDUs waiting to get transmitted. */
/** Tail of PDUs waiting to get transmitted. */
/** PDU we are currently transmitting. */
/** Number of commands waiting for an answer from the target.
* Used for timeout handling for poll.
*/
unsigned cCmdsWaiting;
/** Table of commands waiting for a response from the target. */
/** Release log counter. */
unsigned cLogRelErrors;
} ISCSIIMAGE;
/*******************************************************************************
* Static Variables *
*******************************************************************************/
/** Default initiator basename. */
static const char *s_iscsiDefaultInitiatorBasename = "iqn.2009-08.com.sun.virtualbox.initiator";
/** Default LUN. */
static const char *s_iscsiConfigDefaultLUN = "0";
/** Default timeout, 10 seconds. */
static const char *s_iscsiConfigDefaultTimeout = "10000";
/** Default write split value, less or equal to ISCSI_DATA_LENGTH_MAX. */
static const char *s_iscsiConfigDefaultWriteSplit = "262144";
/** Default host IP stack. */
static const char *s_iscsiConfigDefaultHostIPStack = "1";
/** Description of all accepted config parameters. */
static const VDCONFIGINFO s_iscsiConfigInfo[] =
{
/* LUN is defined of string type to handle the "enc" prefix. */
};
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/* iSCSI low-level functions (only to be used from the iSCSI high-level functions). */
static int iscsiTextAddKeyValue(uint8_t *pbBuf, size_t cbBuf, size_t *pcbBufCurr, const char *pcszKey, const char *pcszValue, size_t cbValue);
static int iscsiTextGetKeyValue(const uint8_t *pbBuf, size_t cbBuf, const char *pcszKey, const char **ppcszValue);
/* Serial number arithmetic comparison. */
/* CHAP-MD5 functions. */
#ifdef IMPLEMENT_TARGET_AUTH
#endif
static void chap_md5_compute_response(uint8_t *pbResponse, uint8_t id, const uint8_t *pbChallenge, size_t cbChallenge,
/**
* Internal: release log wrapper limiting the number of entries.
*/
{
{
}
}
{
}
/**
* Calculates the hash for the given ITT used
* to look up the command in the table.
*/
{
return Itt % ISCSI_CMD_WAITING_ENTRIES;
}
{
while ( pIScsiCmd
return pIScsiCmd;
}
{
pImage->cCmdsWaiting++;
}
{
while ( pIScsiCmd
{
}
if (pIScsiCmd)
{
if (pIScsiCmdPrev)
{
}
else
{
}
pImage->cCmdsWaiting--;
}
return pIScsiCmd;
}
/**
* Removes all commands from the table and returns the
* list head
*
* @returns Pointer to the head of the command list.
* @param pImage iSCSI connection to use.
*/
{
{
if (pHead)
{
/* Get the tail. */
/* Concatenate. */
}
}
pImage->cCmdsWaiting = 0;
return pIScsiCmdHead;
}
{
int rc;
if (!pImage->pszHostname)
return VERR_NET_DEST_ADDRESS_REQUIRED;
if (RT_FAILURE(rc))
{
if ( rc == VERR_NET_CONNECTION_REFUSED
|| rc == VERR_NET_CONNECTION_RESET
|| rc == VERR_NET_UNREACHABLE
|| rc == VERR_NET_HOST_UNREACHABLE
|| rc == VERR_NET_CONNECTION_TIMED_OUT)
{
/* Standardize return value for no connection. */
}
return rc;
}
/* Disable Nagle algorithm, we want things to be sent immediately. */
/* Make initiator name and ISID unique on this host. */
if (RT_FAILURE(rc))
return rc;
/* Eliminate the port so that it isn't included below. */
{
if (pImage->pszInitiatorName)
if (!pImage->pszInitiatorName)
return VERR_NO_MEMORY;
}
LogRel(("iSCSI: connect from initiator %s with source port %u\n", pImage->pszInitiatorName, pImage->ISID & 65535));
return VINF_SUCCESS;
}
{
int rc;
if (iscsiIsClientConnected(pImage))
{
LogRel(("iSCSI: disconnect from initiator %s with source port %u\n", pImage->pszInitiatorName, pImage->ISID & 65535));
}
else
rc = VINF_SUCCESS;
return rc;
}
{
int rc = VINF_SUCCESS;
unsigned int i = 0;
char *pDst;
if (!iscsiIsClientConnected(pImage))
{
/* Reconnecting makes no sense in this case, as there will be nothing
* to receive. We would just run into a timeout. */
}
{
cbToRead = 0;
residual = ISCSI_BHS_SIZE; /* Do not read more than the BHS length before the true PDU length is known. */
do
{
if (cMilliesRemaining <= 0)
{
rc = VERR_TIMEOUT;
break;
}
if (RT_FAILURE(rc))
break;
if (RT_FAILURE(rc))
break;
if (cbActuallyRead == 0)
{
/* The other end has closed the connection. */
break;
}
if (cbToRead == 0)
{
/* Currently reading the BHS. */
pDst += cbActuallyRead;
if (residual <= 40)
{
/* Enough data read to figure out the actual PDU size. */
/* Check whether we are already done with this PDU (no payload). */
if (cbToRead == 0)
break;
}
}
else
{
if (cbToRead == 0)
break;
pDst += cbActuallyRead;
}
if (residual == 0)
{
i++;
if (i >= cnResponse)
{
/* No space left in receive buffers. */
break;
}
}
LogFlowFunc(("cbToRead=%u residual=%u cbSegActual=%u cbActuallRead=%u\n",
} while (true);
}
else
{
if (RT_SUCCESS(rc))
}
if (RT_SUCCESS(rc))
{
for (i++; i < cnResponse; i++)
paResponse[i].cbSeg = 0;
}
&& ( rc == VERR_NET_CONNECTION_RESET
|| rc == VERR_BROKEN_PIPE)))
{
/* Standardize return value for broken connection. */
}
return rc;
}
{
int rc = VINF_SUCCESS;
unsigned int i;
if (!iscsiIsClientConnected(pImage))
{
/* Attempt to reconnect if the connection was previously broken. */
}
if (RT_SUCCESS(rc))
{
* needs twice as many entries to allow for padding. */
unsigned cBuf = 0;
for (i = 0; i < cnRequest; i++)
{
cBuf++;
cBuf++;
}
static char aPad[4] = { 0, 0, 0, 0 };
unsigned iBuf = 0;
for (i = 0; i < cnRequest; i++)
{
/* Actual data chunk. */
iBuf++;
/* Insert proper padding before the next chunk. */
{
iBuf++;
}
}
/* Send out the request, the socket is set to send data immediately,
* avoiding unnecessary delays. */
}
&& ( rc == VERR_NET_CONNECTION_RESET
|| rc == VERR_BROKEN_PIPE)))
{
/* Standardize return value for broken connection. */
}
return rc;
}
{
int rc = VINF_SUCCESS;
char *pszPortEnd;
/* Clean up previous connection data. */
if (pImage->pszHostname)
{
}
/* Locate the port number via the colon separating the hostname from the port. */
if (*pImage->pszTargetAddress)
{
{
/* Normal hostname or IPv4 dotted decimal. */
{
pcszPort++;
}
else
}
else
{
/* IPv6 literal address. Contains colons, so skip to closing square bracket. */
{
pcszPort++;
if (*pcszPort == '\0')
else if (*pcszPort != ':')
else
pcszPort++;
}
else
}
}
else
/* Now split address into hostname and port. */
if (RT_SUCCESS(rc))
{
if (!pImage->pszHostname)
rc = VERR_NO_MEMORY;
else
{
else
{
/* Note that RT_SUCCESS() macro to check the rc value is not strict enough in this case. */
{
}
else
{
}
}
else
}
}
if (RT_SUCCESS(rc))
{
if (!iscsiIsClientConnected(pImage))
}
else
{
if (pImage->pszHostname)
{
}
}
return rc;
}
/**
* Attach to an iSCSI target. Performs all operations necessary to enter
* Full Feature Phase.
*
* @returns VBox status.
* @param pImage The iSCSI connection state to be used.
*/
static int iscsiAttach(void *pvUser)
{
int rc;
bool transit;
char *pszNext;
bool fParameterNeg = true;;
char szMaxDataLength[16];
{
{ "HeaderDigest", "None", 0 },
{ "DataDigest", "None", 0 },
{ "MaxConnections", "1", 0 },
{ "InitialR2T", "No", 0 },
{ "ImmediateData", "Yes", 0 },
{ "MaxRecvDataSegmentLength", szMaxDataLength, 0 },
{ "MaxBurstLength", szMaxDataLength, 0 },
{ "FirstBurstLength", szMaxDataLength, 0 },
{ "DefaultTime2Wait", "0", 0 },
{ "DefaultTime2Retain", "60", 0 },
{ "DataPDUInOrder", "Yes", 0 },
{ "DataSequenceInOrder", "Yes", 0 },
{ "ErrorRecoveryLevel", "0", 0 },
{ "MaxOutstandingR2T", "1", 0 }
};
LogFlowFunc(("entering\n"));
/* Make 100% sure the connection isn't reused for a new login. */
if (!iscsiIsClientConnected(pImage))
{
if (RT_FAILURE(rc))
goto out;
}
pImage->FirstRecvPDU = true;
/*
* Send login request to target.
*/
csg = 0;
nsg = 0;
substate = 0;
do {
transit = false;
cbBuf = 0;
/* Handle all cases with a single switch statement. */
{
case 0x0000: /* security negotiation, step 0: propose authentication. */
if (RT_FAILURE(rc))
goto out;
rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "InitiatorName", pImage->pszInitiatorName, 0);
if (RT_FAILURE(rc))
goto out;
if (RT_FAILURE(rc))
goto out;
{
/* No authentication. Immediately switch to next phase. */
if (RT_FAILURE(rc))
goto out;
nsg = 1;
transit = true;
}
else
{
if (RT_FAILURE(rc))
goto out;
}
break;
case 0x0001: /* security negotiation, step 1: propose CHAP_MD5 variant. */
if (RT_FAILURE(rc))
goto out;
break;
case 0x0002: /* security negotiation, step 2: send authentication info. */
if (RT_FAILURE(rc))
goto out;
rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "CHAP_R", (const char *)aResponse, RTMD5HASHSIZE);
if (RT_FAILURE(rc))
goto out;
nsg = 1;
transit = true;
break;
case 0x0100: /* login operational negotiation, step 0: set parameters. */
if (fParameterNeg)
{
for (unsigned i = 0; i < RT_ELEMENTS(aParameterNeg); i++)
{
if (RT_FAILURE(rc))
goto out;
}
fParameterNeg = false;
}
nsg = 3;
transit = true;
break;
case 0x0300: /* full feature phase. */
default:
/* Should never come here. */
break;
}
| (csg << ISCSI_CSG_SHIFT)
| ISCSI_MY_VERSION /* Minimum version. */
| ISCSIOP_LOGIN_REQ); /* C=0 */
cnISCSIReq = 0;
cnISCSIReq++;
cnISCSIReq++;
if (RT_SUCCESS(rc))
{
cnISCSIRes = 0;
cnISCSIRes++;
cnISCSIRes++;
if (RT_FAILURE(rc))
break;
/** @todo collect partial login responses with Continue bit set. */
if (cmd == ISCSIOP_LOGIN_RES)
{
{
break; /* Give up immediately, as a RFC violation in version fields is very serious. */
}
switch (loginStatusClass)
{
bool targetTransit;
if (pImage->FirstRecvPDU)
{
pImage->FirstRecvPDU = false;
}
/* Handle all cases with a single switch statement. */
{
case 0x0000: /* security negotiation, step 0: receive final authentication. */
if (RT_FAILURE(rc))
break;
const char *pcszAuthMethod;
if (RT_FAILURE(rc))
{
break;
}
{
/* Authentication offered, but none required. Skip to operational parameters. */
csg = 1;
nsg = 1;
transit = true;
substate = 0;
break;
}
{
/* CHAP authentication required, continue with next substate. */
substate++;
break;
}
/* Unknown auth method or login response PDU headers incorrect. */
break;
case 0x0001: /* security negotiation, step 1: receive final CHAP variant and challenge. */
if (RT_FAILURE(rc))
break;
const char *pcszChapAuthMethod;
const char *pcszChapIdxTarget;
const char *pcszChapChallengeStr;
if (RT_FAILURE(rc))
{
break;
}
{
break;
}
if (RT_FAILURE(rc))
{
break;
}
{
break;
}
if (RT_FAILURE(rc))
{
break;
}
cbChallenge = sizeof(pbChallenge);
if (RT_FAILURE(rc))
break;
substate++;
transit = true;
break;
case 0x0002: /* security negotiation, step 2: check authentication success. */
if (RT_FAILURE(rc))
break;
{
/* Target wants to continue in login operational state, authentication success. */
csg = 1;
nsg = 3;
substate = 0;
break;
}
break;
case 0x0100: /* login operational negotiation, step 0: check results. */
if (RT_FAILURE(rc))
break;
{
/* Target wants to continue in full feature phase, login finished. */
csg = 3;
nsg = 3;
substate = 0;
break;
}
{
/* Target wants to negotiate certain parameters and
* stay in login operational negotiation. */
csg = 1;
nsg = 3;
substate = 0;
}
break;
case 0x0300: /* full feature phase. */
default:
break;
}
break;
const char *pcszTargetRedir;
/* Target has moved to some other location, as indicated in the TargetAddress key. */
if (RT_FAILURE(rc))
{
break;
}
if (pImage->pszTargetAddress)
{
if (!pImage->pszTargetAddress)
{
rc = VERR_NO_MEMORY;
break;
}
}
goto restart;
{
{
case 0x00:
pszDetail = "Miscelleanous iSCSI intiaitor error";
break;
case 0x01:
pszDetail = "Authentication failure";
break;
case 0x02:
pszDetail = "Authorization failure";
break;
case 0x03:
pszDetail = "Not found";
break;
case 0x04:
pszDetail = "Target removed";
break;
case 0x05:
pszDetail = "Unsupported version";
break;
case 0x06:
pszDetail = "Too many connections";
break;
case 0x07:
pszDetail = "Missing parameter";
break;
case 0x08:
pszDetail = "Can't include in session";
break;
case 0x09:
pszDetail = "Session type not supported";
break;
case 0x0a:
pszDetail = "Session does not exist";
break;
case 0x0b:
pszDetail = "Invalid request type during login";
break;
default:
pszDetail = "Unknown status detail";
}
goto out;
}
break;
default:
}
if (csg == 3)
{
/*
* Finished login, continuing with Full Feature Phase.
*/
rc = VINF_SUCCESS;
break;
}
}
else
{
AssertMsgFailed(("%s: ignoring unexpected PDU with first word = %#08x\n", __FUNCTION__, RT_N2H_U32(aResBHS[0])));
}
}
else
break;
} while (true);
out:
if (RT_FAILURE(rc))
{
/*
* Close connection to target.
*/
}
else
LogRel(("iSCSI: login to target %s %s\n", pImage->pszTargetName, RT_SUCCESS(rc) ? "successful" : "failed"));
return rc;
}
/**
* Detach from an iSCSI target.
*
* @returns VBox status.
* @param pImage The iSCSI connection state to be used.
*/
static int iscsiDetach(void *pvUser)
{
int rc;
uint32_t cnISCSIReq = 0;
LogFlowFunc(("entering\n"));
{
/*
* Send logout request to target.
*/
cnISCSIReq++;
if (RT_SUCCESS(rc))
{
/*
* Read logout response from target.
*/
if (RT_SUCCESS(rc))
{
AssertMsgFailed(("iSCSI Logout response invalid\n"));
}
else
}
else
}
{
/*
* Close connection to target.
*/
if (RT_FAILURE(rc))
}
LogFlowFunc(("leaving\n"));
return VINF_SUCCESS;
}
/**
* Perform a command on an iSCSI target. Target must be already in
* Full Feature Phase.
*
* @returns VBOX status.
* @param pImage The iSCSI connection state to be used.
* @param pRequest Command descriptor. Contains all information about
* the command, its transfer directions and pointers
* to the buffer(s) used for transferring data and
* status information.
*/
{
int rc;
uint32_t cnISCSIReq = 0;
bool final = false;
Assert(pRequest->enmXfer != SCSIXFER_TO_FROM_TARGET); /**< @todo not yet supported, would require AHS. */
/* If not in normal state, then the transport connection was dropped. Try
* to reestablish by logging in, the target might be responsive again. */
/* If still not in normal state, then the underlying transport connection
* cannot be established. Get out before bad things happen (and make
* sure the caller suspends the VM again). */
{
goto out;
}
/*
* Send SCSI command to target with all I2T data included.
*/
cbData = 0;
else
aReqBHS[1] = RT_H2N_U32(0x00000000 | ((uint32_t)pRequest->cbI2TData & 0xffffff)); /* TotalAHSLength=0 */
cnISCSIReq++;
{
cnISCSIReq++;
}
if (RT_FAILURE(rc))
goto out_release;
/* Place SCSI request in queue. */
/*
*/
{
}
else
cbBufLength = 0;
do {
uint32_t cnISCSIRes = 0;
cnISCSIRes++;
if (cbBufLength != 0 &&
{
cnISCSIRes++;
}
/* Always reserve space for the status - it's impossible to tell
* beforehand whether this will be the final PDU or not. */
cnISCSIRes++;
if (RT_FAILURE(rc))
break;
if (cmd == ISCSIOP_SCSI_RES)
{
/* This is the final PDU which delivers the status (and may be omitted if
* the last Data-In PDU included successful completion status). Note
* that ExpStatSN has been bumped already in iscsiRecvPDU. */
if (!final || ((RT_N2H_U32(aResBHS[0]) & 0x0000ff00) != 0) || (RT_N2H_U32(aResBHS[6]) != pImage->ExpStatSN - 1))
{
/* SCSI Response in the wrong place or with a (target) failure. */
break;
}
/* The following is a bit tricky, as in error situations we may
* get the status only instead of the result data plus optional
* status. Thus the status may have ended up partially in the
* data area. */
if (cbData >= 2)
{
{
break;
}
/* Truncate sense data if it doesn't fit into the buffer. */
{
}
}
else if (cbData == 1)
{
break;
}
else
break;
}
else if (cmd == ISCSIOP_SCSI_DATA_IN)
{
/* A Data-In PDU carries some data that needs to be added to the received
* data in response to the command. There may be both partial and complete
* Data-In PDUs, so collect data until the status is included or the status
* is sent in a separate SCSI Result frame (see above). */
{
/* The received PDU is partially stored in the buffer for status.
* Must not happen under normal circumstances and is a target error. */
break;
}
cbBufLength -= len;
ExpDataSN++;
{
break;
}
}
else
{
break;
}
} while (true);
/* Remove SCSI request from queue. */
if (rc == VERR_TIMEOUT)
{
/* Drop connection in case the target plays dead. Much better than
* delaying the next requests until the timed out command actually
* finishes. Also keep in mind that command shouldn't take longer than
* about 30-40 seconds, or the guest will lose its patience. */
}
out:
return rc;
}
/**
* Generate a new Initiator Task Tag.
*
* @returns Initiator Task Tag.
* @param pImage The iSCSI connection state to be used.
*/
{
return RT_H2N_U32(next_itt);
}
/**
* Send an iSCSI request. The request can consist of several segments, which
* are padded to 4 byte boundaries and concatenated.
*
* @returns VBOX status
* @param pImage The iSCSI connection state to be used.
* @param paReq Pointer to array of iSCSI request sections.
* @param cnReq Number of valid iSCSI request sections in the array.
* @param uFlags Flags controlling the exact send semantics.
*/
{
int rc = VINF_SUCCESS;
/** @todo return VERR_VD_ISCSI_INVALID_STATE in the appropriate situations,
* needs cleaning up of timeout/disconnect handling a bit, as otherwise
* too many incorrect errors are signalled. */
{
if (RT_SUCCESS(rc))
break;
if ( (uFlags & ISCSIPDU_NO_REATTACH)
break;
/* No point in reestablishing the connection for a logout */
break;
RTThreadSleep(500);
{
/* Attempt to re-login when a connection fails, but only when not
* currently logging in. */
if (RT_FAILURE(rc))
break;
}
}
return rc;
}
/**
* Wait for an iSCSI response with a matching Initiator Target Tag. The response is
* split into several segments, as requested by the caller-provided buffer specification.
* Remember that the response can be split into several PDUs by the sender, so make
* sure that all parts are collected and processed appropriately by the caller.
*
* @returns VBOX status
* @param pImage The iSCSI connection state to be used.
* @param paRes Pointer to array of iSCSI response sections.
* @param cnRes Number of valid iSCSI response sections in the array.
*/
{
int rc = VINF_SUCCESS;
{
if (RT_FAILURE(rc))
{
{
/* No point in reestablishing the connection for a logout */
break;
/* Connection broken while waiting for a response - wait a while and
* try to restart by re-sending the original request (if any).
* This also handles the connection reestablishment (login etc.). */
RTThreadSleep(500);
{
/* Attempt to re-login when a connection fails, but only when not
* currently logging in. */
if (RT_FAILURE(rc))
break;
}
{
if (RT_FAILURE(rc))
break;
}
}
else
{
/* Signal other errors (VERR_BUFFER_OVERFLOW etc.) to the caller. */
break;
}
}
else
{
/* Check whether the received PDU is valid, and update the internal state of
* the iSCSI connection/session. */
if (RT_FAILURE(rc))
continue;
switch (cmd)
{
case ISCSIOP_SCSI_RES:
case ISCSIOP_SCSI_DATA_IN:
case ISCSIOP_R2T:
case ISCSIOP_ASYN_MSG:
case ISCSIOP_TEXT_RES:
case ISCSIOP_LOGIN_RES:
case ISCSIOP_LOGOUT_RES:
case ISCSIOP_REJECT:
case ISCSIOP_NOP_IN:
break;
default:
}
if (RT_FAILURE(rc))
continue;
if ( !pImage->FirstRecvPDU
&& ( cmd != ISCSIOP_LOGIN_RES
{
{
/* StatSN counter is not advanced on R2T and on a target SN update NOP-In. */
if ( (cmd != ISCSIOP_R2T)
}
else
{
continue;
}
}
/* Finally check whether the received PDU matches what the caller wants. */
&& itt != ISCSI_TASK_TAG_RSVD)
{
/* Copy received PDU (one segment) to caller-provided buffers. */
uint32_t j;
for (j = 0; j < cnRes; j++)
{
{
}
else
{
cbSeg = 0;
break;
}
}
if (cbSeg != 0)
{
break;
}
for (j++; j < cnRes; j++)
break;
}
else if ( cmd == ISCSIOP_NOP_IN
{
cnISCSIReq = 0;
cnISCSIReq++;
/* Break if the caller wanted to process the NOP-in only. */
if (itt == ISCSI_TASK_TAG_RSVD)
break;
}
}
}
LogFlowFunc(("returns rc=%Rrc\n"));
return rc;
}
/**
* Reset the PDU buffer
*
* @param pImage The iSCSI connection state to be used.
*/
{
pImage->fRecvPDUBHS = true;
}
{
if (!fFront)
{
/* Insert PDU at the tail of the list. */
if (!pImage->pIScsiPDUTxHead)
else
}
else
{
/* Insert PDU at the beginning of the list. */
if (!pImage->pIScsiPDUTxTail)
}
}
/**
* Receives a PDU in a non blocking way.
*
* @returns VBOX status code.
* @param pImage The iSCSI connection state to be used.
*/
{
size_t cbActuallyRead = 0;
int rc = VINF_SUCCESS;
/* Check if we are in the middle of a PDU receive. */
if (pImage->cbRecvPDUResidual == 0)
{
/*
* We are receiving a new PDU, don't read more than the BHS initially
* until we know the real size of the PDU.
*/
LogFlow(("Receiving new PDU\n"));
}
if (RT_SUCCESS(rc))
{
/* Check if we received everything we wanted. */
if ( !pImage->cbRecvPDUResidual
&& pImage->fRecvPDUBHS)
{
/* If we were reading the BHS first get the actual PDU size now. */
}
if (!pImage->cbRecvPDUResidual)
{
/* We received the complete PDU with or without any payload now. */
LogFlow(("Received complete PDU\n"));
}
}
else
return rc;
}
{
int rc = VINF_SUCCESS;
do
{
/*
* If there is no PDU active, get the first one from the list.
* Check that we are allowed to transfer the PDU by comparing the
* command sequence number and the maximum sequence number allowed by the target.
*/
if (!pImage->pIScsiPDUTxCur)
{
if ( !pImage->pIScsiPDUTxHead
break;
if (!pImage->pIScsiPDUTxHead)
}
/* Send as much as we can. */
if (RT_SUCCESS(rc))
{
{
/* PDU completed, free it and place the command on the waiting for response list. */
{
LogFlow(("Sent complete PDU, placing on waiting list\n"));
}
}
}
} while ( RT_SUCCESS(rc)
&& !pImage->pIScsiPDUTxCur);
if (rc == VERR_TRY_AGAIN)
rc = VINF_SUCCESS;
/* Add the write poll flag if we still have something to send, clear it otherwise. */
if (pImage->pIScsiPDUTxCur)
else
return rc;
}
/**
* Process a received PDU.
*
* @return VBOX status code.
* @param pImage The iSCSI connection state to be used.
* @param paRes Pointer to the array of iSCSI response sections.
* @param cnRes Number of valid iSCSI response sections in the array.
*/
{
int rc = VINF_SUCCESS;
/* Validate the PDU first. */
if (RT_SUCCESS(rc))
{
do
{
switch (cmd)
{
case ISCSIOP_SCSI_RES:
case ISCSIOP_SCSI_DATA_IN:
case ISCSIOP_R2T:
case ISCSIOP_ASYN_MSG:
case ISCSIOP_TEXT_RES:
case ISCSIOP_LOGIN_RES:
case ISCSIOP_LOGOUT_RES:
case ISCSIOP_REJECT:
case ISCSIOP_NOP_IN:
break;
default:
}
if (RT_FAILURE(rc))
break;
if ( !pImage->FirstRecvPDU
{
{
/* StatSN counter is not advanced on R2T and on a target SN update NOP-In. */
if ( (cmd != ISCSIOP_R2T)
}
else
{
break;
}
}
{
/*
* This is a response from the target for a request from the initiator.
* Get the request and update its state.
*/
/* Try to send more PDUs now that we updated the MaxCmdSN field */
if ( RT_SUCCESS(rc)
&& !pImage->pIScsiPDUTxCur)
}
else
{
/* This is a target initiated request (we handle only NOP-In request at the moment). */
if ( cmd == ISCSIOP_NOP_IN
{
LogFlowFunc(("Sending NOP-Out\n"));
/* Allocate a new PDU initialize it and put onto the waiting list. */
if (!pIScsiPDUTx)
{
rc = VERR_NO_MEMORY;
break;
}
cnISCSIReq = 0;
cnISCSIReq++;
/*
* Link the PDU to the list.
* Insert at the front of the list to send the response as soon as possible
* to avoid frequent reconnects for a slow connection when there are many PDUs
* waiting.
*/
/* Start transfer of a PDU if there is no one active at the moment. */
if (!pImage->pIScsiPDUTxCur)
}
}
} while (0);
}
return rc;
}
/**
* Check the static (not dependent on the connection/session state) validity of an iSCSI response PDU.
*
* @returns VBOX status
* @param paRes Pointer to array of iSCSI response sections.
* @param cnRes Number of valid iSCSI response sections in the array.
*/
{
const uint32_t *pcrgResBHS;
switch (hw0 & ISCSIOP_MASK)
{
case ISCSIOP_NOP_IN:
/* NOP-In responses must not be split into several PDUs nor it may contain
* ping data for target-initiated pings nor may both task tags be valid task tags. */
if ( (hw0 & ISCSI_FINAL_BIT) == 0
return VERR_PARSE_ERROR;
break;
case ISCSIOP_SCSI_RES:
/* SCSI responses must not be split into several PDUs nor must the residual
* bits be contradicting each other nor may the residual bits be set for PDUs
* containing anything else but a completed command response. Underflow
* is no reason for declaring a PDU as invalid, as the target may choose
* to return less data than we assume to get. */
if ( (hw0 & ISCSI_FINAL_BIT) == 0
|| ( ((hw0 & ISCSI_SCSI_RESPONSE_MASK) == 0)
| ISCSI_RESIDUAL_OVFL_BIT))))
return VERR_PARSE_ERROR;
else
break;
case ISCSIOP_LOGIN_RES:
/* Login responses must not contain contradicting transit and continue bits. */
return VERR_PARSE_ERROR;
break;
case ISCSIOP_TEXT_RES:
/* Text responses must not contain contradicting final and continue bits nor
* may the final bit be set for PDUs containing a target transfer tag other than
* the reserved transfer tag (and vice versa). */
return VERR_PARSE_ERROR;
break;
case ISCSIOP_SCSI_DATA_IN:
/* SCSI Data-in responses must not contain contradicting residual bits when
* status bit is set. */
return VERR_PARSE_ERROR;
break;
case ISCSIOP_LOGOUT_RES:
/* Logout responses must not have the final bit unset and may not contain any
* data or additional header segments. */
if ( ((hw0 & ISCSI_FINAL_BIT) == 0)
return VERR_PARSE_ERROR;
break;
case ISCSIOP_ASYN_MSG:
/* Asynchronous Messages must not have the final bit unset and may not contain
* an initiator task tag. */
if ( ((hw0 & ISCSI_FINAL_BIT) == 0)
return VERR_PARSE_ERROR;
break;
case ISCSIOP_R2T:
case ISCSIOP_REJECT:
default:
/* Do some logging, ignore PDU. */
return VERR_PARSE_ERROR;
}
/* A target must not send PDUs with MaxCmdSN less than ExpCmdSN-1. */
return VERR_PARSE_ERROR;
return VINF_SUCCESS;
}
/**
* Prepares a PDU to transfer for the given command and adds it to the list.
*/
{
int rc = VINF_SUCCESS;
/*
* Allocate twice as much entries as required for padding (worst case).
* The additional segment is for the BHS.
*/
if (!pIScsiPDU)
return VERR_NO_MEMORY;
else
/* Setup the BHS. */
paReqBHS[1] = RT_H2N_U32(0x00000000 | ((uint32_t)pScsiReq->cbI2TData & 0xffffff)); /* TotalAHSLength=0 */
/* Setup the S/G buffers. */
uint32_t cnISCSIReq = 0;
cnISCSIReq++;
/* Padding is not necessary for the BHS. */
{
{
cnISCSIReq++;
/* Add padding if necessary. */
{
cnISCSIReq++;
}
}
}
/* Link the PDU to the list. */
/* Start transfer of a PDU if there is no one active at the moment. */
if (!pImage->pIScsiPDUTxCur)
return rc;
}
/**
* Updates the state of a request from the PDU we received.
*
* @return VBox status code.
* @param pImage iSCSI connection state to use.
* @param paRes Pointer to array of iSCSI response sections.
* @param cnRes Number of valid iSCSI response sections in the array.
*/
{
int rc = VINF_SUCCESS;
if (pIScsiCmd)
{
bool final = false;
if (cmd == ISCSIOP_SCSI_RES)
{
/* This is the final PDU which delivers the status (and may be omitted if
* the last Data-In PDU included successful completion status). Note
* that ExpStatSN has been bumped already in iscsiRecvPDU. */
if (!final || ((RT_N2H_U32(paResBHS[0]) & 0x0000ff00) != 0) || (RT_N2H_U32(paResBHS[6]) != pImage->ExpStatSN - 1))
{
/* SCSI Response in the wrong place or with a (target) failure. */
LogFlow(("Wrong ExpStatSN value in PDU\n"));
}
else
{
if (cbData >= 2)
{
{
}
else
{
/* Truncate sense data if it doesn't fit into the buffer. */
}
}
else if (cbData == 1)
else
}
}
else if (cmd == ISCSIOP_SCSI_DATA_IN)
{
/* A Data-In PDU carries some data that needs to be added to the received
* data in response to the command. There may be both partial and complete
* Data-In PDUs, so collect data until the status is included or the status
* is sent in a separate SCSI Result frame (see above). */
{
/* The received PDU is bigger than what we requested.
* Must not happen under normal circumstances and is a target error. */
}
else
{
/* Copy data from the received PDU into the T2I segments. */
{
}
}
}
else
}
/* Log any errors here but ignore the PDU. */
if (RT_FAILURE(rc))
{
LogRel(("iSCSI: Received malformed PDU from target %s (rc=%Rrc), ignoring\n", pImage->pszTargetName, rc));
rc = VINF_SUCCESS;
}
return rc;
}
/**
* Appends a key-value pair to the buffer. Normal ASCII strings (cbValue == 0) and large binary values
* of a given length (cbValue > 0) are directly supported. Other value types must be converted to ASCII
* by the caller. Strings must be in UTF-8 encoding.
*
* @returns VBOX status
* @param pbBuf Pointer to the key-value buffer.
* @param cbBuf Length of the key-value buffer.
* @param pcbBufCurr Currently used portion of the key-value buffer.
* @param pszKey Pointer to a string containing the key.
* @param pszValue Pointer to either a string containing the value or to a large binary value.
* @param cbValue Length of the binary value if applicable.
*/
static int iscsiTextAddKeyValue(uint8_t *pbBuf, size_t cbBuf, size_t *pcbBufCurr, const char *pcszKey,
{
if (cbValue == 0)
else
{
/* Buffer would overflow, signal error. */
return VERR_BUFFER_OVERFLOW;
}
/*
* Append a key=value pair (zero terminated string) to the end of the buffer.
*/
*pbCurr++ = '=';
if (cbValue == 0)
{
pbCurr += cbValueEnc;
}
else
{
*pbCurr++ = '0';
*pbCurr++ = 'x';
{
uint8_t b;
b = pcszValue[i];
}
}
*pbCurr = '\0';
return VINF_SUCCESS;
}
/**
* Retrieve the value for a given key from the key=value buffer.
*
* @returns VBOX status.
* @param pbBuf Buffer containing key=value pairs.
* @param cbBuf Length of buffer with key=value pairs.
* @param pszKey Pointer to key for which to retrieve the value.
* @param ppszValue Pointer to value string pointer.
*/
static int iscsiTextGetKeyValue(const uint8_t *pbBuf, size_t cbBuf, const char *pcszKey, const char **ppcszValue)
{
while (cbBuf != 0)
{
{
return VINF_SUCCESS;
}
pbBuf += cbKeyValNull;
cbBuf -= cbKeyValNull;
}
return VERR_INVALID_NAME;
}
/**
* Convert a long-binary value from a value string to the binary representation.
*
* @returns VBOX status
* @param pszValue Pointer to a string containing the textual value representation.
* @param pbValue Pointer to the value buffer for the binary value.
* @param pcbValue In: length of value buffer, out: actual length of binary value.
*/
{
return VERR_PARSE_ERROR;
if (*pcszValue++ != '0')
return VERR_PARSE_ERROR;
switch (*pcszValue++)
{
case 'x':
case 'X':
{
cbValue--;
}
while (*pcszValue != '\0')
{
if (cbValue == 0)
return VERR_BUFFER_OVERFLOW;
return VERR_PARSE_ERROR;
return VERR_PARSE_ERROR;
cbValue--;
}
break;
case 'b':
case 'B':
return VERR_PARSE_ERROR;
while (*pcszValue != '\0')
{
if (cbValue == 0)
return VERR_BUFFER_OVERFLOW;
if ((c1 < 'A' || c1 > 'Z') && (c1 < 'a' || c1 >'z') && (c1 < '0' || c1 > '9') && (c1 != '+') && (c1 != '/'))
return VERR_PARSE_ERROR;
if ((c2 < 'A' || c2 > 'Z') && (c2 < 'a' || c2 >'z') && (c2 < '0' || c2 > '9') && (c2 != '+') && (c2 != '/'))
return VERR_PARSE_ERROR;
if ((c3 < 'A' || c3 > 'Z') && (c3 < 'a' || c3 >'z') && (c3 < '0' || c3 > '9') && (c3 != '+') && (c3 != '/') && (c3 != '='))
return VERR_PARSE_ERROR;
|| ((c4 < 'A' || c4 > 'Z') && (c4 < 'a' || c4 >'z') && (c4 < '0' || c4 > '9') && (c4 != '+') && (c4 != '/') && (c4 != '=')))
return VERR_PARSE_ERROR;
if (c3 == '=') {
if (*pcszValue != '\0')
return VERR_PARSE_ERROR;
cbValue--;
} else {
if (c4 == '=') {
if (*pcszValue != '\0')
return VERR_PARSE_ERROR;
if (cbValue < 2)
return VERR_BUFFER_OVERFLOW;
cbValue -= 2;
}
else
{
if (cbValue < 3)
return VERR_BUFFER_OVERFLOW;
cbValue -= 3;
}
}
}
break;
default:
return VERR_PARSE_ERROR;
}
return VINF_SUCCESS;
}
/**
* Retrieve the relevant parameter values and update the initiator state.
*
* @returns VBOX status.
* @param pImage Current iSCSI initiator state.
* @param pbBuf Buffer containing key=value pairs.
* @param cbBuf Length of buffer with key=value pairs.
*/
{
int rc;
const char *pcszMaxRecvDataSegmentLength = NULL;
const char *pcszMaxBurstLength = NULL;
const char *pcszFirstBurstLength = NULL;
if (rc == VERR_INVALID_NAME)
rc = VINF_SUCCESS;
if (RT_FAILURE(rc))
return VERR_PARSE_ERROR;
if (rc == VERR_INVALID_NAME)
rc = VINF_SUCCESS;
if (RT_FAILURE(rc))
return VERR_PARSE_ERROR;
if (rc == VERR_INVALID_NAME)
rc = VINF_SUCCESS;
if (RT_FAILURE(rc))
return VERR_PARSE_ERROR;
{
}
if (pcszMaxBurstLength)
{
}
if (pcszFirstBurstLength)
{
}
return VINF_SUCCESS;
}
{
}
{
}
#ifdef IMPLEMENT_TARGET_AUTH
{
}
#endif
static void chap_md5_compute_response(uint8_t *pbResponse, uint8_t id, const uint8_t *pbChallenge, size_t cbChallenge,
{
}
/**
* Internal. - Wrapper around the extended select callback of the net interface.
*/
DECLINLINE(int) iscsiIoThreadWait(PISCSIIMAGE pImage, RTMSINTERVAL cMillies, uint32_t fEvents, uint32_t *pfEvents)
{
}
/**
* Internal. - Pokes a thread waiting for I/O.
*/
{
}
/**
* Internal. - Get the next request from the queue.
*/
{
int rc;
if (pIScsiCmd)
{
}
return pIScsiCmd;
}
/**
* Internal. - Adds the given command to the queue.
*/
{
return rc;
}
/**
* Internal. - Completes the request with the appropriate action.
* Synchronous requests are completed with waking up the thread
* and asynchronous ones by continuing the associated I/O context.
*/
{
/* Remove from the table first. */
/* Call completion callback. */
/* Free command structure. */
#ifdef DEBUG
#endif
}
/**
* Reattaches the to the target after an error aborting
* pending commands and resending them.
*
* @param pImage iSCSI connection state.
*/
{
int rc = VINF_SUCCESS;
/* Close connection. */
/* Reset PDU we are receiving. */
/*
* Abort all PDUs we are about to transmit,
* the command need a new Itt if the relogin is successful.
*/
while (pImage->pIScsiPDUTxHead)
{
if (pIScsiCmd)
{
/* Place on command list. */
}
}
/* Clear the tail pointer (safety precaution). */
/* Clear the current PDU too. */
if (pImage->pIScsiPDUTxCur)
{
if (pIScsiCmd)
{
}
}
/*
* Get all commands which are waiting for a response
* They need to be resend too after a successful reconnect.
*/
if (pIScsiCmd)
{
while (pIScsiCmdCur->pNext)
/*
* Place them in front of the list because they are the oldest requests
* and need to be processed first to minimize the risk to time out.
*/
}
/* Try to attach. */
if (RT_SUCCESS(rc))
{
/* Phew, we have a connection again.
* Prepare new PDUs for the aborted commands.
*/
while (pIScsiCmdHead)
{
}
}
else
{
/*
* Still no luck, complete commands with error so the caller
* has a chance to inform the user and maybe resend the command.
*/
while (pIScsiCmdHead)
{
}
}
}
/**
* Internal. Main iSCSI I/O worker.
*/
{
/* Initialize the initial event mask. */
{
int rc;
fEvents = 0;
/* Wait for work or for data from the target. */
if (pImage->cCmdsWaiting)
{
}
else
{
}
if (rc == VERR_INTERRUPTED)
{
/* Check the queue. */
while (pIScsiCmd)
{
switch (pIScsiCmd->enmCmdType)
{
case ISCSICMDTYPE_REQ:
{
/* If there is no connection complete the command with an error. */
{
}
else
break;
}
case ISCSICMDTYPE_EXEC:
{
break;
}
default:
}
}
}
{
/*
* We are waiting for a response from the target but
* it didn't answered yet.
* We assume the connection is broken and try to reconnect.
*/
LogFlow(("Timed out while waiting for an answer from the target, reconnecting\n"));
}
{
{
/* Continue or start a new PDU receive task */
LogFlow(("There is data on the socket\n"));
if (rc == VERR_BROKEN_PIPE)
else if (RT_FAILURE(rc))
}
{
LogFlow(("The socket is writable\n"));
if (RT_FAILURE(rc))
{
/*
* Something unexpected happened, log the error and try to reset everything
* by reattaching to the target.
*/
}
}
{
LogFlow(("An error ocurred\n"));
}
}
else
}
return VINF_SUCCESS;
}
/**
* Internal. - Enqueues a request asynchronously.
*/
{
int rc;
{
if (!pIScsiCmd)
return VERR_NO_MEMORY;
/* Init the command structure. */
if (RT_FAILURE(rc))
}
else
return rc;
}
{
}
/**
* Internal. - Enqueues a request in a synchronous fashion
* i.e. returns when the request completed.
*/
{
int rc;
{
/* Create event semaphore. */
if (RT_FAILURE(rc))
return rc;
if (fRetry)
{
for (unsigned i = 0; i < 10; i++)
{
if (RT_FAILURE(rc))
break;
|| RT_FAILURE(rc))
break;
}
}
else
{
if (RT_SUCCESS(rc))
{
}
}
}
else
{
if (fRetry)
{
for (unsigned i = 0; i < 10; i++)
{
|| RT_FAILURE(rc))
break;
}
}
else
{
}
}
return rc;
}
/**
* Internal. - Executes a given function in a synchronous fashion
* on the I/O thread if available.
*/
{
int rc;
{
if (!pIScsiCmd)
return VERR_NO_MEMORY;
/* Create event semaphore. */
if (RT_FAILURE(rc))
{
return rc;
}
/* Init the command structure. */
if (RT_FAILURE(rc))
else
{
}
}
else
{
/* No I/O thread, execute in the current thread. */
}
return rc;
}
{
bool fComplete = true;
size_t cbTransfered = 0;
if ( RT_SUCCESS(rcReq)
{
/* Try again if possible. */
if (pScsiReq->cSenseRetries > 0)
{
if (RT_SUCCESS(rc))
fComplete = false;
else
}
else
}
if (fComplete)
{
else
/* Continue I/O context. */
}
}
/**
* Internal. Free all allocated space for representing an image, and optionally
* delete the image from disk.
*/
{
int rc = VINF_SUCCESS;
/* Freeing a never allocated image (e.g. because the open failed) is
* not signalled as an error. After all nothing bad happens. */
if (pImage)
{
{
/* Detaching only makes sense when the mutex is there. Otherwise the
* failure happened long before we could attach to the target. */
}
{
/* Wait for the thread to terminate. */
}
/* Destroy the socket. */
{
}
{
}
if (pImage->pszTargetName)
{
}
if (pImage->pszInitiatorName)
{
else
}
if (pImage->pszInitiatorUsername)
{
}
if (pImage->pbInitiatorSecret)
{
}
if (pImage->pszTargetUsername)
{
}
if (pImage->pbTargetSecret)
{
}
if (pImage->pvRecvPDUBuf)
{
}
pImage->cbRecvPDUResidual = 0;
}
return rc;
}
/**
* Internal: Open an image, constructing all necessary data structures.
*/
{
int rc;
bool fLunEncoded = false;
uint32_t uWriteSplitDef = 0;
uint32_t uTimeoutDef = 0;
uint64_t uHostIPTmp = 0;
bool fHostIPDef = 0;
fHostIPDef = !!uHostIPTmp;
/* Get error signalling interface. */
/* Get TCP network stack interface. */
{
goto out;
}
/* Get configuration interface. */
{
goto out;
}
/* Get I/O interface. */
{
goto out;
}
/* This ISID will be adjusted later to make it unique on this host. */
{
rc = VERR_NO_MEMORY;
goto out;
}
if (RT_FAILURE(rc))
goto out;
if (RT_FAILURE(rc))
goto out;
/* Validate configuration, detect unknown keys. */
"TargetName\0"
"InitiatorName\0"
"LUN\0"
"TargetAddress\0"
"InitiatorUsername\0"
"InitiatorSecret\0"
"InitiatorSecretEncrypted\0"
"TargetUsername\0"
"TargetSecret\0"
"WriteSplit\0"
"Timeout\0"
"HostIPStack\0"))
{
rc = vdIfError(pImage->pIfError, VERR_VD_ISCSI_UNKNOWN_CFG_VALUES, RT_SRC_POS, N_("iSCSI: configuration error: unknown configuration keys present"));
goto out;
}
/* Query the iSCSI upper level configuration. */
if (RT_FAILURE(rc))
{
rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read TargetName as string"));
goto out;
}
{
pImage->fAutomaticInitiatorName = true;
rc = VINF_SUCCESS;
}
if (RT_FAILURE(rc))
{
rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read InitiatorName as string"));
goto out;
}
if (RT_FAILURE(rc))
{
rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read LUN as string"));
goto out;
}
{
fLunEncoded = true;
pszLUN += 3;
}
if (RT_FAILURE(rc))
{
rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to convert LUN to integer"));
goto out;
}
if (!fLunEncoded)
{
{
}
{
}
else
{
rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: LUN number out of range (0-16383)"));
goto out;
}
}
if (RT_FAILURE(rc))
{
rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read TargetAddress as string"));
goto out;
}
"InitiatorUsername",
rc = VINF_SUCCESS;
if (RT_FAILURE(rc))
{
rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read InitiatorUsername as string"));
goto out;
}
pImage->cbInitiatorSecret = 0;
"InitiatorSecret",
(void **)&pImage->pbInitiatorSecret,
rc = VINF_SUCCESS;
if (RT_FAILURE(rc))
{
rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read InitiatorSecret as byte string"));
goto out;
}
void *pvInitiatorSecretEncrypted;
"InitiatorSecretEncrypted",
if (RT_SUCCESS(rc))
{
if (!pImage->pbInitiatorSecret)
{
/* we have an encrypted initiator secret but not an unencrypted one */
rc = vdIfError(pImage->pIfError, VERR_VD_ISCSI_SECRET_ENCRYPTED, RT_SRC_POS, N_("iSCSI: initiator secret not decrypted"));
goto out;
}
}
"TargetUsername",
rc = VINF_SUCCESS;
if (RT_FAILURE(rc))
{
rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read TargetUsername as string"));
goto out;
}
pImage->cbTargetSecret = 0;
&pImage->cbTargetSecret);
rc = VINF_SUCCESS;
if (RT_FAILURE(rc))
{
rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read TargetSecret as byte string"));
goto out;
}
if (RT_FAILURE(rc))
{
rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read WriteSplit as U32"));
goto out;
}
/* Query the iSCSI lower level configuration. */
if (RT_FAILURE(rc))
{
rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read Timeout as U32"));
goto out;
}
if (RT_FAILURE(rc))
{
rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read HostIPStack as boolean"));
goto out;
}
/* Don't actually establish iSCSI transport connection if this is just an
* open to query the image information and the host IP stack isn't used.
* Even trying is rather useless, as in this context the InTnet IP stack
* isn't present. Returning dummies is the best possible result anyway. */
{
LogFunc(("Not opening the transport connection as IntNet IP stack is not available. Will return dummies\n"));
goto out;
}
pImage->cbRecvPDUResidual = 0;
/* Create the socket structure. */
if (RT_SUCCESS(rc))
{
pImage->fExtendedSelectSupported = true;
if (RT_FAILURE(rc))
{
goto out;
}
}
else if (rc == VERR_NOT_SUPPORTED)
{
/* Async I/O is not supported without extended select. */
if ((uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO))
{
LogFunc(("Extended select is not supported by the interface but async I/O is requested -> %Rrc\n", rc));
goto out;
}
else
{
pImage->fExtendedSelectSupported = false;
if (RT_FAILURE(rc))
{
goto out;
}
}
}
else
{
goto out;
}
/*
* Attach to the iSCSI target. This implicitly establishes the iSCSI
* transport connection.
*/
if (RT_FAILURE(rc))
{
goto out;
}
/*
* Inquire available LUNs - purely dummy request.
*/
if (RT_FAILURE(rc))
{
return rc;
}
/*
* Inquire device characteristics - no tapes, scanners etc., please.
*/
if (RT_SUCCESS(rc))
{
if (devType != SCSI_DEVTYPE_DISK)
{
LogRel(("iSCSI: Unsupported SCSI peripheral device type %d for target %s\n", devType & SCSI_DEVTYPE_MASK, pImage->pszTargetName));
goto out;
}
if (uCmdQueue > 0)
pImage->fCmdQueuingSupported = true;
else if (uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO)
{
goto out;
}
LogRel(("iSCSI: target address %s, target name %s, %s command queuing\n",
}
else
{
goto out;
}
/*
* Query write disable bit in the device specific parameter entry in the
*/
if (RT_SUCCESS(rc))
{
{
goto out;
}
}
else
{
LogRel(("iSCSI: Could not get MODE SENSE info for target %s, rc=%Rrc\n", pImage->pszTargetName, rc));
goto out;
}
/*
* Determine sector size and capacity of the volume immediately.
*/
if (RT_SUCCESS(rc))
{
bool fEnd = false;
do
{
{
case SCSI_STATUS_OK:
{
{
RT_SRC_POS, N_("iSCSI: target address %s, target name %s, SCSI LUN %lld reports media sector count=%llu sector size=%u"),
}
fEnd = true;
break;
}
{
{
{
/** @todo for future: prepare and send command "REQUEST SENSE" which will
return the status of target and will clear any unit attention condition that it reports */
if (RT_FAILURE(rc))
fEnd = true;
cMaxRetries--;
break;
}
}
break;
}
default:
{
if (RT_FAILURE(rc))
fEnd = true;
cMaxRetries--;
break;
}
}
if (!cMaxRetries)
fEnd = true;
} while(!fEnd);
}
else
{
if (RT_SUCCESS(rc))
{
bool fEnd = false;
do
{
{
case SCSI_STATUS_OK:
{
{
RT_SRC_POS, N_("iSCSI: fallback capacity detectio for target address %s, target name %s, SCSI LUN %lld reports media sector count=%llu sector size=%u"),
}
fEnd = true;
break;
}
{
{
{
/** @todo for future: prepare and send command "REQUEST SENSE" which will
return the status of target and will clear any unit attention condition that it reports */
if (RT_FAILURE(rc))
fEnd = true;
cMaxRetries--;
break;
}
}
break;
}
default:
{
if (RT_FAILURE(rc))
fEnd = true;
cMaxRetries--;
break;
}
}
if (!cMaxRetries)
fEnd = true;
} while(!fEnd);
}
else
{
goto out;
}
}
/*
* Check the read and write cache bits.
* Try to enable the cache if it is disabled.
*
* We already checked that this is a block access device. No need
* to do it again.
*/
if ( RT_SUCCESS(rc)
&& (aCachingModePage[0] >= 15)
{
/*
* The write cache is disabled if bit 2 (WCE) is zero and
* the read cache is disabled if bit 0 (RCD) is set.
*/
{
/*
* Write Cache Enable (WCE) bit is zero or the Read Cache Disable (RCD) is one
* So one of the caches is disabled. Enable both caches.
* The rest is unchanged.
*/
if ( RT_SUCCESS(rc)
{
}
else
{
/* Log failures but continue. */
LogRel(("iSCSI: Could not enable read and write cache of target %s, rc=%Rrc status=%#x\n",
rc = VINF_SUCCESS;
}
}
}
else
{
/* Log errors but continue. */
LogRel(("iSCSI: Could not check write cache of target %s, rc=%Rrc, got mode page %#x\n", pImage->pszTargetName, rc,aCachingModePage[0] & 0x3f));
rc = VINF_SUCCESS;
}
out:
if (RT_FAILURE(rc))
iscsiFreeImage(pImage, false);
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnCheckIfValid */
{
/* iSCSI images can't be checked for validity this way, as the filename
* just can't supply enough configuration information. */
int rc = VERR_VD_ISCSI_INVALID_HEADER;
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnOpen */
{
LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p ppBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, ppBackendData));
int rc;
/* Check open flags. All valid flags are supported. */
if (uOpenFlags & ~VD_OPEN_FLAGS_MASK)
{
goto out;
}
/* Check remaining arguments. */
if ( !VALID_PTR(pszFilename)
|| !*pszFilename
{
goto out;
}
if (!pImage)
{
rc = VERR_NO_MEMORY;
goto out;
}
pImage->cLogRelErrors = 0;
if (RT_SUCCESS(rc))
{
LogFlowFunc(("target %s cVolume %d, cbSector %d\n", pImage->pszTargetName, pImage->cVolume, pImage->cbSector));
LogRel(("iSCSI: target address %s, target name %s, SCSI LUN %lld\n", pImage->pszTargetAddress, pImage->pszTargetName, pImage->LUN));
*ppBackendData = pImage;
}
else
out:
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnCreate */
unsigned uImageFlags, const char *pszComment,
unsigned uPercentStart, unsigned uPercentSpan,
{
LogFlowFunc(("pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" pPCHSGeometry=%#p pLCHSGeometry=%#p Uuid=%RTuuid uOpenFlags=%#x uPercentStart=%u uPercentSpan=%u pVDIfsDisk=%#p pVDIfsImage=%#p pVDIfsOperation=%#p ppBackendData=%#p", pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, ppBackendData));
int rc = VERR_NOT_SUPPORTED;
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnClose */
{
int rc;
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnRead */
{
int rc = VINF_SUCCESS;
LogFlowFunc(("pBackendData=%p uOffset=%#llx pIoCtx=%#p cbToRead=%u pcbActuallyRead=%p\n",
|| cbToRead == 0)
return VERR_INVALID_PARAMETER;
/*
* Clip read size to a value which is supported by the target.
*/
unsigned cT2ISegs = 0;
/* Get the number of segments. */
{
{
cbCDB = 10;
pbCDB[0] = SCSI_READ_10;
}
else
{
cbCDB = 16;
pbCDB[0] = SCSI_READ_16;
}
{
if (RT_FAILURE(rc))
{
*pcbActuallyRead = 0;
}
else
}
else
{
if (RT_FAILURE(rc))
else
{
return VERR_VD_IOCTX_HALT; /* Halt the I/O context until further notification from the I/O thread. */
}
}
}
else
rc = VERR_NO_MEMORY;
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnWrite */
{
LogFlowFunc(("pBackendData=%p uOffset=%llu pIoCtx=%#p cbToWrite=%u pcbWriteProcess=%p pcbPreRead=%p pcbPostRead=%p fWrite=%u\n",
int rc = VINF_SUCCESS;
return VERR_INVALID_PARAMETER;
/*
* Clip read size to a value which is supported by the target.
*/
unsigned cI2TSegs = 0;
/* Get the number of segments. */
{
{
cbCDB = 10;
pbCDB[0] = SCSI_WRITE_10;
}
else
{
cbCDB = 16;
pbCDB[0] = SCSI_WRITE_16;
}
{
if (RT_FAILURE(rc))
{
*pcbWriteProcess = 0;
}
else
}
else
{
if (RT_FAILURE(rc))
else
{
return VERR_VD_IOCTX_HALT; /* Halt the I/O context until further notification from the I/O thread. */
}
}
}
else
rc = VERR_NO_MEMORY;
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnFlush */
{
int rc = VINF_SUCCESS;
{
pbCDB[0] = SCSI_SYNCHRONIZE_CACHE;
pReq->cSenseRetries = 0;
{
if (RT_FAILURE(rc))
}
else
{
if (RT_FAILURE(rc))
else
return VERR_VD_IOCTX_HALT; /* Halt the I/O context until further notification from the I/O thread. */
}
}
else
rc = VERR_NO_MEMORY;
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetVersion */
static unsigned iscsiGetVersion(void *pBackendData)
{
return 0;
}
/** @copydoc VBOXHDDBACKEND::pfnGetSize */
{
if (pImage)
else
return 0;
}
/** @copydoc VBOXHDDBACKEND::pfnGetFileSize */
{
if (pImage)
else
return 0;
}
/** @copydoc VBOXHDDBACKEND::pfnGetPCHSGeometry */
{
int rc;
if (pImage)
else
LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetPCHSGeometry */
{
LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
int rc;
if (pImage)
{
{
goto out;
}
}
else
out:
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetLCHSGeometry */
{
int rc;
if (pImage)
else
LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetLCHSGeometry */
{
LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
int rc;
if (pImage)
{
{
goto out;
}
}
else
out:
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetImageFlags */
static unsigned iscsiGetImageFlags(void *pBackendData)
{
unsigned uImageFlags;
return uImageFlags;
}
/** @copydoc VBOXHDDBACKEND::pfnGetOpenFlags */
static unsigned iscsiGetOpenFlags(void *pBackendData)
{
unsigned uOpenFlags;
if (pImage)
else
uOpenFlags = 0;
return uOpenFlags;
}
/** @copydoc VBOXHDDBACKEND::pfnSetOpenFlags */
{
int rc;
/* Image must be opened and the new flags must be valid. */
{
goto out;
}
/* Implement this operation via reopening the image if we actually need
* reopen. In the other direction we don't have the necessary information
* as the "disk is readonly" flag is thrown away. Can be optimized too,
* but it's not worth the effort at the moment. */
if ( !(uOpenFlags & VD_OPEN_FLAGS_READONLY)
{
iscsiFreeImage(pImage, false);
}
else
{
rc = VINF_SUCCESS;
}
out:
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetComment */
{
LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
int rc;
if (pImage)
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetComment */
{
int rc;
if (pImage)
{
else
}
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetUuid */
{
int rc;
if (pImage)
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetUuid */
{
int rc;
if (pImage)
{
else
}
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetModificationUuid */
{
int rc;
if (pImage)
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetModificationUuid */
{
int rc;
if (pImage)
{
else
}
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetParentUuid */
{
int rc;
if (pImage)
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetParentUuid */
{
int rc;
if (pImage)
{
else
}
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetParentModificationUuid */
{
int rc;
if (pImage)
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetParentModificationUuid */
{
int rc;
if (pImage)
{
else
}
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnDump */
static void iscsiDump(void *pBackendData)
{
if (pImage)
{
/** @todo put something useful here */
}
}
/** @copydoc VBOXHDDBACKEND::pfnComposeLocation */
{
char *pszAddress = NULL;
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
rc = VERR_NO_MEMORY;
}
}
}
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnComposeName */
{
char *pszAddress = NULL;
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
/** @todo think about a nicer looking location scheme for iSCSI */
rc = VERR_NO_MEMORY;
}
}
}
return rc;
}
{
/* pszBackendName */
"iSCSI",
/* cbSize */
sizeof(VBOXHDDBACKEND),
/* uBackendCaps */
/* papszFileExtensions */
NULL,
/* paConfigInfo */
/* hPlugin */
/* pfnCheckIfValid */
/* pfnOpen */
/* pfnCreate */
/* pfnRename */
NULL,
/* pfnClose */
/* pfnRead */
/* pfnWrite */
/* pfnFlush */
/* pfnDiscard */
NULL,
/* pfnGetVersion */
/* pfnGetSize */
/* pfnGetFileSize */
/* pfnGetPCHSGeometry */
/* pfnSetPCHSGeometry */
/* pfnGetLCHSGeometry */
/* pfnSetLCHSGeometry */
/* pfnGetImageFlags */
/* pfnGetOpenFlags */
/* pfnSetOpenFlags */
/* pfnGetComment */
/* pfnSetComment */
/* pfnGetUuid */
/* pfnSetUuid */
/* pfnGetModificationUuid */
/* pfnSetModificationUuid */
/* pfnGetParentUuid */
/* pfnSetParentUuid */
/* pfnGetParentModificationUuid */
/* pfnSetParentModificationUuid */
/* pfnDump */
/* pfnGetTimeStamp */
NULL,
/* pfnGetParentTimeStamp */
NULL,
/* pfnSetParentTimeStamp */
NULL,
/* pfnGetParentFilename */
NULL,
/* pfnSetParentFilename */
NULL,
/* pfnComposeLocation */
/* pfnComposeName */
/* pfnCompact */
NULL,
/* pfnResize */
NULL,
/* pfnRepair */
};