VirtualBox

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

Last change on this file since 36217 was 36133, checked in by vboxsync, 14 years ago

iSCSI: Clear tail pointer of waiting PDUs during reattach too (safety) and insert responses to NOP-in requests at the front of the waiting PDUs to transmit

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

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