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