| 1 | /* |
| 2 | * Copyright 2010, Andreas Faerber <andreas.faerber@web.de> |
| 3 | * All rights reserved. Distributed under the terms of the MIT License. |
| 4 | */ |
| 5 | |
| 6 | #include <boot/net/iSCSITarget.h> |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <string.h> |
| 10 | #include <ctype.h> |
| 11 | #include <KernelExport.h> |
| 12 | |
| 13 | #include <boot/net/ChainBuffer.h> |
| 14 | #include <boot/net/TCP.h> |
| 15 | #include <scsi_cmds.h> |
| 16 | |
| 17 | |
| 18 | |
| 19 | #define ISCSI_ALIGNMENT 4 |
| 20 | #define ISCSI_ALIGN(x) (((x) + ISCSI_ALIGNMENT - 1) & ~(ISCSI_ALIGNMENT - 1)) |
| 21 | #define ISCSI_PADDING(x) ((((x) % ISCSI_ALIGNMENT) == 0) ? 0 \ |
| 22 | : (ISCSI_ALIGNMENT - ((x) % ISCSI_ALIGNMENT))) |
| 23 | |
| 24 | // derived from the schedulers |
| 25 | static int |
| 26 | _rand(void) |
| 27 | { |
| 28 | static int next = 0; |
| 29 | if (next == 0) |
| 30 | next = system_time(); |
| 31 | |
| 32 | next = next * 1103515245 + 12345; |
| 33 | return next; |
| 34 | } |
| 35 | |
| 36 | |
| 37 | bool |
| 38 | iSCSITarget::DiscoverTargets(ip_addr_t address, uint16 port, |
| 39 | void (*targetCallback)(Node* node, void* opaque), void* opaque) |
| 40 | { |
| 41 | iSCSISession* session = new(nothrow) iSCSISession(); |
| 42 | if (session == NULL) |
| 43 | return false; |
| 44 | status_t error = session->Init(address, port); |
| 45 | if (error != B_OK) { |
| 46 | delete session; |
| 47 | return false; |
| 48 | } |
| 49 | const char* request = "SendTargets=All"; |
| 50 | char* sendTargets = NULL; |
| 51 | size_t sendTargetsLength = 0; |
| 52 | error = session->Connection()->GetText(request, strlen(request) + 1, |
| 53 | &sendTargets, &sendTargetsLength); |
| 54 | session->Close(); |
| 55 | delete session; |
| 56 | if (error != B_OK) { |
| 57 | return false; |
| 58 | } |
| 59 | bool addedDisk = false; |
| 60 | char* targetName = NULL; |
| 61 | bool seenAddress = false; |
| 62 | for (char* pair = sendTargets; pair < sendTargets + sendTargetsLength; |
| 63 | pair += strlen(pair) + 1) { |
| 64 | printf("%s\n", pair); |
| 65 | if (strncmp(pair, "TargetName=", strlen("TargetName=")) == 0) { |
| 66 | if (targetName != NULL && !seenAddress) { |
| 67 | if (_AddDevice(address, port, targetName, targetCallback, opaque)) |
| 68 | addedDisk = true; |
| 69 | } |
| 70 | seenAddress = false; |
| 71 | targetName = pair + strlen("TargetName="); |
| 72 | } else if (strncmp(pair, "TargetAddress=", strlen("TargetAddress=")) |
| 73 | == 0) { |
| 74 | seenAddress = true; |
| 75 | char* targetAddress = pair + strlen("TargetAddress="); |
| 76 | char* comma = strrchr(targetAddress, ','); |
| 77 | int addressLength = comma - targetAddress; |
| 78 | targetAddress = strndup(targetAddress, addressLength); |
| 79 | uint16 targetPort = ISCSI_PORT; |
| 80 | char* colon = strrchr(targetAddress, ':'); |
| 81 | if (colon != NULL) { |
| 82 | // colon could be part of an IPv6 address, e.g. [::1.2.3.4] |
| 83 | char* bracket = strrchr(targetAddress, ']'); |
| 84 | if (bracket == NULL || bracket < colon) { |
| 85 | targetPort = strtol(colon + 1, NULL, 10); |
| 86 | colon[0] = '\0'; |
| 87 | } |
| 88 | } |
| 89 | if (targetName != NULL && isdigit(targetAddress[0])) { |
| 90 | if (_AddDevice(ip_parse_address(targetAddress), targetPort, |
| 91 | targetName, targetCallback, opaque)) |
| 92 | addedDisk = true; |
| 93 | } |
| 94 | free(targetAddress); |
| 95 | } |
| 96 | } |
| 97 | if (targetName != NULL && !seenAddress) { |
| 98 | if (_AddDevice(address, port, targetName, targetCallback, opaque)) |
| 99 | addedDisk = true; |
| 100 | } |
| 101 | free(sendTargets); |
| 102 | return addedDisk; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | bool |
| 107 | iSCSITarget::_AddDevice(ip_addr_t address, uint16 port, |
| 108 | const char* targetName, void (*callback)(Node* node, void* opaque), |
| 109 | void* opaque) |
| 110 | { |
| 111 | printf("=> %s @ %08lx:%u\n", targetName, address, port); |
| 112 | iSCSITarget* disk = new(nothrow) iSCSITarget(); |
| 113 | if (disk == NULL) |
| 114 | return false; |
| 115 | status_t error = disk->Init(address, port, targetName); |
| 116 | if (error != B_OK) { |
| 117 | delete disk; |
| 118 | return false; |
| 119 | } |
| 120 | printf("disk size = %llu\n", disk->Size()); |
| 121 | callback(disk, opaque); |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | iSCSITarget::iSCSITarget() |
| 127 | : |
| 128 | fSession(NULL), |
| 129 | fTargetName(NULL), |
| 130 | fTargetAlias(NULL) |
| 131 | { |
| 132 | } |
| 133 | |
| 134 | |
| 135 | iSCSITarget::~iSCSITarget() |
| 136 | { |
| 137 | free(fTargetName); |
| 138 | free(fTargetAlias); |
| 139 | delete fSession; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | status_t |
| 144 | iSCSITarget::Init(ip_addr_t address, uint16 port, const char* targetName) |
| 145 | { |
| 146 | fTargetName = strdup(targetName); |
| 147 | if (fTargetName == NULL) |
| 148 | return B_NO_MEMORY; |
| 149 | |
| 150 | fSession = new(nothrow) iSCSISession(fTargetName); |
| 151 | if (fSession == NULL) |
| 152 | return B_NO_MEMORY; |
| 153 | status_t error = fSession->Init(address, port, &fTargetAlias); |
| 154 | if (error != B_OK) |
| 155 | return error; |
| 156 | if (targetName == NULL) |
| 157 | return B_OK; |
| 158 | |
| 159 | scsi_cmd_read_capacity cmd; |
| 160 | memset(&cmd, 0, sizeof(cmd)); |
| 161 | cmd.opcode = SCSI_OP_READ_CAPACITY; |
| 162 | cmd.relative_address = 0; |
| 163 | cmd.lun = 0; |
| 164 | cmd.lba = 0; |
| 165 | cmd.pmi = 0; |
| 166 | cmd.control = 0; |
| 167 | scsi_res_read_capacity* resp; |
| 168 | size_t respLength; |
| 169 | error = fSession->Connection()->SendCommand(&cmd, sizeof(cmd), |
| 170 | false, false, 0, |
| 171 | (void**)&resp, &respLength); |
| 172 | if (error != B_OK) |
| 173 | return error; |
| 174 | printf("lba = %lu, block size = %lu\n", resp->lba, resp->block_size); |
| 175 | fLastLBA = resp->lba; |
| 176 | fBlockSize = resp->block_size; |
| 177 | return B_OK; |
| 178 | } |
| 179 | |
| 180 | |
| 181 | ssize_t |
| 182 | iSCSITarget::ReadAt(void* /*cookie*/, off_t pos, void* buffer, size_t bufferSize) |
| 183 | { |
| 184 | printf("!!!! iSCSITarget::ReadAt(): size = %lu\n", bufferSize); |
| 185 | fSession->Connection()->ReadData(); |
| 186 | |
| 187 | scsi_cmd_rw_12 cmd; |
| 188 | memset(&cmd, 0, sizeof(cmd)); |
| 189 | cmd.opcode = SCSI_OP_READ_12; |
| 190 | cmd.relative_address = 0; |
| 191 | cmd.force_unit_access = 0; |
| 192 | cmd.disable_page_out = 0; |
| 193 | cmd.lun = 0; |
| 194 | cmd.lba = pos / fBlockSize; |
| 195 | cmd.length = bufferSize; |
| 196 | cmd.control = 0; |
| 197 | void* resp; |
| 198 | size_t respLength; |
| 199 | status_t error = fSession->Connection()->SendCommand(&cmd, sizeof(cmd), |
| 200 | true, false, cmd.length, &resp, &respLength); |
| 201 | if (error != B_OK) |
| 202 | return error; |
| 203 | printf("====> length = %lu\n", respLength); |
| 204 | |
| 205 | error = fSession->Connection()->ReadData(); |
| 206 | return B_PERMISSION_DENIED; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | ssize_t |
| 211 | iSCSITarget::WriteAt(void* /*cookie*/, off_t pos, const void* buffer, size_t bufferSize) |
| 212 | { |
| 213 | return B_PERMISSION_DENIED; |
| 214 | } |
| 215 | |
| 216 | |
| 217 | status_t |
| 218 | iSCSITarget::GetName(char* buffer, size_t bufferSize) const |
| 219 | { |
| 220 | if (buffer == NULL) |
| 221 | return B_BAD_VALUE; |
| 222 | |
| 223 | snprintf(buffer, bufferSize, "iSCSI:%s", fTargetName); |
| 224 | |
| 225 | return B_OK; |
| 226 | } |
| 227 | |
| 228 | |
| 229 | off_t |
| 230 | iSCSITarget::Size() const |
| 231 | { |
| 232 | return fLastLBA * fBlockSize; |
| 233 | } |
| 234 | |
| 235 | |
| 236 | iSCSISession::iSCSISession(const char* targetName) |
| 237 | : |
| 238 | fDiscovery(targetName == NULL), |
| 239 | fTargetName(targetName), |
| 240 | fConnection(NULL) |
| 241 | { |
| 242 | fCommandSequenceNumber = _rand(); |
| 243 | } |
| 244 | |
| 245 | |
| 246 | iSCSISession::~iSCSISession() |
| 247 | { |
| 248 | if (Active()) |
| 249 | Close(); |
| 250 | delete fConnection; |
| 251 | } |
| 252 | |
| 253 | |
| 254 | status_t |
| 255 | iSCSISession::Init(ip_addr_t address, uint16 port, char** targetAlias) |
| 256 | { |
| 257 | fConnection = new(nothrow) iSCSIConnection(); |
| 258 | if (fConnection == NULL) |
| 259 | return B_NO_MEMORY; |
| 260 | status_t error = fConnection->Init(this); |
| 261 | if (error != B_OK) |
| 262 | return error; |
| 263 | error = fConnection->Open(address, port); |
| 264 | if (error != B_OK) |
| 265 | return error; |
| 266 | error = fConnection->Login(fTargetName, targetAlias); |
| 267 | if (error != B_OK) |
| 268 | return error; |
| 269 | return B_OK; |
| 270 | } |
| 271 | |
| 272 | |
| 273 | status_t |
| 274 | iSCSISession::Close() |
| 275 | { |
| 276 | if (fConnection == NULL) |
| 277 | return B_NO_INIT; |
| 278 | |
| 279 | status_t error = fConnection->Logout(true); |
| 280 | if (error != B_OK) |
| 281 | return error; |
| 282 | return B_OK; |
| 283 | } |
| 284 | |
| 285 | |
| 286 | iSCSIConnection::iSCSIConnection() |
| 287 | : |
| 288 | fSession(NULL), |
| 289 | fSocket(NULL), |
| 290 | fConnected(false), |
| 291 | fConnectionID(0) |
| 292 | { |
| 293 | } |
| 294 | |
| 295 | |
| 296 | iSCSIConnection::~iSCSIConnection() |
| 297 | { |
| 298 | if (fSocket != NULL && fConnected) { |
| 299 | if (Logout() != B_OK) |
| 300 | fSocket->Close(); |
| 301 | } |
| 302 | delete fSocket; |
| 303 | } |
| 304 | |
| 305 | |
| 306 | status_t |
| 307 | iSCSIConnection::Init(iSCSISession* session) |
| 308 | { |
| 309 | fSession = session; |
| 310 | return B_OK; |
| 311 | } |
| 312 | |
| 313 | status_t |
| 314 | iSCSIConnection::Open(ip_addr_t address, uint16 port) |
| 315 | { |
| 316 | fSocket = new(nothrow) TCPSocket(); |
| 317 | if (fSocket == NULL) |
| 318 | return B_NO_MEMORY; |
| 319 | printf("iSCSI initiator connecting to target...\n"); |
| 320 | status_t error = fSocket->Connect(address, port); |
| 321 | if (error != B_OK) |
| 322 | return error; |
| 323 | fConnected = true; |
| 324 | printf("connected.\n"); |
| 325 | fConnectionID = _rand(); |
| 326 | return B_OK; |
| 327 | } |
| 328 | |
| 329 | |
| 330 | status_t |
| 331 | iSCSIConnection::Login(const char* targetName, char** targetAlias) |
| 332 | { |
| 333 | char data[256]; |
| 334 | size_t dataLength = 0; |
| 335 | const char* keyValue; |
| 336 | keyValue = "InitiatorName=iqn.2002-10.org.haiku-os:haiku.bootloader.hostX"; |
| 337 | strcpy(data + dataLength, keyValue); |
| 338 | dataLength += strlen(keyValue) + 1; |
| 339 | if (targetName != NULL) { |
| 340 | dataLength += sprintf(data + dataLength, "TargetName=%s", targetName) + 1; |
| 341 | } else { |
| 342 | keyValue = "SessionType=Discovery"; |
| 343 | strcpy(data + dataLength, keyValue); |
| 344 | dataLength += strlen(keyValue) + 1; |
| 345 | } |
| 346 | |
| 347 | iscsi_login_request req; |
| 348 | req.transit = true; |
| 349 | req.c = false; |
| 350 | req.currentStage = ISCSI_SESSION_STAGE_LOGIN_OPERATIONAL_NEGOTIATION; |
| 351 | req.nextStage = ISCSI_SESSION_STAGE_FULL_FEATURE_PHASE; |
| 352 | req.versionMax = ISCSI_VERSION; |
| 353 | req.versionMin = ISCSI_VERSION; |
| 354 | req.totalAHSLength = 0; |
| 355 | req.dataSegmentLength = dataLength; |
| 356 | printf("data segment length = %lu\n", req.dataSegmentLength); |
| 357 | req.isid.t = ISCSI_ISID_RANDOM; |
| 358 | req.isid.a = 0; |
| 359 | uint32 random = _rand(); |
| 360 | req.isid.b = random >> 8; |
| 361 | req.isid.c = random & 0xff; |
| 362 | req.isid.d = _rand(); |
| 363 | req.tsih = 0; |
| 364 | req.initiatorTaskTag = _rand(); |
| 365 | req.cid = fConnectionID; |
| 366 | req.cmdSN = fSession->CommandSequenceNumber(); |
| 367 | req.expStatSN = 0; |
| 368 | status_t error = fSocket->Write(&req, sizeof(req)); |
| 369 | if (error != B_OK) { |
| 370 | return error; |
| 371 | } |
| 372 | for (unsigned int i = dataLength; i < ISCSI_ALIGN(dataLength); i++) |
| 373 | data[i] = '\0'; |
| 374 | dataLength = ISCSI_ALIGN(dataLength); |
| 375 | printf("data length = %lu\n", dataLength); |
| 376 | error = fSocket->Write(data, dataLength); |
| 377 | if (error != B_OK) { |
| 378 | printf("write error\n"); |
| 379 | return error; |
| 380 | } |
| 381 | printf("iSCSI login request sent.\n"); |
| 382 | |
| 383 | iscsi_login_response resp; |
| 384 | size_t bytesRead = 0; |
| 385 | error = fSocket->Read(&resp, sizeof(resp), &bytesRead, 10000000LL); |
| 386 | if (error != B_OK) { |
| 387 | printf("iSCSI login response read error\n"); |
| 388 | return error; |
| 389 | } |
| 390 | printf("bytesRead = %lu\n", bytesRead); |
| 391 | printf("iSCSI login response: opcode = %x, status class = %u, status detail = %u, transit = %u, data length = %lu\n", |
| 392 | resp.opcode, resp.statusClass, resp.statusDetail, resp.transit, resp.dataSegmentLength); |
| 393 | if (resp.dataSegmentLength > 0) { |
| 394 | error = fSocket->Read(data, resp.dataSegmentLength, &bytesRead, 1000000LL); |
| 395 | if (error != B_OK) { |
| 396 | printf("iSCSI login response data read error\n"); |
| 397 | return error; |
| 398 | } |
| 399 | printf("bytesRead = %lu\n", bytesRead); |
| 400 | if (resp.dataSegmentLength % ISCSI_ALIGNMENT) { |
| 401 | error = fSocket->Read(NULL, ISCSI_PADDING(resp.dataSegmentLength), &bytesRead, 100000LL); |
| 402 | if (error != B_OK) { |
| 403 | printf("iSCSI login response padding read error\n"); |
| 404 | return error; |
| 405 | } |
| 406 | printf("bytesRead = %lu\n", bytesRead); |
| 407 | } |
| 408 | for (char* pair = data; pair < data + resp.dataSegmentLength; pair += strlen(pair) + 1) { |
| 409 | printf("%s\n", pair); |
| 410 | if (targetAlias != NULL && strncmp(pair, "TargetAlias=", strlen("TargetAlias=")) == 0) { |
| 411 | *targetAlias = strdup(pair + strlen("TargetAlias=")); |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | fStatusSequenceNumber = resp.statSN; |
| 416 | if (resp.statusClass != 0) { |
| 417 | printf("iSCSI login response indicates failure.\n"); |
| 418 | return B_BAD_VALUE; |
| 419 | } |
| 420 | return B_OK; |
| 421 | } |
| 422 | |
| 423 | |
| 424 | status_t |
| 425 | iSCSIConnection::Logout(bool closeSession) |
| 426 | { |
| 427 | iscsi_logout_request req; |
| 428 | printf("req size = %lu\n", sizeof(req)); |
| 429 | req.immediateDelivery = true; |
| 430 | req.reasonCode = closeSession ? ISCSI_LOGOUT_REASON_CLOSE_SESSION |
| 431 | : ISCSI_LOGOUT_REASON_CLOSE_CONNECTION; |
| 432 | req.totalAHSLength = 0; |
| 433 | req.dataSegmentLength = 0; |
| 434 | req.initiatorTaskTag = _rand(); |
| 435 | req.cid = closeSession ? 0 : fConnectionID; |
| 436 | req.cmdSN = fSession->CommandSequenceNumber(); |
| 437 | req.expStatSN = fStatusSequenceNumber; |
| 438 | status_t error = fSocket->Write(&req, sizeof(req)); |
| 439 | if (error != B_OK) { |
| 440 | printf("iSCSI logout request write error\n"); |
| 441 | return error; |
| 442 | } |
| 443 | printf("iSCSI logout request sent.\n"); |
| 444 | |
| 445 | iscsi_logout_response resp; |
| 446 | printf("resp size = %lu\n", sizeof(resp)); |
| 447 | size_t bytesRead = 0; |
| 448 | error = fSocket->Read(&resp, sizeof(resp), &bytesRead, 10000000LL); |
| 449 | if (error != B_OK) { |
| 450 | printf("iSCSI logout response read error\n"); |
| 451 | return error; |
| 452 | } |
| 453 | printf("bytesRead = %lu\n", bytesRead); |
| 454 | printf("iSCSI logout response: opcode = %x, response = %u\n", |
| 455 | resp.opcode, resp.response); |
| 456 | fStatusSequenceNumber = resp.statSN; |
| 457 | if (resp.response != 0) { |
| 458 | printf("iSCSI logout response indicates failure.\n"); |
| 459 | return error; |
| 460 | } |
| 461 | |
| 462 | fSocket->Close(); |
| 463 | fConnected = false; |
| 464 | printf("disconnected\n"); |
| 465 | return B_OK; |
| 466 | } |
| 467 | |
| 468 | |
| 469 | status_t |
| 470 | iSCSIConnection::GetText(const char* request, size_t requestLength, char** response, size_t* responseLength) |
| 471 | { |
| 472 | iscsi_text_request req; |
| 473 | req.immediateDelivery = true; |
| 474 | req.final = true; |
| 475 | req.c = false; |
| 476 | req.totalAHSLength = 0; |
| 477 | req.dataSegmentLength = requestLength; |
| 478 | req.lun = 0; |
| 479 | req.initiatorTaskTag = _rand(); |
| 480 | req.targetTransferTag = 0xffffffffL; |
| 481 | req.cmdSN = fSession->NextCommandSequenceNumber(); |
| 482 | req.expStatSN = fStatusSequenceNumber; |
| 483 | status_t error = fSocket->Write(&req, sizeof(req)); |
| 484 | if (error != B_OK) { |
| 485 | printf("iSCSI text request write error\n"); |
| 486 | return error; |
| 487 | } |
| 488 | error = fSocket->Write(request, requestLength); |
| 489 | if (error != B_OK) { |
| 490 | printf("iSCSI text request data write error\n"); |
| 491 | return error; |
| 492 | } |
| 493 | if (requestLength % ISCSI_ALIGNMENT != 0) { |
| 494 | char buffer[3]; |
| 495 | memset(buffer, 0, 3); |
| 496 | error = fSocket->Write(buffer, ISCSI_ALIGNMENT - (requestLength % ISCSI_ALIGNMENT)); |
| 497 | if (error != B_OK) { |
| 498 | printf("iSCSI text request padding write error\n"); |
| 499 | return error; |
| 500 | } |
| 501 | } |
| 502 | printf("iSCSI text request sent.\n"); |
| 503 | |
| 504 | iscsi_logout_response resp; |
| 505 | printf("resp size = %lu\n", sizeof(resp)); |
| 506 | size_t bytesRead = 0; |
| 507 | error = fSocket->Read(&resp, sizeof(resp), &bytesRead, 10000000LL); |
| 508 | if (error != B_OK) { |
| 509 | printf("iSCSI text response read error\n"); |
| 510 | return error; |
| 511 | } |
| 512 | printf("bytesRead = %lu\n", bytesRead); |
| 513 | printf("iSCSI text response: opcode = %x\n", |
| 514 | resp.opcode); |
| 515 | *response = (char*)malloc(resp.dataSegmentLength); |
| 516 | if (*response == NULL) |
| 517 | return B_NO_MEMORY; |
| 518 | error = fSocket->Read(*response, resp.dataSegmentLength, &bytesRead, 10000000LL); |
| 519 | if (error != B_OK) { |
| 520 | printf("iSCSI text response read error\n"); |
| 521 | return error; |
| 522 | } |
| 523 | printf("bytesRead = %lu\n", bytesRead); |
| 524 | *responseLength = resp.dataSegmentLength; |
| 525 | if (resp.dataSegmentLength % ISCSI_ALIGNMENT != 0) { |
| 526 | error = fSocket->Read(NULL, ISCSI_PADDING(resp.dataSegmentLength), &bytesRead, 1000000LL); |
| 527 | if (error != B_OK) { |
| 528 | printf("iSCSI text response padding read error\n"); |
| 529 | return error; |
| 530 | } |
| 531 | } |
| 532 | fStatusSequenceNumber = resp.statSN; |
| 533 | |
| 534 | return B_OK; |
| 535 | } |
| 536 | |
| 537 | |
| 538 | status_t |
| 539 | iSCSIConnection::SendCommand(const void* command, size_t commandSize, |
| 540 | bool r, bool w, uint32 expectedDataTransferLength, |
| 541 | void** response, size_t* responseLength) |
| 542 | { |
| 543 | printf("iSCSIConnection::SendCommand(): command size = %lu\n", commandSize); |
| 544 | iscsi_scsi_command req; |
| 545 | printf("req size = %lu\n", sizeof(req)); |
| 546 | req.immediateDelivery = true; |
| 547 | req.final = true; |
| 548 | req.r = r; |
| 549 | req.w = w; |
| 550 | req.attr = 0; |
| 551 | req.totalAHSLength = 0; |
| 552 | req.dataSegmentLength = 0; |
| 553 | req.lun = 0; |
| 554 | req.initiatorTaskTag = _rand(); |
| 555 | req.expectedDataTransferLength = expectedDataTransferLength; |
| 556 | req.cmdSN = fSession->NextCommandSequenceNumber(); |
| 557 | req.expStatSN = fStatusSequenceNumber; |
| 558 | if (commandSize > 16) |
| 559 | memset(req.cdb, 0, 16); |
| 560 | memcpy(req.cdb, command, commandSize <= 16 ? commandSize : 16); |
| 561 | status_t error = fSocket->Write(&req, sizeof(req)); |
| 562 | if (error != B_OK) { |
| 563 | printf("iSCSI SCSI command write error\n"); |
| 564 | return error; |
| 565 | } |
| 566 | printf("iSCSI SCSI command sent.\n"); |
| 567 | |
| 568 | iscsi_scsi_response resp; |
| 569 | printf("resp size = %lu\n", sizeof(resp)); |
| 570 | size_t bytesRead = 0; |
| 571 | error = fSocket->Read(&resp, sizeof(resp), &bytesRead, 10000000LL); |
| 572 | if (error != B_OK) { |
| 573 | printf("iSCSI SCSI response read error\n"); |
| 574 | return error; |
| 575 | } |
| 576 | printf("bytesRead = %lu\n", bytesRead); |
| 577 | printf("iSCSI SCSI response: opcode = %x, response = %x, status = %x, AHS length = %x\n", |
| 578 | resp.opcode, resp.response, resp.status, resp.totalAHSLength); |
| 579 | if (resp.dataSegmentLength > 0) { |
| 580 | *response = malloc(resp.dataSegmentLength); |
| 581 | if (*response == NULL) |
| 582 | return B_NO_MEMORY; |
| 583 | error = fSocket->Read(*response, resp.dataSegmentLength, &bytesRead, 10000000LL); |
| 584 | if (error != B_OK) { |
| 585 | printf("iSCSI text response read error\n"); |
| 586 | return error; |
| 587 | } |
| 588 | printf("bytesRead = %lu\n", bytesRead); |
| 589 | } |
| 590 | *responseLength = resp.dataSegmentLength; |
| 591 | if (resp.dataSegmentLength % ISCSI_ALIGNMENT != 0) { |
| 592 | error = fSocket->Read(NULL, ISCSI_PADDING(resp.dataSegmentLength), &bytesRead, 1000000LL); |
| 593 | if (error != B_OK) { |
| 594 | printf("iSCSI SCSI response padding read error\n"); |
| 595 | return error; |
| 596 | } |
| 597 | } |
| 598 | if (resp.response != 0x00 || resp.status != SCSI_STATUS_GOOD) |
| 599 | return B_BAD_VALUE; |
| 600 | |
| 601 | return B_OK; |
| 602 | } |
| 603 | |
| 604 | |
| 605 | status_t |
| 606 | iSCSIConnection::ReadData() |
| 607 | { |
| 608 | iscsi_basic_header_segment resp; |
| 609 | size_t bytesRead = 0; |
| 610 | status_t error = fSocket->Read(&resp, sizeof(resp), &bytesRead, 10000000LL); |
| 611 | if (error != B_OK) { |
| 612 | printf("iSCSI SCSI response read error\n"); |
| 613 | return error; |
| 614 | } |
| 615 | printf("bytesRead = %lu\n", bytesRead); |
| 616 | printf("iSCSI response: opcode = %x, AHS length = %u, data length = %lu\n", |
| 617 | resp.opcode, resp.totalAHSLength, resp.dataSegmentLength); |
| 618 | if (resp.totalAHSLength > 0) { |
| 619 | error = fSocket->Read(NULL, resp.totalAHSLength, &bytesRead, 1000000LL); |
| 620 | if (error != B_OK) { |
| 621 | printf("iSCSI response header read error\n"); |
| 622 | return error; |
| 623 | } |
| 624 | printf("bytesRead = %lu\n", bytesRead); |
| 625 | } |
| 626 | if (resp.dataSegmentLength > 0) { |
| 627 | error = fSocket->Read(NULL, resp.dataSegmentLength, &bytesRead, 1000000LL); |
| 628 | if (error != B_OK) { |
| 629 | printf("iSCSI response data read error\n"); |
| 630 | return error; |
| 631 | } |
| 632 | printf("bytesRead = %lu\n", bytesRead); |
| 633 | } |
| 634 | if (resp.dataSegmentLength % ISCSI_ALIGNMENT != 0) { |
| 635 | error = fSocket->Read(NULL, ISCSI_PADDING(resp.dataSegmentLength), &bytesRead, 1000000LL); |
| 636 | if (error != B_OK) { |
| 637 | printf("iSCSI SCSI response padding read error\n"); |
| 638 | return error; |
| 639 | } |
| 640 | } |
| 641 | return B_OK; |
| 642 | } |
| 643 | |
| 644 | |
| 645 | status_t |
| 646 | iSCSIConnection::_ReadResponse(void* buffer, size_t bufferSize, size_t* bytesRead, bigtime_t timeout) |
| 647 | { |
| 648 | // assuming bufferSize >= 48 |
| 649 | status_t error = fSocket->Read(buffer, bufferSize, bytesRead, timeout); |
| 650 | if (error != B_OK) |
| 651 | return error; |
| 652 | //if (*bytesRead >= 1) |
| 653 | return error; |
| 654 | } |