VirtualBox

source: vbox/trunk/src/VBox/Storage/ISCSI.cpp@ 64226

Last change on this file since 64226 was 63905, checked in by vboxsync, 8 years ago

Storage/VD: Add proper versioning of the backend structures instead of just relying on the structure size to make changing callback signatures possible in the future and still being able to reject incompatible plugins

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 200.0 KB
Line 
1/* $Id: ISCSI.cpp 63905 2016-09-20 08:31:05Z vboxsync $ */
2/** @file
3 * iSCSI initiator driver, VD backend.
4 */
5
6/*
7 * Copyright (C) 2006-2016 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_VD_ISCSI
23#include <VBox/vd-plugin.h>
24#include <VBox/err.h>
25
26#include <VBox/log.h>
27#include <iprt/alloc.h>
28#include <iprt/assert.h>
29#include <iprt/uuid.h>
30#include <iprt/string.h>
31#include <iprt/asm.h>
32#include <iprt/thread.h>
33#include <iprt/semaphore.h>
34#include <iprt/md5.h>
35#include <iprt/tcp.h>
36#include <iprt/time.h>
37#include <VBox/scsi.h>
38
39#include "VDBackends.h"
40
41
42/*********************************************************************************************************************************
43* Defined Constants And Macros *
44*********************************************************************************************************************************/
45
46/** The maximum number of release log entries per image. */
47#define MAX_LOG_REL_ERRORS 1024
48
49/** Default port number to use for iSCSI. */
50#define ISCSI_DEFAULT_PORT 3260
51
52
53/** Converts a number in the range of 0 - 15 into the corresponding hex char. */
54#define NUM_2_HEX(b) ('0' + (b) + (((b) > 9) ? 39 : 0))
55/** Converts a hex char into the corresponding number in the range 0-15. */
56#define HEX_2_NUM(c) (((c) <= '9') ? ((c) - '0') : (((c - 'A' + 10) & 0xf)))
57/* Converts a base64 char into the corresponding number in the range 0-63. */
58#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)
59
60
61/** Minimum CHAP_MD5 challenge length in bytes. */
62#define CHAP_MD5_CHALLENGE_MIN 16
63/** Maximum CHAP_MD5 challenge length in bytes. */
64#define CHAP_MD5_CHALLENGE_MAX 24
65
66
67/**
68 * SCSI peripheral device type. */
69typedef enum SCSIDEVTYPE
70{
71 /** direct-access device. */
72 SCSI_DEVTYPE_DISK = 0,
73 /** sequential-access device. */
74 SCSI_DEVTYPE_TAPE,
75 /** printer device. */
76 SCSI_DEVTYPE_PRINTER,
77 /** processor device. */
78 SCSI_DEVTYPE_PROCESSOR,
79 /** write-once device. */
80 SCSI_DEVTYPE_WORM,
81 /** CD/DVD device. */
82 SCSI_DEVTYPE_CDROM,
83 /** scanner device. */
84 SCSI_DEVTYPE_SCANNER,
85 /** optical memory device. */
86 SCSI_DEVTYPE_OPTICAL,
87 /** medium changer. */
88 SCSI_DEVTYPE_CHANGER,
89 /** communications device. */
90 SCSI_DEVTYPE_COMMUNICATION,
91 /** storage array controller device. */
92 SCSI_DEVTYPE_RAIDCTL = 0x0c,
93 /** enclosure services device. */
94 SCSI_DEVTYPE_ENCLOSURE,
95 /** simplified direct-access device. */
96 SCSI_DEVTYPE_SIMPLEDISK,
97 /** optical card reader/writer device. */
98 SCSI_DEVTYPE_OCRW,
99 /** bridge controller device. */
100 SCSI_DEVTYPE_BRIDGE,
101 /** object-based storage device. */
102 SCSI_DEVTYPE_OSD
103} SCSIDEVTYPE;
104
105/** Mask for extracting the SCSI device type out of the first byte of the INQUIRY response. */
106#define SCSI_DEVTYPE_MASK 0x1f
107
108/** Mask to extract the CmdQue bit out of the seventh byte of the INQUIRY response. */
109#define SCSI_INQUIRY_CMDQUE_MASK 0x02
110
111/** Maximum PDU payload size we can handle in one piece. Greater or equal than
112 * s_iscsiConfigDefaultWriteSplit. */
113#define ISCSI_DATA_LENGTH_MAX _256K
114
115/** Maximum PDU size we can handle in one piece. */
116#define ISCSI_RECV_PDU_BUFFER_SIZE (ISCSI_DATA_LENGTH_MAX + ISCSI_BHS_SIZE)
117
118
119/** Version of the iSCSI standard which this initiator driver can handle. */
120#define ISCSI_MY_VERSION 0
121
122
123/** Length of ISCSI basic header segment. */
124#define ISCSI_BHS_SIZE 48
125
126
127/** Reserved task tag value. */
128#define ISCSI_TASK_TAG_RSVD 0xffffffff
129
130
131/**
132 * iSCSI opcodes. */
133typedef enum ISCSIOPCODE
134{
135 /** NOP-Out. */
136 ISCSIOP_NOP_OUT = 0x00000000,
137 /** SCSI command. */
138 ISCSIOP_SCSI_CMD = 0x01000000,
139 /** SCSI task management request. */
140 ISCSIOP_SCSI_TASKMGMT_REQ = 0x02000000,
141 /** Login request. */
142 ISCSIOP_LOGIN_REQ = 0x03000000,
143 /** Text request. */
144 ISCSIOP_TEXT_REQ = 0x04000000,
145 /** SCSI Data-Out. */
146 ISCSIOP_SCSI_DATA_OUT = 0x05000000,
147 /** Logout request. */
148 ISCSIOP_LOGOUT_REQ = 0x06000000,
149 /** SNACK request. */
150 ISCSIOP_SNACK_REQ = 0x10000000,
151
152 /** NOP-In. */
153 ISCSIOP_NOP_IN = 0x20000000,
154 /** SCSI response. */
155 ISCSIOP_SCSI_RES = 0x21000000,
156 /** SCSI Task Management response. */
157 ISCSIOP_SCSI_TASKMGMT_RES = 0x22000000,
158 /** Login response. */
159 ISCSIOP_LOGIN_RES = 0x23000000,
160 /** Text response. */
161 ISCSIOP_TEXT_RES = 0x24000000,
162 /** SCSI Data-In. */
163 ISCSIOP_SCSI_DATA_IN = 0x25000000,
164 /** Logout response. */
165 ISCSIOP_LOGOUT_RES = 0x26000000,
166 /** Ready To Transfer (R2T). */
167 ISCSIOP_R2T = 0x31000000,
168 /** Asynchronous message. */
169 ISCSIOP_ASYN_MSG = 0x32000000,
170 /** Reject. */
171 ISCSIOP_REJECT = 0x3f000000
172} ISCSIOPCODE;
173
174/** Mask for extracting the iSCSI opcode out of the first header word. */
175#define ISCSIOP_MASK 0x3f000000
176
177
178/** ISCSI BHS word 0: Request should be processed immediately. */
179#define ISCSI_IMMEDIATE_DELIVERY_BIT 0x40000000
180
181/** ISCSI BHS word 0: This is the final PDU for this request/response. */
182#define ISCSI_FINAL_BIT 0x00800000
183/** ISCSI BHS word 0: Mask for extracting the CSG. */
184#define ISCSI_CSG_MASK 0x000c0000
185/** ISCSI BHS word 0: Shift offset for extracting the CSG. */
186#define ISCSI_CSG_SHIFT 18
187/** ISCSI BHS word 0: Mask for extracting the NSG. */
188#define ISCSI_NSG_MASK 0x00030000
189/** ISCSI BHS word 0: Shift offset for extracting the NSG. */
190#define ISCSI_NSG_SHIFT 16
191
192/** ISCSI BHS word 0: task attribute untagged */
193#define ISCSI_TASK_ATTR_UNTAGGED 0x00000000
194/** ISCSI BHS word 0: task attribute simple */
195#define ISCSI_TASK_ATTR_SIMPLE 0x00010000
196/** ISCSI BHS word 0: task attribute ordered */
197#define ISCSI_TASK_ATTR_ORDERED 0x00020000
198/** ISCSI BHS word 0: task attribute head of queue */
199#define ISCSI_TASK_ATTR_HOQ 0x00030000
200/** ISCSI BHS word 0: task attribute ACA */
201#define ISCSI_TASK_ATTR_ACA 0x00040000
202
203/** ISCSI BHS word 0: transit to next login phase. */
204#define ISCSI_TRANSIT_BIT 0x00800000
205/** ISCSI BHS word 0: continue with login negotiation. */
206#define ISCSI_CONTINUE_BIT 0x00400000
207
208/** ISCSI BHS word 0: residual underflow. */
209#define ISCSI_RESIDUAL_UNFL_BIT 0x00020000
210/** ISCSI BHS word 0: residual overflow. */
211#define ISCSI_RESIDUAL_OVFL_BIT 0x00040000
212/** ISCSI BHS word 0: Bidirectional read residual underflow. */
213#define ISCSI_BI_READ_RESIDUAL_UNFL_BIT 0x00080000
214/** ISCSI BHS word 0: Bidirectional read residual overflow. */
215#define ISCSI_BI_READ_RESIDUAL_OVFL_BIT 0x00100000
216
217/** ISCSI BHS word 0: SCSI response mask. */
218#define ISCSI_SCSI_RESPONSE_MASK 0x0000ff00
219/** ISCSI BHS word 0: SCSI status mask. */
220#define ISCSI_SCSI_STATUS_MASK 0x000000ff
221
222/** ISCSI BHS word 0: response includes status. */
223#define ISCSI_STATUS_BIT 0x00010000
224
225/** Maximum number of scatter/gather segments needed to send a PDU. */
226#define ISCSI_SG_SEGMENTS_MAX 4
227
228/** Number of entries in the command table. */
229#define ISCSI_CMD_WAITING_ENTRIES 32
230
231/**
232 * iSCSI login status class. */
233typedef enum ISCSILOGINSTATUSCLASS
234{
235 /** Success. */
236 ISCSI_LOGIN_STATUS_CLASS_SUCCESS = 0,
237 /** Redirection. */
238 ISCSI_LOGIN_STATUS_CLASS_REDIRECTION,
239 /** Initiator error. */
240 ISCSI_LOGIN_STATUS_CLASS_INITIATOR_ERROR,
241 /** Target error. */
242 ISCSI_LOGIN_STATUS_CLASS_TARGET_ERROR
243} ISCSILOGINSTATUSCLASS;
244
245
246/**
247 * iSCSI connection state. */
248typedef enum ISCSISTATE
249{
250 /** Not having a connection/session at all. */
251 ISCSISTATE_FREE,
252 /** Currently trying to login. */
253 ISCSISTATE_IN_LOGIN,
254 /** Normal operation, corresponds roughly to the Full Feature Phase. */
255 ISCSISTATE_NORMAL,
256 /** Currently trying to logout. */
257 ISCSISTATE_IN_LOGOUT
258} ISCSISTATE;
259
260/**
261 * iSCSI PDU send/receive flags (and maybe more in the future). */
262typedef enum ISCSIPDUFLAGS
263{
264 /** No special flags */
265 ISCSIPDU_DEFAULT = 0,
266 /** Do not attempt to re-attach to the target if the connection is lost */
267 ISCSIPDU_NO_REATTACH = RT_BIT(1)
268} ISCSIPDUFLAGS;
269
270
271/*********************************************************************************************************************************
272* Structures and Typedefs *
273*********************************************************************************************************************************/
274
275/**
276 * iSCSI login negotiation parameter
277 */
278typedef struct ISCSIPARAMETER
279{
280 /** Name of the parameter. */
281 const char *pszParamName;
282 /** Value of the parameter. */
283 const char *pszParamValue;
284 /** Length of the binary parameter. 0=zero-terminated string. */
285 size_t cbParamValue;
286} ISCSIPARAMETER;
287
288
289/**
290 * iSCSI Response PDU buffer (scatter).
291 */
292typedef struct ISCSIRES
293{
294 /** Length of PDU segment. */
295 size_t cbSeg;
296 /** Pointer to PDU segment. */
297 void *pvSeg;
298} ISCSIRES;
299/** Pointer to an iSCSI Response PDU buffer. */
300typedef ISCSIRES *PISCSIRES;
301/** Pointer to a const iSCSI Response PDU buffer. */
302typedef ISCSIRES const *PCISCSIRES;
303
304
305/**
306 * iSCSI Request PDU buffer (gather).
307 */
308typedef struct ISCSIREQ
309{
310 /** Length of PDU segment in bytes. */
311 size_t cbSeg;
312 /** Pointer to PDU segment. */
313 const void *pcvSeg;
314} ISCSIREQ;
315/** Pointer to an iSCSI Request PDU buffer. */
316typedef ISCSIREQ *PISCSIREQ;
317/** Pointer to a const iSCSI Request PDU buffer. */
318typedef ISCSIREQ const *PCISCSIREQ;
319
320
321/**
322 * SCSI transfer directions.
323 */
324typedef enum SCSIXFER
325{
326 SCSIXFER_NONE = 0,
327 SCSIXFER_TO_TARGET,
328 SCSIXFER_FROM_TARGET,
329 SCSIXFER_TO_FROM_TARGET
330} SCSIXFER, *PSCSIXFER;
331
332/** Forward declaration. */
333typedef struct ISCSIIMAGE *PISCSIIMAGE;
334
335/**
336 * SCSI request structure.
337 */
338typedef struct SCSIREQ
339{
340 /** I/O context associated with this request. */
341 PVDIOCTX pIoCtx;
342 /** Transfer direction. */
343 SCSIXFER enmXfer;
344 /** Length of command block. */
345 size_t cbCDB;
346 /** Length of Initiator2Target data buffer. */
347 size_t cbI2TData;
348 /** Length of Target2Initiator data buffer. */
349 size_t cbT2IData;
350 /** Length of sense buffer
351 * This contains the number of sense bytes received upon completion. */
352 size_t cbSense;
353 /** Completion status of the command. */
354 uint8_t status;
355 /** The CDB. */
356 uint8_t abCDB[16];
357 /** The sense buffer. */
358 uint8_t abSense[96];
359 /** Status code to return if we got sense data. */
360 int rcSense;
361 /** Pointer to the Initiator2Target S/G list. */
362 PRTSGSEG paI2TSegs;
363 /** Number of entries in the I2T S/G list. */
364 unsigned cI2TSegs;
365 /** Pointer to the Target2Initiator S/G list. */
366 PRTSGSEG paT2ISegs;
367 /** Number of entries in the T2I S/G list. */
368 unsigned cT2ISegs;
369 /** S/G buffer for the target to initiator bits. */
370 RTSGBUF SgBufT2I;
371 /** Number of retries if the command completes with sense
372 * data before we return with an error.
373 */
374 unsigned cSenseRetries;
375 /** The S/G list - variable in size.
376 * This array holds both the I2T and T2I segments.
377 * The I2T segments are first and the T2I are second.
378 */
379 RTSGSEG aSegs[1];
380} SCSIREQ, *PSCSIREQ;
381
382typedef enum ISCSICMDTYPE
383{
384 /** Process a SCSI request. */
385 ISCSICMDTYPE_REQ = 0,
386 /** Call a function in the I/O thread. */
387 ISCSICMDTYPE_EXEC,
388 /** Usual 32bit hack. */
389 ISCSICMDTYPE_32BIT_HACK = 0x7fffffff
390} ISCSICMDTYPE;
391
392
393/** The command completion function. */
394typedef DECLCALLBACK(void) FNISCSICMDCOMPLETED(PISCSIIMAGE pImage, int rcReq, void *pvUser);
395/** Pointer to a command completion function. */
396typedef FNISCSICMDCOMPLETED *PFNISCSICMDCOMPLETED;
397
398/** The command execution function. */
399typedef DECLCALLBACK(int) FNISCSIEXEC(void *pvUser);
400/** Pointer to a command execution function. */
401typedef FNISCSIEXEC *PFNISCSIEXEC;
402
403/**
404 * Structure used to complete a synchronous request.
405 */
406typedef struct ISCSICMDSYNC
407{
408 /** Event semaphore to wakeup the waiting thread. */
409 RTSEMEVENT EventSem;
410 /** Status code of the command. */
411 int rcCmd;
412} ISCSICMDSYNC, *PISCSICMDSYNC;
413
414/**
415 * iSCSI command.
416 * Used to forward requests to the I/O thread
417 * if existing.
418 */
419typedef struct ISCSICMD
420{
421 /** Next one in the list. */
422 struct ISCSICMD *pNext;
423 /** Assigned ITT. */
424 uint32_t Itt;
425 /** Completion callback. */
426 PFNISCSICMDCOMPLETED pfnComplete;
427 /** Opaque user data. */
428 void *pvUser;
429 /** Command to execute. */
430 ISCSICMDTYPE enmCmdType;
431 /** Command type dependent data. */
432 union
433 {
434 /** Process a SCSI request. */
435 struct
436 {
437 /** The SCSI request to process. */
438 PSCSIREQ pScsiReq;
439 } ScsiReq;
440 /** Call a function in the I/O thread. */
441 struct
442 {
443 /** The method to execute. */
444 PFNISCSIEXEC pfnExec;
445 /** User data. */
446 void *pvUser;
447 } Exec;
448 } CmdType;
449} ISCSICMD, *PISCSICMD;
450
451/**
452 * Send iSCSI PDU.
453 * Contains all necessary data to send a PDU.
454 */
455typedef struct ISCSIPDUTX
456{
457 /** Pointer to the next PDu to send. */
458 struct ISCSIPDUTX *pNext;
459 /** The BHS. */
460 uint32_t aBHS[12];
461 /** Assigned CmdSN for this PDU. */
462 uint32_t CmdSN;
463 /** The S/G buffer used for sending. */
464 RTSGBUF SgBuf;
465 /** Number of bytes to send until the PDU completed. */
466 size_t cbSgLeft;
467 /** The iSCSI command this PDU belongs to. */
468 PISCSICMD pIScsiCmd;
469 /** Number of segments in the request segments array. */
470 unsigned cISCSIReq;
471 /** The request segments - variable in size. */
472 RTSGSEG aISCSIReq[1];
473} ISCSIPDUTX, *PISCSIPDUTX;
474
475/**
476 * Block driver instance data.
477 */
478typedef struct ISCSIIMAGE
479{
480 /** Pointer to the filename (location). Not really used. */
481 const char *pszFilename;
482 /** Pointer to the initiator name. */
483 char *pszInitiatorName;
484 /** Pointer to the target name. */
485 char *pszTargetName;
486 /** Pointer to the target address. */
487 char *pszTargetAddress;
488 /** Pointer to the user name for authenticating the Initiator. */
489 char *pszInitiatorUsername;
490 /** Pointer to the secret for authenticating the Initiator. */
491 uint8_t *pbInitiatorSecret;
492 /** Length of the secret for authenticating the Initiator. */
493 size_t cbInitiatorSecret;
494 /** Pointer to the user name for authenticating the Target. */
495 char *pszTargetUsername;
496 /** Pointer to the secret for authenticating the Initiator. */
497 uint8_t *pbTargetSecret;
498 /** Length of the secret for authenticating the Initiator. */
499 size_t cbTargetSecret;
500 /** Limit for iSCSI writes, essentially limiting the amount of data
501 * written in a single write. This is negotiated with the target, so
502 * the actual size might be smaller. */
503 uint32_t cbWriteSplit;
504 /** Initiator session identifier. */
505 uint64_t ISID;
506 /** SCSI Logical Unit Number. */
507 uint64_t LUN;
508 /** Pointer to the per-disk VD interface list. */
509 PVDINTERFACE pVDIfsDisk;
510 /** Pointer to the per-image VD interface list. */
511 PVDINTERFACE pVDIfsImage;
512 /** Error interface. */
513 PVDINTERFACEERROR pIfError;
514 /** Config interface. */
515 PVDINTERFACECONFIG pIfConfig;
516 /** I/O interface. */
517 PVDINTERFACEIOINT pIfIo;
518 /** TCP network stack interface. */
519 PVDINTERFACETCPNET pIfNet;
520 /** Image open flags. */
521 unsigned uOpenFlags;
522 /** Number of re-login retries when a connection fails. */
523 uint32_t cISCSIRetries;
524 /** Sector size on volume. */
525 uint32_t cbSector;
526 /** Size of volume in sectors. */
527 uint64_t cVolume;
528 /** Total volume size in bytes. Easier than multiplying the above values all the time. */
529 uint64_t cbSize;
530
531 /** Negotiated maximum data length when sending to target. */
532 uint32_t cbSendDataLength;
533 /** Negotiated maximum data length when receiving from target. */
534 uint32_t cbRecvDataLength;
535
536 /** Current state of the connection/session. */
537 ISCSISTATE state;
538 /** Flag whether the first Login Response PDU has been seen. */
539 bool FirstRecvPDU;
540 /** Initiator Task Tag of the last iSCSI request PDU. */
541 uint32_t ITT;
542 /** Sequence number of the last command. */
543 uint32_t CmdSN;
544 /** Sequence number of the next command expected by the target. */
545 uint32_t ExpCmdSN;
546 /** Maximum sequence number accepted by the target (determines size of window). */
547 uint32_t MaxCmdSN;
548 /** Expected sequence number of next status. */
549 uint32_t ExpStatSN;
550 /** Currently active request. */
551 PISCSIREQ paCurrReq;
552 /** Segment number of currently active request. */
553 uint32_t cnCurrReq;
554 /** Pointer to receive PDU buffer. (Freed by RT) */
555 void *pvRecvPDUBuf;
556 /** Length of receive PDU buffer. */
557 size_t cbRecvPDUBuf;
558 /** Mutex protecting against concurrent use from several threads. */
559 RTSEMMUTEX Mutex;
560
561 /** Pointer to the target hostname. */
562 char *pszHostname;
563 /** Port to use on the target host. */
564 uint32_t uPort;
565 /** Socket handle of the TCP connection. */
566 VDSOCKET Socket;
567 /** Timeout for read operations on the TCP connection (in milliseconds). */
568 uint32_t uReadTimeout;
569 /** Flag whether to automatically generate the initiator name. */
570 bool fAutomaticInitiatorName;
571 /** Flag whether to use the host IP stack or DevINIP. */
572 bool fHostIP;
573 /** Flag whether to dump malformed packets in the release log. */
574 bool fDumpMalformedPackets;
575 /** Flag whtether the target is readonly. */
576 bool fTargetReadOnly;
577 /** Flag whether to retry the connection before processing new requests. */
578 bool fTryReconnect;
579
580 /** Head of request queue */
581 PISCSICMD pScsiReqQueue;
582 /** Mutex protecting the request queue from concurrent access. */
583 RTSEMMUTEX MutexReqQueue;
584 /** I/O thread. */
585 RTTHREAD hThreadIo;
586 /** Flag whether the thread should be still running. */
587 volatile bool fRunning;
588 /* Flag whether the target supports command queuing. */
589 bool fCmdQueuingSupported;
590 /** Flag whether extended select is supported. */
591 bool fExtendedSelectSupported;
592 /** Padding used for aligning the PDUs. */
593 uint8_t aPadding[4];
594 /** Socket events to poll for. */
595 uint32_t fPollEvents;
596 /** Number of bytes to read to complete the current PDU. */
597 size_t cbRecvPDUResidual;
598 /** Current position in the PDU buffer. */
599 uint8_t *pbRecvPDUBufCur;
600 /** Flag whether we are currently reading the BHS. */
601 bool fRecvPDUBHS;
602 /** List of PDUs waiting to get transmitted. */
603 PISCSIPDUTX pIScsiPDUTxHead;
604 /** Tail of PDUs waiting to get transmitted. */
605 PISCSIPDUTX pIScsiPDUTxTail;
606 /** PDU we are currently transmitting. */
607 PISCSIPDUTX pIScsiPDUTxCur;
608 /** Number of commands waiting for an answer from the target.
609 * Used for timeout handling for poll.
610 */
611 unsigned cCmdsWaiting;
612 /** Table of commands waiting for a response from the target. */
613 PISCSICMD aCmdsWaiting[ISCSI_CMD_WAITING_ENTRIES];
614 /** Number of logins since last successful I/O.
615 * Used to catch the case where logging succeeds but
616 * processing read/write/flushes cause a disconnect.
617 */
618 volatile uint32_t cLoginsSinceIo;
619
620 /** Release log counter. */
621 unsigned cLogRelErrors;
622} ISCSIIMAGE;
623
624
625/*********************************************************************************************************************************
626* Static Variables *
627*********************************************************************************************************************************/
628
629/** Default initiator basename. */
630static const char *s_iscsiDefaultInitiatorBasename = "iqn.2009-08.com.sun.virtualbox.initiator";
631
632/** Default LUN. */
633static const char *s_iscsiConfigDefaultLUN = "0";
634
635/** Default timeout, 10 seconds. */
636static const char *s_iscsiConfigDefaultTimeout = "10000";
637
638/** Default write split value, less or equal to ISCSI_DATA_LENGTH_MAX. */
639static const char *s_iscsiConfigDefaultWriteSplit = "262144";
640
641/** Default host IP stack. */
642static const char *s_iscsiConfigDefaultHostIPStack = "1";
643
644/** Default dump malformed packet configuration value. */
645static const char *s_iscsiConfigDefaultDumpMalformedPackets = "0";
646
647/** Description of all accepted config parameters. */
648static const VDCONFIGINFO s_iscsiConfigInfo[] =
649{
650 { "TargetName", NULL, VDCFGVALUETYPE_STRING, VD_CFGKEY_MANDATORY },
651 /* LUN is defined of string type to handle the "enc" prefix. */
652 { "LUN", s_iscsiConfigDefaultLUN, VDCFGVALUETYPE_STRING, VD_CFGKEY_MANDATORY },
653 { "TargetAddress", NULL, VDCFGVALUETYPE_STRING, VD_CFGKEY_MANDATORY },
654 { "InitiatorName", NULL, VDCFGVALUETYPE_STRING, 0 },
655 { "InitiatorUsername", NULL, VDCFGVALUETYPE_STRING, 0 },
656 { "InitiatorSecret", NULL, VDCFGVALUETYPE_BYTES, 0 },
657 { "TargetUsername", NULL, VDCFGVALUETYPE_STRING, VD_CFGKEY_EXPERT },
658 { "TargetSecret", NULL, VDCFGVALUETYPE_BYTES, VD_CFGKEY_EXPERT },
659 { "WriteSplit", s_iscsiConfigDefaultWriteSplit, VDCFGVALUETYPE_INTEGER, VD_CFGKEY_EXPERT },
660 { "Timeout", s_iscsiConfigDefaultTimeout, VDCFGVALUETYPE_INTEGER, VD_CFGKEY_EXPERT },
661 { "HostIPStack", s_iscsiConfigDefaultHostIPStack, VDCFGVALUETYPE_INTEGER, VD_CFGKEY_EXPERT },
662 { "DumpMalformedPackets", s_iscsiConfigDefaultDumpMalformedPackets, VDCFGVALUETYPE_INTEGER, VD_CFGKEY_EXPERT },
663 { NULL, NULL, VDCFGVALUETYPE_INTEGER, 0 }
664};
665
666
667/*********************************************************************************************************************************
668* Internal Functions *
669*********************************************************************************************************************************/
670
671/* iSCSI low-level functions (only to be used from the iSCSI high-level functions). */
672static uint32_t iscsiNewITT(PISCSIIMAGE pImage);
673static int iscsiSendPDU(PISCSIIMAGE pImage, PISCSIREQ paReq, uint32_t cnReq, uint32_t uFlags);
674static int iscsiRecvPDU(PISCSIIMAGE pImage, uint32_t itt, PISCSIRES paRes, uint32_t cnRes, uint32_t fFlags);
675static int iscsiRecvPDUAsync(PISCSIIMAGE pImage);
676static int iscsiSendPDUAsync(PISCSIIMAGE pImage);
677static int iscsiValidatePDU(PISCSIRES paRes, uint32_t cnRes);
678static int iscsiRecvPDUProcess(PISCSIIMAGE pImage, PISCSIRES paRes, uint32_t cnRes);
679static int iscsiPDUTxPrepare(PISCSIIMAGE pImage, PISCSICMD pIScsiCmd);
680static int iscsiRecvPDUUpdateRequest(PISCSIIMAGE pImage, PISCSIRES paRes, uint32_t cnRes);
681static void iscsiCmdComplete(PISCSIIMAGE pImage, PISCSICMD pIScsiCmd, int rcCmd);
682static int iscsiTextAddKeyValue(uint8_t *pbBuf, size_t cbBuf, size_t *pcbBufCurr, const char *pcszKey, const char *pcszValue, size_t cbValue);
683static int iscsiTextGetKeyValue(const uint8_t *pbBuf, size_t cbBuf, const char *pcszKey, const char **ppcszValue);
684static int iscsiStrToBinary(const char *pcszValue, uint8_t *pbValue, size_t *pcbValue);
685static int iscsiUpdateParameters(PISCSIIMAGE pImage, const uint8_t *pbBuf, size_t cbBuf);
686
687/* Serial number arithmetic comparison. */
688static bool serial_number_less(uint32_t sn1, uint32_t sn2);
689static bool serial_number_greater(uint32_t sn1, uint32_t sn2);
690
691/* CHAP-MD5 functions. */
692#ifdef IMPLEMENT_TARGET_AUTH
693static void chap_md5_generate_challenge(uint8_t *pbChallenge, size_t *pcbChallenge);
694#endif
695static void chap_md5_compute_response(uint8_t *pbResponse, uint8_t id, const uint8_t *pbChallenge, size_t cbChallenge,
696 const uint8_t *pbSecret, size_t cbSecret);
697
698/**
699 * Internal: release log wrapper limiting the number of entries.
700 */
701DECLINLINE(void) iscsiLogRel(PISCSIIMAGE pImage, const char *pcszFormat, ...)
702{
703 if (pImage->cLogRelErrors++ < MAX_LOG_REL_ERRORS)
704 {
705 va_list va;
706
707 va_start(va, pcszFormat);
708 LogRel(("%N\n", pcszFormat, &va));
709 va_end(va);
710 }
711}
712
713DECLINLINE(bool) iscsiIsClientConnected(PISCSIIMAGE pImage)
714{
715 return pImage->Socket != NIL_VDSOCKET
716 && pImage->pIfNet->pfnIsClientConnected(pImage->Socket);
717}
718
719/**
720 * Calculates the hash for the given ITT used
721 * to look up the command in the table.
722 */
723DECLINLINE(uint32_t) iscsiIttHash(uint32_t Itt)
724{
725 return Itt % ISCSI_CMD_WAITING_ENTRIES;
726}
727
728static PISCSICMD iscsiCmdGetFromItt(PISCSIIMAGE pImage, uint32_t Itt)
729{
730 PISCSICMD pIScsiCmd = NULL;
731
732 pIScsiCmd = pImage->aCmdsWaiting[iscsiIttHash(Itt)];
733
734 while ( pIScsiCmd
735 && pIScsiCmd->Itt != Itt)
736 pIScsiCmd = pIScsiCmd->pNext;
737
738 return pIScsiCmd;
739}
740
741static void iscsiCmdInsert(PISCSIIMAGE pImage, PISCSICMD pIScsiCmd)
742{
743 PISCSICMD pIScsiCmdOld;
744 uint32_t idx = iscsiIttHash(pIScsiCmd->Itt);
745
746 Assert(!pIScsiCmd->pNext);
747
748 pIScsiCmdOld = pImage->aCmdsWaiting[idx];
749 pIScsiCmd->pNext = pIScsiCmdOld;
750 pImage->aCmdsWaiting[idx] = pIScsiCmd;
751 pImage->cCmdsWaiting++;
752}
753
754static PISCSICMD iscsiCmdRemove(PISCSIIMAGE pImage, uint32_t Itt)
755{
756 PISCSICMD pIScsiCmd = NULL;
757 PISCSICMD pIScsiCmdPrev = NULL;
758 uint32_t idx = iscsiIttHash(Itt);
759
760 pIScsiCmd = pImage->aCmdsWaiting[idx];
761
762 while ( pIScsiCmd
763 && pIScsiCmd->Itt != Itt)
764 {
765 pIScsiCmdPrev = pIScsiCmd;
766 pIScsiCmd = pIScsiCmd->pNext;
767 }
768
769 if (pIScsiCmd)
770 {
771 if (pIScsiCmdPrev)
772 {
773 Assert(!pIScsiCmd->pNext || VALID_PTR(pIScsiCmd->pNext));
774 pIScsiCmdPrev->pNext = pIScsiCmd->pNext;
775 }
776 else
777 {
778 pImage->aCmdsWaiting[idx] = pIScsiCmd->pNext;
779 Assert(!pImage->aCmdsWaiting[idx] || VALID_PTR(pImage->aCmdsWaiting[idx]));
780 }
781 pImage->cCmdsWaiting--;
782 }
783
784 return pIScsiCmd;
785}
786
787/**
788 * Removes all commands from the table and returns the
789 * list head
790 *
791 * @returns Pointer to the head of the command list.
792 * @param pImage iSCSI connection to use.
793 */
794static PISCSICMD iscsiCmdRemoveAll(PISCSIIMAGE pImage)
795{
796 PISCSICMD pIScsiCmdHead = NULL;
797
798 for (unsigned idx = 0; idx < RT_ELEMENTS(pImage->aCmdsWaiting); idx++)
799 {
800 PISCSICMD pHead;
801 PISCSICMD pTail;
802
803 pHead = pImage->aCmdsWaiting[idx];
804 pImage->aCmdsWaiting[idx] = NULL;
805
806 if (pHead)
807 {
808 /* Get the tail. */
809 pTail = pHead;
810 while (pTail->pNext)
811 pTail = pTail->pNext;
812
813 /* Concatenate. */
814 pTail->pNext = pIScsiCmdHead;
815 pIScsiCmdHead = pHead;
816 }
817 }
818 pImage->cCmdsWaiting = 0;
819
820 return pIScsiCmdHead;
821}
822
823/**
824 * Dumps an iSCSI packet if enabled.
825 *
826 * @returns nothing.
827 * @param pImage The iSCSI image instance data.
828 * @param paISCSISegs Pointer to the segments array.
829 * @param cnISCSISegs Number of segments in the array.
830 * @param rc Status code for this packet.
831 * @param fRequest Flag whether this is request or response packet.
832 */
833static void iscsiDumpPacket(PISCSIIMAGE pImage, PISCSIREQ paISCSISegs, unsigned cnISCSISegs, int rc, bool fRequest)
834{
835 if (pImage->fDumpMalformedPackets)
836 {
837 LogRel(("iSCSI{%s}: Dumping %s packet completed with status code %Rrc\n", pImage->pszTargetName, fRequest ? "request" : "response", rc));
838 for (unsigned i = 0; i < cnISCSISegs; i++)
839 {
840 if (paISCSISegs[i].cbSeg)
841 {
842 LogRel(("iSCSI{%s}: Segment %u, size %zu\n"
843 "%.*Rhxd\n",
844 pImage->pszTargetName, i, paISCSISegs[i].cbSeg,
845 paISCSISegs[i].cbSeg, paISCSISegs[i].pcvSeg));
846 }
847 }
848 }
849}
850
851static int iscsiTransportConnect(PISCSIIMAGE pImage)
852{
853 int rc;
854 if (!pImage->pszHostname)
855 return VERR_NET_DEST_ADDRESS_REQUIRED;
856
857 rc = pImage->pIfNet->pfnClientConnect(pImage->Socket, pImage->pszHostname, pImage->uPort, pImage->uReadTimeout);
858 if (RT_FAILURE(rc))
859 {
860 if ( rc == VERR_NET_CONNECTION_REFUSED
861 || rc == VERR_NET_CONNECTION_RESET
862 || rc == VERR_NET_UNREACHABLE
863 || rc == VERR_NET_HOST_UNREACHABLE
864 || rc == VERR_NET_CONNECTION_TIMED_OUT)
865 {
866 /* Standardize return value for no connection. */
867 rc = VERR_NET_CONNECTION_REFUSED;
868 }
869 return rc;
870 }
871
872 /* Disable Nagle algorithm, we want things to be sent immediately. */
873 pImage->pIfNet->pfnSetSendCoalescing(pImage->Socket, false);
874
875 /* Make initiator name and ISID unique on this host. */
876 RTNETADDR LocalAddr;
877 rc = pImage->pIfNet->pfnGetLocalAddress(pImage->Socket, &LocalAddr);
878 if (RT_FAILURE(rc))
879 return rc;
880 if ( LocalAddr.uPort == RTNETADDR_PORT_NA
881 || LocalAddr.uPort > 65535)
882 return VERR_NET_ADDRESS_FAMILY_NOT_SUPPORTED;
883 pImage->ISID &= ~65535ULL;
884 pImage->ISID |= LocalAddr.uPort;
885 /* Eliminate the port so that it isn't included below. */
886 LocalAddr.uPort = RTNETADDR_PORT_NA;
887 if (pImage->fAutomaticInitiatorName)
888 {
889 if (pImage->pszInitiatorName)
890 RTStrFree(pImage->pszInitiatorName);
891 RTStrAPrintf(&pImage->pszInitiatorName, "%s:01:%RTnaddr",
892 s_iscsiDefaultInitiatorBasename, &LocalAddr);
893 if (!pImage->pszInitiatorName)
894 return VERR_NO_MEMORY;
895 }
896 LogRel(("iSCSI: connect from initiator %s with source port %u\n", pImage->pszInitiatorName, pImage->ISID & 65535));
897 return VINF_SUCCESS;
898}
899
900
901static int iscsiTransportClose(PISCSIIMAGE pImage)
902{
903 int rc;
904
905 LogFlowFunc(("(%s:%d)\n", pImage->pszHostname, pImage->uPort));
906 if (iscsiIsClientConnected(pImage))
907 {
908 LogRel(("iSCSI: disconnect from initiator %s with source port %u\n", pImage->pszInitiatorName, pImage->ISID & 65535));
909 rc = pImage->pIfNet->pfnClientClose(pImage->Socket);
910 }
911 else
912 rc = VINF_SUCCESS;
913 LogFlowFunc(("returns %Rrc\n", rc));
914 return rc;
915}
916
917
918static int iscsiTransportRead(PISCSIIMAGE pImage, PISCSIRES paResponse, unsigned int cnResponse)
919{
920 int rc = VINF_SUCCESS;
921 unsigned int i = 0;
922 size_t cbToRead, cbActuallyRead, residual, cbSegActual = 0, cbAHSLength, cbDataLength;
923 char *pDst;
924
925 LogFlowFunc(("cnResponse=%d (%s:%d)\n", cnResponse, pImage->pszHostname, pImage->uPort));
926 if (!iscsiIsClientConnected(pImage))
927 {
928 /* Reconnecting makes no sense in this case, as there will be nothing
929 * to receive. We would just run into a timeout. */
930 rc = VERR_BROKEN_PIPE;
931 }
932
933 if (RT_SUCCESS(rc) && paResponse[0].cbSeg >= ISCSI_BHS_SIZE)
934 {
935 cbToRead = 0;
936 residual = ISCSI_BHS_SIZE; /* Do not read more than the BHS length before the true PDU length is known. */
937 cbSegActual = residual;
938 pDst = (char *)paResponse[i].pvSeg;
939 uint64_t u64Timeout = RTTimeMilliTS() + pImage->uReadTimeout;
940 do
941 {
942 int64_t cMilliesRemaining = u64Timeout - RTTimeMilliTS();
943 if (cMilliesRemaining <= 0)
944 {
945 rc = VERR_TIMEOUT;
946 break;
947 }
948 Assert(cMilliesRemaining < 1000000);
949 rc = pImage->pIfNet->pfnSelectOne(pImage->Socket, cMilliesRemaining);
950 if (RT_FAILURE(rc))
951 break;
952 rc = pImage->pIfNet->pfnRead(pImage->Socket, pDst, residual, &cbActuallyRead);
953 if (RT_FAILURE(rc))
954 break;
955 if (cbActuallyRead == 0)
956 {
957 /* The other end has closed the connection. */
958 iscsiTransportClose(pImage);
959 pImage->state = ISCSISTATE_FREE;
960 rc = VERR_NET_CONNECTION_RESET;
961 break;
962 }
963 if (cbToRead == 0)
964 {
965 /* Currently reading the BHS. */
966 residual -= cbActuallyRead;
967 pDst += cbActuallyRead;
968 if (residual <= 40)
969 {
970 /* Enough data read to figure out the actual PDU size. */
971 uint32_t word1 = RT_N2H_U32(((uint32_t *)(paResponse[0].pvSeg))[1]);
972 cbAHSLength = (word1 & 0xff000000) >> 24;
973 cbAHSLength = ((cbAHSLength - 1) | 3) + 1; /* Add padding. */
974 cbDataLength = word1 & 0x00ffffff;
975 cbDataLength = ((cbDataLength - 1) | 3) + 1; /* Add padding. */
976 cbToRead = residual + cbAHSLength + cbDataLength;
977 residual += paResponse[0].cbSeg - ISCSI_BHS_SIZE;
978 if (residual > cbToRead)
979 residual = cbToRead;
980 cbSegActual = ISCSI_BHS_SIZE + cbAHSLength + cbDataLength;
981 /* Check whether we are already done with this PDU (no payload). */
982 if (cbToRead == 0)
983 break;
984 }
985 }
986 else
987 {
988 cbToRead -= cbActuallyRead;
989 if (cbToRead == 0)
990 break;
991 pDst += cbActuallyRead;
992 residual -= cbActuallyRead;
993 }
994 if (residual == 0)
995 {
996 i++;
997 if (i >= cnResponse)
998 {
999 /* No space left in receive buffers. */
1000 rc = VERR_BUFFER_OVERFLOW;
1001 break;
1002 }
1003 pDst = (char *)paResponse[i].pvSeg;
1004 residual = paResponse[i].cbSeg;
1005 if (residual > cbToRead)
1006 residual = cbToRead;
1007 cbSegActual = residual;
1008 }
1009 LogFlowFunc(("cbToRead=%u residual=%u cbSegActual=%u cbActuallRead=%u\n",
1010 cbToRead, residual, cbSegActual, cbActuallyRead));
1011 } while (true);
1012 }
1013 else
1014 {
1015 if (RT_SUCCESS(rc))
1016 rc = VERR_BUFFER_OVERFLOW;
1017 }
1018 if (RT_SUCCESS(rc))
1019 {
1020 paResponse[i].cbSeg = cbSegActual;
1021 for (i++; i < cnResponse; i++)
1022 paResponse[i].cbSeg = 0;
1023 }
1024
1025 if (RT_UNLIKELY( RT_FAILURE(rc)
1026 && ( rc == VERR_NET_CONNECTION_RESET
1027 || rc == VERR_NET_CONNECTION_ABORTED
1028 || rc == VERR_NET_CONNECTION_RESET_BY_PEER
1029 || rc == VERR_NET_CONNECTION_REFUSED
1030 || rc == VERR_BROKEN_PIPE)))
1031 {
1032 /* Standardize return value for broken connection. */
1033 rc = VERR_BROKEN_PIPE;
1034 }
1035
1036 LogFlowFunc(("returns %Rrc\n", rc));
1037 return rc;
1038}
1039
1040
1041static int iscsiTransportWrite(PISCSIIMAGE pImage, PISCSIREQ paRequest, unsigned int cnRequest)
1042{
1043 int rc = VINF_SUCCESS;
1044 unsigned int i;
1045
1046 LogFlowFunc(("cnRequest=%d (%s:%d)\n", cnRequest, pImage->pszHostname, pImage->uPort));
1047 if (!iscsiIsClientConnected(pImage))
1048 {
1049 /* Attempt to reconnect if the connection was previously broken. */
1050 rc = iscsiTransportConnect(pImage);
1051 }
1052
1053 if (RT_SUCCESS(rc))
1054 {
1055 /* Construct scatter/gather buffer for entire request, worst case
1056 * needs twice as many entries to allow for padding. */
1057 unsigned cBuf = 0;
1058 for (i = 0; i < cnRequest; i++)
1059 {
1060 cBuf++;
1061 if (paRequest[i].cbSeg & 3)
1062 cBuf++;
1063 }
1064 Assert(cBuf < ISCSI_SG_SEGMENTS_MAX);
1065 RTSGBUF buf;
1066 RTSGSEG aSeg[ISCSI_SG_SEGMENTS_MAX];
1067 static char aPad[4] = { 0, 0, 0, 0 };
1068 RTSgBufInit(&buf, &aSeg[0], cBuf);
1069 unsigned iBuf = 0;
1070 for (i = 0; i < cnRequest; i++)
1071 {
1072 /* Actual data chunk. */
1073 aSeg[iBuf].pvSeg = (void *)paRequest[i].pcvSeg;
1074 aSeg[iBuf].cbSeg = paRequest[i].cbSeg;
1075 iBuf++;
1076 /* Insert proper padding before the next chunk. */
1077 if (paRequest[i].cbSeg & 3)
1078 {
1079 aSeg[iBuf].pvSeg = &aPad[0];
1080 aSeg[iBuf].cbSeg = 4 - (paRequest[i].cbSeg & 3);
1081 iBuf++;
1082 }
1083 }
1084 /* Send out the request, the socket is set to send data immediately,
1085 * avoiding unnecessary delays. */
1086 rc = pImage->pIfNet->pfnSgWrite(pImage->Socket, &buf);
1087
1088 }
1089
1090 if (RT_UNLIKELY( RT_FAILURE(rc)
1091 && ( rc == VERR_NET_CONNECTION_RESET
1092 || rc == VERR_NET_CONNECTION_ABORTED
1093 || rc == VERR_NET_CONNECTION_RESET_BY_PEER
1094 || rc == VERR_NET_CONNECTION_REFUSED
1095 || rc == VERR_BROKEN_PIPE)))
1096 {
1097 /* Standardize return value for broken connection. */
1098 rc = VERR_BROKEN_PIPE;
1099 }
1100
1101 LogFlowFunc(("returns %Rrc\n", rc));
1102 return rc;
1103}
1104
1105
1106static int iscsiTransportOpen(PISCSIIMAGE pImage)
1107{
1108 int rc = VINF_SUCCESS;
1109 size_t cbHostname = 0; /* shut up gcc */
1110 const char *pcszPort = NULL; /* shut up gcc */
1111 char *pszPortEnd;
1112 uint16_t uPort;
1113
1114 /* Clean up previous connection data. */
1115 iscsiTransportClose(pImage);
1116 if (pImage->pszHostname)
1117 {
1118 RTMemFree(pImage->pszHostname);
1119 pImage->pszHostname = NULL;
1120 pImage->uPort = 0;
1121 }
1122
1123 /* Locate the port number via the colon separating the hostname from the port. */
1124 if (*pImage->pszTargetAddress)
1125 {
1126 if (*pImage->pszTargetAddress != '[')
1127 {
1128 /* Normal hostname or IPv4 dotted decimal. */
1129 pcszPort = strchr(pImage->pszTargetAddress, ':');
1130 if (pcszPort != NULL)
1131 {
1132 cbHostname = pcszPort - pImage->pszTargetAddress;
1133 pcszPort++;
1134 }
1135 else
1136 cbHostname = strlen(pImage->pszTargetAddress);
1137 }
1138 else
1139 {
1140 /* IPv6 literal address. Contains colons, so skip to closing square bracket. */
1141 pcszPort = strchr(pImage->pszTargetAddress, ']');
1142 if (pcszPort != NULL)
1143 {
1144 pcszPort++;
1145 cbHostname = pcszPort - pImage->pszTargetAddress;
1146 if (*pcszPort == '\0')
1147 pcszPort = NULL;
1148 else if (*pcszPort != ':')
1149 rc = VERR_PARSE_ERROR;
1150 else
1151 pcszPort++;
1152 }
1153 else
1154 rc = VERR_PARSE_ERROR;
1155 }
1156 }
1157 else
1158 rc = VERR_PARSE_ERROR;
1159
1160 /* Now split address into hostname and port. */
1161 if (RT_SUCCESS(rc))
1162 {
1163 pImage->pszHostname = (char *)RTMemAlloc(cbHostname + 1);
1164 if (!pImage->pszHostname)
1165 rc = VERR_NO_MEMORY;
1166 else
1167 {
1168 if (pImage->pszTargetAddress[0] == '[')
1169 memcpy(pImage->pszHostname, pImage->pszTargetAddress + 1, cbHostname);
1170 else
1171 memcpy(pImage->pszHostname, pImage->pszTargetAddress, cbHostname);
1172 pImage->pszHostname[cbHostname] = '\0';
1173 if (pcszPort != NULL)
1174 {
1175 rc = RTStrToUInt16Ex(pcszPort, &pszPortEnd, 0, &uPort);
1176 /* Note that RT_SUCCESS() macro to check the rc value is not strict enough in this case. */
1177 if (rc == VINF_SUCCESS && *pszPortEnd == '\0' && uPort != 0)
1178 {
1179 pImage->uPort = uPort;
1180 }
1181 else
1182 {
1183 rc = VERR_PARSE_ERROR;
1184 }
1185 }
1186 else
1187 pImage->uPort = ISCSI_DEFAULT_PORT;
1188 }
1189 }
1190
1191 if (RT_SUCCESS(rc))
1192 {
1193 if (!iscsiIsClientConnected(pImage))
1194 rc = iscsiTransportConnect(pImage);
1195 }
1196 else
1197 {
1198 if (pImage->pszHostname)
1199 {
1200 RTMemFree(pImage->pszHostname);
1201 pImage->pszHostname = NULL;
1202 }
1203 pImage->uPort = 0;
1204 }
1205
1206 LogFlowFunc(("returns %Rrc\n", rc));
1207 return rc;
1208}
1209
1210/**
1211 * Returns a human readable version of the given initiator login error detail.
1212 *
1213 * @returns String with the error detail.
1214 * @param u8Detail The detail indicator from the response.
1215 */
1216static const char *iscsiGetLoginErrorDetail(uint8_t u8Detail)
1217{
1218 const char *pszDetail = NULL;
1219
1220 switch (u8Detail)
1221 {
1222 case 0x00:
1223 pszDetail = "Miscelleanous iSCSI intiaitor error";
1224 break;
1225 case 0x01:
1226 pszDetail = "Authentication failure";
1227 break;
1228 case 0x02:
1229 pszDetail = "Authorization failure";
1230 break;
1231 case 0x03:
1232 pszDetail = "Not found";
1233 break;
1234 case 0x04:
1235 pszDetail = "Target removed";
1236 break;
1237 case 0x05:
1238 pszDetail = "Unsupported version";
1239 break;
1240 case 0x06:
1241 pszDetail = "Too many connections";
1242 break;
1243 case 0x07:
1244 pszDetail = "Missing parameter";
1245 break;
1246 case 0x08:
1247 pszDetail = "Can't include in session";
1248 break;
1249 case 0x09:
1250 pszDetail = "Session type not supported";
1251 break;
1252 case 0x0a:
1253 pszDetail = "Session does not exist";
1254 break;
1255 case 0x0b:
1256 pszDetail = "Invalid request type during login";
1257 break;
1258 default:
1259 pszDetail = "Unknown status detail";
1260 }
1261
1262 return pszDetail;
1263}
1264
1265/**
1266 * Attempts one login attempt to the given target.
1267 *
1268 * @returns VBox status code.
1269 * @retval VINF_TRY_AGAIN when getting redirected and having to start over.
1270 * @retval VERR_TRY_AGAIN in case the connection was lost while receiving a reply
1271 * from the target and the login attempt can be repeated.
1272 * @param pImage The iSCSI connection state to be used.
1273 */
1274static int iscsiLogin(PISCSIIMAGE pImage)
1275{
1276 int rc = VINF_SUCCESS;
1277 uint32_t itt;
1278 uint32_t csg, nsg, substate;
1279 uint64_t isid_tsih;
1280 uint8_t bBuf[4096]; /* Should be large enough even for large authentication values. */
1281 size_t cbBuf;
1282 bool transit;
1283 uint8_t pbChallenge[1024]; /* RFC3720 specifies this as maximum. */
1284 size_t cbChallenge = 0; /* shut up gcc */
1285 uint8_t bChapIdx = 0; /* (MSC is used uninitialized) */
1286 uint8_t aResponse[RTMD5HASHSIZE];
1287 uint32_t cnISCSIReq = 0;
1288 ISCSIREQ aISCSIReq[4];
1289 uint32_t aReqBHS[12];
1290 uint32_t cnISCSIRes = 0;
1291 ISCSIRES aISCSIRes[2];
1292 uint32_t aResBHS[12];
1293 char *pszNext;
1294 bool fParameterNeg = true;
1295 pImage->cbRecvDataLength = ISCSI_DATA_LENGTH_MAX;
1296 pImage->cbSendDataLength = RT_MIN(ISCSI_DATA_LENGTH_MAX, pImage->cbWriteSplit);
1297 char szMaxDataLength[16];
1298 RTStrPrintf(szMaxDataLength, sizeof(szMaxDataLength), "%u", ISCSI_DATA_LENGTH_MAX);
1299 ISCSIPARAMETER aParameterNeg[] =
1300 {
1301 { "HeaderDigest", "None", 0 },
1302 { "DataDigest", "None", 0 },
1303 { "MaxConnections", "1", 0 },
1304 { "InitialR2T", "No", 0 },
1305 { "ImmediateData", "Yes", 0 },
1306 { "MaxRecvDataSegmentLength", szMaxDataLength, 0 },
1307 { "MaxBurstLength", szMaxDataLength, 0 },
1308 { "FirstBurstLength", szMaxDataLength, 0 },
1309 { "DefaultTime2Wait", "0", 0 },
1310 { "DefaultTime2Retain", "60", 0 },
1311 { "DataPDUInOrder", "Yes", 0 },
1312 { "DataSequenceInOrder", "Yes", 0 },
1313 { "ErrorRecoveryLevel", "0", 0 },
1314 { "MaxOutstandingR2T", "1", 0 }
1315 };
1316
1317 if (!iscsiIsClientConnected(pImage))
1318 {
1319 rc = iscsiTransportOpen(pImage);
1320 if (RT_FAILURE(rc))
1321 return rc;
1322 }
1323
1324 pImage->state = ISCSISTATE_IN_LOGIN;
1325 pImage->ITT = 1;
1326 pImage->FirstRecvPDU = true;
1327 pImage->CmdSN = 1;
1328 pImage->ExpCmdSN = 0;
1329 pImage->MaxCmdSN = 1;
1330 pImage->ExpStatSN = 0;
1331
1332 /*
1333 * Send login request to target.
1334 */
1335 itt = iscsiNewITT(pImage);
1336 csg = 0;
1337 nsg = 0;
1338 substate = 0;
1339 isid_tsih = pImage->ISID << 16; /* TSIH field currently always 0 */
1340
1341 do
1342 {
1343 transit = false;
1344 cbBuf = 0;
1345 /* Handle all cases with a single switch statement. */
1346 switch (csg << 8 | substate)
1347 {
1348 case 0x0000: /* security negotiation, step 0: propose authentication. */
1349 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "SessionType", "Normal", 0);
1350 if (RT_FAILURE(rc))
1351 break;
1352 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "InitiatorName", pImage->pszInitiatorName, 0);
1353 if (RT_FAILURE(rc))
1354 break;
1355 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "TargetName", pImage->pszTargetName, 0);
1356 if (RT_FAILURE(rc))
1357 break;
1358 if (pImage->pszInitiatorUsername == NULL)
1359 {
1360 /* No authentication. Immediately switch to next phase. */
1361 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "AuthMethod", "None", 0);
1362 if (RT_FAILURE(rc))
1363 break;
1364 nsg = 1;
1365 transit = true;
1366 }
1367 else
1368 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "AuthMethod", "CHAP,None", 0);
1369 break;
1370 case 0x0001: /* security negotiation, step 1: propose CHAP_MD5 variant. */
1371 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "CHAP_A", "5", 0);
1372 break;
1373 case 0x0002: /* security negotiation, step 2: send authentication info. */
1374 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "CHAP_N", pImage->pszInitiatorUsername, 0);
1375 if (RT_FAILURE(rc))
1376 break;
1377 chap_md5_compute_response(aResponse, bChapIdx, pbChallenge, cbChallenge,
1378 pImage->pbInitiatorSecret, pImage->cbInitiatorSecret);
1379 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf, "CHAP_R", (const char *)aResponse, RTMD5HASHSIZE);
1380 if (RT_FAILURE(rc))
1381 break;
1382 nsg = 1;
1383 transit = true;
1384 break;
1385 case 0x0100: /* login operational negotiation, step 0: set parameters. */
1386 if (fParameterNeg)
1387 {
1388 for (unsigned i = 0; i < RT_ELEMENTS(aParameterNeg); i++)
1389 {
1390 rc = iscsiTextAddKeyValue(bBuf, sizeof(bBuf), &cbBuf,
1391 aParameterNeg[i].pszParamName,
1392 aParameterNeg[i].pszParamValue,
1393 aParameterNeg[i].cbParamValue);
1394 if (RT_FAILURE(rc))
1395 break;
1396 }
1397 fParameterNeg = false;
1398 }
1399
1400 nsg = 3;
1401 transit = true;
1402 break;
1403 case 0x0300: /* full feature phase. */
1404 default:
1405 /* Should never come here. */
1406 AssertMsgFailed(("send: Undefined login state %d substate %d\n", csg, substate));
1407 break;
1408 }
1409
1410 if (RT_FAILURE(rc))
1411 break;
1412
1413 aReqBHS[0] = RT_H2N_U32( ISCSI_IMMEDIATE_DELIVERY_BIT
1414 | (csg << ISCSI_CSG_SHIFT)
1415 | (transit ? (nsg << ISCSI_NSG_SHIFT | ISCSI_TRANSIT_BIT) : 0)
1416 | ISCSI_MY_VERSION /* Minimum version. */
1417 | (ISCSI_MY_VERSION << 8) /* Maximum version. */
1418 | ISCSIOP_LOGIN_REQ); /* C=0 */
1419 aReqBHS[1] = RT_H2N_U32((uint32_t)cbBuf); /* TotalAHSLength=0 */
1420 aReqBHS[2] = RT_H2N_U32(isid_tsih >> 32);
1421 aReqBHS[3] = RT_H2N_U32(isid_tsih & 0xffffffff);
1422 aReqBHS[4] = itt;
1423 aReqBHS[5] = RT_H2N_U32(1 << 16); /* CID=1,reserved */
1424 aReqBHS[6] = RT_H2N_U32(pImage->CmdSN);
1425 aReqBHS[7] = RT_H2N_U32(pImage->ExpStatSN);
1426 aReqBHS[8] = 0; /* reserved */
1427 aReqBHS[9] = 0; /* reserved */
1428 aReqBHS[10] = 0; /* reserved */
1429 aReqBHS[11] = 0; /* reserved */
1430
1431 cnISCSIReq = 0;
1432 aISCSIReq[cnISCSIReq].pcvSeg = aReqBHS;
1433 aISCSIReq[cnISCSIReq].cbSeg = sizeof(aReqBHS);
1434 cnISCSIReq++;
1435
1436 aISCSIReq[cnISCSIReq].pcvSeg = bBuf;
1437 aISCSIReq[cnISCSIReq].cbSeg = cbBuf;
1438 cnISCSIReq++;
1439
1440 rc = iscsiSendPDU(pImage, aISCSIReq, cnISCSIReq, ISCSIPDU_NO_REATTACH);
1441 if (RT_SUCCESS(rc))
1442 {
1443 ISCSIOPCODE cmd;
1444 ISCSILOGINSTATUSCLASS loginStatusClass;
1445
1446 cnISCSIRes = 0;
1447 aISCSIRes[cnISCSIRes].pvSeg = aResBHS;
1448 aISCSIRes[cnISCSIRes].cbSeg = sizeof(aResBHS);
1449 cnISCSIRes++;
1450 aISCSIRes[cnISCSIRes].pvSeg = bBuf;
1451 aISCSIRes[cnISCSIRes].cbSeg = sizeof(bBuf);
1452 cnISCSIRes++;
1453
1454 rc = iscsiRecvPDU(pImage, itt, aISCSIRes, cnISCSIRes, ISCSIPDU_NO_REATTACH);
1455 if (RT_FAILURE(rc))
1456 {
1457 /*
1458 * We lost connection to the target while receiving the answer,
1459 * start from the beginning.
1460 */
1461 if (rc == VERR_BROKEN_PIPE || rc == VERR_NET_CONNECTION_REFUSED)
1462 rc = VERR_TRY_AGAIN;
1463 break;
1464 }
1465
1466 /** @todo collect partial login responses with Continue bit set. */
1467 Assert(aISCSIRes[0].pvSeg == aResBHS);
1468 Assert(aISCSIRes[0].cbSeg >= ISCSI_BHS_SIZE);
1469 Assert((RT_N2H_U32(aResBHS[0]) & ISCSI_CONTINUE_BIT) == 0);
1470
1471 cmd = (ISCSIOPCODE)(RT_N2H_U32(aResBHS[0]) & ISCSIOP_MASK);
1472 if (cmd == ISCSIOP_LOGIN_RES)
1473 {
1474 if ((RT_N2H_U32(aResBHS[0]) & 0xff) != ISCSI_MY_VERSION)
1475 {
1476 iscsiTransportClose(pImage);
1477 rc = VERR_PARSE_ERROR;
1478 break; /* Give up immediately, as a RFC violation in version fields is very serious. */
1479 }
1480
1481 loginStatusClass = (ISCSILOGINSTATUSCLASS)(RT_N2H_U32(aResBHS[9]) >> 24);
1482 switch (loginStatusClass)
1483 {
1484 case ISCSI_LOGIN_STATUS_CLASS_SUCCESS:
1485 uint32_t targetCSG;
1486 uint32_t targetNSG;
1487 bool targetTransit;
1488
1489 if (pImage->FirstRecvPDU)
1490 {
1491 pImage->FirstRecvPDU = false;
1492 pImage->ExpStatSN = RT_N2H_U32(aResBHS[6]) + 1;
1493 }
1494
1495 targetCSG = (RT_N2H_U32(aResBHS[0]) & ISCSI_CSG_MASK) >> ISCSI_CSG_SHIFT;
1496 targetNSG = (RT_N2H_U32(aResBHS[0]) & ISCSI_NSG_MASK) >> ISCSI_NSG_SHIFT;
1497 targetTransit = !!(RT_N2H_U32(aResBHS[0]) & ISCSI_TRANSIT_BIT);
1498
1499 /* Handle all cases with a single switch statement. */
1500 switch (csg << 8 | substate)
1501 {
1502 case 0x0000: /* security negotiation, step 0: receive final authentication. */
1503 rc = iscsiUpdateParameters(pImage, bBuf, aISCSIRes[1].cbSeg);
1504 if (RT_FAILURE(rc))
1505 break;
1506
1507 const char *pcszAuthMethod;
1508
1509 rc = iscsiTextGetKeyValue(bBuf, aISCSIRes[1].cbSeg, "AuthMethod", &pcszAuthMethod);
1510 if (RT_FAILURE(rc))
1511 {
1512 rc = VERR_PARSE_ERROR;
1513 break;
1514 }
1515 if (strcmp(pcszAuthMethod, "None") == 0)
1516 {
1517 /* Authentication offered, but none required. Skip to operational parameters. */
1518 csg = 1;
1519 nsg = 1;
1520 transit = true;
1521 substate = 0;
1522 break;
1523 }
1524 else if (strcmp(pcszAuthMethod, "CHAP") == 0 && targetNSG == 0 && !targetTransit)
1525 {
1526 /* CHAP authentication required, continue with next substate. */
1527 substate++;
1528 break;
1529 }
1530
1531 /* Unknown auth method or login response PDU headers incorrect. */
1532 rc = VERR_PARSE_ERROR;
1533 break;
1534 case 0x0001: /* security negotiation, step 1: receive final CHAP variant and challenge. */
1535 {
1536 rc = iscsiUpdateParameters(pImage, bBuf, aISCSIRes[1].cbSeg);
1537 if (RT_FAILURE(rc))
1538 break;
1539
1540 const char *pcszChapAuthMethod;
1541 const char *pcszChapIdxTarget;
1542 const char *pcszChapChallengeStr;
1543
1544 rc = iscsiTextGetKeyValue(bBuf, aISCSIRes[1].cbSeg, "CHAP_A", &pcszChapAuthMethod);
1545 if (RT_FAILURE(rc))
1546 {
1547 rc = VERR_PARSE_ERROR;
1548 break;
1549 }
1550 if (strcmp(pcszChapAuthMethod, "5") != 0)
1551 {
1552 rc = VERR_PARSE_ERROR;
1553 break;
1554 }
1555 rc = iscsiTextGetKeyValue(bBuf, aISCSIRes[1].cbSeg, "CHAP_I", &pcszChapIdxTarget);
1556 if (RT_FAILURE(rc))
1557 {
1558 rc = VERR_PARSE_ERROR;
1559 break;
1560 }
1561 rc = RTStrToUInt8Ex(pcszChapIdxTarget, &pszNext, 0, &bChapIdx);
1562/** @todo r=bird: Unsafe use of pszNext on failure. The code should probably
1563 * use RTStrToUInt8Full and check for rc != VINF_SUCCESS. */
1564 if (rc > VINF_SUCCESS || *pszNext != '\0')
1565 {
1566 rc = VERR_PARSE_ERROR;
1567 break;
1568 }
1569 rc = iscsiTextGetKeyValue(bBuf, aISCSIRes[1].cbSeg, "CHAP_C", &pcszChapChallengeStr);
1570 if (RT_FAILURE(rc))
1571 {
1572 rc = VERR_PARSE_ERROR;
1573 break;
1574 }
1575 cbChallenge = sizeof(pbChallenge);
1576 rc = iscsiStrToBinary(pcszChapChallengeStr, pbChallenge, &cbChallenge);
1577 if (RT_FAILURE(rc))
1578 break;
1579 substate++;
1580 transit = true;
1581 break;
1582 }
1583 case 0x0002: /* security negotiation, step 2: check authentication success. */
1584 rc = iscsiUpdateParameters(pImage, bBuf, aISCSIRes[1].cbSeg);
1585 if (RT_FAILURE(rc))
1586 break;
1587
1588 if (targetCSG == 0 && targetNSG == 1 && targetTransit)
1589 {
1590 /* Target wants to continue in login operational state, authentication success. */
1591 csg = 1;
1592 nsg = 3;
1593 substate = 0;
1594 break;
1595 }
1596 rc = VERR_PARSE_ERROR;
1597 break;
1598 case 0x0100: /* login operational negotiation, step 0: check results. */
1599 rc = iscsiUpdateParameters(pImage, bBuf, aISCSIRes[1].cbSeg);
1600 if (RT_FAILURE(rc))
1601 break;
1602
1603 if (targetCSG == 1 && targetNSG == 3 && targetTransit)
1604 {
1605 /* Target wants to continue in full feature phase, login finished. */
1606 csg = 3;
1607 nsg = 3;
1608 substate = 0;
1609 break;
1610 }
1611 else if (targetCSG == 1 && targetNSG == 1 && !targetTransit)
1612 {
1613 /* Target wants to negotiate certain parameters and
1614 * stay in login operational negotiation. */
1615 csg = 1;
1616 nsg = 3;
1617 substate = 0;
1618 }
1619 rc = VERR_PARSE_ERROR;
1620 break;
1621 case 0x0300: /* full feature phase. */
1622 default:
1623 AssertMsgFailed(("recv: Undefined login state %d substate %d\n", csg, substate));
1624 rc = VERR_PARSE_ERROR;
1625 break;
1626 }
1627 break;
1628 case ISCSI_LOGIN_STATUS_CLASS_REDIRECTION:
1629 const char *pcszTargetRedir;
1630
1631 /* Target has moved to some other location, as indicated in the TargetAddress key. */
1632 rc = iscsiTextGetKeyValue(bBuf, aISCSIRes[1].cbSeg, "TargetAddress", &pcszTargetRedir);
1633 if (RT_FAILURE(rc))
1634 {
1635 rc = VERR_PARSE_ERROR;
1636 break;
1637 }
1638 if (pImage->pszTargetAddress)
1639 RTMemFree(pImage->pszTargetAddress);
1640 {
1641 size_t cb = strlen(pcszTargetRedir) + 1;
1642 pImage->pszTargetAddress = (char *)RTMemAlloc(cb);
1643 if (!pImage->pszTargetAddress)
1644 {
1645 rc = VERR_NO_MEMORY;
1646 break;
1647 }
1648 memcpy(pImage->pszTargetAddress, pcszTargetRedir, cb);
1649 }
1650 rc = VINF_TRY_AGAIN;
1651 break;
1652 case ISCSI_LOGIN_STATUS_CLASS_INITIATOR_ERROR:
1653 {
1654 LogRel(("iSCSI: login to target failed with: %s\n",
1655 iscsiGetLoginErrorDetail((RT_N2H_U32(aResBHS[9]) >> 16) & 0xff)));
1656 iscsiTransportClose(pImage);
1657 rc = VERR_IO_GEN_FAILURE;
1658 break;
1659 }
1660 case ISCSI_LOGIN_STATUS_CLASS_TARGET_ERROR:
1661 iscsiTransportClose(pImage);
1662 rc = VINF_EOF;
1663 break;
1664 default:
1665 rc = VERR_PARSE_ERROR;
1666 }
1667
1668 if (RT_FAILURE(rc) || rc == VINF_TRY_AGAIN)
1669 break;
1670
1671 if (csg == 3)
1672 {
1673 /*
1674 * Finished login, continuing with Full Feature Phase.
1675 */
1676 rc = VINF_SUCCESS;
1677 break;
1678 }
1679 }
1680 else
1681 AssertMsgFailed(("%s: ignoring unexpected PDU with first word = %#08x\n", __FUNCTION__, RT_N2H_U32(aResBHS[0])));
1682 }
1683 else
1684 break;
1685 } while (true);
1686
1687 if ( RT_FAILURE(rc)
1688 && rc != VERR_TRY_AGAIN)
1689 {
1690 /*
1691 * Dump the last request and response of we are supposed to do so and there is a request
1692 * or response.
1693 */
1694 if (cnISCSIReq)
1695 iscsiDumpPacket(pImage, aISCSIReq, cnISCSIReq, VINF_SUCCESS, true /* fRequest */);
1696
1697 if (cnISCSIRes)
1698 iscsiDumpPacket(pImage, (PISCSIREQ)aISCSIRes, cnISCSIRes, rc, false /* fRequest */);
1699
1700 /*
1701 * Close connection to target.
1702 */
1703 iscsiTransportClose(pImage);
1704 pImage->state = ISCSISTATE_FREE;
1705 }
1706 else if (RT_FAILURE(rc))
1707 pImage->state = ISCSISTATE_NORMAL;
1708
1709 return rc;
1710}
1711
1712/**
1713 * Attach to an iSCSI target. Performs all operations necessary to enter
1714 * Full Feature Phase.
1715 *
1716 * @returns VBox status code.
1717 * @param pImage The iSCSI connection state to be used.
1718 */
1719static DECLCALLBACK(int) iscsiAttach(void *pvUser)
1720{
1721 int rc = VINF_SUCCESS;
1722 unsigned cRetries = 5;
1723 PISCSIIMAGE pImage = (PISCSIIMAGE)pvUser;
1724
1725 LogFlowFunc(("entering\n"));
1726
1727 Assert(pImage->state == ISCSISTATE_FREE);
1728
1729 /*
1730 * If there were too many logins without any successful I/O just fail
1731 * and assume the target is not working properly.
1732 */
1733 if (ASMAtomicReadU32(&pImage->cLoginsSinceIo) == 3)
1734 return VERR_BROKEN_PIPE;
1735
1736 RTSemMutexRequest(pImage->Mutex, RT_INDEFINITE_WAIT);
1737
1738 /* Make 100% sure the connection isn't reused for a new login. */
1739 iscsiTransportClose(pImage);
1740
1741 /* Try to log in a few number of times. */
1742 while (cRetries > 0)
1743 {
1744 rc = iscsiLogin(pImage);
1745 if (rc == VINF_SUCCESS) /* Login succeeded, continue with full feature phase. */
1746 break;
1747 else if (rc == VERR_TRY_AGAIN) /* Lost connection during receive. */
1748 cRetries--;
1749 else if (RT_FAILURE(rc))
1750 break;
1751 else /* For redirects try again. */
1752 AssertMsg(rc == VINF_TRY_AGAIN, ("Unexpected status code %Rrc\n", rc));
1753 }
1754
1755 if (RT_SUCCESS(rc))
1756 ASMAtomicIncU32(&pImage->cLoginsSinceIo);
1757
1758 RTSemMutexRelease(pImage->Mutex);
1759
1760 LogFlowFunc(("returning %Rrc\n", rc));
1761 LogRel(("iSCSI: login to target %s %s (%Rrc)\n", pImage->pszTargetName, RT_SUCCESS(rc) ? "successful" : "failed", rc));
1762 return rc;
1763}
1764
1765
1766/**
1767 * Detach from an iSCSI target.
1768 *
1769 * @returns VBox status code.
1770 * @param pImage The iSCSI connection state to be used.
1771 */
1772static DECLCALLBACK(int) iscsiDetach(void *pvUser)
1773{
1774 int rc;
1775 uint32_t itt;
1776 uint32_t cnISCSIReq = 0;
1777 ISCSIREQ aISCSIReq[4];
1778 uint32_t aReqBHS[12];
1779 PISCSIIMAGE pImage = (PISCSIIMAGE)pvUser;
1780
1781 LogFlowFunc(("entering\n"));
1782
1783 RTSemMutexRequest(pImage->Mutex, RT_INDEFINITE_WAIT);
1784
1785 if (pImage->state != ISCSISTATE_FREE && pImage->state != ISCSISTATE_IN_LOGOUT)
1786 {
1787 pImage->state = ISCSISTATE_IN_LOGOUT;
1788
1789 /*
1790 * Send logout request to target.
1791 */
1792 itt = iscsiNewITT(pImage);
1793 aReqBHS[0] = RT_H2N_U32(ISCSI_FINAL_BIT | ISCSIOP_LOGOUT_REQ); /* I=0,F=1,Reason=close session */
1794 aReqBHS[1] = RT_H2N_U32(0); /* TotalAHSLength=0,DataSementLength=0 */
1795 aReqBHS[2] = 0; /* reserved */
1796 aReqBHS[3] = 0; /* reserved */
1797 aReqBHS[4] = itt;
1798 aReqBHS[5] = 0; /* reserved */
1799 aReqBHS[6] = RT_H2N_U32(pImage->CmdSN);
1800 aReqBHS[7] = RT_H2N_U32(pImage->ExpStatSN);
1801 aReqBHS[8] = 0; /* reserved */
1802 aReqBHS[9] = 0; /* reserved */
1803 aReqBHS[10] = 0; /* reserved */
1804 aReqBHS[11] = 0; /* reserved */
1805 pImage->CmdSN++;
1806
1807 aISCSIReq[cnISCSIReq].pcvSeg = aReqBHS;
1808 aISCSIReq[cnISCSIReq].cbSeg = sizeof(aReqBHS);
1809 cnISCSIReq++;
1810
1811 rc = iscsiSendPDU(pImage, aISCSIReq, cnISCSIReq, ISCSIPDU_NO_REATTACH);
1812 if (RT_SUCCESS(rc))
1813 {
1814 /*
1815 * Read logout response from target.
1816 */
1817 ISCSIRES aISCSIRes;
1818 uint32_t aResBHS[12];
1819
1820 aISCSIRes.pvSeg = aResBHS;
1821 aISCSIRes.cbSeg = sizeof(aResBHS);
1822 rc = iscsiRecvPDU(pImage, itt, &aISCSIRes, 1, ISCSIPDU_NO_REATTACH);
1823 if (RT_SUCCESS(rc))
1824 {
1825 if (RT_N2H_U32(aResBHS[0]) != (ISCSI_FINAL_BIT | ISCSIOP_LOGOUT_RES))
1826 AssertMsgFailed(("iSCSI Logout response invalid\n"));
1827 }
1828 else
1829 AssertMsgFailed(("iSCSI Logout response error, rc=%Rrc\n", rc));
1830 }
1831 else
1832 AssertMsgFailed(("Could not send iSCSI Logout request, rc=%Rrc\n", rc));
1833 }
1834
1835 if (pImage->state != ISCSISTATE_FREE)
1836 {
1837 /*
1838 * Close connection to target.
1839 */
1840 rc = iscsiTransportClose(pImage);
1841 if (RT_FAILURE(rc))
1842 AssertMsgFailed(("Could not close connection to target, rc=%Rrc\n", rc));
1843 }
1844
1845 pImage->state = ISCSISTATE_FREE;
1846
1847 RTSemMutexRelease(pImage->Mutex);
1848
1849 LogFlowFunc(("leaving\n"));
1850 LogRel(("iSCSI: logout to target %s\n", pImage->pszTargetName));
1851 return VINF_SUCCESS;
1852}
1853
1854
1855/**
1856 * Perform a command on an iSCSI target. Target must be already in
1857 * Full Feature Phase.
1858 *
1859 * @returns VBox status code.
1860 * @param pImage The iSCSI connection state to be used.
1861 * @param pRequest Command descriptor. Contains all information about
1862 * the command, its transfer directions and pointers
1863 * to the buffer(s) used for transferring data and
1864 * status information.
1865 */
1866static int iscsiCommand(PISCSIIMAGE pImage, PSCSIREQ pRequest)
1867{
1868 int rc;
1869 uint32_t itt;
1870 uint32_t cbData;
1871 uint32_t cnISCSIReq = 0;
1872 ISCSIREQ aISCSIReq[4];
1873 uint32_t aReqBHS[12];
1874
1875 uint32_t *pDst = NULL;
1876 size_t cbBufLength;
1877 uint32_t aStatus[256]; /**< Plenty of buffer for status information. */
1878 uint32_t ExpDataSN = 0;
1879 bool final = false;
1880
1881
1882 LogFlowFunc(("entering, CmdSN=%d\n", pImage->CmdSN));
1883
1884 Assert(pRequest->enmXfer != SCSIXFER_TO_FROM_TARGET); /**< @todo not yet supported, would require AHS. */
1885 Assert(pRequest->cbI2TData <= 0xffffff); /* larger transfers would require R2T support. */
1886 Assert(pRequest->cbCDB <= 16); /* would cause buffer overrun below. */
1887
1888 /* If not in normal state, then the transport connection was dropped. Try
1889 * to reestablish by logging in, the target might be responsive again. */
1890 if (pImage->state == ISCSISTATE_FREE)
1891 rc = iscsiAttach(pImage);
1892
1893 /* If still not in normal state, then the underlying transport connection
1894 * cannot be established. Get out before bad things happen (and make
1895 * sure the caller suspends the VM again). */
1896 if (pImage->state == ISCSISTATE_NORMAL)
1897 {
1898 /*
1899 * Send SCSI command to target with all I2T data included.
1900 */
1901 cbData = 0;
1902 if (pRequest->enmXfer == SCSIXFER_FROM_TARGET)
1903 cbData = (uint32_t)pRequest->cbT2IData;
1904 else
1905 cbData = (uint32_t)pRequest->cbI2TData;
1906
1907 RTSemMutexRequest(pImage->Mutex, RT_INDEFINITE_WAIT);
1908
1909 itt = iscsiNewITT(pImage);
1910 memset(aReqBHS, 0, sizeof(aReqBHS));
1911 aReqBHS[0] = RT_H2N_U32( ISCSI_FINAL_BIT | ISCSI_TASK_ATTR_SIMPLE | ISCSIOP_SCSI_CMD
1912 | (pRequest->enmXfer << 21)); /* I=0,F=1,Attr=Simple */
1913 aReqBHS[1] = RT_H2N_U32(0x00000000 | ((uint32_t)pRequest->cbI2TData & 0xffffff)); /* TotalAHSLength=0 */
1914 aReqBHS[2] = RT_H2N_U32(pImage->LUN >> 32);
1915 aReqBHS[3] = RT_H2N_U32(pImage->LUN & 0xffffffff);
1916 aReqBHS[4] = itt;
1917 aReqBHS[5] = RT_H2N_U32(cbData);
1918 aReqBHS[6] = RT_H2N_U32(pImage->CmdSN);
1919 aReqBHS[7] = RT_H2N_U32(pImage->ExpStatSN);
1920 memcpy(aReqBHS + 8, pRequest->abCDB, pRequest->cbCDB);
1921 pImage->CmdSN++;
1922
1923 aISCSIReq[cnISCSIReq].pcvSeg = aReqBHS;
1924 aISCSIReq[cnISCSIReq].cbSeg = sizeof(aReqBHS);
1925 cnISCSIReq++;
1926
1927 if ( pRequest->enmXfer == SCSIXFER_TO_TARGET
1928 || pRequest->enmXfer == SCSIXFER_TO_FROM_TARGET)
1929 {
1930 Assert(pRequest->cI2TSegs == 1);
1931 aISCSIReq[cnISCSIReq].pcvSeg = pRequest->paI2TSegs[0].pvSeg;
1932 aISCSIReq[cnISCSIReq].cbSeg = pRequest->paI2TSegs[0].cbSeg; /* Padding done by transport. */
1933 cnISCSIReq++;
1934 }
1935
1936 rc = iscsiSendPDU(pImage, aISCSIReq, cnISCSIReq, ISCSIPDU_DEFAULT);
1937 if (RT_SUCCESS(rc))
1938 {
1939 /* Place SCSI request in queue. */
1940 pImage->paCurrReq = aISCSIReq;
1941 pImage->cnCurrReq = cnISCSIReq;
1942
1943 /*
1944 * Read SCSI response/data in PDUs from target.
1945 */
1946 if ( pRequest->enmXfer == SCSIXFER_FROM_TARGET
1947 || pRequest->enmXfer == SCSIXFER_TO_FROM_TARGET)
1948 {
1949 Assert(pRequest->cT2ISegs == 1);
1950 pDst = (uint32_t *)pRequest->paT2ISegs[0].pvSeg;
1951 cbBufLength = pRequest->paT2ISegs[0].cbSeg;
1952 }
1953 else
1954 cbBufLength = 0;
1955
1956 do
1957 {
1958 uint32_t cnISCSIRes = 0;
1959 ISCSIRES aISCSIRes[4];
1960 uint32_t aResBHS[12];
1961
1962 aISCSIRes[cnISCSIRes].pvSeg = aResBHS;
1963 aISCSIRes[cnISCSIRes].cbSeg = sizeof(aResBHS);
1964 cnISCSIRes++;
1965 if (cbBufLength != 0 &&
1966 ( pRequest->enmXfer == SCSIXFER_FROM_TARGET
1967 || pRequest->enmXfer == SCSIXFER_TO_FROM_TARGET))
1968 {
1969 aISCSIRes[cnISCSIRes].pvSeg = pDst;
1970 aISCSIRes[cnISCSIRes].cbSeg = cbBufLength;
1971 cnISCSIRes++;
1972 }
1973 /* Always reserve space for the status - it's impossible to tell
1974 * beforehand whether this will be the final PDU or not. */
1975 aISCSIRes[cnISCSIRes].pvSeg = aStatus;
1976 aISCSIRes[cnISCSIRes].cbSeg = sizeof(aStatus);
1977 cnISCSIRes++;
1978
1979 rc = iscsiRecvPDU(pImage, itt, aISCSIRes, cnISCSIRes, ISCSIPDU_DEFAULT);
1980 if (RT_FAILURE(rc))
1981 break;
1982
1983 final = !!(RT_N2H_U32(aResBHS[0]) & ISCSI_FINAL_BIT);
1984 ISCSIOPCODE cmd = (ISCSIOPCODE)(RT_N2H_U32(aResBHS[0]) & ISCSIOP_MASK);
1985 if (cmd == ISCSIOP_SCSI_RES)
1986 {
1987 /* This is the final PDU which delivers the status (and may be omitted if
1988 * the last Data-In PDU included successful completion status). Note
1989 * that ExpStatSN has been bumped already in iscsiRecvPDU. */
1990 if (!final || ((RT_N2H_U32(aResBHS[0]) & 0x0000ff00) != 0) || (RT_N2H_U32(aResBHS[6]) != pImage->ExpStatSN - 1))
1991 {
1992 /* SCSI Response in the wrong place or with a (target) failure. */
1993 rc = VERR_PARSE_ERROR;
1994 break;
1995 }
1996 /* The following is a bit tricky, as in error situations we may
1997 * get the status only instead of the result data plus optional
1998 * status. Thus the status may have ended up partially in the
1999 * data area. */
2000 pRequest->status = RT_N2H_U32(aResBHS[0]) & 0x000000ff;
2001 cbData = RT_N2H_U32(aResBHS[1]) & 0x00ffffff;
2002 if (cbData >= 2)
2003 {
2004 uint32_t cbStat = RT_N2H_U32(((uint32_t *)aISCSIRes[1].pvSeg)[0]) >> 16;
2005 if (cbStat + 2 > cbData)
2006 {
2007 rc = VERR_BUFFER_OVERFLOW;
2008 break;
2009 }
2010 /* Truncate sense data if it doesn't fit into the buffer. */
2011 pRequest->cbSense = RT_MIN(cbStat, pRequest->cbSense);
2012 memcpy(pRequest->abSense,
2013 ((const char *)aISCSIRes[1].pvSeg) + 2,
2014 RT_MIN(aISCSIRes[1].cbSeg - 2, pRequest->cbSense));
2015 if ( cnISCSIRes > 2 && aISCSIRes[2].cbSeg
2016 && (ssize_t)pRequest->cbSense - aISCSIRes[1].cbSeg + 2 > 0)
2017 {
2018 memcpy((char *)pRequest->abSense + aISCSIRes[1].cbSeg - 2,
2019 aISCSIRes[2].pvSeg,
2020 pRequest->cbSense - aISCSIRes[1].cbSeg + 2);
2021 }
2022 }
2023 else if (cbData == 1)
2024 {
2025 rc = VERR_PARSE_ERROR;
2026 break;
2027 }
2028 else
2029 pRequest->cbSense = 0;
2030 break;
2031 }
2032 else if (cmd == ISCSIOP_SCSI_DATA_IN)
2033 {
2034 /* A Data-In PDU carries some data that needs to be added to the received
2035 * data in response to the command. There may be both partial and complete
2036 * Data-In PDUs, so collect data until the status is included or the status
2037 * is sent in a separate SCSI Result frame (see above). */
2038 if (final && aISCSIRes[2].cbSeg != 0)
2039 {
2040 /* The received PDU is partially stored in the buffer for status.
2041 * Must not happen under normal circumstances and is a target error. */
2042 rc = VERR_BUFFER_OVERFLOW;
2043 break;
2044 }
2045 uint32_t len = RT_N2H_U32(aResBHS[1]) & 0x00ffffff;
2046 pDst = (uint32_t *)((char *)pDst + len);
2047 cbBufLength -= len;
2048 ExpDataSN++;
2049 if (final && (RT_N2H_U32(aResBHS[0]) & ISCSI_STATUS_BIT) != 0)
2050 {
2051 pRequest->status = RT_N2H_U32(aResBHS[0]) & 0x000000ff;
2052 pRequest->cbSense = 0;
2053 break;
2054 }
2055 }
2056 else
2057 {
2058 rc = VERR_PARSE_ERROR;
2059 break;
2060 }
2061 } while (true);
2062
2063 /* Remove SCSI request from queue. */
2064 pImage->paCurrReq = NULL;
2065 pImage->cnCurrReq = 0;
2066 }
2067
2068 if (rc == VERR_TIMEOUT)
2069 {
2070 /* Drop connection in case the target plays dead. Much better than
2071 * delaying the next requests until the timed out command actually
2072 * finishes. Also keep in mind that command shouldn't take longer than
2073 * about 30-40 seconds, or the guest will lose its patience. */
2074 iscsiTransportClose(pImage);
2075 pImage->state = ISCSISTATE_FREE;
2076 rc = VERR_BROKEN_PIPE;
2077 }
2078 RTSemMutexRelease(pImage->Mutex);
2079 }
2080 else
2081 rc = VERR_NET_CONNECTION_REFUSED;
2082
2083 if (RT_SUCCESS(rc))
2084 ASMAtomicWriteU32(&pImage->cLoginsSinceIo, 0);
2085 LogFlowFunc(("returns %Rrc\n", rc));
2086 return rc;
2087}
2088
2089
2090/**
2091 * Generate a new Initiator Task Tag.
2092 *
2093 * @returns Initiator Task Tag.
2094 * @param pImage The iSCSI connection state to be used.
2095 */
2096static uint32_t iscsiNewITT(PISCSIIMAGE pImage)
2097{
2098 uint32_t next_itt;
2099
2100 next_itt = pImage->ITT++;
2101 if (pImage->ITT == ISCSI_TASK_TAG_RSVD)
2102 pImage->ITT = 0;
2103 return RT_H2N_U32(next_itt);
2104}
2105
2106
2107/**
2108 * Send an iSCSI request. The request can consist of several segments, which
2109 * are padded to 4 byte boundaries and concatenated.
2110 *
2111 * @returns VBOX status
2112 * @param pImage The iSCSI connection state to be used.
2113 * @param paReq Pointer to array of iSCSI request sections.
2114 * @param cnReq Number of valid iSCSI request sections in the array.
2115 * @param uFlags Flags controlling the exact send semantics.
2116 */
2117static int iscsiSendPDU(PISCSIIMAGE pImage, PISCSIREQ paReq, uint32_t cnReq,
2118 uint32_t uFlags)
2119{
2120 int rc = VINF_SUCCESS;
2121 /** @todo return VERR_VD_ISCSI_INVALID_STATE in the appropriate situations,
2122 * needs cleaning up of timeout/disconnect handling a bit, as otherwise
2123 * too many incorrect errors are signalled. */
2124 Assert(cnReq >= 1);
2125 Assert(paReq[0].cbSeg >= ISCSI_BHS_SIZE);
2126
2127 for (uint32_t i = 0; i < pImage->cISCSIRetries; i++)
2128 {
2129 rc = iscsiTransportWrite(pImage, paReq, cnReq);
2130 if (RT_SUCCESS(rc))
2131 break;
2132 if ( (uFlags & ISCSIPDU_NO_REATTACH)
2133 || (rc != VERR_BROKEN_PIPE && rc != VERR_NET_CONNECTION_REFUSED))
2134 break;
2135 /* No point in reestablishing the connection for a logout */
2136 if (pImage->state == ISCSISTATE_IN_LOGOUT)
2137 break;
2138 RTThreadSleep(500);
2139 if (pImage->state != ISCSISTATE_IN_LOGIN)
2140 {
2141 /* Attempt to re-login when a connection fails, but only when not
2142 * currently logging in. */
2143 rc = iscsiAttach(pImage);
2144 if (RT_FAILURE(rc))
2145 break;
2146 }
2147 }
2148 return rc;
2149}
2150
2151
2152/**
2153 * Wait for an iSCSI response with a matching Initiator Target Tag. The response is
2154 * split into several segments, as requested by the caller-provided buffer specification.
2155 * Remember that the response can be split into several PDUs by the sender, so make
2156 * sure that all parts are collected and processed appropriately by the caller.
2157 *
2158 * @returns VBOX status
2159 * @param pImage The iSCSI connection state to be used.
2160 * @param paRes Pointer to array of iSCSI response sections.
2161 * @param cnRes Number of valid iSCSI response sections in the array.
2162 * @param fRecvFlags PDU receive flags.
2163 */
2164static int iscsiRecvPDU(PISCSIIMAGE pImage, uint32_t itt, PISCSIRES paRes, uint32_t cnRes,
2165 uint32_t fRecvFlags)
2166{
2167 int rc = VINF_SUCCESS;
2168 ISCSIRES aResBuf;
2169
2170 for (uint32_t i = 0; i < pImage->cISCSIRetries; i++)
2171 {
2172 aResBuf.pvSeg = pImage->pvRecvPDUBuf;
2173 aResBuf.cbSeg = pImage->cbRecvPDUBuf;
2174 rc = iscsiTransportRead(pImage, &aResBuf, 1);
2175 if (RT_FAILURE(rc))
2176 {
2177 if (rc == VERR_BROKEN_PIPE || rc == VERR_NET_CONNECTION_REFUSED)
2178 {
2179 /* No point in reestablishing the connection for a logout */
2180 if (pImage->state == ISCSISTATE_IN_LOGOUT)
2181 break;
2182 /* Connection broken while waiting for a response - wait a while and
2183 * try to restart by re-sending the original request (if any).
2184 * This also handles the connection reestablishment (login etc.). */
2185 RTThreadSleep(500);
2186 if ( pImage->state != ISCSISTATE_IN_LOGIN
2187 && !(fRecvFlags & ISCSIPDU_NO_REATTACH))
2188 {
2189 /* Attempt to re-login when a connection fails, but only when not
2190 * currently logging in. */
2191 rc = iscsiAttach(pImage);
2192 if (RT_FAILURE(rc))
2193 break;
2194
2195 if (pImage->paCurrReq != NULL)
2196 {
2197 rc = iscsiSendPDU(pImage, pImage->paCurrReq, pImage->cnCurrReq, ISCSIPDU_DEFAULT);
2198 if (RT_FAILURE(rc))
2199 break;
2200 }
2201 }
2202 }
2203 else
2204 {
2205 /* Signal other errors (VERR_BUFFER_OVERFLOW etc.) to the caller. */
2206 break;
2207 }
2208 }
2209 else
2210 {
2211 ISCSIOPCODE cmd;
2212 const uint32_t *pcvResSeg = (const uint32_t *)aResBuf.pvSeg;
2213
2214 /* Check whether the received PDU is valid, and update the internal state of
2215 * the iSCSI connection/session. */
2216 rc = iscsiValidatePDU(&aResBuf, 1);
2217 if (RT_FAILURE(rc))
2218 {
2219 iscsiDumpPacket(pImage, (PISCSIREQ)&aResBuf, 1, rc, false /* fRequest */);
2220 continue;
2221 }
2222 cmd = (ISCSIOPCODE)(RT_N2H_U32(pcvResSeg[0]) & ISCSIOP_MASK);
2223 switch (cmd)
2224 {
2225 case ISCSIOP_SCSI_RES:
2226 case ISCSIOP_SCSI_TASKMGMT_RES:
2227 case ISCSIOP_SCSI_DATA_IN:
2228 case ISCSIOP_R2T:
2229 case ISCSIOP_ASYN_MSG:
2230 case ISCSIOP_TEXT_RES:
2231 case ISCSIOP_LOGIN_RES:
2232 case ISCSIOP_LOGOUT_RES:
2233 case ISCSIOP_REJECT:
2234 case ISCSIOP_NOP_IN:
2235 if (serial_number_less(pImage->MaxCmdSN, RT_N2H_U32(pcvResSeg[8])))
2236 pImage->MaxCmdSN = RT_N2H_U32(pcvResSeg[8]);
2237 if (serial_number_less(pImage->ExpCmdSN, RT_N2H_U32(pcvResSeg[7])))
2238 pImage->ExpCmdSN = RT_N2H_U32(pcvResSeg[7]);
2239 break;
2240 default:
2241 rc = VERR_PARSE_ERROR;
2242 iscsiDumpPacket(pImage, (PISCSIREQ)&aResBuf, 1, rc, false /* fRequest */);
2243 }
2244 if (RT_FAILURE(rc))
2245 continue;
2246 if ( !pImage->FirstRecvPDU
2247 && (cmd != ISCSIOP_SCSI_DATA_IN || (RT_N2H_U32(pcvResSeg[0]) & ISCSI_STATUS_BIT))
2248 && ( cmd != ISCSIOP_LOGIN_RES
2249 || (ISCSILOGINSTATUSCLASS)((RT_N2H_U32(pcvResSeg[9]) >> 24) == ISCSI_LOGIN_STATUS_CLASS_SUCCESS)))
2250 {
2251 if (pImage->ExpStatSN == RT_N2H_U32(pcvResSeg[6]))
2252 {
2253 /* StatSN counter is not advanced on R2T and on a target SN update NOP-In. */
2254 if ( (cmd != ISCSIOP_R2T)
2255 && ((cmd != ISCSIOP_NOP_IN) || (RT_N2H_U32(pcvResSeg[4]) != ISCSI_TASK_TAG_RSVD)))
2256 pImage->ExpStatSN++;
2257 }
2258 else
2259 {
2260 rc = VERR_PARSE_ERROR;
2261 iscsiDumpPacket(pImage, (PISCSIREQ)&aResBuf, 1, rc, false /* fRequest */);
2262 continue;
2263 }
2264 }
2265 /* Finally check whether the received PDU matches what the caller wants. */
2266 if ( itt == pcvResSeg[4]
2267 && itt != ISCSI_TASK_TAG_RSVD)
2268 {
2269 /* Copy received PDU (one segment) to caller-provided buffers. */
2270 uint32_t j;
2271 size_t cbSeg;
2272 const uint8_t *pSrc;
2273
2274 pSrc = (const uint8_t *)aResBuf.pvSeg;
2275 cbSeg = aResBuf.cbSeg;
2276 for (j = 0; j < cnRes; j++)
2277 {
2278 if (cbSeg > paRes[j].cbSeg)
2279 {
2280 memcpy(paRes[j].pvSeg, pSrc, paRes[j].cbSeg);
2281 pSrc += paRes[j].cbSeg;
2282 cbSeg -= paRes[j].cbSeg;
2283 }
2284 else
2285 {
2286 memcpy(paRes[j].pvSeg, pSrc, cbSeg);
2287 paRes[j].cbSeg = cbSeg;
2288 cbSeg = 0;
2289 break;
2290 }
2291 }
2292 if (cbSeg != 0)
2293 {
2294 rc = VERR_BUFFER_OVERFLOW;
2295 break;
2296 }
2297 for (j++; j < cnRes; j++)
2298 paRes[j].cbSeg = 0;
2299 break;
2300 }
2301 else if ( cmd == ISCSIOP_NOP_IN
2302 && RT_N2H_U32(pcvResSeg[5]) != ISCSI_TASK_TAG_RSVD)
2303 {
2304 uint32_t cnISCSIReq;
2305 ISCSIREQ aISCSIReq[4];
2306 uint32_t aReqBHS[12];
2307
2308 aReqBHS[0] = RT_H2N_U32(ISCSI_IMMEDIATE_DELIVERY_BIT | ISCSI_FINAL_BIT | ISCSIOP_NOP_OUT);
2309 aReqBHS[1] = RT_H2N_U32(0); /* TotalAHSLength=0,DataSementLength=0 */
2310 aReqBHS[2] = pcvResSeg[2]; /* copy LUN from NOP-In */
2311 aReqBHS[3] = pcvResSeg[3]; /* copy LUN from NOP-In */
2312 aReqBHS[4] = RT_H2N_U32(ISCSI_TASK_TAG_RSVD); /* ITT, reply */
2313 aReqBHS[5] = pcvResSeg[5]; /* copy TTT from NOP-In */
2314 aReqBHS[6] = RT_H2N_U32(pImage->CmdSN);
2315 aReqBHS[7] = RT_H2N_U32(pImage->ExpStatSN);
2316 aReqBHS[8] = 0; /* reserved */
2317 aReqBHS[9] = 0; /* reserved */
2318 aReqBHS[10] = 0; /* reserved */
2319 aReqBHS[11] = 0; /* reserved */
2320
2321 cnISCSIReq = 0;
2322 aISCSIReq[cnISCSIReq].pcvSeg = aReqBHS;
2323 aISCSIReq[cnISCSIReq].cbSeg = sizeof(aReqBHS);
2324 cnISCSIReq++;
2325
2326 iscsiSendPDU(pImage, aISCSIReq, cnISCSIReq, ISCSIPDU_NO_REATTACH);
2327 /* Break if the caller wanted to process the NOP-in only. */
2328 if (itt == ISCSI_TASK_TAG_RSVD)
2329 break;
2330 }
2331 }
2332 }
2333
2334 LogFlowFunc(("returns rc=%Rrc\n", rc));
2335 return rc;
2336}
2337
2338
2339/**
2340 * Reset the PDU buffer
2341 *
2342 * @param pImage The iSCSI connection state to be used.
2343 */
2344static void iscsiRecvPDUReset(PISCSIIMAGE pImage)
2345{
2346 pImage->cbRecvPDUResidual = ISCSI_BHS_SIZE;
2347 pImage->fRecvPDUBHS = true;
2348 pImage->pbRecvPDUBufCur = (uint8_t *)pImage->pvRecvPDUBuf;
2349}
2350
2351static void iscsiPDUTxAdd(PISCSIIMAGE pImage, PISCSIPDUTX pIScsiPDUTx, bool fFront)
2352{
2353 if (!fFront)
2354 {
2355 /* Insert PDU at the tail of the list. */
2356 if (!pImage->pIScsiPDUTxHead)
2357 pImage->pIScsiPDUTxHead = pIScsiPDUTx;
2358 else
2359 pImage->pIScsiPDUTxTail->pNext = pIScsiPDUTx;
2360 pImage->pIScsiPDUTxTail = pIScsiPDUTx;
2361 }
2362 else
2363 {
2364 /* Insert PDU at the beginning of the list. */
2365 pIScsiPDUTx->pNext = pImage->pIScsiPDUTxHead;
2366 pImage->pIScsiPDUTxHead = pIScsiPDUTx;
2367 if (!pImage->pIScsiPDUTxTail)
2368 pImage->pIScsiPDUTxTail = pIScsiPDUTx;
2369 }
2370}
2371
2372/**
2373 * Receives a PDU in a non blocking way.
2374 *
2375 * @returns VBOX status code.
2376 * @param pImage The iSCSI connection state to be used.
2377 */
2378static int iscsiRecvPDUAsync(PISCSIIMAGE pImage)
2379{
2380 size_t cbActuallyRead = 0;
2381 int rc = VINF_SUCCESS;
2382
2383 LogFlowFunc(("pImage=%#p\n", pImage));
2384
2385 /* Check if we are in the middle of a PDU receive. */
2386 if (pImage->cbRecvPDUResidual == 0)
2387 {
2388 /*
2389 * We are receiving a new PDU, don't read more than the BHS initially
2390 * until we know the real size of the PDU.
2391 */
2392 iscsiRecvPDUReset(pImage);
2393 LogFlow(("Receiving new PDU\n"));
2394 }
2395
2396 rc = pImage->pIfNet->pfnReadNB(pImage->Socket, pImage->pbRecvPDUBufCur,
2397 pImage->cbRecvPDUResidual, &cbActuallyRead);
2398 if (RT_SUCCESS(rc) && cbActuallyRead == 0)
2399 rc = VERR_BROKEN_PIPE;
2400
2401 if (RT_SUCCESS(rc))
2402 {
2403 LogFlow(("Received %zu bytes\n", cbActuallyRead));
2404 pImage->cbRecvPDUResidual -= cbActuallyRead;
2405 pImage->pbRecvPDUBufCur += cbActuallyRead;
2406
2407 /* Check if we received everything we wanted. */
2408 if ( !pImage->cbRecvPDUResidual
2409 && pImage->fRecvPDUBHS)
2410 {
2411 size_t cbAHSLength, cbDataLength;
2412
2413 /* If we were reading the BHS first get the actual PDU size now. */
2414 uint32_t word1 = RT_N2H_U32(((uint32_t *)(pImage->pvRecvPDUBuf))[1]);
2415 cbAHSLength = (word1 & 0xff000000) >> 24;
2416 cbAHSLength = ((cbAHSLength - 1) | 3) + 1; /* Add padding. */
2417 cbDataLength = word1 & 0x00ffffff;
2418 cbDataLength = ((cbDataLength - 1) | 3) + 1; /* Add padding. */
2419 pImage->cbRecvPDUResidual = cbAHSLength + cbDataLength;
2420 pImage->fRecvPDUBHS = false; /* Start receiving the rest of the PDU. */
2421 }
2422
2423 if (!pImage->cbRecvPDUResidual)
2424 {
2425 /* We received the complete PDU with or without any payload now. */
2426 LogFlow(("Received complete PDU\n"));
2427 ISCSIRES aResBuf;
2428 aResBuf.pvSeg = pImage->pvRecvPDUBuf;
2429 aResBuf.cbSeg = pImage->cbRecvPDUBuf;
2430 rc = iscsiRecvPDUProcess(pImage, &aResBuf, 1);
2431 }
2432 }
2433 else
2434 LogFlowFunc(("Reading from the socket returned with rc=%Rrc\n", rc));
2435
2436 return rc;
2437}
2438
2439static int iscsiSendPDUAsync(PISCSIIMAGE pImage)
2440{
2441 size_t cbSent = 0;
2442 int rc = VINF_SUCCESS;
2443
2444 LogFlowFunc(("pImage=%#p\n", pImage));
2445
2446 do
2447 {
2448 /*
2449 * If there is no PDU active, get the first one from the list.
2450 * Check that we are allowed to transfer the PDU by comparing the
2451 * command sequence number and the maximum sequence number allowed by the target.
2452 */
2453 if (!pImage->pIScsiPDUTxCur)
2454 {
2455 if ( !pImage->pIScsiPDUTxHead
2456 || serial_number_greater(pImage->pIScsiPDUTxHead->CmdSN, pImage->MaxCmdSN))
2457 break;
2458
2459 pImage->pIScsiPDUTxCur = pImage->pIScsiPDUTxHead;
2460 pImage->pIScsiPDUTxHead = pImage->pIScsiPDUTxCur->pNext;
2461 if (!pImage->pIScsiPDUTxHead)
2462 pImage->pIScsiPDUTxTail = NULL;
2463 }
2464
2465 /* Send as much as we can. */
2466 rc = pImage->pIfNet->pfnSgWriteNB(pImage->Socket, &pImage->pIScsiPDUTxCur->SgBuf, &cbSent);
2467 LogFlow(("SgWriteNB returned rc=%Rrc cbSent=%zu\n", rc, cbSent));
2468 if (RT_SUCCESS(rc))
2469 {
2470 LogFlow(("Sent %zu bytes for PDU %#p\n", cbSent, pImage->pIScsiPDUTxCur));
2471 pImage->pIScsiPDUTxCur->cbSgLeft -= cbSent;
2472 RTSgBufAdvance(&pImage->pIScsiPDUTxCur->SgBuf, cbSent);
2473 if (!pImage->pIScsiPDUTxCur->cbSgLeft)
2474 {
2475 /* PDU completed, free it and place the command on the waiting for response list. */
2476 if (pImage->pIScsiPDUTxCur->pIScsiCmd)
2477 {
2478 LogFlow(("Sent complete PDU, placing on waiting list\n"));
2479 iscsiCmdInsert(pImage, pImage->pIScsiPDUTxCur->pIScsiCmd);
2480 }
2481 RTMemFree(pImage->pIScsiPDUTxCur);
2482 pImage->pIScsiPDUTxCur = NULL;
2483 }
2484 }
2485 } while ( RT_SUCCESS(rc)
2486 && !pImage->pIScsiPDUTxCur);
2487
2488 if (rc == VERR_TRY_AGAIN)
2489 rc = VINF_SUCCESS;
2490
2491 /* Add the write poll flag if we still have something to send, clear it otherwise. */
2492 if (pImage->pIScsiPDUTxCur)
2493 pImage->fPollEvents |= VD_INTERFACETCPNET_EVT_WRITE;
2494 else
2495 pImage->fPollEvents &= ~VD_INTERFACETCPNET_EVT_WRITE;
2496
2497 LogFlowFunc(("rc=%Rrc pIScsiPDUTxCur=%#p\n", rc, pImage->pIScsiPDUTxCur));
2498 return rc;
2499}
2500
2501/**
2502 * Process a received PDU.
2503 *
2504 * @return VBOX status code.
2505 * @param pImage The iSCSI connection state to be used.
2506 * @param paRes Pointer to the array of iSCSI response sections.
2507 * @param cnRes Number of valid iSCSI response sections in the array.
2508 */
2509static int iscsiRecvPDUProcess(PISCSIIMAGE pImage, PISCSIRES paRes, uint32_t cnRes)
2510{
2511 int rc = VINF_SUCCESS;
2512
2513 LogFlowFunc(("pImage=%#p paRes=%#p cnRes=%u\n", pImage, paRes, cnRes));
2514
2515 /* Validate the PDU first. */
2516 rc = iscsiValidatePDU(paRes, cnRes);
2517 if (RT_SUCCESS(rc))
2518 {
2519 ISCSIOPCODE cmd;
2520 const uint32_t *pcvResSeg = (const uint32_t *)paRes[0].pvSeg;
2521
2522 Assert(paRes[0].cbSeg > 9 * sizeof(uint32_t));
2523
2524 do
2525 {
2526 cmd = (ISCSIOPCODE)(RT_N2H_U32(pcvResSeg[0]) & ISCSIOP_MASK);
2527 switch (cmd)
2528 {
2529 case ISCSIOP_SCSI_RES:
2530 case ISCSIOP_SCSI_TASKMGMT_RES:
2531 case ISCSIOP_SCSI_DATA_IN:
2532 case ISCSIOP_R2T:
2533 case ISCSIOP_ASYN_MSG:
2534 case ISCSIOP_TEXT_RES:
2535 case ISCSIOP_LOGIN_RES:
2536 case ISCSIOP_LOGOUT_RES:
2537 case ISCSIOP_REJECT:
2538 case ISCSIOP_NOP_IN:
2539 if (serial_number_less(pImage->MaxCmdSN, RT_N2H_U32(pcvResSeg[8])))
2540 pImage->MaxCmdSN = RT_N2H_U32(pcvResSeg[8]);
2541 if (serial_number_less(pImage->ExpCmdSN, RT_N2H_U32(pcvResSeg[7])))
2542 pImage->ExpCmdSN = RT_N2H_U32(pcvResSeg[7]);
2543 break;
2544 default:
2545 rc = VERR_PARSE_ERROR;
2546 iscsiDumpPacket(pImage, (PISCSIREQ)paRes, cnRes, rc, false /* fRequest */);
2547 }
2548
2549 if (RT_FAILURE(rc))
2550 break;
2551
2552 if ( !pImage->FirstRecvPDU
2553 && (cmd != ISCSIOP_SCSI_DATA_IN || (RT_N2H_U32(pcvResSeg[0]) & ISCSI_STATUS_BIT)))
2554 {
2555 if (pImage->ExpStatSN == RT_N2H_U32(pcvResSeg[6]))
2556 {
2557 /* StatSN counter is not advanced on R2T and on a target SN update NOP-In. */
2558 if ( (cmd != ISCSIOP_R2T)
2559 && ((cmd != ISCSIOP_NOP_IN) || (RT_N2H_U32(pcvResSeg[4]) != ISCSI_TASK_TAG_RSVD)))
2560 pImage->ExpStatSN++;
2561 }
2562 else
2563 {
2564 rc = VERR_PARSE_ERROR;
2565 iscsiDumpPacket(pImage, (PISCSIREQ)paRes, cnRes, rc, false /* fRequest */);
2566 break;
2567 }
2568 }
2569
2570 if (pcvResSeg[4] != ISCSI_TASK_TAG_RSVD)
2571 {
2572 /*
2573 * This is a response from the target for a request from the initiator.
2574 * Get the request and update its state.
2575 */
2576 rc = iscsiRecvPDUUpdateRequest(pImage, paRes, cnRes);
2577 /* Try to send more PDUs now that we updated the MaxCmdSN field */
2578 if ( RT_SUCCESS(rc)
2579 && !pImage->pIScsiPDUTxCur)
2580 rc = iscsiSendPDUAsync(pImage);
2581 }
2582 else
2583 {
2584 /* This is a target initiated request (we handle only NOP-In request at the moment). */
2585 if ( cmd == ISCSIOP_NOP_IN
2586 && RT_N2H_U32(pcvResSeg[5]) != ISCSI_TASK_TAG_RSVD)
2587 {
2588 PISCSIPDUTX pIScsiPDUTx;
2589 uint32_t cnISCSIReq;
2590 uint32_t *paReqBHS;
2591
2592 LogFlowFunc(("Sending NOP-Out\n"));
2593
2594 /* Allocate a new PDU initialize it and put onto the waiting list. */
2595 pIScsiPDUTx = (PISCSIPDUTX)RTMemAllocZ(sizeof(ISCSIPDUTX));
2596 if (!pIScsiPDUTx)
2597 {
2598 rc = VERR_NO_MEMORY;
2599 break;
2600 }
2601 paReqBHS = &pIScsiPDUTx->aBHS[0];
2602 paReqBHS[0] = RT_H2N_U32(ISCSI_IMMEDIATE_DELIVERY_BIT | ISCSI_FINAL_BIT | ISCSIOP_NOP_OUT);
2603 paReqBHS[1] = RT_H2N_U32(0); /* TotalAHSLength=0,DataSementLength=0 */
2604 paReqBHS[2] = pcvResSeg[2]; /* copy LUN from NOP-In */
2605 paReqBHS[3] = pcvResSeg[3]; /* copy LUN from NOP-In */
2606 paReqBHS[4] = RT_H2N_U32(ISCSI_TASK_TAG_RSVD); /* ITT, reply */
2607 paReqBHS[5] = pcvResSeg[5]; /* copy TTT from NOP-In */
2608 paReqBHS[6] = RT_H2N_U32(pImage->CmdSN);
2609 paReqBHS[7] = RT_H2N_U32(pImage->ExpStatSN);
2610 paReqBHS[8] = 0; /* reserved */
2611 paReqBHS[9] = 0; /* reserved */
2612 paReqBHS[10] = 0; /* reserved */
2613 paReqBHS[11] = 0; /* reserved */
2614
2615 cnISCSIReq = 0;
2616 pIScsiPDUTx->aISCSIReq[cnISCSIReq].pvSeg = paReqBHS;
2617 pIScsiPDUTx->aISCSIReq[cnISCSIReq].cbSeg = sizeof(pIScsiPDUTx->aBHS);
2618 cnISCSIReq++;
2619 pIScsiPDUTx->cbSgLeft = sizeof(pIScsiPDUTx->aBHS);
2620 RTSgBufInit(&pIScsiPDUTx->SgBuf, pIScsiPDUTx->aISCSIReq, cnISCSIReq);
2621
2622 /*
2623 * Link the PDU to the list.
2624 * Insert at the front of the list to send the response as soon as possible
2625 * to avoid frequent reconnects for a slow connection when there are many PDUs
2626 * waiting.
2627 */
2628 iscsiPDUTxAdd(pImage, pIScsiPDUTx, true /* fFront */);
2629
2630 /* Start transfer of a PDU if there is no one active at the moment. */
2631 if (!pImage->pIScsiPDUTxCur)
2632 rc = iscsiSendPDUAsync(pImage);
2633 }
2634 }
2635 } while (0);
2636 }
2637 else
2638 iscsiDumpPacket(pImage, (PISCSIREQ)paRes, cnRes, rc, false /* fRequest */);
2639
2640 return rc;
2641}
2642
2643/**
2644 * Check the static (not dependent on the connection/session state) validity of an iSCSI response PDU.
2645 *
2646 * @returns VBOX status
2647 * @param paRes Pointer to array of iSCSI response sections.
2648 * @param cnRes Number of valid iSCSI response sections in the array.
2649 */
2650static int iscsiValidatePDU(PISCSIRES paRes, uint32_t cnRes)
2651{
2652 RT_NOREF1(cnRes);
2653 const uint32_t *pcrgResBHS;
2654 uint32_t hw0;
2655 Assert(cnRes >= 1);
2656 Assert(paRes[0].cbSeg >= ISCSI_BHS_SIZE);
2657
2658 LogFlowFunc(("paRes=%#p cnRes=%u\n", paRes, cnRes));
2659
2660 pcrgResBHS = (const uint32_t *)(paRes[0].pvSeg);
2661 hw0 = RT_N2H_U32(pcrgResBHS[0]);
2662 switch (hw0 & ISCSIOP_MASK)
2663 {
2664 case ISCSIOP_NOP_IN:
2665 /* NOP-In responses must not be split into several PDUs nor it may contain
2666 * ping data for target-initiated pings nor may both task tags be valid task tags. */
2667 if ( (hw0 & ISCSI_FINAL_BIT) == 0
2668 || ( RT_N2H_U32(pcrgResBHS[4]) == ISCSI_TASK_TAG_RSVD
2669 && RT_N2H_U32(pcrgResBHS[1]) != 0)
2670 || ( RT_N2H_U32(pcrgResBHS[4]) != ISCSI_TASK_TAG_RSVD
2671 && RT_N2H_U32(pcrgResBHS[5]) != ISCSI_TASK_TAG_RSVD))
2672 return VERR_PARSE_ERROR;
2673 break;
2674 case ISCSIOP_SCSI_RES:
2675 /* SCSI responses must not be split into several PDUs nor must the residual
2676 * bits be contradicting each other nor may the residual bits be set for PDUs
2677 * containing anything else but a completed command response. Underflow
2678 * is no reason for declaring a PDU as invalid, as the target may choose
2679 * to return less data than we assume to get. */
2680 if ( (hw0 & ISCSI_FINAL_BIT) == 0
2681 || ((hw0 & ISCSI_BI_READ_RESIDUAL_OVFL_BIT) && (hw0 & ISCSI_BI_READ_RESIDUAL_UNFL_BIT))
2682 || ((hw0 & ISCSI_RESIDUAL_OVFL_BIT) && (hw0 & ISCSI_RESIDUAL_UNFL_BIT))
2683 || ( ((hw0 & ISCSI_SCSI_RESPONSE_MASK) == 0)
2684 && ((hw0 & ISCSI_SCSI_STATUS_MASK) == SCSI_STATUS_OK)
2685 && (hw0 & ( ISCSI_BI_READ_RESIDUAL_OVFL_BIT | ISCSI_BI_READ_RESIDUAL_UNFL_BIT
2686 | ISCSI_RESIDUAL_OVFL_BIT))))
2687 return VERR_PARSE_ERROR;
2688 else
2689 LogFlowFunc(("good SCSI response, first word %#08x\n", RT_N2H_U32(pcrgResBHS[0])));
2690 break;
2691 case ISCSIOP_LOGIN_RES:
2692 /* Login responses must not contain contradicting transit and continue bits. */
2693 if ((hw0 & ISCSI_CONTINUE_BIT) && ((hw0 & ISCSI_TRANSIT_BIT) != 0))
2694 return VERR_PARSE_ERROR;
2695 break;
2696 case ISCSIOP_TEXT_RES:
2697 /* Text responses must not contain contradicting final and continue bits nor
2698 * may the final bit be set for PDUs containing a target transfer tag other than
2699 * the reserved transfer tag (and vice versa). */
2700 if ( (((hw0 & ISCSI_CONTINUE_BIT) && (hw0 & ISCSI_FINAL_BIT) != 0))
2701 || (((hw0 & ISCSI_FINAL_BIT) && (RT_N2H_U32(pcrgResBHS[5]) != ISCSI_TASK_TAG_RSVD)))
2702 || (((hw0 & ISCSI_FINAL_BIT) == 0) && (RT_N2H_U32(pcrgResBHS[5]) == ISCSI_TASK_TAG_RSVD)))
2703 return VERR_PARSE_ERROR;
2704 break;
2705 case ISCSIOP_SCSI_DATA_IN:
2706 /* SCSI Data-in responses must not contain contradicting residual bits when
2707 * status bit is set. */
2708 if ((hw0 & ISCSI_STATUS_BIT) && (hw0 & ISCSI_RESIDUAL_OVFL_BIT) && (hw0 & ISCSI_RESIDUAL_UNFL_BIT))
2709 return VERR_PARSE_ERROR;
2710 break;
2711 case ISCSIOP_LOGOUT_RES:
2712 /* Logout responses must not have the final bit unset and may not contain any
2713 * data or additional header segments. */
2714 if ( ((hw0 & ISCSI_FINAL_BIT) == 0)
2715 || (RT_N2H_U32(pcrgResBHS[1]) != 0))
2716 return VERR_PARSE_ERROR;
2717 break;
2718 case ISCSIOP_ASYN_MSG:
2719 /* Asynchronous Messages must not have the final bit unset and may not contain
2720 * an initiator task tag. */
2721 if ( ((hw0 & ISCSI_FINAL_BIT) == 0)
2722 || (RT_N2H_U32(pcrgResBHS[4]) != ISCSI_TASK_TAG_RSVD))
2723 return VERR_PARSE_ERROR;
2724 break;
2725 case ISCSIOP_SCSI_TASKMGMT_RES:
2726 case ISCSIOP_R2T:
2727 case ISCSIOP_REJECT:
2728 default:
2729 /* Do some logging, ignore PDU. */
2730 LogFlowFunc(("ignore unhandled PDU, first word %#08x\n", RT_N2H_U32(pcrgResBHS[0])));
2731 return VERR_PARSE_ERROR;
2732 }
2733 /* A target must not send PDUs with MaxCmdSN less than ExpCmdSN-1. */
2734
2735 if (serial_number_less(RT_N2H_U32(pcrgResBHS[8]), RT_N2H_U32(pcrgResBHS[7])-1))
2736 return VERR_PARSE_ERROR;
2737
2738 return VINF_SUCCESS;
2739}
2740
2741
2742/**
2743 * Prepares a PDU to transfer for the given command and adds it to the list.
2744 */
2745static int iscsiPDUTxPrepare(PISCSIIMAGE pImage, PISCSICMD pIScsiCmd)
2746{
2747 int rc = VINF_SUCCESS;
2748 uint32_t *paReqBHS;
2749 size_t cbData = 0;
2750 size_t cbSegs = 0;
2751 PSCSIREQ pScsiReq;
2752 PISCSIPDUTX pIScsiPDU = NULL;
2753
2754 LogFlowFunc(("pImage=%#p pIScsiCmd=%#p\n", pImage, pIScsiCmd));
2755
2756 Assert(pIScsiCmd->enmCmdType == ISCSICMDTYPE_REQ);
2757
2758 pIScsiCmd->Itt = iscsiNewITT(pImage);
2759 pScsiReq = pIScsiCmd->CmdType.ScsiReq.pScsiReq;
2760
2761 if (pScsiReq->cT2ISegs)
2762 RTSgBufInit(&pScsiReq->SgBufT2I, pScsiReq->paT2ISegs, pScsiReq->cT2ISegs);
2763
2764 /*
2765 * Allocate twice as much entries as required for padding (worst case).
2766 * The additional segment is for the BHS.
2767 */
2768 size_t cI2TSegs = 2*(pScsiReq->cI2TSegs + 1);
2769 pIScsiPDU = (PISCSIPDUTX)RTMemAllocZ(RT_OFFSETOF(ISCSIPDUTX, aISCSIReq[cI2TSegs]));
2770 if (!pIScsiPDU)
2771 return VERR_NO_MEMORY;
2772
2773 pIScsiPDU->pIScsiCmd = pIScsiCmd;
2774
2775 if (pScsiReq->enmXfer == SCSIXFER_FROM_TARGET)
2776 cbData = (uint32_t)pScsiReq->cbT2IData;
2777 else
2778 cbData = (uint32_t)pScsiReq->cbI2TData;
2779
2780 paReqBHS = pIScsiPDU->aBHS;
2781
2782 /* Setup the BHS. */
2783 paReqBHS[0] = RT_H2N_U32( ISCSI_FINAL_BIT | ISCSI_TASK_ATTR_SIMPLE | ISCSIOP_SCSI_CMD
2784 | (pScsiReq->enmXfer << 21)); /* I=0,F=1,Attr=Simple */
2785 paReqBHS[1] = RT_H2N_U32(0x00000000 | ((uint32_t)pScsiReq->cbI2TData & 0xffffff)); /* TotalAHSLength=0 */
2786 paReqBHS[2] = RT_H2N_U32(pImage->LUN >> 32);
2787 paReqBHS[3] = RT_H2N_U32(pImage->LUN & 0xffffffff);
2788 paReqBHS[4] = pIScsiCmd->Itt;
2789 paReqBHS[5] = RT_H2N_U32((uint32_t)cbData); Assert((uint32_t)cbData == cbData);
2790 paReqBHS[6] = RT_H2N_U32(pImage->CmdSN);
2791 paReqBHS[7] = RT_H2N_U32(pImage->ExpStatSN);
2792 memcpy(paReqBHS + 8, pScsiReq->abCDB, pScsiReq->cbCDB);
2793
2794 pIScsiPDU->CmdSN = pImage->CmdSN;
2795 pImage->CmdSN++;
2796
2797 /* Setup the S/G buffers. */
2798 uint32_t cnISCSIReq = 0;
2799 pIScsiPDU->aISCSIReq[cnISCSIReq].cbSeg = sizeof(pIScsiPDU->aBHS);
2800 pIScsiPDU->aISCSIReq[cnISCSIReq].pvSeg = pIScsiPDU->aBHS;
2801 cnISCSIReq++;
2802 cbSegs = sizeof(pIScsiPDU->aBHS);
2803 /* Padding is not necessary for the BHS. */
2804
2805 if (pScsiReq->cbI2TData)
2806 {
2807 for (unsigned cSeg = 0; cSeg < pScsiReq->cI2TSegs; cSeg++)
2808 {
2809 Assert(cnISCSIReq < cI2TSegs);
2810 pIScsiPDU->aISCSIReq[cnISCSIReq].cbSeg = pScsiReq->paI2TSegs[cSeg].cbSeg;
2811 pIScsiPDU->aISCSIReq[cnISCSIReq].pvSeg = pScsiReq->paI2TSegs[cSeg].pvSeg;
2812 cbSegs += pScsiReq->paI2TSegs[cSeg].cbSeg;
2813 cnISCSIReq++;
2814
2815 /* Add padding if necessary. */
2816 if (pScsiReq->paI2TSegs[cSeg].cbSeg & 3)
2817 {
2818 Assert(cnISCSIReq < cI2TSegs);
2819 pIScsiPDU->aISCSIReq[cnISCSIReq].pvSeg = &pImage->aPadding[0];
2820 pIScsiPDU->aISCSIReq[cnISCSIReq].cbSeg = 4 - (pScsiReq->paI2TSegs[cSeg].cbSeg & 3);
2821 cbSegs += pIScsiPDU->aISCSIReq[cnISCSIReq].cbSeg;
2822 cnISCSIReq++;
2823 }
2824 }
2825 }
2826
2827 pIScsiPDU->cISCSIReq = cnISCSIReq;
2828 pIScsiPDU->cbSgLeft = cbSegs;
2829 RTSgBufInit(&pIScsiPDU->SgBuf, pIScsiPDU->aISCSIReq, cnISCSIReq);
2830
2831 /* Link the PDU to the list. */
2832 iscsiPDUTxAdd(pImage, pIScsiPDU, false /* fFront */);
2833
2834 /* Start transfer of a PDU if there is no one active at the moment. */
2835 if (!pImage->pIScsiPDUTxCur)
2836 rc = iscsiSendPDUAsync(pImage);
2837
2838 return rc;
2839}
2840
2841
2842/**
2843 * Updates the state of a request from the PDU we received.
2844 *
2845 * @return VBox status code.
2846 * @param pImage iSCSI connection state to use.
2847 * @param paRes Pointer to array of iSCSI response sections.
2848 * @param cnRes Number of valid iSCSI response sections in the array.
2849 */
2850static int iscsiRecvPDUUpdateRequest(PISCSIIMAGE pImage, PISCSIRES paRes, uint32_t cnRes)
2851{
2852 int rc = VINF_SUCCESS;
2853 PISCSICMD pIScsiCmd;
2854 uint32_t *paResBHS;
2855
2856 LogFlowFunc(("pImage=%#p paRes=%#p cnRes=%u\n", pImage, paRes, cnRes));
2857
2858 Assert(cnRes == 1);
2859 Assert(paRes[0].cbSeg >= ISCSI_BHS_SIZE);
2860
2861 paResBHS = (uint32_t *)paRes[0].pvSeg;
2862
2863 pIScsiCmd = iscsiCmdGetFromItt(pImage, paResBHS[4]);
2864
2865 if (pIScsiCmd)
2866 {
2867 bool final = false;
2868 PSCSIREQ pScsiReq;
2869
2870 LogFlow(("Found SCSI command %#p for Itt=%#u\n", pIScsiCmd, paResBHS[4]));
2871
2872 Assert(pIScsiCmd->enmCmdType == ISCSICMDTYPE_REQ);
2873 pScsiReq = pIScsiCmd->CmdType.ScsiReq.pScsiReq;
2874
2875 final = !!(RT_N2H_U32(paResBHS[0]) & ISCSI_FINAL_BIT);
2876 ISCSIOPCODE cmd = (ISCSIOPCODE)(RT_N2H_U32(paResBHS[0]) & ISCSIOP_MASK);
2877 if (cmd == ISCSIOP_SCSI_RES)
2878 {
2879 /* This is the final PDU which delivers the status (and may be omitted if
2880 * the last Data-In PDU included successful completion status). Note
2881 * that ExpStatSN has been bumped already in iscsiRecvPDU. */
2882 if (!final || ((RT_N2H_U32(paResBHS[0]) & 0x0000ff00) != 0) || (RT_N2H_U32(paResBHS[6]) != pImage->ExpStatSN - 1))
2883 {
2884 /* SCSI Response in the wrong place or with a (target) failure. */
2885 LogFlow(("Wrong ExpStatSN value in PDU\n"));
2886 rc = VERR_PARSE_ERROR;
2887 }
2888 else
2889 {
2890 pScsiReq->status = RT_N2H_U32(paResBHS[0]) & 0x000000ff;
2891 size_t cbData = RT_N2H_U32(paResBHS[1]) & 0x00ffffff;
2892 void *pvSense = (uint8_t *)paRes[0].pvSeg + ISCSI_BHS_SIZE;
2893
2894 if (cbData >= 2)
2895 {
2896 uint32_t cbStat = RT_N2H_U32(((uint32_t *)pvSense)[0]) >> 16;
2897 if (cbStat + 2 > cbData)
2898 {
2899 rc = VERR_BUFFER_OVERFLOW;
2900 }
2901 else
2902 {
2903 /* Truncate sense data if it doesn't fit into the buffer. */
2904 pScsiReq->cbSense = RT_MIN(cbStat, pScsiReq->cbSense);
2905 memcpy(pScsiReq->abSense, (uint8_t *)pvSense + 2,
2906 RT_MIN(paRes[0].cbSeg - ISCSI_BHS_SIZE - 2, pScsiReq->cbSense));
2907 }
2908 }
2909 else if (cbData == 1)
2910 rc = VERR_PARSE_ERROR;
2911 else
2912 pScsiReq->cbSense = 0;
2913 }
2914 iscsiCmdComplete(pImage, pIScsiCmd, rc);
2915 }
2916 else if (cmd == ISCSIOP_SCSI_DATA_IN)
2917 {
2918 /* A Data-In PDU carries some data that needs to be added to the received
2919 * data in response to the command. There may be both partial and complete
2920 * Data-In PDUs, so collect data until the status is included or the status
2921 * is sent in a separate SCSI Result frame (see above). */
2922 size_t cbData = RT_N2H_U32(paResBHS[1]) & 0x00ffffff;
2923 void *pvData = (uint8_t *)paRes[0].pvSeg + ISCSI_BHS_SIZE;
2924
2925 if (final && cbData > pScsiReq->cbT2IData)
2926 {
2927 /* The received PDU is bigger than what we requested.
2928 * Must not happen under normal circumstances and is a target error. */
2929 rc = VERR_BUFFER_OVERFLOW;
2930 }
2931 else
2932 {
2933 /* Copy data from the received PDU into the T2I segments. */
2934 size_t cbCopied = RTSgBufCopyFromBuf(&pScsiReq->SgBufT2I, pvData, cbData);
2935 Assert(cbCopied == cbData); NOREF(cbCopied);
2936
2937 if (final && (RT_N2H_U32(paResBHS[0]) & ISCSI_STATUS_BIT) != 0)
2938 {
2939 pScsiReq->status = RT_N2H_U32(paResBHS[0]) & 0x000000ff;
2940 pScsiReq->cbSense = 0;
2941 iscsiCmdComplete(pImage, pIScsiCmd, VINF_SUCCESS);
2942 }
2943 }
2944 }
2945 else
2946 rc = VERR_PARSE_ERROR;
2947 }
2948
2949 /* Log any errors here but ignore the PDU. */
2950 if (RT_FAILURE(rc))
2951 {
2952 LogRel(("iSCSI: Received malformed PDU from target %s (rc=%Rrc), ignoring\n", pImage->pszTargetName, rc));
2953 iscsiDumpPacket(pImage, (PISCSIREQ)paRes, cnRes, rc, false /* fRequest */);
2954 rc = VINF_SUCCESS;
2955 }
2956
2957 return rc;
2958}
2959
2960/**
2961 * Appends a key-value pair to the buffer. Normal ASCII strings (cbValue == 0) and large binary values
2962 * of a given length (cbValue > 0) are directly supported. Other value types must be converted to ASCII
2963 * by the caller. Strings must be in UTF-8 encoding.
2964 *
2965 * @returns VBOX status
2966 * @param pbBuf Pointer to the key-value buffer.
2967 * @param cbBuf Length of the key-value buffer.
2968 * @param pcbBufCurr Currently used portion of the key-value buffer.
2969 * @param pszKey Pointer to a string containing the key.
2970 * @param pszValue Pointer to either a string containing the value or to a large binary value.
2971 * @param cbValue Length of the binary value if applicable.
2972 */
2973static int iscsiTextAddKeyValue(uint8_t *pbBuf, size_t cbBuf, size_t *pcbBufCurr, const char *pcszKey,
2974 const char *pcszValue, size_t cbValue)
2975{
2976 size_t cbBufTmp = *pcbBufCurr;
2977 size_t cbKey = strlen(pcszKey);
2978 size_t cbValueEnc;
2979 uint8_t *pbCurr;
2980
2981 if (cbValue == 0)
2982 cbValueEnc = strlen(pcszValue);
2983 else
2984 cbValueEnc = cbValue * 2 + 2; /* 2 hex bytes per byte, 2 bytes prefix */
2985
2986 if (cbBuf < cbBufTmp + cbKey + 1 + cbValueEnc + 1)
2987 {
2988 /* Buffer would overflow, signal error. */
2989 return VERR_BUFFER_OVERFLOW;
2990 }
2991
2992 /*
2993 * Append a key=value pair (zero terminated string) to the end of the buffer.
2994 */
2995 pbCurr = pbBuf + cbBufTmp;
2996 memcpy(pbCurr, pcszKey, cbKey);
2997 pbCurr += cbKey;
2998 *pbCurr++ = '=';
2999 if (cbValue == 0)
3000 {
3001 memcpy(pbCurr, pcszValue, cbValueEnc);
3002 pbCurr += cbValueEnc;
3003 }
3004 else
3005 {
3006 *pbCurr++ = '0';
3007 *pbCurr++ = 'x';
3008 for (uint32_t i = 0; i < cbValue; i++)
3009 {
3010 uint8_t b;
3011 b = pcszValue[i];
3012 *pbCurr++ = NUM_2_HEX(b >> 4);
3013 *pbCurr++ = NUM_2_HEX(b & 0xf);
3014 }
3015 }
3016 *pbCurr = '\0';
3017 *pcbBufCurr = cbBufTmp + cbKey + 1 + cbValueEnc + 1;
3018
3019 return VINF_SUCCESS;
3020}
3021
3022
3023/**
3024 * Retrieve the value for a given key from the key=value buffer.
3025 *
3026 * @returns VBox status code.
3027 * @param pbBuf Buffer containing key=value pairs.
3028 * @param cbBuf Length of buffer with key=value pairs.
3029 * @param pszKey Pointer to key for which to retrieve the value.
3030 * @param ppszValue Pointer to value string pointer.
3031 */
3032static int iscsiTextGetKeyValue(const uint8_t *pbBuf, size_t cbBuf, const char *pcszKey, const char **ppcszValue)
3033{
3034 size_t cbKey = strlen(pcszKey);
3035
3036 while (cbBuf != 0)
3037 {
3038 size_t cbKeyValNull = strlen((const char *)pbBuf) + 1;
3039
3040 if (strncmp(pcszKey, (const char *)pbBuf, cbKey) == 0 && pbBuf[cbKey] == '=')
3041 {
3042 *ppcszValue = (const char *)(pbBuf + cbKey + 1);
3043 return VINF_SUCCESS;
3044 }
3045 pbBuf += cbKeyValNull;
3046 cbBuf -= cbKeyValNull;
3047 }
3048 return VERR_INVALID_NAME;
3049}
3050
3051
3052/**
3053 * Convert a long-binary value from a value string to the binary representation.
3054 *
3055 * @returns VBOX status
3056 * @param pszValue Pointer to a string containing the textual value representation.
3057 * @param pbValue Pointer to the value buffer for the binary value.
3058 * @param pcbValue In: length of value buffer, out: actual length of binary value.
3059 */
3060static int iscsiStrToBinary(const char *pcszValue, uint8_t *pbValue, size_t *pcbValue)
3061{
3062 size_t cbValue = *pcbValue;
3063 char c1, c2, c3, c4;
3064 Assert(cbValue >= 1);
3065
3066 if (strlen(pcszValue) < 3)
3067 return VERR_PARSE_ERROR;
3068 if (*pcszValue++ != '0')
3069 return VERR_PARSE_ERROR;
3070 switch (*pcszValue++)
3071 {
3072 case 'x':
3073 case 'X':
3074 if (strlen(pcszValue) & 1)
3075 {
3076 c1 = *pcszValue++;
3077 *pbValue++ = HEX_2_NUM(c1);
3078 cbValue--;
3079 }
3080 while (*pcszValue != '\0')
3081 {
3082 if (cbValue == 0)
3083 return VERR_BUFFER_OVERFLOW;
3084 c1 = *pcszValue++;
3085 if ((c1 < '0' || c1 > '9') && (c1 < 'a' || c1 > 'f') && (c1 < 'A' || c1 > 'F'))
3086 return VERR_PARSE_ERROR;
3087 c2 = *pcszValue++;
3088 if ((c2 < '0' || c2 > '9') && (c2 < 'a' || c2 > 'f') && (c2 < 'A' || c2 > 'F'))
3089 return VERR_PARSE_ERROR;
3090 *pbValue++ = (HEX_2_NUM(c1) << 4) | HEX_2_NUM(c2);
3091 cbValue--;
3092 }
3093 *pcbValue -= cbValue;
3094 break;
3095 case 'b':
3096 case 'B':
3097 if ((strlen(pcszValue) & 3) != 0)
3098 return VERR_PARSE_ERROR;
3099 while (*pcszValue != '\0')
3100 {
3101 uint32_t temp;
3102 if (cbValue == 0)
3103 return VERR_BUFFER_OVERFLOW;
3104 c1 = *pcszValue++;
3105 if ((c1 < 'A' || c1 > 'Z') && (c1 < 'a' || c1 >'z') && (c1 < '0' || c1 > '9') && (c1 != '+') && (c1 != '/'))
3106 return VERR_PARSE_ERROR;
3107 c2 = *pcszValue++;
3108 if ((c2 < 'A' || c2 > 'Z') && (c2 < 'a' || c2 >'z') && (c2 < '0' || c2 > '9') && (c2 != '+') && (c2 != '/'))
3109 return VERR_PARSE_ERROR;
3110 c3 = *pcszValue++;
3111 if ((c3 < 'A' || c3 > 'Z') && (c3 < 'a' || c3 >'z') && (c3 < '0' || c3 > '9') && (c3 != '+') && (c3 != '/') && (c3 != '='))
3112 return VERR_PARSE_ERROR;
3113 c4 = *pcszValue++;
3114 if ( (c3 == '=' && c4 != '=')
3115 || ((c4 < 'A' || c4 > 'Z') && (c4 < 'a' || c4 >'z') && (c4 < '0' || c4 > '9') && (c4 != '+') && (c4 != '/') && (c4 != '=')))
3116 return VERR_PARSE_ERROR;
3117 temp = (B64_2_NUM(c1) << 18) | (B64_2_NUM(c2) << 12);
3118 if (c3 == '=') {
3119 if (*pcszValue != '\0')
3120 return VERR_PARSE_ERROR;
3121 *pbValue++ = temp >> 16;
3122 cbValue--;
3123 } else {
3124 temp |= B64_2_NUM(c3) << 6;
3125 if (c4 == '=') {
3126 if (*pcszValue != '\0')
3127 return VERR_PARSE_ERROR;
3128 if (cbValue < 2)
3129 return VERR_BUFFER_OVERFLOW;
3130 *pbValue++ = temp >> 16;
3131 *pbValue++ = (temp >> 8) & 0xff;
3132 cbValue -= 2;
3133 }
3134 else
3135 {
3136 temp |= B64_2_NUM(c4);
3137 if (cbValue < 3)
3138 return VERR_BUFFER_OVERFLOW;
3139 *pbValue++ = temp >> 16;
3140 *pbValue++ = (temp >> 8) & 0xff;
3141 *pbValue++ = temp & 0xff;
3142 cbValue -= 3;
3143 }
3144 }
3145 }
3146 *pcbValue -= cbValue;
3147 break;
3148 default:
3149 return VERR_PARSE_ERROR;
3150 }
3151 return VINF_SUCCESS;
3152}
3153
3154
3155/**
3156 * Retrieve the relevant parameter values and update the initiator state.
3157 *
3158 * @returns VBox status code.
3159 * @param pImage Current iSCSI initiator state.
3160 * @param pbBuf Buffer containing key=value pairs.
3161 * @param cbBuf Length of buffer with key=value pairs.
3162 */
3163static int iscsiUpdateParameters(PISCSIIMAGE pImage, const uint8_t *pbBuf, size_t cbBuf)
3164{
3165 int rc;
3166 const char *pcszMaxRecvDataSegmentLength = NULL;
3167 const char *pcszMaxBurstLength = NULL;
3168 const char *pcszFirstBurstLength = NULL;
3169 rc = iscsiTextGetKeyValue(pbBuf, cbBuf, "MaxRecvDataSegmentLength", &pcszMaxRecvDataSegmentLength);
3170 if (rc == VERR_INVALID_NAME)
3171 rc = VINF_SUCCESS;
3172 if (RT_FAILURE(rc))
3173 return VERR_PARSE_ERROR;
3174 rc = iscsiTextGetKeyValue(pbBuf, cbBuf, "MaxBurstLength", &pcszMaxBurstLength);
3175 if (rc == VERR_INVALID_NAME)
3176 rc = VINF_SUCCESS;
3177 if (RT_FAILURE(rc))
3178 return VERR_PARSE_ERROR;
3179 rc = iscsiTextGetKeyValue(pbBuf, cbBuf, "FirstBurstLength", &pcszFirstBurstLength);
3180 if (rc == VERR_INVALID_NAME)
3181 rc = VINF_SUCCESS;
3182 if (RT_FAILURE(rc))
3183 return VERR_PARSE_ERROR;
3184 if (pcszMaxRecvDataSegmentLength)
3185 {
3186 uint32_t cb = pImage->cbSendDataLength;
3187 rc = RTStrToUInt32Full(pcszMaxRecvDataSegmentLength, 0, &cb);
3188 AssertRC(rc);
3189 pImage->cbSendDataLength = RT_MIN(pImage->cbSendDataLength, cb);
3190 }
3191 if (pcszMaxBurstLength)
3192 {
3193 uint32_t cb = pImage->cbSendDataLength;
3194 rc = RTStrToUInt32Full(pcszMaxBurstLength, 0, &cb);
3195 AssertRC(rc);
3196 pImage->cbSendDataLength = RT_MIN(pImage->cbSendDataLength, cb);
3197 }
3198 if (pcszFirstBurstLength)
3199 {
3200 uint32_t cb = pImage->cbSendDataLength;
3201 rc = RTStrToUInt32Full(pcszFirstBurstLength, 0, &cb);
3202 AssertRC(rc);
3203 pImage->cbSendDataLength = RT_MIN(pImage->cbSendDataLength, cb);
3204 }
3205 return VINF_SUCCESS;
3206}
3207
3208
3209static bool serial_number_less(uint32_t s1, uint32_t s2)
3210{
3211 return (s1 < s2 && s2 - s1 < 0x80000000) || (s1 > s2 && s1 - s2 > 0x80000000);
3212}
3213
3214static bool serial_number_greater(uint32_t s1, uint32_t s2)
3215{
3216 return (s1 < s2 && s2 - s1 > 0x80000000) || (s1 > s2 && s1 - s2 < 0x80000000);
3217}
3218
3219
3220#ifdef IMPLEMENT_TARGET_AUTH
3221static void chap_md5_generate_challenge(uint8_t *pbChallenge, size_t *pcbChallenge)
3222{
3223 uint8_t cbChallenge;
3224
3225 cbChallenge = RTrand_U8(CHAP_MD5_CHALLENGE_MIN, CHAP_MD5_CHALLENGE_MAX);
3226 RTrand_bytes(pbChallenge, cbChallenge);
3227 *pcbChallenge = cbChallenge;
3228}
3229#endif
3230
3231
3232static void chap_md5_compute_response(uint8_t *pbResponse, uint8_t id, const uint8_t *pbChallenge, size_t cbChallenge,
3233 const uint8_t *pbSecret, size_t cbSecret)
3234{
3235 RTMD5CONTEXT ctx;
3236 uint8_t bId;
3237
3238 bId = id;
3239 RTMd5Init(&ctx);
3240 RTMd5Update(&ctx, &bId, 1);
3241 RTMd5Update(&ctx, pbSecret, cbSecret);
3242 RTMd5Update(&ctx, pbChallenge, cbChallenge);
3243 RTMd5Final(pbResponse, &ctx);
3244}
3245
3246/**
3247 * Internal. - Wrapper around the extended select callback of the net interface.
3248 */
3249DECLINLINE(int) iscsiIoThreadWait(PISCSIIMAGE pImage, RTMSINTERVAL cMillies, uint32_t fEvents, uint32_t *pfEvents)
3250{
3251 return pImage->pIfNet->pfnSelectOneEx(pImage->Socket, fEvents, pfEvents, cMillies);
3252}
3253
3254/**
3255 * Internal. - Pokes a thread waiting for I/O.
3256 */
3257DECLINLINE(int) iscsiIoThreadPoke(PISCSIIMAGE pImage)
3258{
3259 return pImage->pIfNet->pfnPoke(pImage->Socket);
3260}
3261
3262/**
3263 * Internal. - Get the next request from the queue.
3264 */
3265DECLINLINE(PISCSICMD) iscsiCmdGet(PISCSIIMAGE pImage)
3266{
3267 int rc;
3268 PISCSICMD pIScsiCmd = NULL;
3269
3270 rc = RTSemMutexRequest(pImage->MutexReqQueue, RT_INDEFINITE_WAIT);
3271 AssertRC(rc);
3272
3273 pIScsiCmd = pImage->pScsiReqQueue;
3274 if (pIScsiCmd)
3275 {
3276 pImage->pScsiReqQueue = pIScsiCmd->pNext;
3277 pIScsiCmd->pNext = NULL;
3278 }
3279
3280 rc = RTSemMutexRelease(pImage->MutexReqQueue);
3281 AssertRC(rc);
3282
3283 return pIScsiCmd;
3284}
3285
3286
3287/**
3288 * Internal. - Adds the given command to the queue.
3289 */
3290DECLINLINE(int) iscsiCmdPut(PISCSIIMAGE pImage, PISCSICMD pIScsiCmd)
3291{
3292 int rc = RTSemMutexRequest(pImage->MutexReqQueue, RT_INDEFINITE_WAIT);
3293 AssertRC(rc);
3294
3295 pIScsiCmd->pNext = pImage->pScsiReqQueue;
3296 pImage->pScsiReqQueue = pIScsiCmd;
3297
3298 rc = RTSemMutexRelease(pImage->MutexReqQueue);
3299 AssertRC(rc);
3300
3301 iscsiIoThreadPoke(pImage);
3302
3303 return rc;
3304}
3305
3306/**
3307 * Internal. - Completes the request with the appropriate action.
3308 * Synchronous requests are completed with waking up the thread
3309 * and asynchronous ones by continuing the associated I/O context.
3310 */
3311static void iscsiCmdComplete(PISCSIIMAGE pImage, PISCSICMD pIScsiCmd, int rcCmd)
3312{
3313 LogFlowFunc(("pImage=%#p pIScsiCmd=%#p rcCmd=%Rrc\n", pImage, pIScsiCmd, rcCmd));
3314
3315 /* Remove from the table first. */
3316 iscsiCmdRemove(pImage, pIScsiCmd->Itt);
3317
3318 /* Call completion callback. */
3319 pIScsiCmd->pfnComplete(pImage, rcCmd, pIScsiCmd->pvUser);
3320
3321 /* Free command structure. */
3322#ifdef DEBUG
3323 memset(pIScsiCmd, 0xff, sizeof(ISCSICMD));
3324#endif
3325 RTMemFree(pIScsiCmd);
3326}
3327
3328/**
3329 * Clears all RX/TX PDU states and returns the command for the current
3330 * pending TX PDU if existing.
3331 *
3332 * @returns Pointer to the iSCSI command for the current PDU transmitted or NULL
3333 * if none is waiting.
3334 * @param pImage iSCSI connection state.
3335 */
3336static PISCSICMD iscsiPDURxTxClear(PISCSIIMAGE pImage)
3337{
3338 PISCSICMD pIScsiCmdHead = NULL;
3339 PISCSIPDUTX pIScsiPDUTx = NULL;
3340
3341 /* Reset PDU we are receiving. */
3342 iscsiRecvPDUReset(pImage);
3343
3344 /*
3345 * Abort all PDUs we are about to transmit,
3346 * the command need a new Itt if the relogin is successful.
3347 */
3348 while (pImage->pIScsiPDUTxHead)
3349 {
3350 pIScsiPDUTx = pImage->pIScsiPDUTxHead;
3351 pImage->pIScsiPDUTxHead = pIScsiPDUTx->pNext;
3352
3353 PISCSICMD pIScsiCmd = pIScsiPDUTx->pIScsiCmd;
3354 if (pIScsiCmd)
3355 {
3356 /* Place on command list. */
3357 pIScsiCmd->pNext = pIScsiCmdHead;
3358 pIScsiCmdHead = pIScsiCmd;
3359 }
3360 RTMemFree(pIScsiPDUTx);
3361 }
3362
3363 /* Clear the tail pointer (safety precaution). */
3364 pImage->pIScsiPDUTxTail = NULL;
3365
3366 /* Clear the current PDU too. */
3367 if (pImage->pIScsiPDUTxCur)
3368 {
3369 pIScsiPDUTx = pImage->pIScsiPDUTxCur;
3370
3371 pImage->pIScsiPDUTxCur = NULL;
3372 PISCSICMD pIScsiCmd = pIScsiPDUTx->pIScsiCmd;
3373 if (pIScsiCmd)
3374 {
3375 pIScsiCmd->pNext = pIScsiCmdHead;
3376 pIScsiCmdHead = pIScsiCmd;
3377 }
3378 RTMemFree(pIScsiPDUTx);
3379 }
3380
3381 return pIScsiCmdHead;
3382}
3383
3384/**
3385 * Rests the iSCSI connection state and returns a list of iSCSI commands pending
3386 * when this was called.
3387 *
3388 * @returns Pointer to the head of the pending iSCSI command list.
3389 * @param pImage iSCSI connection state.
3390 */
3391static PISCSICMD iscsiReset(PISCSIIMAGE pImage)
3392{
3393 PISCSICMD pIScsiCmdHead = NULL;
3394 PISCSICMD pIScsiCmdCur = NULL;
3395
3396 /* Clear all in flight PDUs. */
3397 pIScsiCmdHead = iscsiPDURxTxClear(pImage);
3398
3399 /*
3400 * Get all commands which are waiting for a response
3401 * They need to be resend too after a successful reconnect.
3402 */
3403 PISCSICMD pIScsiCmd = iscsiCmdRemoveAll(pImage);
3404 if (pIScsiCmd)
3405 {
3406 pIScsiCmdCur = pIScsiCmd;
3407 while (pIScsiCmdCur->pNext)
3408 pIScsiCmdCur = pIScsiCmdCur->pNext;
3409
3410 /*
3411 * Place them in front of the list because they are the oldest requests
3412 * and need to be processed first to minimize the risk to time out.
3413 */
3414 pIScsiCmdCur->pNext = pIScsiCmdHead;
3415 pIScsiCmdHead = pIScsiCmd;
3416 }
3417
3418 return pIScsiCmdHead;
3419}
3420
3421/**
3422 * Reattaches the to the target after an error aborting
3423 * pending commands and resending them.
3424 *
3425 * @param pImage iSCSI connection state.
3426 */
3427static void iscsiReattach(PISCSIIMAGE pImage)
3428{
3429 /* Close connection. */
3430 iscsiTransportClose(pImage);
3431 pImage->state = ISCSISTATE_FREE;
3432
3433 /* Reset the state and get the currently pending commands. */
3434 PISCSICMD pIScsiCmdHead = iscsiReset(pImage);
3435
3436 /* Try to attach. */
3437 int rc = iscsiAttach(pImage);
3438 if (RT_SUCCESS(rc))
3439 {
3440 /* Phew, we have a connection again.
3441 * Prepare new PDUs for the aborted commands.
3442 */
3443 while (pIScsiCmdHead)
3444 {
3445 PISCSICMD pIScsiCmd = pIScsiCmdHead;
3446 pIScsiCmdHead = pIScsiCmdHead->pNext;
3447
3448 pIScsiCmd->pNext = NULL;
3449
3450 rc = iscsiPDUTxPrepare(pImage, pIScsiCmd);
3451 if (RT_FAILURE(rc))
3452 break;
3453 }
3454
3455 if (RT_FAILURE(rc))
3456 {
3457 /* Another error, just give up and report an error. */
3458 PISCSICMD pIScsiCmd = iscsiReset(pImage);
3459
3460 /* Concatenate both lists together so we can abort all requests below. */
3461 if (pIScsiCmd)
3462 {
3463 PISCSICMD pIScsiCmdCur = pIScsiCmd;
3464 while (pIScsiCmdCur->pNext)
3465 pIScsiCmdCur = pIScsiCmdCur->pNext;
3466
3467 pIScsiCmdCur->pNext = pIScsiCmdHead;
3468 pIScsiCmdHead = pIScsiCmd;
3469 }
3470 }
3471 }
3472
3473 if (RT_FAILURE(rc))
3474 {
3475 /*
3476 * Still no luck, complete commands with error so the caller
3477 * has a chance to inform the user and maybe resend the command.
3478 */
3479 while (pIScsiCmdHead)
3480 {
3481 PISCSICMD pIScsiCmd = pIScsiCmdHead;
3482 pIScsiCmdHead = pIScsiCmdHead->pNext;
3483
3484 iscsiCmdComplete(pImage, pIScsiCmd, VERR_BROKEN_PIPE);
3485 }
3486 }
3487}
3488
3489/**
3490 * Internal. Main iSCSI I/O worker.
3491 */
3492static DECLCALLBACK(int) iscsiIoThreadWorker(RTTHREAD hThreadSelf, void *pvUser)
3493{
3494 RT_NOREF1(hThreadSelf);
3495 PISCSIIMAGE pImage = (PISCSIIMAGE)pvUser;
3496
3497 /* Initialize the initial event mask. */
3498 pImage->fPollEvents = VD_INTERFACETCPNET_EVT_READ | VD_INTERFACETCPNET_EVT_ERROR;
3499
3500 while (pImage->fRunning)
3501 {
3502 uint32_t fEvents;
3503 int rc;
3504
3505 fEvents = 0;
3506
3507 /* Wait for work or for data from the target. */
3508 RTMSINTERVAL msWait;
3509
3510 if (pImage->cCmdsWaiting)
3511 {
3512 pImage->fPollEvents &= ~VD_INTERFACETCPNET_HINT_INTERRUPT;
3513 msWait = pImage->uReadTimeout;
3514 }
3515 else
3516 {
3517 pImage->fPollEvents |= VD_INTERFACETCPNET_HINT_INTERRUPT;
3518 msWait = RT_INDEFINITE_WAIT;
3519 }
3520
3521 LogFlow(("Waiting for events fPollEvents=%#x\n", pImage->fPollEvents));
3522 rc = iscsiIoThreadWait(pImage, msWait, pImage->fPollEvents, &fEvents);
3523 if (rc == VERR_INTERRUPTED)
3524 {
3525 /* Check the queue. */
3526 PISCSICMD pIScsiCmd = iscsiCmdGet(pImage);
3527
3528 while (pIScsiCmd)
3529 {
3530 switch (pIScsiCmd->enmCmdType)
3531 {
3532 case ISCSICMDTYPE_REQ:
3533 {
3534 if ( !iscsiIsClientConnected(pImage)
3535 && pImage->fTryReconnect)
3536 {
3537 pImage->fTryReconnect = false;
3538 iscsiReattach(pImage);
3539 }
3540
3541 /* If there is no connection complete the command with an error. */
3542 if (RT_LIKELY(iscsiIsClientConnected(pImage)))
3543 {
3544 rc = iscsiPDUTxPrepare(pImage, pIScsiCmd);
3545 if (RT_FAILURE(rc))
3546 iscsiReattach(pImage);
3547 }
3548 else
3549 iscsiCmdComplete(pImage, pIScsiCmd, VERR_NET_CONNECTION_REFUSED);
3550 break;
3551 }
3552 case ISCSICMDTYPE_EXEC:
3553 {
3554 rc = pIScsiCmd->CmdType.Exec.pfnExec(pIScsiCmd->CmdType.Exec.pvUser);
3555 iscsiCmdComplete(pImage, pIScsiCmd, rc);
3556 break;
3557 }
3558 default:
3559 AssertMsgFailed(("Invalid command type %d\n", pIScsiCmd->enmCmdType));
3560 }
3561
3562 pIScsiCmd = iscsiCmdGet(pImage);
3563 }
3564 }
3565 else if (rc == VERR_TIMEOUT && pImage->cCmdsWaiting)
3566 {
3567 /*
3568 * We are waiting for a response from the target but
3569 * it didn't answered yet.
3570 * We assume the connection is broken and try to reconnect.
3571 */
3572 LogFlow(("Timed out while waiting for an answer from the target, reconnecting\n"));
3573 iscsiReattach(pImage);
3574 }
3575 else if (RT_SUCCESS(rc) || rc == VERR_TIMEOUT)
3576 {
3577 Assert(pImage->state == ISCSISTATE_NORMAL);
3578 LogFlow(("Got socket events %#x\n", fEvents));
3579
3580 if (fEvents & VD_INTERFACETCPNET_EVT_READ)
3581 {
3582 /* Continue or start a new PDU receive task */
3583 LogFlow(("There is data on the socket\n"));
3584 rc = iscsiRecvPDUAsync(pImage);
3585 if (rc == VERR_BROKEN_PIPE)
3586 iscsiReattach(pImage);
3587 else if (RT_FAILURE(rc))
3588 iscsiLogRel(pImage, "iSCSI: Handling incoming request failed %Rrc\n", rc);
3589 }
3590
3591 if (fEvents & VD_INTERFACETCPNET_EVT_WRITE)
3592 {
3593 LogFlow(("The socket is writable\n"));
3594 rc = iscsiSendPDUAsync(pImage);
3595 if (RT_FAILURE(rc))
3596 {
3597 /*
3598 * Something unexpected happened, log the error and try to reset everything
3599 * by reattaching to the target.
3600 */
3601 iscsiLogRel(pImage, "iSCSI: Sending PDU failed %Rrc\n", rc);
3602 iscsiReattach(pImage);
3603 }
3604 }
3605
3606 if (fEvents & VD_INTERFACETCPNET_EVT_ERROR)
3607 {
3608 LogFlow(("An error ocurred\n"));
3609 iscsiReattach(pImage);
3610 }
3611 }
3612 else
3613 iscsiLogRel(pImage, "iSCSI: Waiting for I/O failed rc=%Rrc\n", rc);
3614 }
3615
3616 return VINF_SUCCESS;
3617}
3618
3619/**
3620 * Internal. - Enqueues a request asynchronously.
3621 */
3622static int iscsiCommandAsync(PISCSIIMAGE pImage, PSCSIREQ pScsiReq,
3623 PFNISCSICMDCOMPLETED pfnComplete, void *pvUser)
3624{
3625 int rc;
3626
3627 if (pImage->fExtendedSelectSupported)
3628 {
3629 PISCSICMD pIScsiCmd = (PISCSICMD)RTMemAllocZ(sizeof(ISCSICMD));
3630 if (!pIScsiCmd)
3631 return VERR_NO_MEMORY;
3632
3633 /* Init the command structure. */
3634 pIScsiCmd->pNext = NULL;
3635 pIScsiCmd->enmCmdType = ISCSICMDTYPE_REQ;
3636 pIScsiCmd->pfnComplete = pfnComplete;
3637 pIScsiCmd->pvUser = pvUser;
3638 pIScsiCmd->CmdType.ScsiReq.pScsiReq = pScsiReq;
3639
3640 rc = iscsiCmdPut(pImage, pIScsiCmd);
3641 if (RT_FAILURE(rc))
3642 RTMemFree(pIScsiCmd);
3643 }
3644 else
3645 rc = VERR_NOT_SUPPORTED;
3646
3647 return rc;
3648}
3649
3650static DECLCALLBACK(void) iscsiCommandCompleteSync(PISCSIIMAGE pImage, int rcReq, void *pvUser)
3651{
3652 RT_NOREF1(pImage);
3653 PISCSICMDSYNC pIScsiCmdSync = (PISCSICMDSYNC)pvUser;
3654
3655 pIScsiCmdSync->rcCmd = rcReq;
3656 int rc = RTSemEventSignal(pIScsiCmdSync->EventSem);
3657 AssertRC(rc);
3658}
3659
3660/**
3661 * Internal. - Enqueues a request in a synchronous fashion
3662 * i.e. returns when the request completed.
3663 */
3664static int iscsiCommandSync(PISCSIIMAGE pImage, PSCSIREQ pScsiReq, bool fRetry, int rcSense)
3665{
3666 int rc;
3667
3668 if (pImage->fExtendedSelectSupported)
3669 {
3670 ISCSICMDSYNC IScsiCmdSync;
3671
3672 /* Create event semaphore. */
3673 rc = RTSemEventCreate(&IScsiCmdSync.EventSem);
3674 if (RT_FAILURE(rc))
3675 return rc;
3676
3677 if (fRetry)
3678 {
3679 for (unsigned i = 0; i < 10; i++)
3680 {
3681 rc = iscsiCommandAsync(pImage, pScsiReq, iscsiCommandCompleteSync, &IScsiCmdSync);
3682 if (RT_FAILURE(rc))
3683 break;
3684
3685 rc = RTSemEventWait(IScsiCmdSync.EventSem, RT_INDEFINITE_WAIT);
3686 AssertRC(rc);
3687 rc = IScsiCmdSync.rcCmd;
3688
3689 if ( (RT_SUCCESS(rc) && !pScsiReq->cbSense)
3690 || RT_FAILURE(rc))
3691 break;
3692 rc = rcSense;
3693 }
3694 }
3695 else
3696 {
3697 rc = iscsiCommandAsync(pImage, pScsiReq, iscsiCommandCompleteSync, &IScsiCmdSync);
3698 if (RT_SUCCESS(rc))
3699 {
3700 rc = RTSemEventWait(IScsiCmdSync.EventSem, RT_INDEFINITE_WAIT);
3701 AssertRC(rc);
3702 rc = IScsiCmdSync.rcCmd;
3703
3704 if (RT_FAILURE(rc) || pScsiReq->cbSense > 0)
3705 rc = rcSense;
3706 }
3707 }
3708
3709 RTSemEventDestroy(IScsiCmdSync.EventSem);
3710 }
3711 else
3712 {
3713 if (fRetry)
3714 {
3715 rc = VINF_SUCCESS; /* (MSC incorrectly thinks it can be uninitialized) */
3716 for (unsigned i = 0; i < 10; i++)
3717 {
3718 rc = iscsiCommand(pImage, pScsiReq);
3719 if ( (RT_SUCCESS(rc) && !pScsiReq->cbSense)
3720 || RT_FAILURE(rc))
3721 break;
3722 rc = rcSense;
3723 }
3724 }
3725 else
3726 {
3727 rc = iscsiCommand(pImage, pScsiReq);
3728 if (RT_FAILURE(rc) || pScsiReq->cbSense > 0)
3729 rc = rcSense;
3730 }
3731 }
3732
3733 return rc;
3734}
3735
3736
3737/**
3738 * Internal. - Executes a given function in a synchronous fashion
3739 * on the I/O thread if available.
3740 */
3741static int iscsiExecSync(PISCSIIMAGE pImage, PFNISCSIEXEC pfnExec, void *pvUser)
3742{
3743 int rc;
3744
3745 if (pImage->fExtendedSelectSupported)
3746 {
3747 ISCSICMDSYNC IScsiCmdSync;
3748 PISCSICMD pIScsiCmd = (PISCSICMD)RTMemAllocZ(sizeof(ISCSICMD));
3749 if (!pIScsiCmd)
3750 return VERR_NO_MEMORY;
3751
3752 /* Create event semaphore. */
3753 rc = RTSemEventCreate(&IScsiCmdSync.EventSem);
3754 if (RT_FAILURE(rc))
3755 {
3756 RTMemFree(pIScsiCmd);
3757 return rc;
3758 }
3759
3760 /* Init the command structure. */
3761 pIScsiCmd->pNext = NULL;
3762 pIScsiCmd->enmCmdType = ISCSICMDTYPE_EXEC;
3763 pIScsiCmd->pfnComplete = iscsiCommandCompleteSync;
3764 pIScsiCmd->pvUser = &IScsiCmdSync;
3765 pIScsiCmd->CmdType.Exec.pfnExec = pfnExec;
3766 pIScsiCmd->CmdType.Exec.pvUser = pvUser;
3767
3768 rc = iscsiCmdPut(pImage, pIScsiCmd);
3769 if (RT_FAILURE(rc))
3770 RTMemFree(pIScsiCmd);
3771 else
3772 {
3773 rc = RTSemEventWait(IScsiCmdSync.EventSem, RT_INDEFINITE_WAIT);
3774 AssertRC(rc);
3775 rc = IScsiCmdSync.rcCmd;
3776 }
3777
3778 RTSemEventDestroy(IScsiCmdSync.EventSem);
3779 }
3780 else
3781 {
3782 /* No I/O thread, execute in the current thread. */
3783 rc = pfnExec(pvUser);
3784 }
3785
3786 return rc;
3787}
3788
3789
3790static DECLCALLBACK(void) iscsiCommandAsyncComplete(PISCSIIMAGE pImage, int rcReq, void *pvUser)
3791{
3792 bool fComplete = true;
3793 size_t cbTransfered = 0;
3794 PSCSIREQ pScsiReq = (PSCSIREQ)pvUser;
3795
3796 if (RT_SUCCESS(rcReq))
3797 ASMAtomicWriteU32(&pImage->cLoginsSinceIo, 0);
3798
3799 if ( RT_SUCCESS(rcReq)
3800 && pScsiReq->cbSense > 0)
3801 {
3802 /* Try again if possible. */
3803 if (pScsiReq->cSenseRetries > 0)
3804 {
3805 pScsiReq->cSenseRetries--;
3806 pScsiReq->cbSense = sizeof(pScsiReq->abSense);
3807 int rc = iscsiCommandAsync(pImage, pScsiReq, iscsiCommandAsyncComplete, pScsiReq);
3808 if (RT_SUCCESS(rc))
3809 fComplete = false;
3810 else
3811 rcReq = pScsiReq->rcSense;
3812 }
3813 else
3814 rcReq = pScsiReq->rcSense;
3815 }
3816
3817 if (fComplete)
3818 {
3819 if (pScsiReq->enmXfer == SCSIXFER_FROM_TARGET)
3820 cbTransfered = pScsiReq->cbT2IData;
3821 else if (pScsiReq->enmXfer == SCSIXFER_TO_TARGET)
3822 cbTransfered = pScsiReq->cbI2TData;
3823 else
3824 AssertMsg(pScsiReq->enmXfer == SCSIXFER_NONE, ("To/From transfers are not supported yet\n"));
3825
3826 /* Continue I/O context. */
3827 pImage->pIfIo->pfnIoCtxCompleted(pImage->pIfIo->Core.pvUser,
3828 pScsiReq->pIoCtx, rcReq,
3829 cbTransfered);
3830
3831 RTMemFree(pScsiReq);
3832 }
3833}
3834
3835
3836/**
3837 * Internal. Free all allocated space for representing an image, and optionally
3838 * delete the image from disk.
3839 */
3840static int iscsiFreeImage(PISCSIIMAGE pImage, bool fDelete)
3841{
3842 int rc = VINF_SUCCESS;
3843 Assert(!fDelete); NOREF(fDelete); /* This MUST be false, the flag isn't supported. */
3844
3845 /* Freeing a never allocated image (e.g. because the open failed) is
3846 * not signalled as an error. After all nothing bad happens. */
3847 if (pImage)
3848 {
3849 if (pImage->Mutex != NIL_RTSEMMUTEX)
3850 {
3851 /* Detaching only makes sense when the mutex is there. Otherwise the
3852 * failure happened long before we could attach to the target. */
3853 iscsiExecSync(pImage, iscsiDetach, pImage);
3854 RTSemMutexDestroy(pImage->Mutex);
3855 pImage->Mutex = NIL_RTSEMMUTEX;
3856 }
3857 if (pImage->hThreadIo != NIL_RTTHREAD)
3858 {
3859 ASMAtomicXchgBool(&pImage->fRunning, false);
3860 rc = iscsiIoThreadPoke(pImage);
3861 AssertRC(rc);
3862
3863 /* Wait for the thread to terminate. */
3864 rc = RTThreadWait(pImage->hThreadIo, RT_INDEFINITE_WAIT, NULL);
3865 AssertRC(rc);
3866 }
3867 /* Destroy the socket. */
3868 if (pImage->Socket != NIL_VDSOCKET)
3869 {
3870 pImage->pIfNet->pfnSocketDestroy(pImage->Socket);
3871 }
3872 if (pImage->MutexReqQueue != NIL_RTSEMMUTEX)
3873 {
3874 RTSemMutexDestroy(pImage->MutexReqQueue);
3875 pImage->MutexReqQueue = NIL_RTSEMMUTEX;
3876 }
3877 if (pImage->pszTargetName)
3878 {
3879 RTMemFree(pImage->pszTargetName);
3880 pImage->pszTargetName = NULL;
3881 }
3882 if (pImage->pszTargetAddress)
3883 {
3884 RTMemFree(pImage->pszTargetAddress);
3885 pImage->pszTargetAddress = NULL;
3886 }
3887 if (pImage->pszInitiatorName)
3888 {
3889 if (pImage->fAutomaticInitiatorName)
3890 RTStrFree(pImage->pszInitiatorName);
3891 else
3892 RTMemFree(pImage->pszInitiatorName);
3893 pImage->pszInitiatorName = NULL;
3894 }
3895 if (pImage->pszInitiatorUsername)
3896 {
3897 RTMemFree(pImage->pszInitiatorUsername);
3898 pImage->pszInitiatorUsername = NULL;
3899 }
3900 if (pImage->pbInitiatorSecret)
3901 {
3902 RTMemFree(pImage->pbInitiatorSecret);
3903 pImage->pbInitiatorSecret = NULL;
3904 }
3905 if (pImage->pszTargetUsername)
3906 {
3907 RTMemFree(pImage->pszTargetUsername);
3908 pImage->pszTargetUsername = NULL;
3909 }
3910 if (pImage->pbTargetSecret)
3911 {
3912 RTMemFree(pImage->pbTargetSecret);
3913 pImage->pbTargetSecret = NULL;
3914 }
3915 if (pImage->pvRecvPDUBuf)
3916 {
3917 RTMemFree(pImage->pvRecvPDUBuf);
3918 pImage->pvRecvPDUBuf = NULL;
3919 }
3920 if (pImage->pszHostname)
3921 {
3922 RTMemFree(pImage->pszHostname);
3923 pImage->pszHostname = NULL;
3924 }
3925
3926 pImage->cbRecvPDUResidual = 0;
3927 }
3928
3929 LogFlowFunc(("returns %Rrc\n", rc));
3930 return rc;
3931}
3932
3933/**
3934 * Inits the basic iSCSI image state, allocating vital resources.
3935 *
3936 * @returns VBox status code.
3937 * @param pImage The iSCSI image instance.
3938 */
3939static int iscsiOpenImageInit(PISCSIIMAGE pImage)
3940{
3941 int rc = VINF_SUCCESS;
3942
3943 /* Get error signalling interface. */
3944 pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
3945
3946 /* Get TCP network stack interface. */
3947 pImage->pIfNet = VDIfTcpNetGet(pImage->pVDIfsImage);
3948 if (pImage->pIfNet)
3949 {
3950 /* Get configuration interface. */
3951 pImage->pIfConfig = VDIfConfigGet(pImage->pVDIfsImage);
3952 if (pImage->pIfConfig)
3953 {
3954 /* Get I/O interface. */
3955 pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
3956 if (pImage->pIfIo)
3957 {
3958 /* This ISID will be adjusted later to make it unique on this host. */
3959 pImage->pszHostname = NULL;
3960 pImage->uPort = 0;
3961 pImage->Socket = NIL_VDSOCKET;
3962 pImage->ISID = 0x800000000000ULL | 0x001234560000ULL;
3963 pImage->cISCSIRetries = 10;
3964 pImage->state = ISCSISTATE_FREE;
3965 pImage->cLoginsSinceIo = 0;
3966 pImage->Mutex = NIL_RTSEMMUTEX;
3967 pImage->MutexReqQueue = NIL_RTSEMMUTEX;
3968 pImage->pszInitiatorUsername = NULL;
3969 pImage->pbInitiatorSecret = NULL;
3970 pImage->cbInitiatorSecret = 0;
3971 pImage->pszTargetUsername = NULL;
3972 pImage->pbTargetSecret = NULL;
3973 pImage->cbTargetSecret = 0;
3974
3975 memset(pImage->aCmdsWaiting, 0, sizeof(pImage->aCmdsWaiting));
3976 pImage->cbRecvPDUResidual = 0;
3977
3978 pImage->pvRecvPDUBuf = RTMemAlloc(ISCSI_RECV_PDU_BUFFER_SIZE);
3979 pImage->cbRecvPDUBuf = ISCSI_RECV_PDU_BUFFER_SIZE;
3980 if (!pImage->pvRecvPDUBuf)
3981 rc = VERR_NO_MEMORY;
3982
3983 if (RT_SUCCESS(rc))
3984 rc = RTSemMutexCreate(&pImage->Mutex);
3985 if (RT_SUCCESS(rc))
3986 rc = RTSemMutexCreate(&pImage->MutexReqQueue);
3987 }
3988 else
3989 rc = vdIfError(pImage->pIfError, VERR_VD_UNKNOWN_INTERFACE,
3990 RT_SRC_POS, N_("iSCSI: I/O interface missing"));
3991 }
3992 else
3993 rc = vdIfError(pImage->pIfError, VERR_VD_UNKNOWN_INTERFACE,
3994 RT_SRC_POS, N_("iSCSI: configuration interface missing"));
3995 }
3996 else
3997 rc = vdIfError(pImage->pIfError, VERR_VD_UNKNOWN_INTERFACE,
3998 RT_SRC_POS, N_("iSCSI: TCP network stack interface missing"));
3999
4000 return rc;
4001}
4002
4003/**
4004 * Parses the user supplied config before opening the connection to the target.
4005 *
4006 * @returns VBox status code.
4007 * @param pImage The iSCSI image instance.
4008 */
4009static int iscsiOpenImageParseCfg(PISCSIIMAGE pImage)
4010{
4011 char *pszLUN = NULL, *pszLUNInitial = NULL;
4012 bool fLunEncoded = false;
4013 uint32_t uWriteSplitDef = 0;
4014 uint32_t uTimeoutDef = 0;
4015 uint64_t uCfgTmp = 0;
4016 bool fHostIPDef = false;
4017 bool fDumpMalformedPacketsDef = false;
4018
4019 int rc = RTStrToUInt32Full(s_iscsiConfigDefaultWriteSplit, 0, &uWriteSplitDef);
4020 AssertRC(rc);
4021 rc = RTStrToUInt32Full(s_iscsiConfigDefaultTimeout, 0, &uTimeoutDef);
4022 AssertRC(rc);
4023 rc = RTStrToUInt64Full(s_iscsiConfigDefaultHostIPStack, 0, &uCfgTmp);
4024 AssertRC(rc);
4025 fHostIPDef = RT_BOOL(uCfgTmp);
4026 rc = RTStrToUInt64Full(s_iscsiConfigDefaultDumpMalformedPackets, 0, &uCfgTmp);
4027 AssertRC(rc);
4028 fDumpMalformedPacketsDef = RT_BOOL(uCfgTmp);
4029
4030 /* Validate configuration, detect unknown keys. */
4031 if (!VDCFGAreKeysValid(pImage->pIfConfig,
4032 "TargetName\0"
4033 "InitiatorName\0"
4034 "LUN\0"
4035 "TargetAddress\0"
4036 "InitiatorUsername\0"
4037 "InitiatorSecret\0"
4038 "InitiatorSecretEncrypted\0"
4039 "TargetUsername\0"
4040 "TargetSecret\0"
4041 "WriteSplit\0"
4042 "Timeout\0"
4043 "HostIPStack\0"
4044 "DumpMalformedPackets\0"))
4045 return vdIfError(pImage->pIfError, VERR_VD_UNKNOWN_CFG_VALUES, RT_SRC_POS, N_("iSCSI: configuration error: unknown configuration keys present"));
4046
4047 /* Query the iSCSI upper level configuration. */
4048 rc = VDCFGQueryStringAlloc(pImage->pIfConfig, "TargetName", &pImage->pszTargetName);
4049 if (RT_FAILURE(rc))
4050 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read TargetName as string"));
4051
4052 rc = VDCFGQueryStringAlloc(pImage->pIfConfig, "InitiatorName", &pImage->pszInitiatorName);
4053 if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT)
4054 pImage->fAutomaticInitiatorName = true;
4055 else if (RT_FAILURE(rc))
4056 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read InitiatorName as string"));
4057
4058 rc = VDCFGQueryStringAllocDef(pImage->pIfConfig, "LUN", &pszLUN, s_iscsiConfigDefaultLUN);
4059 if (RT_FAILURE(rc))
4060 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read LUN as string"));
4061
4062 pszLUNInitial = pszLUN;
4063 if (!strncmp(pszLUN, "enc", 3))
4064 {
4065 fLunEncoded = true;
4066 pszLUN += 3;
4067 }
4068 rc = RTStrToUInt64Full(pszLUN, 0, &pImage->LUN);
4069 if (RT_FAILURE(rc))
4070 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to convert LUN to integer"));
4071
4072 RTMemFree(pszLUNInitial);
4073 if (RT_SUCCESS(rc) && !fLunEncoded)
4074 {
4075 if (pImage->LUN <= 255)
4076 pImage->LUN = pImage->LUN << 48; /* uses peripheral device addressing method */
4077 else if (pImage->LUN <= 16383)
4078 pImage->LUN = (pImage->LUN << 48) | RT_BIT_64(62); /* uses flat space addressing method */
4079 else
4080 rc = vdIfError(pImage->pIfError, VERR_OUT_OF_RANGE, RT_SRC_POS, N_("iSCSI: configuration error: LUN number out of range (0-16383)"));
4081 }
4082
4083 if (RT_FAILURE(rc))
4084 return rc;
4085
4086 rc = VDCFGQueryStringAlloc(pImage->pIfConfig, "TargetAddress", &pImage->pszTargetAddress);
4087 if (RT_FAILURE(rc))
4088 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read TargetAddress as string"));
4089
4090 rc = VDCFGQueryStringAlloc(pImage->pIfConfig, "InitiatorUsername", &pImage->pszInitiatorUsername);
4091 if (RT_FAILURE(rc) && rc != VERR_CFGM_VALUE_NOT_FOUND && rc != VERR_CFGM_NO_PARENT)
4092 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read InitiatorUsername as string"));
4093
4094 rc = VDCFGQueryBytesAlloc(pImage->pIfConfig, "InitiatorSecret",
4095 (void **)&pImage->pbInitiatorSecret, &pImage->cbInitiatorSecret);
4096 if (RT_FAILURE(rc) && rc != VERR_CFGM_VALUE_NOT_FOUND && rc != VERR_CFGM_NO_PARENT)
4097 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read InitiatorSecret as byte string"));
4098
4099 void *pvInitiatorSecretEncrypted;
4100 size_t cbInitiatorSecretEncrypted;
4101 rc = VDCFGQueryBytesAlloc(pImage->pIfConfig, "InitiatorSecretEncrypted",
4102 &pvInitiatorSecretEncrypted, &cbInitiatorSecretEncrypted);
4103 if (RT_SUCCESS(rc))
4104 {
4105 RTMemFree(pvInitiatorSecretEncrypted);
4106 if (!pImage->pbInitiatorSecret)
4107 {
4108 /* we have an encrypted initiator secret but not an unencrypted one */
4109 return vdIfError(pImage->pIfError, VERR_VD_ISCSI_SECRET_ENCRYPTED, RT_SRC_POS, N_("iSCSI: initiator secret not decrypted"));
4110 }
4111 }
4112
4113 rc = VDCFGQueryStringAlloc(pImage->pIfConfig, "TargetUsername", &pImage->pszTargetUsername);
4114 if (RT_FAILURE(rc) && rc != VERR_CFGM_VALUE_NOT_FOUND && rc != VERR_CFGM_NO_PARENT)
4115 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read TargetUsername as string"));
4116
4117 rc = VDCFGQueryBytesAlloc(pImage->pIfConfig, "TargetSecret",
4118 (void **)&pImage->pbTargetSecret, &pImage->cbTargetSecret);
4119 if (RT_FAILURE(rc) && rc != VERR_CFGM_VALUE_NOT_FOUND && rc != VERR_CFGM_NO_PARENT)
4120 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read TargetSecret as byte string"));
4121
4122 rc = VDCFGQueryU32Def(pImage->pIfConfig, "WriteSplit", &pImage->cbWriteSplit, uWriteSplitDef);
4123 if (RT_FAILURE(rc))
4124 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read WriteSplit as U32"));
4125
4126 /* Query the iSCSI lower level configuration. */
4127 rc = VDCFGQueryU32Def(pImage->pIfConfig, "Timeout", &pImage->uReadTimeout, uTimeoutDef);
4128 if (RT_FAILURE(rc))
4129 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read Timeout as U32"));
4130
4131 rc = VDCFGQueryBoolDef(pImage->pIfConfig, "HostIPStack", &pImage->fHostIP, fHostIPDef);
4132 if (RT_FAILURE(rc))
4133 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read HostIPStack as boolean"));
4134
4135 rc = VDCFGQueryBoolDef(pImage->pIfConfig, "DumpMalformedPackets",
4136 &pImage->fDumpMalformedPackets, fDumpMalformedPacketsDef);
4137 if (RT_FAILURE(rc))
4138 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("iSCSI: configuration error: failed to read DumpMalformedPackets as boolean"));
4139
4140 return VINF_SUCCESS;
4141}
4142
4143/**
4144 * Creates the necessary socket structure.
4145 *
4146 * @returns VBox status code.
4147 * @param pImage The iSCSI image instance.
4148 */
4149static int iscsiOpenImageSocketCreate(PISCSIIMAGE pImage)
4150{
4151 /* Create the socket structure. */
4152 int rc = pImage->pIfNet->pfnSocketCreate(VD_INTERFACETCPNET_CONNECT_EXTENDED_SELECT,
4153 &pImage->Socket);
4154 if (RT_SUCCESS(rc))
4155 {
4156 pImage->fExtendedSelectSupported = true;
4157 pImage->fRunning = true;
4158 rc = RTThreadCreate(&pImage->hThreadIo, iscsiIoThreadWorker, pImage, 0,
4159 RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "iSCSI-Io");
4160 if (RT_FAILURE(rc))
4161 LogFunc(("Creating iSCSI I/O thread failed rc=%Rrc\n", rc));
4162 }
4163 else if (rc == VERR_NOT_SUPPORTED)
4164 {
4165 /* Async I/O is not supported without extended select. */
4166 if ((pImage->uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO))
4167 LogFunc(("Extended select is not supported by the interface but async I/O is requested -> %Rrc\n", rc));
4168 else
4169 {
4170 pImage->fExtendedSelectSupported = false;
4171 rc = pImage->pIfNet->pfnSocketCreate(0, &pImage->Socket);
4172 }
4173 }
4174
4175 if (RT_FAILURE(rc))
4176 LogFunc(("Creating socket failed -> %Rrc\n", rc));
4177
4178 return rc;
4179}
4180
4181/**
4182 * Issues a REPORT LUNS to the target.
4183 *
4184 * @returns VBox status code.
4185 * @param pImage The iSCSI image instance.
4186 */
4187static int iscsiOpenImageReportLuns(PISCSIIMAGE pImage)
4188{
4189 SCSIREQ sr;
4190 RTSGSEG DataSeg;
4191 uint8_t rlundata[16];
4192
4193 /*
4194 * Inquire available LUNs - purely dummy request.
4195 */
4196 RT_ZERO(sr.abCDB);
4197 sr.abCDB[0] = SCSI_REPORT_LUNS;
4198 sr.abCDB[1] = 0; /* reserved */
4199 sr.abCDB[2] = 0; /* reserved */
4200 sr.abCDB[3] = 0; /* reserved */
4201 sr.abCDB[4] = 0; /* reserved */
4202 sr.abCDB[5] = 0; /* reserved */
4203 sr.abCDB[6] = sizeof(rlundata) >> 24;
4204 sr.abCDB[7] = (sizeof(rlundata) >> 16) & 0xff;
4205 sr.abCDB[8] = (sizeof(rlundata) >> 8) & 0xff;
4206 sr.abCDB[9] = sizeof(rlundata) & 0xff;
4207 sr.abCDB[10] = 0; /* reserved */
4208 sr.abCDB[11] = 0; /* control */
4209
4210 DataSeg.pvSeg = rlundata;
4211 DataSeg.cbSeg = sizeof(rlundata);
4212
4213 sr.enmXfer = SCSIXFER_FROM_TARGET;
4214 sr.cbCDB = 12;
4215 sr.cbI2TData = 0;
4216 sr.paI2TSegs = NULL;
4217 sr.cI2TSegs = 0;
4218 sr.cbT2IData = DataSeg.cbSeg;
4219 sr.paT2ISegs = &DataSeg;
4220 sr.cT2ISegs = 1;
4221 sr.cbSense = sizeof(sr.abSense);
4222 int rc = iscsiCommandSync(pImage, &sr, false, VERR_INVALID_STATE);
4223 if (RT_FAILURE(rc))
4224 LogRel(("iSCSI: Could not get LUN info for target %s, rc=%Rrc\n", pImage->pszTargetName, rc));
4225
4226 return rc;
4227}
4228
4229/**
4230 * Issues the INQUIRY command to the target and checks for the correct device type.
4231 *
4232 * @returns VBox status code.
4233 * @param pImage The iSCSI image instance.
4234 */
4235static int iscsiOpenImageInquiry(PISCSIIMAGE pImage)
4236{
4237 SCSIREQ sr;
4238 RTSGSEG DataSeg;
4239 uint8_t data8[8];
4240
4241 /*
4242 * Inquire device characteristics - no tapes, scanners etc., please.
4243 */
4244 RT_ZERO(sr.abCDB);
4245 sr.abCDB[0] = SCSI_INQUIRY;
4246 sr.abCDB[1] = 0; /* reserved */
4247 sr.abCDB[2] = 0; /* reserved */
4248 sr.abCDB[3] = 0; /* reserved */
4249 sr.abCDB[4] = sizeof(data8);
4250 sr.abCDB[5] = 0; /* control */
4251
4252 DataSeg.pvSeg = data8;
4253 DataSeg.cbSeg = sizeof(data8);
4254
4255 sr.enmXfer = SCSIXFER_FROM_TARGET;
4256 sr.cbCDB = 6;
4257 sr.cbI2TData = 0;
4258 sr.paI2TSegs = NULL;
4259 sr.cI2TSegs = 0;
4260 sr.cbT2IData = DataSeg.cbSeg;
4261 sr.paT2ISegs = &DataSeg;
4262 sr.cT2ISegs = 1;
4263 sr.cbSense = sizeof(sr.abSense);
4264 int rc = iscsiCommandSync(pImage, &sr, true /* fRetry */, VERR_INVALID_STATE);
4265 if (RT_SUCCESS(rc))
4266 {
4267 uint8_t devType = (sr.cbT2IData > 0) ? data8[0] & SCSI_DEVTYPE_MASK : 255;
4268 if (devType == SCSI_DEVTYPE_DISK)
4269 {
4270 uint8_t uCmdQueue = (sr.cbT2IData >= 8) ? data8[7] & SCSI_INQUIRY_CMDQUE_MASK : 0;
4271 if (uCmdQueue > 0)
4272 pImage->fCmdQueuingSupported = true;
4273 else if (pImage->uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO)
4274 rc = VERR_NOT_SUPPORTED;
4275 else
4276 LogRel(("iSCSI: target address %s, target name %s, %s command queuing\n",
4277 pImage->pszTargetAddress, pImage->pszTargetName,
4278 pImage->fCmdQueuingSupported ? "supports" : "doesn't support"));
4279 }
4280 else
4281 {
4282 rc = vdIfError(pImage->pIfError, VERR_VD_ISCSI_INVALID_TYPE,
4283 RT_SRC_POS, N_("iSCSI: target address %s, target name %s, SCSI LUN %lld reports device type=%u"),
4284 pImage->pszTargetAddress, pImage->pszTargetName,
4285 pImage->LUN, devType);
4286 LogRel(("iSCSI: Unsupported SCSI peripheral device type %d for target %s\n", devType & SCSI_DEVTYPE_MASK, pImage->pszTargetName));
4287 }
4288 }
4289 else
4290 LogRel(("iSCSI: Could not get INQUIRY info for target %s, rc=%Rrc\n", pImage->pszTargetName, rc));
4291
4292 return rc;
4293}
4294
4295/**
4296 * Checks that the target allows write access if the caller requested it.
4297 *
4298 * @returns VBox status code.
4299 * @param pImage The iSCSI image instance.
4300 */
4301static int iscsiOpenImageCheckWriteAccess(PISCSIIMAGE pImage)
4302{
4303 SCSIREQ sr;
4304 RTSGSEG DataSeg;
4305 uint8_t data4[4];
4306
4307 /*
4308 * Query write disable bit in the device specific parameter entry in the
4309 * mode parameter header. Refuse read/write opening of read only disks.
4310 */
4311 RT_ZERO(sr.abCDB);
4312 sr.abCDB[0] = SCSI_MODE_SENSE_6;
4313 sr.abCDB[1] = 0; /* dbd=0/reserved */
4314 sr.abCDB[2] = 0x3f; /* pc=0/page code=0x3f, ask for all pages */
4315 sr.abCDB[3] = 0; /* subpage code=0, return everything in page_0 format */
4316 sr.abCDB[4] = sizeof(data4); /* allocation length=4 */
4317 sr.abCDB[5] = 0; /* control */
4318
4319 DataSeg.pvSeg = data4;
4320 DataSeg.cbSeg = sizeof(data4);
4321
4322 sr.enmXfer = SCSIXFER_FROM_TARGET;
4323 sr.cbCDB = 6;
4324 sr.cbI2TData = 0;
4325 sr.paI2TSegs = NULL;
4326 sr.cI2TSegs = 0;
4327 sr.cbT2IData = DataSeg.cbSeg;
4328 sr.paT2ISegs = &DataSeg;
4329 sr.cT2ISegs = 1;
4330 sr.cbSense = sizeof(sr.abSense);
4331 int rc = iscsiCommandSync(pImage, &sr, true /* fRetry */, VERR_INVALID_STATE);
4332 if (RT_SUCCESS(rc))
4333 {
4334 pImage->fTargetReadOnly = !!(data4[2] & 0x80);
4335 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY) && pImage->fTargetReadOnly)
4336 rc = VERR_VD_IMAGE_READ_ONLY;
4337 }
4338 else
4339 LogRel(("iSCSI: Could not get MODE SENSE info for target %s, rc=%Rrc\n", pImage->pszTargetName, rc));
4340
4341 return rc;
4342}
4343
4344/**
4345 * Queries the media and sector size of the target.
4346 *
4347 * @returns VBox status code.
4348 * @param pImage The iSCSI image instance.
4349 */
4350static int iscsiOpenImageQueryTargetSizes(PISCSIIMAGE pImage)
4351{
4352 SCSIREQ sr;
4353 RTSGSEG DataSeg;
4354 uint8_t data12[12];
4355
4356 /*
4357 * Determine sector size and capacity of the volume immediately.
4358 */
4359 RT_ZERO(data12);
4360 RT_ZERO(sr.abCDB);
4361 sr.abCDB[0] = SCSI_SERVICE_ACTION_IN_16;
4362 sr.abCDB[1] = SCSI_SVC_ACTION_IN_READ_CAPACITY_16; /* subcommand */
4363 sr.abCDB[10+3] = sizeof(data12); /* allocation length (dword) */
4364
4365 DataSeg.pvSeg = data12;
4366 DataSeg.cbSeg = sizeof(data12);
4367
4368 sr.enmXfer = SCSIXFER_FROM_TARGET;
4369 sr.cbCDB = 16;
4370 sr.cbI2TData = 0;
4371 sr.paI2TSegs = NULL;
4372 sr.cI2TSegs = 0;
4373 sr.cbT2IData = DataSeg.cbSeg;
4374 sr.paT2ISegs = &DataSeg;
4375 sr.cT2ISegs = 1;
4376 sr.cbSense = sizeof(sr.abSense);
4377
4378 int rc = iscsiCommandSync(pImage, &sr, false /* fRetry */, VINF_SUCCESS);
4379 if (RT_SUCCESS(rc))
4380 {
4381 bool fEnd = false;
4382 uint8_t cMaxRetries = 10;
4383 do
4384 {
4385 switch (sr.status)
4386 {
4387 case SCSI_STATUS_OK:
4388 {
4389 pImage->cVolume = RT_BE2H_U64(*(uint64_t *)&data12[0]);
4390 pImage->cVolume++;
4391 pImage->cbSector = RT_BE2H_U32(*(uint32_t *)&data12[8]);
4392 pImage->cbSize = pImage->cVolume * pImage->cbSector;
4393 if (pImage->cVolume == 0 || pImage->cbSize < pImage->cVolume)
4394 {
4395 rc = vdIfError(pImage->pIfError, VERR_VD_ISCSI_INVALID_TYPE,
4396 RT_SRC_POS, N_("iSCSI: target address %s, target name %s, SCSI LUN %lld reports media sector count=%llu sector size=%u"),
4397 pImage->pszTargetAddress, pImage->pszTargetName,
4398 pImage->LUN, pImage->cVolume, pImage->cbSector);
4399 }
4400 fEnd = true;
4401 break;
4402 }
4403 case SCSI_STATUS_CHECK_CONDITION:
4404 {
4405 if((sr.abSense[2] & 0x0f) == SCSI_SENSE_UNIT_ATTENTION)
4406 {
4407 if( sr.abSense[12] == SCSI_ASC_POWER_ON_RESET_BUS_DEVICE_RESET_OCCURRED
4408 && sr.abSense[13] == SCSI_ASCQ_POWER_ON_RESET_BUS_DEVICE_RESET_OCCURRED)
4409 {
4410 /** @todo for future: prepare and send command "REQUEST SENSE" which will
4411 * return the status of target and will clear any unit attention
4412 * condition that it reports */
4413 rc = iscsiCommandSync(pImage, &sr, false /* fRetry */, VINF_SUCCESS);
4414 if (RT_FAILURE(rc))
4415 fEnd = true;
4416 cMaxRetries--;
4417 break;
4418 }
4419 }
4420 break;
4421 }
4422 default:
4423 {
4424 rc = iscsiCommandSync(pImage, &sr, false /* fRetry */, VINF_SUCCESS);
4425 if (RT_FAILURE(rc))
4426 fEnd = true;
4427 cMaxRetries--;
4428 break;
4429 }
4430 }
4431 if (!cMaxRetries)
4432 fEnd = true;
4433 } while(!fEnd);
4434 }
4435 else
4436 {
4437 uint8_t data8[8];
4438
4439 RT_ZERO(data8);
4440 sr.abCDB[0] = SCSI_READ_CAPACITY;
4441 sr.abCDB[1] = 0; /* reserved */
4442 sr.abCDB[2] = 0; /* reserved */
4443 sr.abCDB[3] = 0; /* reserved */
4444 sr.abCDB[4] = 0; /* reserved */
4445 sr.abCDB[5] = 0; /* reserved */
4446 sr.abCDB[6] = 0; /* reserved */
4447 sr.abCDB[7] = 0; /* reserved */
4448 sr.abCDB[8] = 0; /* reserved */
4449 sr.abCDB[9] = 0; /* control */
4450
4451 DataSeg.pvSeg = data8;
4452 DataSeg.cbSeg = sizeof(data8);
4453
4454 sr.enmXfer = SCSIXFER_FROM_TARGET;
4455 sr.cbCDB = 10;
4456 sr.cbI2TData = 0;
4457 sr.paI2TSegs = NULL;
4458 sr.cI2TSegs = 0;
4459 sr.cbT2IData = DataSeg.cbSeg;
4460 sr.paT2ISegs = &DataSeg;
4461 sr.cT2ISegs = 1;
4462 sr.cbSense = sizeof(sr.abSense);
4463 rc = iscsiCommandSync(pImage, &sr, false /* fRetry */, VINF_SUCCESS);
4464 if (RT_SUCCESS(rc))
4465 {
4466 bool fEnd = false;
4467 uint8_t cMaxRetries = 10;
4468 do
4469 {
4470 switch (sr.status)
4471 {
4472 case SCSI_STATUS_OK:
4473 {
4474 pImage->cVolume = (data8[0] << 24) | (data8[1] << 16) | (data8[2] << 8) | data8[3];
4475 pImage->cVolume++;
4476 pImage->cbSector = (data8[4] << 24) | (data8[5] << 16) | (data8[6] << 8) | data8[7];
4477 pImage->cbSize = pImage->cVolume * pImage->cbSector;
4478 if (pImage->cVolume == 0)
4479 {
4480 rc = vdIfError(pImage->pIfError, VERR_VD_ISCSI_INVALID_TYPE,
4481 RT_SRC_POS, N_("iSCSI: fallback capacity detection for target address %s, target name %s, SCSI LUN %lld reports media sector count=%llu sector size=%u"),
4482 pImage->pszTargetAddress, pImage->pszTargetName,
4483 pImage->LUN, pImage->cVolume, pImage->cbSector);
4484 }
4485
4486 fEnd = true;
4487 break;
4488 }
4489 case SCSI_STATUS_CHECK_CONDITION:
4490 {
4491 if((sr.abSense[2] & 0x0f) == SCSI_SENSE_UNIT_ATTENTION)
4492 {
4493 if( sr.abSense[12] == SCSI_ASC_POWER_ON_RESET_BUS_DEVICE_RESET_OCCURRED
4494 && sr.abSense[13] == SCSI_ASCQ_POWER_ON_RESET_BUS_DEVICE_RESET_OCCURRED)
4495 {
4496 /** @todo for future: prepare and send command "REQUEST SENSE" which will
4497 * return the status of target and will clear any unit attention
4498 * condition that it reports */
4499 rc = iscsiCommandSync(pImage, &sr, false /* fRetry */, VINF_SUCCESS);
4500 if (RT_FAILURE(rc))
4501 fEnd = true;
4502 cMaxRetries--;
4503 break;
4504
4505 }
4506 }
4507 break;
4508 }
4509 default:
4510 {
4511 rc = iscsiCommandSync(pImage, &sr, false /* fRetry */, VINF_SUCCESS);
4512 if (RT_FAILURE(rc))
4513 fEnd = true;
4514 cMaxRetries--;
4515 break;
4516 }
4517 }
4518 if (!cMaxRetries)
4519 fEnd = true;
4520 } while(!fEnd);
4521 }
4522 else
4523 LogRel(("iSCSI: Could not determine capacity of target %s, rc=%Rrc\n", pImage->pszTargetName, rc));
4524 }
4525
4526 return rc;
4527}
4528
4529/**
4530 * Queries the state of the read/write caches and tries to enable them if disabled.
4531 *
4532 * @returns VBox status code.
4533 * @param pImage The iSCSI image instance.
4534 */
4535static int iscsiOpenImageEnableReadWriteCache(PISCSIIMAGE pImage)
4536{
4537 /*
4538 * Check the read and write cache bits.
4539 * Try to enable the cache if it is disabled.
4540 *
4541 * We already checked that this is a block access device. No need
4542 * to do it again.
4543 */
4544 SCSIREQ sr;
4545 RTSGSEG DataSeg;
4546 uint8_t aCachingModePage[32];
4547
4548 memset(aCachingModePage, '\0', sizeof(aCachingModePage));
4549 sr.abCDB[0] = SCSI_MODE_SENSE_6;
4550 sr.abCDB[1] = 0;
4551 sr.abCDB[2] = (0x00 << 6) | (0x08 & 0x3f); /* Current values and caching mode page */
4552 sr.abCDB[3] = 0; /* Sub page code. */
4553 sr.abCDB[4] = sizeof(aCachingModePage) & 0xff;
4554 sr.abCDB[5] = 0;
4555
4556 DataSeg.pvSeg = aCachingModePage;
4557 DataSeg.cbSeg = sizeof(aCachingModePage);
4558
4559 sr.enmXfer = SCSIXFER_FROM_TARGET;
4560 sr.cbCDB = 6;
4561 sr.cbI2TData = 0;
4562 sr.paI2TSegs = NULL;
4563 sr.cI2TSegs = 0;
4564 sr.cbT2IData = DataSeg.cbSeg;
4565 sr.paT2ISegs = &DataSeg;
4566 sr.cT2ISegs = 1;
4567 sr.cbSense = sizeof(sr.abSense);
4568 int rc = iscsiCommandSync(pImage, &sr, false /* fRetry */, VINF_SUCCESS);
4569 if ( RT_SUCCESS(rc)
4570 && (sr.status == SCSI_STATUS_OK)
4571 && (aCachingModePage[0] >= 15)
4572 && (aCachingModePage[4 + aCachingModePage[3]] & 0x3f) == 0x08
4573 && (aCachingModePage[4 + aCachingModePage[3] + 1] > 3))
4574 {
4575 uint32_t Offset = 4 + aCachingModePage[3];
4576 /*
4577 * Check if the read and/or the write cache is disabled.
4578 * The write cache is disabled if bit 2 (WCE) is zero and
4579 * the read cache is disabled if bit 0 (RCD) is set.
4580 */
4581 if (!ASMBitTest(&aCachingModePage[Offset + 2], 2) || ASMBitTest(&aCachingModePage[Offset + 2], 0))
4582 {
4583 /*
4584 * Write Cache Enable (WCE) bit is zero or the Read Cache Disable (RCD) is one
4585 * So one of the caches is disabled. Enable both caches.
4586 * The rest is unchanged.
4587 */
4588 ASMBitSet(&aCachingModePage[Offset + 2], 2);
4589 ASMBitClear(&aCachingModePage[Offset + 2], 0);
4590
4591 sr.abCDB[0] = SCSI_MODE_SELECT_6;
4592 sr.abCDB[1] = 0; /* Don't write the page into NV RAM. */
4593 sr.abCDB[2] = 0;
4594 sr.abCDB[3] = 0;
4595 sr.abCDB[4] = sizeof(aCachingModePage) & 0xff;
4596 sr.abCDB[5] = 0;
4597
4598 DataSeg.pvSeg = aCachingModePage;
4599 DataSeg.cbSeg = sizeof(aCachingModePage);
4600
4601 sr.enmXfer = SCSIXFER_TO_TARGET;
4602 sr.cbCDB = 6;
4603 sr.cbI2TData = DataSeg.cbSeg;
4604 sr.paI2TSegs = &DataSeg;
4605 sr.cI2TSegs = 1;
4606 sr.cbT2IData = 0;
4607 sr.paT2ISegs = NULL;
4608 sr.cT2ISegs = 0;
4609 sr.cbSense = sizeof(sr.abSense);
4610 sr.status = 0;
4611 rc = iscsiCommandSync(pImage, &sr, false /* fRetry */, VINF_SUCCESS);
4612 if ( RT_SUCCESS(rc)
4613 && (sr.status == SCSI_STATUS_OK))
4614 LogRel(("iSCSI: Enabled read and write cache of target %s\n", pImage->pszTargetName));
4615 else
4616 {
4617 /* Log failures but continue. */
4618 LogRel(("iSCSI: Could not enable read and write cache of target %s, rc=%Rrc status=%#x\n",
4619 pImage->pszTargetName, rc, sr.status));
4620 LogRel(("iSCSI: Sense:\n%.*Rhxd\n", sr.cbSense, sr.abSense));
4621 rc = VINF_SUCCESS;
4622 }
4623 }
4624 }
4625 else
4626 {
4627 /* Log errors but continue. */
4628 LogRel(("iSCSI: Could not check write cache of target %s, rc=%Rrc, got mode page %#x\n", pImage->pszTargetName, rc, aCachingModePage[0] & 0x3f));
4629 LogRel(("iSCSI: Sense:\n%.*Rhxd\n", sr.cbSense, sr.abSense));
4630 rc = VINF_SUCCESS;
4631 }
4632
4633 return rc;
4634}
4635
4636/**
4637 * Internal: Open an image, constructing all necessary data structures.
4638 */
4639static int iscsiOpenImage(PISCSIIMAGE pImage, unsigned uOpenFlags)
4640{
4641 pImage->uOpenFlags = uOpenFlags;
4642
4643 int rc = iscsiOpenImageInit(pImage);
4644 if (RT_SUCCESS(rc))
4645 rc = iscsiOpenImageParseCfg(pImage);
4646
4647 if (RT_SUCCESS(rc))
4648 {
4649 /* Don't actually establish iSCSI transport connection if this is just an
4650 * open to query the image information and the host IP stack isn't used.
4651 * Even trying is rather useless, as in this context the InTnet IP stack
4652 * isn't present. Returning dummies is the best possible result anyway. */
4653 if ((uOpenFlags & VD_OPEN_FLAGS_INFO) && !pImage->fHostIP)
4654 LogFunc(("Not opening the transport connection as IntNet IP stack is not available. Will return dummies\n"));
4655 else
4656 {
4657 rc = iscsiOpenImageSocketCreate(pImage);
4658 if (RT_SUCCESS(rc))
4659 {
4660 /*
4661 * Attach to the iSCSI target. This implicitly establishes the iSCSI
4662 * transport connection.
4663 */
4664 rc = iscsiExecSync(pImage, iscsiAttach, pImage);
4665 if (RT_SUCCESS(rc))
4666 {
4667 LogFlowFunc(("target '%s' opened successfully\n", pImage->pszTargetName));
4668
4669 rc = iscsiOpenImageReportLuns(pImage);
4670 if (RT_SUCCESS(rc))
4671 rc = iscsiOpenImageInquiry(pImage);
4672 if (RT_SUCCESS(rc))
4673 rc = iscsiOpenImageCheckWriteAccess(pImage);
4674 if (RT_SUCCESS(rc))
4675 rc = iscsiOpenImageQueryTargetSizes(pImage);
4676 if (RT_SUCCESS(rc))
4677 rc = iscsiOpenImageEnableReadWriteCache(pImage);
4678 }
4679 else
4680 LogRel(("iSCSI: could not open target %s, rc=%Rrc\n", pImage->pszTargetName, rc));
4681 }
4682 }
4683 }
4684
4685 if (RT_FAILURE(rc))
4686 iscsiFreeImage(pImage, false);
4687 return rc;
4688}
4689
4690
4691/** @copydoc VDIMAGEBACKEND::pfnProbe */
4692static DECLCALLBACK(int) iscsiProbe(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
4693 PVDINTERFACE pVDIfsImage, VDTYPE *penmType)
4694{
4695 RT_NOREF4(pszFilename, pVDIfsDisk, pVDIfsImage, penmType);
4696 LogFlowFunc(("pszFilename=\"%s\"\n", pszFilename));
4697
4698 /* iSCSI images can't be checked for validity this way, as the filename
4699 * just can't supply enough configuration information. */
4700 int rc = VERR_VD_ISCSI_INVALID_HEADER;
4701
4702 LogFlowFunc(("returns %Rrc\n", rc));
4703 return rc;
4704}
4705
4706/** @copydoc VDIMAGEBACKEND::pfnOpen */
4707static DECLCALLBACK(int) iscsiOpen(const char *pszFilename, unsigned uOpenFlags,
4708 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
4709 VDTYPE enmType, void **ppBackendData)
4710{
4711 RT_NOREF1(enmType); /**< @todo r=klaus make use of the type info. */
4712
4713 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p enmType=%u ppBackendData=%#p\n",
4714 pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, enmType, ppBackendData));
4715 int rc;
4716
4717 /* Check open flags. All valid flags are supported. */
4718 AssertReturn(!(uOpenFlags & ~VD_OPEN_FLAGS_MASK), VERR_INVALID_PARAMETER);
4719 AssertReturn((VALID_PTR(pszFilename) && *pszFilename), VERR_INVALID_PARAMETER);
4720
4721 PISCSIIMAGE pImage = (PISCSIIMAGE)RTMemAllocZ(sizeof(ISCSIIMAGE));
4722 if (RT_LIKELY(pImage))
4723 {
4724 pImage->pszFilename = pszFilename;
4725 pImage->pszInitiatorName = NULL;
4726 pImage->pszTargetName = NULL;
4727 pImage->pszTargetAddress = NULL;
4728 pImage->pszInitiatorUsername = NULL;
4729 pImage->pbInitiatorSecret = NULL;
4730 pImage->pszTargetUsername = NULL;
4731 pImage->pbTargetSecret = NULL;
4732 pImage->paCurrReq = NULL;
4733 pImage->pvRecvPDUBuf = NULL;
4734 pImage->pszHostname = NULL;
4735 pImage->pVDIfsDisk = pVDIfsDisk;
4736 pImage->pVDIfsImage = pVDIfsImage;
4737 pImage->cLogRelErrors = 0;
4738
4739 rc = iscsiOpenImage(pImage, uOpenFlags);
4740 if (RT_SUCCESS(rc))
4741 {
4742 LogFlowFunc(("target %s cVolume %d, cbSector %d\n", pImage->pszTargetName, pImage->cVolume, pImage->cbSector));
4743 LogRel(("iSCSI: target address %s, target name %s, SCSI LUN %lld\n", pImage->pszTargetAddress, pImage->pszTargetName, pImage->LUN));
4744 *ppBackendData = pImage;
4745 }
4746 else
4747 RTMemFree(pImage);
4748 }
4749 else
4750 rc = VERR_NO_MEMORY;
4751
4752 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
4753 return rc;
4754}
4755
4756/** @copydoc VDIMAGEBACKEND::pfnCreate */
4757static DECLCALLBACK(int) iscsiCreate(const char *pszFilename, uint64_t cbSize,
4758 unsigned uImageFlags, const char *pszComment,
4759 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
4760 PCRTUUID pUuid, unsigned uOpenFlags,
4761 unsigned uPercentStart, unsigned uPercentSpan,
4762 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
4763 PVDINTERFACE pVDIfsOperation, VDTYPE enmType,
4764 void **ppBackendData)
4765{
4766 RT_NOREF8(pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags);
4767 RT_NOREF7(uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData);
4768 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 enmType=%u ppBackendData=%#p",
4769 pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData));
4770 int rc = VERR_NOT_SUPPORTED;
4771
4772 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
4773 return rc;
4774}
4775
4776/** @copydoc VDIMAGEBACKEND::pfnClose */
4777static DECLCALLBACK(int) iscsiClose(void *pBackendData, bool fDelete)
4778{
4779 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
4780 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
4781 int rc;
4782
4783 Assert(!fDelete); /* This flag is unsupported. */
4784
4785 rc = iscsiFreeImage(pImage, fDelete);
4786 RTMemFree(pImage);
4787
4788 LogFlowFunc(("returns %Rrc\n", rc));
4789 return rc;
4790}
4791
4792/** @copydoc VDIMAGEBACKEND::pfnRead */
4793static DECLCALLBACK(int) iscsiRead(void *pBackendData, uint64_t uOffset, size_t cbToRead,
4794 PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
4795{
4796 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
4797 int rc = VINF_SUCCESS;
4798
4799 LogFlowFunc(("pBackendData=%p uOffset=%#llx pIoCtx=%#p cbToRead=%u pcbActuallyRead=%p\n",
4800 pBackendData, uOffset, pIoCtx, cbToRead, pcbActuallyRead));
4801
4802 if ( uOffset + cbToRead > pImage->cbSize
4803 || cbToRead == 0)
4804 return VERR_INVALID_PARAMETER;
4805
4806 /*
4807 * Clip read size to a value which is supported by the target.
4808 */
4809 cbToRead = RT_MIN(cbToRead, pImage->cbRecvDataLength);
4810
4811 unsigned cT2ISegs = 0;
4812 size_t cbSegs = 0;
4813
4814 /* Get the number of segments. */
4815 cbSegs = pImage->pIfIo->pfnIoCtxSegArrayCreate(pImage->pIfIo->Core.pvUser, pIoCtx,
4816 NULL, &cT2ISegs, cbToRead);
4817 Assert(cbSegs == cbToRead);
4818
4819 PSCSIREQ pReq = (PSCSIREQ)RTMemAllocZ(RT_OFFSETOF(SCSIREQ, aSegs[cT2ISegs]));
4820 if (RT_LIKELY(pReq))
4821 {
4822 uint64_t lba;
4823 uint16_t tls;
4824 uint8_t *pbCDB = &pReq->abCDB[0];
4825 size_t cbCDB;
4826
4827 lba = uOffset / pImage->cbSector;
4828 tls = (uint16_t)(cbToRead / pImage->cbSector);
4829
4830 cbSegs = pImage->pIfIo->pfnIoCtxSegArrayCreate(pImage->pIfIo->Core.pvUser, pIoCtx,
4831 &pReq->aSegs[0],
4832 &cT2ISegs, cbToRead);
4833 Assert(cbSegs == cbToRead);
4834
4835 if (pImage->cVolume < _4G)
4836 {
4837 cbCDB = 10;
4838 pbCDB[0] = SCSI_READ_10;
4839 pbCDB[1] = 0; /* reserved */
4840 pbCDB[2] = (lba >> 24) & 0xff;
4841 pbCDB[3] = (lba >> 16) & 0xff;
4842 pbCDB[4] = (lba >> 8) & 0xff;
4843 pbCDB[5] = lba & 0xff;
4844 pbCDB[6] = 0; /* reserved */
4845 pbCDB[7] = (tls >> 8) & 0xff;
4846 pbCDB[8] = tls & 0xff;
4847 pbCDB[9] = 0; /* control */
4848 }
4849 else
4850 {
4851 cbCDB = 16;
4852 pbCDB[0] = SCSI_READ_16;
4853 pbCDB[1] = 0; /* reserved */
4854 pbCDB[2] = (lba >> 56) & 0xff;
4855 pbCDB[3] = (lba >> 48) & 0xff;
4856 pbCDB[4] = (lba >> 40) & 0xff;
4857 pbCDB[5] = (lba >> 32) & 0xff;
4858 pbCDB[6] = (lba >> 24) & 0xff;
4859 pbCDB[7] = (lba >> 16) & 0xff;
4860 pbCDB[8] = (lba >> 8) & 0xff;
4861 pbCDB[9] = lba & 0xff;
4862 pbCDB[10] = 0; /* tls unused */
4863 pbCDB[11] = 0; /* tls unused */
4864 pbCDB[12] = (tls >> 8) & 0xff;
4865 pbCDB[13] = tls & 0xff;
4866 pbCDB[14] = 0; /* reserved */
4867 pbCDB[15] = 0; /* reserved */
4868 }
4869
4870 pReq->enmXfer = SCSIXFER_FROM_TARGET;
4871 pReq->cbCDB = cbCDB;
4872 pReq->cbI2TData = 0;
4873 pReq->paI2TSegs = NULL;
4874 pReq->cI2TSegs = 0;
4875 pReq->cbT2IData = cbToRead;
4876 pReq->paT2ISegs = &pReq->aSegs[pReq->cI2TSegs];
4877 pReq->cbSense = sizeof(pReq->abSense);
4878 pReq->cT2ISegs = cT2ISegs;
4879 pReq->pIoCtx = pIoCtx;
4880 pReq->cSenseRetries = 10;
4881 pReq->rcSense = VERR_READ_ERROR;
4882
4883 if (vdIfIoIntIoCtxIsSynchronous(pImage->pIfIo, pIoCtx))
4884 {
4885 rc = iscsiCommandSync(pImage, pReq, true, VERR_READ_ERROR);
4886 if (RT_FAILURE(rc))
4887 {
4888 LogFlow(("iscsiCommandSync(%s, %#llx) -> %Rrc\n", pImage->pszTargetName, uOffset, rc));
4889 *pcbActuallyRead = 0;
4890 }
4891 else
4892 *pcbActuallyRead = pReq->cbT2IData;
4893 }
4894 else
4895 {
4896 rc = iscsiCommandAsync(pImage, pReq, iscsiCommandAsyncComplete, pReq);
4897 if (RT_FAILURE(rc))
4898 AssertMsgFailed(("iscsiCommandAsync(%s, %#llx) -> %Rrc\n", pImage->pszTargetName, uOffset, rc));
4899 else
4900 {
4901 *pcbActuallyRead = cbToRead;
4902 return VERR_VD_IOCTX_HALT; /* Halt the I/O context until further notification from the I/O thread. */
4903 }
4904 }
4905
4906 RTMemFree(pReq);
4907 }
4908 else
4909 rc = VERR_NO_MEMORY;
4910
4911 LogFlowFunc(("returns rc=%Rrc\n", rc));
4912 return rc;
4913}
4914
4915/** @copydoc VDIMAGEBACKEND::pfnWrite */
4916static DECLCALLBACK(int) iscsiWrite(void *pBackendData, uint64_t uOffset, size_t cbToWrite,
4917 PVDIOCTX pIoCtx, size_t *pcbWriteProcess, size_t *pcbPreRead,
4918 size_t *pcbPostRead, unsigned fWrite)
4919{
4920 RT_NOREF3(pcbPreRead, pcbPostRead, fWrite);
4921 LogFlowFunc(("pBackendData=%p uOffset=%llu pIoCtx=%#p cbToWrite=%u pcbWriteProcess=%p pcbPreRead=%p pcbPostRead=%p fWrite=%u\n",
4922 pBackendData, uOffset, pIoCtx, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead, fWrite));
4923 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
4924 int rc = VINF_SUCCESS;
4925
4926 AssertPtr(pImage);
4927 Assert(uOffset % 512 == 0);
4928 Assert(cbToWrite % 512 == 0);
4929
4930 if (uOffset + cbToWrite > pImage->cbSize)
4931 return VERR_INVALID_PARAMETER;
4932
4933 /*
4934 * Clip read size to a value which is supported by the target.
4935 */
4936 cbToWrite = RT_MIN(cbToWrite, pImage->cbSendDataLength);
4937
4938 unsigned cI2TSegs = 0;
4939 size_t cbSegs = 0;
4940
4941 /* Get the number of segments. */
4942 cbSegs = pImage->pIfIo->pfnIoCtxSegArrayCreate(pImage->pIfIo->Core.pvUser, pIoCtx,
4943 NULL, &cI2TSegs, cbToWrite);
4944 Assert(cbSegs == cbToWrite);
4945
4946 PSCSIREQ pReq = (PSCSIREQ)RTMemAllocZ(RT_OFFSETOF(SCSIREQ, aSegs[cI2TSegs]));
4947 if (RT_LIKELY(pReq))
4948 {
4949 uint64_t lba;
4950 uint16_t tls;
4951 uint8_t *pbCDB = &pReq->abCDB[0];
4952 size_t cbCDB;
4953
4954 lba = uOffset / pImage->cbSector;
4955 tls = (uint16_t)(cbToWrite / pImage->cbSector);
4956
4957 cbSegs = pImage->pIfIo->pfnIoCtxSegArrayCreate(pImage->pIfIo->Core.pvUser, pIoCtx,
4958 &pReq->aSegs[0],
4959 &cI2TSegs, cbToWrite);
4960 Assert(cbSegs == cbToWrite);
4961
4962 if (pImage->cVolume < _4G)
4963 {
4964 cbCDB = 10;
4965 pbCDB[0] = SCSI_WRITE_10;
4966 pbCDB[1] = 0; /* reserved */
4967 pbCDB[2] = (lba >> 24) & 0xff;
4968 pbCDB[3] = (lba >> 16) & 0xff;
4969 pbCDB[4] = (lba >> 8) & 0xff;
4970 pbCDB[5] = lba & 0xff;
4971 pbCDB[6] = 0; /* reserved */
4972 pbCDB[7] = (tls >> 8) & 0xff;
4973 pbCDB[8] = tls & 0xff;
4974 pbCDB[9] = 0; /* control */
4975 }
4976 else
4977 {
4978 cbCDB = 16;
4979 pbCDB[0] = SCSI_WRITE_16;
4980 pbCDB[1] = 0; /* reserved */
4981 pbCDB[2] = (lba >> 56) & 0xff;
4982 pbCDB[3] = (lba >> 48) & 0xff;
4983 pbCDB[4] = (lba >> 40) & 0xff;
4984 pbCDB[5] = (lba >> 32) & 0xff;
4985 pbCDB[6] = (lba >> 24) & 0xff;
4986 pbCDB[7] = (lba >> 16) & 0xff;
4987 pbCDB[8] = (lba >> 8) & 0xff;
4988 pbCDB[9] = lba & 0xff;
4989 pbCDB[10] = 0; /* tls unused */
4990 pbCDB[11] = 0; /* tls unused */
4991 pbCDB[12] = (tls >> 8) & 0xff;
4992 pbCDB[13] = tls & 0xff;
4993 pbCDB[14] = 0; /* reserved */
4994 pbCDB[15] = 0; /* reserved */
4995 }
4996
4997 pReq->enmXfer = SCSIXFER_TO_TARGET;
4998 pReq->cbCDB = cbCDB;
4999 pReq->cbI2TData = cbToWrite;
5000 pReq->paI2TSegs = &pReq->aSegs[0];
5001 pReq->cI2TSegs = cI2TSegs;
5002 pReq->cbT2IData = 0;
5003 pReq->paT2ISegs = NULL;
5004 pReq->cT2ISegs = 0;
5005 pReq->cbSense = sizeof(pReq->abSense);
5006 pReq->pIoCtx = pIoCtx;
5007 pReq->cSenseRetries = 10;
5008 pReq->rcSense = VERR_WRITE_ERROR;
5009
5010 if (vdIfIoIntIoCtxIsSynchronous(pImage->pIfIo, pIoCtx))
5011 {
5012 rc = iscsiCommandSync(pImage, pReq, true, VERR_WRITE_ERROR);
5013 if (RT_FAILURE(rc))
5014 {
5015 LogFlow(("iscsiCommandSync(%s, %#llx) -> %Rrc\n", pImage->pszTargetName, uOffset, rc));
5016 *pcbWriteProcess = 0;
5017 }
5018 else
5019 *pcbWriteProcess = cbToWrite;
5020 }
5021 else
5022 {
5023 rc = iscsiCommandAsync(pImage, pReq, iscsiCommandAsyncComplete, pReq);
5024 if (RT_FAILURE(rc))
5025 AssertMsgFailed(("iscsiCommandAsync(%s, %#llx) -> %Rrc\n", pImage->pszTargetName, uOffset, rc));
5026 else
5027 {
5028 *pcbWriteProcess = cbToWrite;
5029 return VERR_VD_IOCTX_HALT; /* Halt the I/O context until further notification from the I/O thread. */
5030 }
5031 }
5032
5033 RTMemFree(pReq);
5034 }
5035 else
5036 rc = VERR_NO_MEMORY;
5037
5038 LogFlowFunc(("returns rc=%Rrc\n", rc));
5039 return rc;
5040}
5041
5042/** @copydoc VDIMAGEBACKEND::pfnFlush */
5043static DECLCALLBACK(int) iscsiFlush(void *pBackendData, PVDIOCTX pIoCtx)
5044{
5045 LogFlowFunc(("pBackendData=%p pIoCtx=%#p\n", pBackendData, pIoCtx));
5046 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5047 int rc = VINF_SUCCESS;
5048
5049 PSCSIREQ pReq = (PSCSIREQ)RTMemAllocZ(sizeof(SCSIREQ));
5050 if (RT_LIKELY(pReq))
5051 {
5052 uint8_t *pbCDB = &pReq->abCDB[0];
5053
5054 pbCDB[0] = SCSI_SYNCHRONIZE_CACHE;
5055 pbCDB[1] = 0; /* reserved */
5056 pbCDB[2] = 0; /* reserved */
5057 pbCDB[3] = 0; /* reserved */
5058 pbCDB[4] = 0; /* reserved */
5059 pbCDB[5] = 0; /* reserved */
5060 pbCDB[6] = 0; /* reserved */
5061 pbCDB[7] = 0; /* reserved */
5062 pbCDB[8] = 0; /* reserved */
5063 pbCDB[9] = 0; /* control */
5064
5065 pReq->enmXfer = SCSIXFER_NONE;
5066 pReq->cbCDB = 10;
5067 pReq->cbI2TData = 0;
5068 pReq->paI2TSegs = NULL;
5069 pReq->cI2TSegs = 0;
5070 pReq->cbT2IData = 0;
5071 pReq->paT2ISegs = NULL;
5072 pReq->cT2ISegs = 0;
5073 pReq->cbSense = sizeof(pReq->abSense);
5074 pReq->pIoCtx = pIoCtx;
5075 pReq->cSenseRetries = 0;
5076 pReq->rcSense = VINF_SUCCESS;
5077
5078 if (vdIfIoIntIoCtxIsSynchronous(pImage->pIfIo, pIoCtx))
5079 {
5080 rc = iscsiCommandSync(pImage, pReq, false, VINF_SUCCESS);
5081 if (RT_FAILURE(rc))
5082 AssertMsgFailed(("iscsiCommand(%s) -> %Rrc\n", pImage->pszTargetName, rc));
5083 }
5084 else
5085 {
5086 rc = iscsiCommandAsync(pImage, pReq, iscsiCommandAsyncComplete, pReq);
5087 if (RT_FAILURE(rc))
5088 AssertMsgFailed(("iscsiCommand(%s) -> %Rrc\n", pImage->pszTargetName, rc));
5089 else
5090 return VERR_VD_IOCTX_HALT; /* Halt the I/O context until further notification from the I/O thread. */
5091 }
5092
5093 RTMemFree(pReq);
5094 }
5095 else
5096 rc = VERR_NO_MEMORY;
5097
5098 LogFlowFunc(("returns rc=%Rrc\n", rc));
5099 return rc;
5100}
5101
5102/** @copydoc VDIMAGEBACKEND::pfnGetVersion */
5103static DECLCALLBACK(unsigned) iscsiGetVersion(void *pBackendData)
5104{
5105 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
5106 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5107
5108 AssertPtr(pImage);
5109 RT_NOREF1(pImage);
5110
5111 return 0;
5112}
5113
5114/** @copydoc VDIMAGEBACKEND::pfnGetSectorSize */
5115static DECLCALLBACK(uint32_t) iscsiGetSectorSize(void *pBackendData)
5116{
5117 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
5118 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5119
5120 AssertPtrReturn(pImage, 0);
5121
5122 return pImage->cbSector;
5123}
5124
5125/** @copydoc VDIMAGEBACKEND::pfnGetSize */
5126static DECLCALLBACK(uint64_t) iscsiGetSize(void *pBackendData)
5127{
5128 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
5129 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5130
5131 AssertPtrReturn(pImage, 0);
5132
5133 return pImage->cbSize;
5134}
5135
5136/** @copydoc VDIMAGEBACKEND::pfnGetFileSize */
5137static DECLCALLBACK(uint64_t) iscsiGetFileSize(void *pBackendData)
5138{
5139 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
5140 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5141
5142 AssertPtrReturn(pImage, 0);
5143
5144 return pImage->cbSize;
5145}
5146
5147/** @copydoc VDIMAGEBACKEND::pfnGetPCHSGeometry */
5148static DECLCALLBACK(int) iscsiGetPCHSGeometry(void *pBackendData, PVDGEOMETRY pPCHSGeometry)
5149{
5150 RT_NOREF1(pPCHSGeometry);
5151 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
5152 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5153
5154 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
5155
5156 LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", VERR_VD_GEOMETRY_NOT_SET,
5157 pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
5158 return VERR_VD_GEOMETRY_NOT_SET;
5159}
5160
5161/** @copydoc VDIMAGEBACKEND::pfnSetPCHSGeometry */
5162static DECLCALLBACK(int) iscsiSetPCHSGeometry(void *pBackendData, PCVDGEOMETRY pPCHSGeometry)
5163{
5164 RT_NOREF1(pPCHSGeometry);
5165 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
5166 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5167
5168 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
5169
5170 int rc;
5171 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
5172 rc = VERR_VD_IMAGE_READ_ONLY;
5173 else
5174 rc = VERR_VD_GEOMETRY_NOT_SET;
5175
5176 LogFlowFunc(("returns %Rrc\n", rc));
5177 return rc;
5178}
5179
5180/** @copydoc VDIMAGEBACKEND::pfnGetLCHSGeometry */
5181static DECLCALLBACK(int) iscsiGetLCHSGeometry(void *pBackendData, PVDGEOMETRY pLCHSGeometry)
5182{
5183 RT_NOREF1(pLCHSGeometry);
5184 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
5185 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5186
5187 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
5188
5189 LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", VERR_VD_GEOMETRY_NOT_SET,
5190 pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
5191 return VERR_VD_GEOMETRY_NOT_SET;
5192}
5193
5194/** @copydoc VDIMAGEBACKEND::pfnSetLCHSGeometry */
5195static DECLCALLBACK(int) iscsiSetLCHSGeometry(void *pBackendData, PCVDGEOMETRY pLCHSGeometry)
5196{
5197 RT_NOREF1(pLCHSGeometry);
5198 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
5199 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5200
5201 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
5202
5203 int rc;
5204 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
5205 rc = VERR_VD_IMAGE_READ_ONLY;
5206 else
5207 rc = VERR_VD_GEOMETRY_NOT_SET;
5208
5209 LogFlowFunc(("returns %Rrc\n", rc));
5210 return rc;
5211}
5212
5213/** @copydoc VDIMAGEBACKEND::pfnGetImageFlags */
5214static DECLCALLBACK(unsigned) iscsiGetImageFlags(void *pBackendData)
5215{
5216 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
5217 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5218
5219 AssertPtrReturn(pImage, 0);
5220
5221 LogFlowFunc(("returns %#x\n", VD_IMAGE_FLAGS_FIXED));
5222 return VD_IMAGE_FLAGS_FIXED;
5223}
5224
5225/** @copydoc VDIMAGEBACKEND::pfnGetOpenFlags */
5226static DECLCALLBACK(unsigned) iscsiGetOpenFlags(void *pBackendData)
5227{
5228 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
5229 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5230
5231 AssertPtrReturn(pImage, 0);
5232
5233 LogFlowFunc(("returns %#x\n", pImage->uOpenFlags));
5234 return pImage->uOpenFlags;
5235}
5236
5237/** @copydoc VDIMAGEBACKEND::pfnSetOpenFlags */
5238static DECLCALLBACK(int) iscsiSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
5239{
5240 LogFlowFunc(("pBackendData=%#p uOpenFlags=%#x\n", pBackendData, uOpenFlags));
5241 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5242 int rc = VINF_SUCCESS;
5243
5244 /* Image must be opened and the new flags must be valid. */
5245 AssertReturn(pImage && !(uOpenFlags & ~( VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO
5246 | VD_OPEN_FLAGS_ASYNC_IO | VD_OPEN_FLAGS_SHAREABLE
5247 | VD_OPEN_FLAGS_SEQUENTIAL | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)),
5248 VERR_INVALID_PARAMETER);
5249
5250 /*
5251 * A read/write -> readonly transition is always possible,
5252 * for the reverse direction check that the target didn't present itself
5253 * as readonly during the first attach.
5254 */
5255 if ( !(uOpenFlags & VD_OPEN_FLAGS_READONLY)
5256 && (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
5257 && pImage->fTargetReadOnly)
5258 rc = VERR_VD_IMAGE_READ_ONLY;
5259 else
5260 {
5261 pImage->uOpenFlags = uOpenFlags;
5262 pImage->fTryReconnect = true;
5263 }
5264
5265 LogFlowFunc(("returns %Rrc\n", rc));
5266 return rc;
5267}
5268
5269/** @copydoc VDIMAGEBACKEND::pfnGetComment */
5270static DECLCALLBACK(int) iscsiGetComment(void *pBackendData, char *pszComment,
5271 size_t cbComment)
5272{
5273 RT_NOREF2(pszComment, cbComment);
5274 LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
5275 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5276
5277 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
5278
5279 LogFlowFunc(("returns %Rrc comment='%s'\n", VERR_NOT_SUPPORTED, pszComment));
5280 return VERR_NOT_SUPPORTED;
5281}
5282
5283/** @copydoc VDIMAGEBACKEND::pfnSetComment */
5284static DECLCALLBACK(int) iscsiSetComment(void *pBackendData, const char *pszComment)
5285{
5286 RT_NOREF1(pszComment);
5287 LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
5288 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5289
5290 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
5291
5292 int rc;
5293 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
5294 rc = VERR_NOT_SUPPORTED;
5295 else
5296 rc = VERR_VD_IMAGE_READ_ONLY;
5297
5298 LogFlowFunc(("returns %Rrc\n", rc));
5299 return rc;
5300}
5301
5302/** @copydoc VDIMAGEBACKEND::pfnGetUuid */
5303static DECLCALLBACK(int) iscsiGetUuid(void *pBackendData, PRTUUID pUuid)
5304{
5305 RT_NOREF1(pUuid);
5306 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
5307 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5308
5309 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
5310
5311 LogFlowFunc(("returns %Rrc (%RTuuid)\n", VERR_NOT_SUPPORTED, pUuid));
5312 return VERR_NOT_SUPPORTED;
5313}
5314
5315/** @copydoc VDIMAGEBACKEND::pfnSetUuid */
5316static DECLCALLBACK(int) iscsiSetUuid(void *pBackendData, PCRTUUID pUuid)
5317{
5318 RT_NOREF1(pUuid);
5319 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
5320 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5321
5322 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
5323
5324 int rc;
5325 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
5326 rc = VERR_NOT_SUPPORTED;
5327 else
5328 rc = VERR_VD_IMAGE_READ_ONLY;
5329
5330 LogFlowFunc(("returns %Rrc\n", rc));
5331 return rc;
5332}
5333
5334/** @copydoc VDIMAGEBACKEND::pfnGetModificationUuid */
5335static DECLCALLBACK(int) iscsiGetModificationUuid(void *pBackendData, PRTUUID pUuid)
5336{
5337 RT_NOREF1(pUuid);
5338 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
5339 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5340
5341 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
5342
5343 LogFlowFunc(("returns %Rrc (%RTuuid)\n", VERR_NOT_SUPPORTED, pUuid));
5344 return VERR_NOT_SUPPORTED;
5345}
5346
5347/** @copydoc VDIMAGEBACKEND::pfnSetModificationUuid */
5348static DECLCALLBACK(int) iscsiSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
5349{
5350 RT_NOREF1(pUuid);
5351 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
5352 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5353
5354 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
5355
5356 int rc;
5357 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
5358 rc = VERR_NOT_SUPPORTED;
5359 else
5360 rc = VERR_VD_IMAGE_READ_ONLY;
5361
5362 LogFlowFunc(("returns %Rrc\n", rc));
5363 return rc;
5364}
5365
5366/** @copydoc VDIMAGEBACKEND::pfnGetParentUuid */
5367static DECLCALLBACK(int) iscsiGetParentUuid(void *pBackendData, PRTUUID pUuid)
5368{
5369 RT_NOREF1(pUuid);
5370 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
5371 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5372
5373 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
5374
5375 LogFlowFunc(("returns %Rrc (%RTuuid)\n", VERR_NOT_SUPPORTED, pUuid));
5376 return VERR_NOT_SUPPORTED;
5377}
5378
5379/** @copydoc VDIMAGEBACKEND::pfnSetParentUuid */
5380static DECLCALLBACK(int) iscsiSetParentUuid(void *pBackendData, PCRTUUID pUuid)
5381{
5382 RT_NOREF1(pUuid);
5383 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
5384 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5385
5386 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
5387
5388 int rc;
5389 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
5390 rc = VERR_NOT_SUPPORTED;
5391 else
5392 rc = VERR_VD_IMAGE_READ_ONLY;
5393
5394 LogFlowFunc(("returns %Rrc\n", rc));
5395 return rc;
5396}
5397
5398/** @copydoc VDIMAGEBACKEND::pfnGetParentModificationUuid */
5399static DECLCALLBACK(int) iscsiGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
5400{
5401 RT_NOREF1(pUuid);
5402 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
5403 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5404
5405 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
5406
5407 LogFlowFunc(("returns %Rrc (%RTuuid)\n", VERR_NOT_SUPPORTED, pUuid));
5408 return VERR_NOT_SUPPORTED;
5409}
5410
5411/** @copydoc VDIMAGEBACKEND::pfnSetParentModificationUuid */
5412static DECLCALLBACK(int) iscsiSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
5413{
5414 RT_NOREF1(pUuid);
5415 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
5416 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5417
5418 AssertPtrReturn(pImage, VERR_VD_NOT_OPENED);
5419
5420 int rc;
5421 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
5422 rc = VERR_NOT_SUPPORTED;
5423 else
5424 rc = VERR_VD_IMAGE_READ_ONLY;
5425
5426 LogFlowFunc(("returns %Rrc\n", rc));
5427 return rc;
5428}
5429
5430/** @copydoc VDIMAGEBACKEND::pfnDump */
5431static DECLCALLBACK(void) iscsiDump(void *pBackendData)
5432{
5433 PISCSIIMAGE pImage = (PISCSIIMAGE)pBackendData;
5434
5435 AssertPtrReturnVoid(pImage);
5436 /** @todo put something useful here */
5437 vdIfErrorMessage(pImage->pIfError, "Header: cVolume=%u\n", pImage->cVolume);
5438}
5439
5440/** @copydoc VDIMAGEBACKEND::pfnComposeLocation */
5441static DECLCALLBACK(int) iscsiComposeLocation(PVDINTERFACE pConfig, char **pszLocation)
5442{
5443 char *pszTarget = NULL;
5444 char *pszLUN = NULL;
5445 char *pszAddress = NULL;
5446 int rc = VDCFGQueryStringAlloc(VDIfConfigGet(pConfig), "TargetName", &pszTarget);
5447 if (RT_SUCCESS(rc))
5448 {
5449 rc = VDCFGQueryStringAlloc(VDIfConfigGet(pConfig), "LUN", &pszLUN);
5450 if (RT_SUCCESS(rc))
5451 {
5452 rc = VDCFGQueryStringAlloc(VDIfConfigGet(pConfig), "TargetAddress", &pszAddress);
5453 if (RT_SUCCESS(rc))
5454 {
5455 if (RTStrAPrintf(pszLocation, "iscsi://%s/%s/%s",
5456 pszAddress, pszTarget, pszLUN) < 0)
5457 rc = VERR_NO_MEMORY;
5458 }
5459 }
5460 }
5461 RTMemFree(pszTarget);
5462 RTMemFree(pszLUN);
5463 RTMemFree(pszAddress);
5464 return rc;
5465}
5466
5467/** @copydoc VDIMAGEBACKEND::pfnComposeName */
5468static DECLCALLBACK(int) iscsiComposeName(PVDINTERFACE pConfig, char **pszName)
5469{
5470 char *pszTarget = NULL;
5471 char *pszLUN = NULL;
5472 char *pszAddress = NULL;
5473 int rc = VDCFGQueryStringAlloc(VDIfConfigGet(pConfig), "TargetName", &pszTarget);
5474 if (RT_SUCCESS(rc))
5475 {
5476 rc = VDCFGQueryStringAlloc(VDIfConfigGet(pConfig), "LUN", &pszLUN);
5477 if (RT_SUCCESS(rc))
5478 {
5479 rc = VDCFGQueryStringAlloc(VDIfConfigGet(pConfig), "TargetAddress", &pszAddress);
5480 if (RT_SUCCESS(rc))
5481 {
5482 /** @todo think about a nicer looking location scheme for iSCSI */
5483 if (RTStrAPrintf(pszName, "%s/%s/%s",
5484 pszAddress, pszTarget, pszLUN) < 0)
5485 rc = VERR_NO_MEMORY;
5486 }
5487 }
5488 }
5489 RTMemFree(pszTarget);
5490 RTMemFree(pszLUN);
5491 RTMemFree(pszAddress);
5492
5493 return rc;
5494}
5495
5496
5497const VDIMAGEBACKEND g_ISCSIBackend =
5498{
5499 /* u32Version */
5500 VD_IMGBACKEND_VERSION,
5501 /* pszBackendName */
5502 "iSCSI",
5503 /* uBackendCaps */
5504 VD_CAP_CONFIG | VD_CAP_TCPNET | VD_CAP_ASYNC,
5505 /* papszFileExtensions */
5506 NULL,
5507 /* paConfigInfo */
5508 s_iscsiConfigInfo,
5509 /* prnProbe */
5510 iscsiProbe,
5511 /* pfnOpen */
5512 iscsiOpen,
5513 /* pfnCreate */
5514 iscsiCreate,
5515 /* pfnRename */
5516 NULL,
5517 /* pfnClose */
5518 iscsiClose,
5519 /* pfnRead */
5520 iscsiRead,
5521 /* pfnWrite */
5522 iscsiWrite,
5523 /* pfnFlush */
5524 iscsiFlush,
5525 /* pfnDiscard */
5526 NULL,
5527 /* pfnGetVersion */
5528 iscsiGetVersion,
5529 /* pfnGetSectorSize */
5530 iscsiGetSectorSize,
5531 /* pfnGetSize */
5532 iscsiGetSize,
5533 /* pfnGetFileSize */
5534 iscsiGetFileSize,
5535 /* pfnGetPCHSGeometry */
5536 iscsiGetPCHSGeometry,
5537 /* pfnSetPCHSGeometry */
5538 iscsiSetPCHSGeometry,
5539 /* pfnGetLCHSGeometry */
5540 iscsiGetLCHSGeometry,
5541 /* pfnSetLCHSGeometry */
5542 iscsiSetLCHSGeometry,
5543 /* pfnGetImageFlags */
5544 iscsiGetImageFlags,
5545 /* pfnGetOpenFlags */
5546 iscsiGetOpenFlags,
5547 /* pfnSetOpenFlags */
5548 iscsiSetOpenFlags,
5549 /* pfnGetComment */
5550 iscsiGetComment,
5551 /* pfnSetComment */
5552 iscsiSetComment,
5553 /* pfnGetUuid */
5554 iscsiGetUuid,
5555 /* pfnSetUuid */
5556 iscsiSetUuid,
5557 /* pfnGetModificationUuid */
5558 iscsiGetModificationUuid,
5559 /* pfnSetModificationUuid */
5560 iscsiSetModificationUuid,
5561 /* pfnGetParentUuid */
5562 iscsiGetParentUuid,
5563 /* pfnSetParentUuid */
5564 iscsiSetParentUuid,
5565 /* pfnGetParentModificationUuid */
5566 iscsiGetParentModificationUuid,
5567 /* pfnSetParentModificationUuid */
5568 iscsiSetParentModificationUuid,
5569 /* pfnDump */
5570 iscsiDump,
5571 /* pfnGetTimestamp */
5572 NULL,
5573 /* pfnGetParentTimestamp */
5574 NULL,
5575 /* pfnSetParentTimestamp */
5576 NULL,
5577 /* pfnGetParentFilename */
5578 NULL,
5579 /* pfnSetParentFilename */
5580 NULL,
5581 /* pfnComposeLocation */
5582 iscsiComposeLocation,
5583 /* pfnComposeName */
5584 iscsiComposeName,
5585 /* pfnCompact */
5586 NULL,
5587 /* pfnResize */
5588 NULL,
5589 /* pfnRepair */
5590 NULL,
5591 /* pfnTraverseMetadata */
5592 NULL,
5593 /* u32VersionEnd */
5594 VD_IMGBACKEND_VERSION
5595};
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette