1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>. 5 * Copyright (c) Intel Corporation. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * * Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * * Neither the name of Intel Corporation nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include "spdk/stdinc.h" 36 37 #include "spdk/conf.h" 38 #include "spdk/sock.h" 39 #include "spdk/scsi.h" 40 41 #include "spdk_internal/log.h" 42 43 #include "iscsi/iscsi.h" 44 #include "iscsi/conn.h" 45 #include "iscsi/tgt_node.h" 46 #include "iscsi/portal_grp.h" 47 #include "iscsi/init_grp.h" 48 #include "iscsi/task.h" 49 50 #define MAX_TMPBUF 4096 51 #define MAX_MASKBUF 128 52 53 static bool 54 iscsi_ipv6_netmask_allow_addr(const char *netmask, const char *addr) 55 { 56 struct in6_addr in6_mask; 57 struct in6_addr in6_addr; 58 char mask[MAX_MASKBUF]; 59 const char *p; 60 size_t n; 61 int bits, bmask; 62 int i; 63 64 if (netmask[0] != '[') { 65 return false; 66 } 67 p = strchr(netmask, ']'); 68 if (p == NULL) { 69 return false; 70 } 71 n = p - (netmask + 1); 72 if (n + 1 > sizeof mask) { 73 return false; 74 } 75 76 memcpy(mask, netmask + 1, n); 77 mask[n] = '\0'; 78 p++; 79 80 if (p[0] == '/') { 81 bits = (int) strtol(p + 1, NULL, 10); 82 if (bits <= 0 || bits > 128) { 83 return false; 84 } 85 } else { 86 bits = 128; 87 } 88 89 #if 0 90 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "input %s\n", addr); 91 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "mask %s / %d\n", mask, bits); 92 #endif 93 94 /* presentation to network order binary */ 95 if (inet_pton(AF_INET6, mask, &in6_mask) <= 0 96 || inet_pton(AF_INET6, addr, &in6_addr) <= 0) { 97 return false; 98 } 99 100 /* check 128bits */ 101 for (i = 0; i < (bits / 8); i++) { 102 if (in6_mask.s6_addr[i] != in6_addr.s6_addr[i]) { 103 return false; 104 } 105 } 106 if (bits % 8) { 107 bmask = (0xffU << (8 - (bits % 8))) & 0xffU; 108 if ((in6_mask.s6_addr[i] & bmask) != (in6_addr.s6_addr[i] & bmask)) { 109 return false; 110 } 111 } 112 113 /* match */ 114 return true; 115 } 116 117 static bool 118 iscsi_ipv4_netmask_allow_addr(const char *netmask, const char *addr) 119 { 120 struct in_addr in4_mask; 121 struct in_addr in4_addr; 122 char mask[MAX_MASKBUF]; 123 const char *p; 124 uint32_t bmask; 125 size_t n; 126 int bits; 127 128 p = strchr(netmask, '/'); 129 if (p == NULL) { 130 p = netmask + strlen(netmask); 131 } 132 n = p - netmask; 133 if (n + 1 > sizeof mask) { 134 return false; 135 } 136 137 memcpy(mask, netmask, n); 138 mask[n] = '\0'; 139 140 if (p[0] == '/') { 141 bits = (int) strtol(p + 1, NULL, 10); 142 if (bits <= 0 || bits > 32) { 143 return false; 144 } 145 } else { 146 bits = 32; 147 } 148 149 /* presentation to network order binary */ 150 if (inet_pton(AF_INET, mask, &in4_mask) <= 0 151 || inet_pton(AF_INET, addr, &in4_addr) <= 0) { 152 return false; 153 } 154 155 /* check 32bits */ 156 bmask = (0xffffffffU << (32 - bits)) & 0xffffffffU; 157 if ((ntohl(in4_mask.s_addr) & bmask) != (ntohl(in4_addr.s_addr) & bmask)) { 158 return false; 159 } 160 161 /* match */ 162 return true; 163 } 164 165 static bool 166 iscsi_netmask_allow_addr(const char *netmask, const char *addr) 167 { 168 if (netmask == NULL || addr == NULL) { 169 return false; 170 } 171 if (strcasecmp(netmask, "ANY") == 0) { 172 return true; 173 } 174 if (netmask[0] == '[') { 175 /* IPv6 */ 176 if (iscsi_ipv6_netmask_allow_addr(netmask, addr)) { 177 return true; 178 } 179 } else { 180 /* IPv4 */ 181 if (iscsi_ipv4_netmask_allow_addr(netmask, addr)) { 182 return true; 183 } 184 } 185 return false; 186 } 187 188 static bool 189 iscsi_init_grp_allow_addr(struct spdk_iscsi_init_grp *igp, 190 const char *addr) 191 { 192 struct spdk_iscsi_initiator_netmask *imask; 193 194 TAILQ_FOREACH(imask, &igp->netmask_head, tailq) { 195 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "netmask=%s, addr=%s\n", 196 imask->mask, addr); 197 if (iscsi_netmask_allow_addr(imask->mask, addr)) { 198 return true; 199 } 200 } 201 return false; 202 } 203 204 static int 205 iscsi_init_grp_allow_iscsi_name(struct spdk_iscsi_init_grp *igp, 206 const char *iqn, bool *result) 207 { 208 struct spdk_iscsi_initiator_name *iname; 209 210 TAILQ_FOREACH(iname, &igp->initiator_head, tailq) { 211 /* denied if iqn is matched */ 212 if ((iname->name[0] == '!') 213 && (strcasecmp(&iname->name[1], "ANY") == 0 214 || strcasecmp(&iname->name[1], iqn) == 0)) { 215 *result = false; 216 return 0; 217 } 218 /* allowed if iqn is matched */ 219 if (strcasecmp(iname->name, "ANY") == 0 220 || strcasecmp(iname->name, iqn) == 0) { 221 *result = true; 222 return 0; 223 } 224 } 225 return -1; 226 } 227 228 static struct spdk_iscsi_pg_map * 229 iscsi_tgt_node_find_pg_map(struct spdk_iscsi_tgt_node *target, 230 struct spdk_iscsi_portal_grp *pg); 231 232 bool 233 iscsi_tgt_node_access(struct spdk_iscsi_conn *conn, 234 struct spdk_iscsi_tgt_node *target, const char *iqn, const char *addr) 235 { 236 struct spdk_iscsi_portal_grp *pg; 237 struct spdk_iscsi_pg_map *pg_map; 238 struct spdk_iscsi_ig_map *ig_map; 239 int rc; 240 bool allowed = false; 241 242 if (conn == NULL || target == NULL || iqn == NULL || addr == NULL) { 243 return false; 244 } 245 pg = conn->portal->group; 246 247 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "pg=%d, iqn=%s, addr=%s\n", 248 pg->tag, iqn, addr); 249 pg_map = iscsi_tgt_node_find_pg_map(target, pg); 250 if (pg_map == NULL) { 251 return false; 252 } 253 TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) { 254 rc = iscsi_init_grp_allow_iscsi_name(ig_map->ig, iqn, &allowed); 255 if (rc == 0) { 256 if (allowed == false) { 257 goto denied; 258 } else { 259 if (iscsi_init_grp_allow_addr(ig_map->ig, addr)) { 260 return true; 261 } 262 } 263 } else { 264 /* netmask is denied in this initiator group */ 265 } 266 } 267 268 denied: 269 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "access denied from %s (%s) to %s (%s:%s,%d)\n", 270 iqn, addr, target->name, conn->portal_host, 271 conn->portal_port, conn->pg_tag); 272 return false; 273 } 274 275 static bool 276 iscsi_tgt_node_allow_iscsi_name(struct spdk_iscsi_tgt_node *target, const char *iqn) 277 { 278 struct spdk_iscsi_pg_map *pg_map; 279 struct spdk_iscsi_ig_map *ig_map; 280 int rc; 281 bool result = false; 282 283 if (target == NULL || iqn == NULL) { 284 return false; 285 } 286 287 TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) { 288 TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) { 289 rc = iscsi_init_grp_allow_iscsi_name(ig_map->ig, iqn, &result); 290 if (rc == 0) { 291 return result; 292 } 293 } 294 } 295 296 return false; 297 } 298 299 int 300 iscsi_send_tgts(struct spdk_iscsi_conn *conn, const char *iiqn, 301 const char *iaddr, const char *tiqn, uint8_t *data, int alloc_len, 302 int data_len) 303 { 304 char buf[MAX_TMPBUF]; 305 struct spdk_iscsi_portal_grp *pg; 306 struct spdk_iscsi_pg_map *pg_map; 307 struct spdk_iscsi_portal *p; 308 struct spdk_iscsi_tgt_node *target; 309 char *host; 310 int total; 311 int len; 312 int rc; 313 314 if (conn == NULL) { 315 return 0; 316 } 317 318 total = data_len; 319 if (alloc_len < 1) { 320 return 0; 321 } 322 if (total >= alloc_len) { 323 total = alloc_len; 324 data[total - 1] = '\0'; 325 return total; 326 } 327 328 pthread_mutex_lock(&g_iscsi.mutex); 329 TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) { 330 if (strcasecmp(tiqn, "ALL") != 0 331 && strcasecmp(tiqn, target->name) != 0) { 332 continue; 333 } 334 rc = iscsi_tgt_node_allow_iscsi_name(target, iiqn); 335 if (rc == 0) { 336 continue; 337 } 338 339 /* DO SENDTARGETS */ 340 len = snprintf((char *) data + total, alloc_len - total, 341 "TargetName=%s", target->name); 342 total += len + 1; 343 344 /* write to data */ 345 TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) { 346 pg = pg_map->pg; 347 TAILQ_FOREACH(p, &pg->head, per_pg_tailq) { 348 if (alloc_len - total < 1) { 349 pthread_mutex_unlock(&g_iscsi.mutex); 350 /* TODO: long text responses support */ 351 SPDK_ERRLOG("SPDK doesn't support long text responses now, " 352 "you can use larger MaxRecvDataSegmentLength" 353 "value in initiator\n"); 354 return alloc_len; 355 } 356 host = p->host; 357 /* wildcard? */ 358 if (strcasecmp(host, "[::]") == 0 359 || strcasecmp(host, "0.0.0.0") == 0) { 360 if (spdk_sock_is_ipv6(conn->sock)) { 361 snprintf(buf, sizeof buf, "[%s]", 362 conn->target_addr); 363 host = buf; 364 } else if (spdk_sock_is_ipv4(conn->sock)) { 365 snprintf(buf, sizeof buf, "%s", 366 conn->target_addr); 367 host = buf; 368 } else { 369 /* skip portal for the family */ 370 continue; 371 } 372 } 373 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, 374 "TargetAddress=%s:%s,%d\n", 375 host, p->port, pg->tag); 376 len = snprintf((char *) data + total, 377 alloc_len - total, 378 "TargetAddress=%s:%s,%d", 379 host, p->port, pg->tag); 380 total += len + 1; 381 } 382 } 383 } 384 pthread_mutex_unlock(&g_iscsi.mutex); 385 386 return total; 387 } 388 389 struct spdk_iscsi_tgt_node * 390 iscsi_find_tgt_node(const char *target_name) 391 { 392 struct spdk_iscsi_tgt_node *target; 393 394 if (target_name == NULL) { 395 return NULL; 396 } 397 TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) { 398 if (strcasecmp(target_name, target->name) == 0) { 399 return target; 400 } 401 } 402 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "can't find target %s\n", target_name); 403 return NULL; 404 } 405 406 static int 407 iscsi_tgt_node_register(struct spdk_iscsi_tgt_node *target) 408 { 409 pthread_mutex_lock(&g_iscsi.mutex); 410 411 if (iscsi_find_tgt_node(target->name) != NULL) { 412 pthread_mutex_unlock(&g_iscsi.mutex); 413 return -EEXIST; 414 } 415 416 TAILQ_INSERT_TAIL(&g_iscsi.target_head, target, tailq); 417 418 pthread_mutex_unlock(&g_iscsi.mutex); 419 return 0; 420 } 421 422 static int 423 iscsi_tgt_node_unregister(struct spdk_iscsi_tgt_node *target) 424 { 425 struct spdk_iscsi_tgt_node *t; 426 427 TAILQ_FOREACH(t, &g_iscsi.target_head, tailq) { 428 if (t == target) { 429 TAILQ_REMOVE(&g_iscsi.target_head, t, tailq); 430 return 0; 431 } 432 } 433 434 return -1; 435 } 436 437 static struct spdk_iscsi_ig_map * 438 iscsi_pg_map_find_ig_map(struct spdk_iscsi_pg_map *pg_map, 439 struct spdk_iscsi_init_grp *ig) 440 { 441 struct spdk_iscsi_ig_map *ig_map; 442 443 TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) { 444 if (ig_map->ig == ig) { 445 return ig_map; 446 } 447 } 448 449 return NULL; 450 } 451 452 static struct spdk_iscsi_ig_map * 453 iscsi_pg_map_add_ig_map(struct spdk_iscsi_pg_map *pg_map, 454 struct spdk_iscsi_init_grp *ig) 455 { 456 struct spdk_iscsi_ig_map *ig_map; 457 458 if (iscsi_pg_map_find_ig_map(pg_map, ig) != NULL) { 459 return NULL; 460 } 461 462 ig_map = malloc(sizeof(*ig_map)); 463 if (ig_map == NULL) { 464 return NULL; 465 } 466 467 ig_map->ig = ig; 468 ig->ref++; 469 pg_map->num_ig_maps++; 470 TAILQ_INSERT_TAIL(&pg_map->ig_map_head, ig_map, tailq); 471 472 return ig_map; 473 } 474 475 static void 476 _iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map, 477 struct spdk_iscsi_ig_map *ig_map) 478 { 479 TAILQ_REMOVE(&pg_map->ig_map_head, ig_map, tailq); 480 pg_map->num_ig_maps--; 481 ig_map->ig->ref--; 482 free(ig_map); 483 } 484 485 static int 486 iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map, 487 struct spdk_iscsi_init_grp *ig) 488 { 489 struct spdk_iscsi_ig_map *ig_map; 490 491 ig_map = iscsi_pg_map_find_ig_map(pg_map, ig); 492 if (ig_map == NULL) { 493 return -ENOENT; 494 } 495 496 _iscsi_pg_map_delete_ig_map(pg_map, ig_map); 497 return 0; 498 } 499 500 static void 501 iscsi_pg_map_delete_all_ig_maps(struct spdk_iscsi_pg_map *pg_map) 502 { 503 struct spdk_iscsi_ig_map *ig_map, *tmp; 504 505 TAILQ_FOREACH_SAFE(ig_map, &pg_map->ig_map_head, tailq, tmp) { 506 _iscsi_pg_map_delete_ig_map(pg_map, ig_map); 507 } 508 } 509 510 static struct spdk_iscsi_pg_map * 511 iscsi_tgt_node_find_pg_map(struct spdk_iscsi_tgt_node *target, 512 struct spdk_iscsi_portal_grp *pg) 513 { 514 struct spdk_iscsi_pg_map *pg_map; 515 516 TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) { 517 if (pg_map->pg == pg) { 518 return pg_map; 519 } 520 } 521 522 return NULL; 523 } 524 525 static struct spdk_iscsi_pg_map * 526 iscsi_tgt_node_add_pg_map(struct spdk_iscsi_tgt_node *target, 527 struct spdk_iscsi_portal_grp *pg) 528 { 529 struct spdk_iscsi_pg_map *pg_map; 530 char port_name[MAX_TMPBUF]; 531 int rc; 532 533 if (iscsi_tgt_node_find_pg_map(target, pg) != NULL) { 534 return NULL; 535 } 536 537 if (target->num_pg_maps >= SPDK_SCSI_DEV_MAX_PORTS) { 538 SPDK_ERRLOG("Number of PG maps is more than allowed (max=%d)\n", 539 SPDK_SCSI_DEV_MAX_PORTS); 540 return NULL; 541 } 542 543 pg_map = malloc(sizeof(*pg_map)); 544 if (pg_map == NULL) { 545 return NULL; 546 } 547 548 snprintf(port_name, sizeof(port_name), "%s,t,0x%4.4x", 549 spdk_scsi_dev_get_name(target->dev), pg->tag); 550 rc = spdk_scsi_dev_add_port(target->dev, pg->tag, port_name); 551 if (rc != 0) { 552 free(pg_map); 553 return NULL; 554 } 555 556 TAILQ_INIT(&pg_map->ig_map_head); 557 pg_map->num_ig_maps = 0; 558 pg->ref++; 559 pg_map->pg = pg; 560 target->num_pg_maps++; 561 TAILQ_INSERT_TAIL(&target->pg_map_head, pg_map, tailq); 562 563 return pg_map; 564 } 565 566 static void 567 _iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target, 568 struct spdk_iscsi_pg_map *pg_map) 569 { 570 TAILQ_REMOVE(&target->pg_map_head, pg_map, tailq); 571 target->num_pg_maps--; 572 pg_map->pg->ref--; 573 574 spdk_scsi_dev_delete_port(target->dev, pg_map->pg->tag); 575 576 free(pg_map); 577 } 578 579 static int 580 iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target, 581 struct spdk_iscsi_portal_grp *pg) 582 { 583 struct spdk_iscsi_pg_map *pg_map; 584 585 pg_map = iscsi_tgt_node_find_pg_map(target, pg); 586 if (pg_map == NULL) { 587 return -ENOENT; 588 } 589 590 if (pg_map->num_ig_maps > 0) { 591 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "delete %d ig_maps forcefully\n", 592 pg_map->num_ig_maps); 593 } 594 595 iscsi_pg_map_delete_all_ig_maps(pg_map); 596 _iscsi_tgt_node_delete_pg_map(target, pg_map); 597 return 0; 598 } 599 600 static void 601 iscsi_tgt_node_delete_ig_maps(struct spdk_iscsi_tgt_node *target, 602 struct spdk_iscsi_init_grp *ig) 603 { 604 struct spdk_iscsi_pg_map *pg_map, *tmp; 605 606 TAILQ_FOREACH_SAFE(pg_map, &target->pg_map_head, tailq, tmp) { 607 iscsi_pg_map_delete_ig_map(pg_map, ig); 608 if (pg_map->num_ig_maps == 0) { 609 _iscsi_tgt_node_delete_pg_map(target, pg_map); 610 } 611 } 612 } 613 614 static void 615 iscsi_tgt_node_delete_all_pg_maps(struct spdk_iscsi_tgt_node *target) 616 { 617 struct spdk_iscsi_pg_map *pg_map, *tmp; 618 619 TAILQ_FOREACH_SAFE(pg_map, &target->pg_map_head, tailq, tmp) { 620 iscsi_pg_map_delete_all_ig_maps(pg_map); 621 _iscsi_tgt_node_delete_pg_map(target, pg_map); 622 } 623 } 624 625 static void 626 _iscsi_tgt_node_destruct(void *cb_arg, int rc) 627 { 628 struct spdk_iscsi_tgt_node *target = cb_arg; 629 iscsi_tgt_node_destruct_cb destruct_cb_fn = target->destruct_cb_fn; 630 void *destruct_cb_arg = target->destruct_cb_arg; 631 632 if (rc != 0) { 633 if (destruct_cb_fn) { 634 destruct_cb_fn(destruct_cb_arg, rc); 635 } 636 return; 637 } 638 639 pthread_mutex_lock(&g_iscsi.mutex); 640 iscsi_tgt_node_delete_all_pg_maps(target); 641 pthread_mutex_unlock(&g_iscsi.mutex); 642 643 pthread_mutex_destroy(&target->mutex); 644 free(target); 645 646 if (destruct_cb_fn) { 647 destruct_cb_fn(destruct_cb_arg, 0); 648 } 649 } 650 651 static int 652 iscsi_tgt_node_check_active_conns(void *arg) 653 { 654 struct spdk_iscsi_tgt_node *target = arg; 655 656 if (iscsi_get_active_conns(target) != 0) { 657 return SPDK_POLLER_BUSY; 658 } 659 660 spdk_poller_unregister(&target->destruct_poller); 661 662 spdk_scsi_dev_destruct(target->dev, _iscsi_tgt_node_destruct, target); 663 664 return SPDK_POLLER_BUSY; 665 } 666 667 static void 668 iscsi_tgt_node_destruct(struct spdk_iscsi_tgt_node *target, 669 iscsi_tgt_node_destruct_cb cb_fn, void *cb_arg) 670 { 671 if (target == NULL) { 672 if (cb_fn) { 673 cb_fn(cb_arg, -ENOENT); 674 } 675 return; 676 } 677 678 if (target->destructed) { 679 SPDK_ERRLOG("Destructing %s is already started\n", target->name); 680 if (cb_fn) { 681 cb_fn(cb_arg, -EBUSY); 682 } 683 return; 684 } 685 686 target->destructed = true; 687 target->destruct_cb_fn = cb_fn; 688 target->destruct_cb_arg = cb_arg; 689 690 iscsi_conns_request_logout(target); 691 692 if (iscsi_get_active_conns(target) != 0) { 693 target->destruct_poller = SPDK_POLLER_REGISTER(iscsi_tgt_node_check_active_conns, 694 target, 10); 695 } else { 696 spdk_scsi_dev_destruct(target->dev, _iscsi_tgt_node_destruct, target); 697 } 698 699 } 700 701 static int 702 iscsi_tgt_node_delete_pg_ig_map(struct spdk_iscsi_tgt_node *target, 703 int pg_tag, int ig_tag) 704 { 705 struct spdk_iscsi_portal_grp *pg; 706 struct spdk_iscsi_init_grp *ig; 707 struct spdk_iscsi_pg_map *pg_map; 708 struct spdk_iscsi_ig_map *ig_map; 709 710 pg = iscsi_portal_grp_find_by_tag(pg_tag); 711 if (pg == NULL) { 712 SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag); 713 return -ENOENT; 714 } 715 ig = iscsi_init_grp_find_by_tag(ig_tag); 716 if (ig == NULL) { 717 SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag); 718 return -ENOENT; 719 } 720 721 pg_map = iscsi_tgt_node_find_pg_map(target, pg); 722 if (pg_map == NULL) { 723 SPDK_ERRLOG("%s: PortalGroup%d is not mapped\n", target->name, pg_tag); 724 return -ENOENT; 725 } 726 ig_map = iscsi_pg_map_find_ig_map(pg_map, ig); 727 if (ig_map == NULL) { 728 SPDK_ERRLOG("%s: InitiatorGroup%d is not mapped\n", target->name, pg_tag); 729 return -ENOENT; 730 } 731 732 _iscsi_pg_map_delete_ig_map(pg_map, ig_map); 733 if (pg_map->num_ig_maps == 0) { 734 _iscsi_tgt_node_delete_pg_map(target, pg_map); 735 } 736 737 return 0; 738 } 739 740 static int 741 iscsi_tgt_node_add_pg_ig_map(struct spdk_iscsi_tgt_node *target, 742 int pg_tag, int ig_tag) 743 { 744 struct spdk_iscsi_portal_grp *pg; 745 struct spdk_iscsi_pg_map *pg_map; 746 struct spdk_iscsi_init_grp *ig; 747 struct spdk_iscsi_ig_map *ig_map; 748 bool new_pg_map = false; 749 750 pg = iscsi_portal_grp_find_by_tag(pg_tag); 751 if (pg == NULL) { 752 SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag); 753 return -ENOENT; 754 } 755 ig = iscsi_init_grp_find_by_tag(ig_tag); 756 if (ig == NULL) { 757 SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag); 758 return -ENOENT; 759 } 760 761 /* get existing pg_map or create new pg_map and add it to target */ 762 pg_map = iscsi_tgt_node_find_pg_map(target, pg); 763 if (pg_map == NULL) { 764 pg_map = iscsi_tgt_node_add_pg_map(target, pg); 765 if (pg_map == NULL) { 766 goto failed; 767 } 768 new_pg_map = true; 769 } 770 771 /* create new ig_map and add it to pg_map */ 772 ig_map = iscsi_pg_map_add_ig_map(pg_map, ig); 773 if (ig_map == NULL) { 774 goto failed; 775 } 776 777 return 0; 778 779 failed: 780 if (new_pg_map) { 781 _iscsi_tgt_node_delete_pg_map(target, pg_map); 782 } 783 784 return -1; 785 } 786 787 int 788 iscsi_target_node_add_pg_ig_maps(struct spdk_iscsi_tgt_node *target, 789 int *pg_tag_list, int *ig_tag_list, uint16_t num_maps) 790 { 791 uint16_t i; 792 int rc; 793 794 pthread_mutex_lock(&g_iscsi.mutex); 795 for (i = 0; i < num_maps; i++) { 796 rc = iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i], 797 ig_tag_list[i]); 798 if (rc != 0) { 799 SPDK_ERRLOG("could not add map to target\n"); 800 goto invalid; 801 } 802 } 803 pthread_mutex_unlock(&g_iscsi.mutex); 804 return 0; 805 806 invalid: 807 for (; i > 0; --i) { 808 iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i - 1], 809 ig_tag_list[i - 1]); 810 } 811 pthread_mutex_unlock(&g_iscsi.mutex); 812 return -1; 813 } 814 815 int 816 iscsi_target_node_remove_pg_ig_maps(struct spdk_iscsi_tgt_node *target, 817 int *pg_tag_list, int *ig_tag_list, uint16_t num_maps) 818 { 819 uint16_t i; 820 int rc; 821 822 pthread_mutex_lock(&g_iscsi.mutex); 823 for (i = 0; i < num_maps; i++) { 824 rc = iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i], 825 ig_tag_list[i]); 826 if (rc != 0) { 827 SPDK_ERRLOG("could not delete map from target\n"); 828 goto invalid; 829 } 830 } 831 pthread_mutex_unlock(&g_iscsi.mutex); 832 return 0; 833 834 invalid: 835 for (; i > 0; --i) { 836 rc = iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i - 1], 837 ig_tag_list[i - 1]); 838 if (rc != 0) { 839 iscsi_tgt_node_delete_all_pg_maps(target); 840 break; 841 } 842 } 843 pthread_mutex_unlock(&g_iscsi.mutex); 844 return -1; 845 } 846 847 static int 848 check_iscsi_name(const char *name) 849 { 850 const unsigned char *up = (const unsigned char *) name; 851 size_t n; 852 853 /* valid iSCSI name no larger than 223 bytes */ 854 if (strlen(name) > MAX_TARGET_NAME) { 855 return -1; 856 } 857 858 /* valid iSCSI name? */ 859 for (n = 0; up[n] != 0; n++) { 860 if (up[n] > 0x00U && up[n] <= 0x2cU) { 861 return -1; 862 } 863 if (up[n] == 0x2fU) { 864 return -1; 865 } 866 if (up[n] >= 0x3bU && up[n] <= 0x40U) { 867 return -1; 868 } 869 if (up[n] >= 0x5bU && up[n] <= 0x60U) { 870 return -1; 871 } 872 if (up[n] >= 0x7bU && up[n] <= 0x7fU) { 873 return -1; 874 } 875 if (isspace(up[n])) { 876 return -1; 877 } 878 } 879 /* valid format? */ 880 if (strncasecmp(name, "iqn.", 4) == 0) { 881 /* iqn.YYYY-MM.reversed.domain.name */ 882 if (!isdigit(up[4]) || !isdigit(up[5]) || !isdigit(up[6]) 883 || !isdigit(up[7]) || up[8] != '-' || !isdigit(up[9]) 884 || !isdigit(up[10]) || up[11] != '.') { 885 SPDK_ERRLOG("invalid iqn format. " 886 "expect \"iqn.YYYY-MM.reversed.domain.name\"\n"); 887 return -1; 888 } 889 } else if (strncasecmp(name, "eui.", 4) == 0) { 890 /* EUI-64 -> 16bytes */ 891 /* XXX */ 892 } else if (strncasecmp(name, "naa.", 4) == 0) { 893 /* 64bit -> 16bytes, 128bit -> 32bytes */ 894 /* XXX */ 895 } 896 /* OK */ 897 return 0; 898 } 899 900 bool 901 iscsi_check_chap_params(bool disable, bool require, bool mutual, int group) 902 { 903 if (group < 0) { 904 SPDK_ERRLOG("Invalid auth group ID (%d)\n", group); 905 return false; 906 } 907 if ((!disable && !require && !mutual) || /* Auto */ 908 (disable && !require && !mutual) || /* None */ 909 (!disable && require && !mutual) || /* CHAP */ 910 (!disable && require && mutual)) { /* CHAP Mutual */ 911 return true; 912 } 913 SPDK_ERRLOG("Invalid combination of CHAP params (d=%d,r=%d,m=%d)\n", 914 disable, require, mutual); 915 return false; 916 } 917 918 struct spdk_iscsi_tgt_node *iscsi_tgt_node_construct(int target_index, 919 const char *name, const char *alias, 920 int *pg_tag_list, int *ig_tag_list, uint16_t num_maps, 921 const char *bdev_name_list[], int *lun_id_list, int num_luns, 922 int queue_depth, 923 bool disable_chap, bool require_chap, bool mutual_chap, int chap_group, 924 bool header_digest, bool data_digest) 925 { 926 char fullname[MAX_TMPBUF]; 927 struct spdk_iscsi_tgt_node *target; 928 int rc; 929 930 if (!iscsi_check_chap_params(disable_chap, require_chap, 931 mutual_chap, chap_group)) { 932 return NULL; 933 } 934 935 if (num_maps == 0) { 936 SPDK_ERRLOG("num_maps = 0\n"); 937 return NULL; 938 } 939 940 if (name == NULL) { 941 SPDK_ERRLOG("TargetName not found\n"); 942 return NULL; 943 } 944 945 if (strncasecmp(name, "iqn.", 4) != 0 946 && strncasecmp(name, "eui.", 4) != 0 947 && strncasecmp(name, "naa.", 4) != 0) { 948 snprintf(fullname, sizeof(fullname), "%s:%s", g_iscsi.nodebase, name); 949 } else { 950 snprintf(fullname, sizeof(fullname), "%s", name); 951 } 952 953 if (check_iscsi_name(fullname) != 0) { 954 SPDK_ERRLOG("TargetName %s contains an invalid character or format.\n", 955 name); 956 return NULL; 957 } 958 959 target = calloc(1, sizeof(*target)); 960 if (!target) { 961 SPDK_ERRLOG("could not allocate target\n"); 962 return NULL; 963 } 964 965 rc = pthread_mutex_init(&target->mutex, NULL); 966 if (rc != 0) { 967 SPDK_ERRLOG("tgt_node%d: mutex_init() failed\n", target->num); 968 iscsi_tgt_node_destruct(target, NULL, NULL); 969 return NULL; 970 } 971 972 target->num = target_index; 973 974 memcpy(target->name, fullname, strlen(fullname)); 975 976 if (alias != NULL) { 977 if (strlen(alias) > MAX_TARGET_NAME) { 978 iscsi_tgt_node_destruct(target, NULL, NULL); 979 return NULL; 980 } 981 memcpy(target->alias, alias, strlen(alias)); 982 } 983 984 target->dev = spdk_scsi_dev_construct(fullname, bdev_name_list, lun_id_list, num_luns, 985 SPDK_SPC_PROTOCOL_IDENTIFIER_ISCSI, NULL, NULL); 986 if (!target->dev) { 987 SPDK_ERRLOG("Could not construct SCSI device\n"); 988 iscsi_tgt_node_destruct(target, NULL, NULL); 989 return NULL; 990 } 991 992 TAILQ_INIT(&target->pg_map_head); 993 rc = iscsi_target_node_add_pg_ig_maps(target, pg_tag_list, 994 ig_tag_list, num_maps); 995 if (rc != 0) { 996 SPDK_ERRLOG("could not add map to target\n"); 997 iscsi_tgt_node_destruct(target, NULL, NULL); 998 return NULL; 999 } 1000 1001 target->disable_chap = disable_chap; 1002 target->require_chap = require_chap; 1003 target->mutual_chap = mutual_chap; 1004 target->chap_group = chap_group; 1005 target->header_digest = header_digest; 1006 target->data_digest = data_digest; 1007 1008 if (queue_depth > 0 && ((uint32_t)queue_depth <= g_iscsi.MaxQueueDepth)) { 1009 target->queue_depth = queue_depth; 1010 } else { 1011 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "QueueDepth %d is invalid and %d is used instead.\n", 1012 queue_depth, g_iscsi.MaxQueueDepth); 1013 target->queue_depth = g_iscsi.MaxQueueDepth; 1014 } 1015 1016 rc = iscsi_tgt_node_register(target); 1017 if (rc != 0) { 1018 SPDK_ERRLOG("register target is failed\n"); 1019 iscsi_tgt_node_destruct(target, NULL, NULL); 1020 return NULL; 1021 } 1022 1023 return target; 1024 } 1025 1026 static int 1027 iscsi_parse_tgt_node(struct spdk_conf_section *sp) 1028 { 1029 char buf[MAX_TMPBUF]; 1030 struct spdk_iscsi_tgt_node *target; 1031 int pg_tag_list[MAX_TARGET_MAP], ig_tag_list[MAX_TARGET_MAP]; 1032 int num_target_maps; 1033 const char *alias, *pg_tag, *ig_tag; 1034 const char *ag_tag; 1035 const char *val, *name; 1036 int target_num, chap_group, pg_tag_i, ig_tag_i; 1037 bool header_digest, data_digest; 1038 bool disable_chap, require_chap, mutual_chap; 1039 int i; 1040 int lun_id_list[SPDK_SCSI_DEV_MAX_LUN]; 1041 const char *bdev_name_list[SPDK_SCSI_DEV_MAX_LUN]; 1042 int num_luns, queue_depth; 1043 1044 target_num = spdk_conf_section_get_num(sp); 1045 1046 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "add unit %d\n", target_num); 1047 1048 data_digest = false; 1049 header_digest = false; 1050 1051 name = spdk_conf_section_get_val(sp, "TargetName"); 1052 1053 if (name == NULL) { 1054 SPDK_ERRLOG("tgt_node%d: TargetName not found\n", target_num); 1055 return -1; 1056 } 1057 1058 alias = spdk_conf_section_get_val(sp, "TargetAlias"); 1059 1060 /* Setup initiator and portal group mapping */ 1061 val = spdk_conf_section_get_val(sp, "Mapping"); 1062 if (val == NULL) { 1063 /* no map */ 1064 SPDK_ERRLOG("tgt_node%d: no Mapping\n", target_num); 1065 return -1; 1066 } 1067 1068 for (i = 0; i < MAX_TARGET_MAP; i++) { 1069 val = spdk_conf_section_get_nmval(sp, "Mapping", i, 0); 1070 if (val == NULL) { 1071 break; 1072 } 1073 pg_tag = spdk_conf_section_get_nmval(sp, "Mapping", i, 0); 1074 ig_tag = spdk_conf_section_get_nmval(sp, "Mapping", i, 1); 1075 if (pg_tag == NULL || ig_tag == NULL) { 1076 SPDK_ERRLOG("tgt_node%d: mapping error\n", target_num); 1077 return -1; 1078 } 1079 if (strncasecmp(pg_tag, "PortalGroup", 1080 strlen("PortalGroup")) != 0 1081 || sscanf(pg_tag, "%*[^0-9]%d", &pg_tag_i) != 1) { 1082 SPDK_ERRLOG("tgt_node%d: mapping portal error\n", target_num); 1083 return -1; 1084 } 1085 if (strncasecmp(ig_tag, "InitiatorGroup", 1086 strlen("InitiatorGroup")) != 0 1087 || sscanf(ig_tag, "%*[^0-9]%d", &ig_tag_i) != 1) { 1088 SPDK_ERRLOG("tgt_node%d: mapping initiator error\n", target_num); 1089 return -1; 1090 } 1091 if (pg_tag_i < 1 || ig_tag_i < 1) { 1092 SPDK_ERRLOG("tgt_node%d: invalid group tag\n", target_num); 1093 return -1; 1094 } 1095 pg_tag_list[i] = pg_tag_i; 1096 ig_tag_list[i] = ig_tag_i; 1097 } 1098 1099 num_target_maps = i; 1100 1101 /* Setup AuthMethod */ 1102 val = spdk_conf_section_get_val(sp, "AuthMethod"); 1103 disable_chap = false; 1104 require_chap = false; 1105 mutual_chap = false; 1106 if (val != NULL) { 1107 for (i = 0; ; i++) { 1108 val = spdk_conf_section_get_nmval(sp, "AuthMethod", 0, i); 1109 if (val == NULL) { 1110 break; 1111 } 1112 if (strcasecmp(val, "CHAP") == 0) { 1113 require_chap = true; 1114 } else if (strcasecmp(val, "Mutual") == 0) { 1115 mutual_chap = true; 1116 } else if (strcasecmp(val, "Auto") == 0) { 1117 disable_chap = false; 1118 require_chap = false; 1119 mutual_chap = false; 1120 } else if (strcasecmp(val, "None") == 0) { 1121 disable_chap = true; 1122 require_chap = false; 1123 mutual_chap = false; 1124 } else { 1125 SPDK_ERRLOG("tgt_node%d: unknown auth\n", target_num); 1126 return -1; 1127 } 1128 } 1129 if (mutual_chap && !require_chap) { 1130 SPDK_ERRLOG("tgt_node%d: Mutual but not CHAP\n", target_num); 1131 return -1; 1132 } 1133 } 1134 if (disable_chap) { 1135 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod None\n"); 1136 } else if (!require_chap) { 1137 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod Auto\n"); 1138 } else { 1139 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod CHAP %s\n", 1140 mutual_chap ? "Mutual" : ""); 1141 } 1142 1143 val = spdk_conf_section_get_val(sp, "AuthGroup"); 1144 if (val == NULL) { 1145 chap_group = 0; 1146 } else { 1147 ag_tag = val; 1148 if (strcasecmp(ag_tag, "None") == 0) { 1149 chap_group = 0; 1150 } else { 1151 if (strncasecmp(ag_tag, "AuthGroup", 1152 strlen("AuthGroup")) != 0 1153 || sscanf(ag_tag, "%*[^0-9]%d", &chap_group) != 1) { 1154 SPDK_ERRLOG("tgt_node%d: auth group error\n", target_num); 1155 return -1; 1156 } 1157 if (chap_group == 0) { 1158 SPDK_ERRLOG("tgt_node%d: invalid auth group 0\n", target_num); 1159 return -1; 1160 } 1161 } 1162 } 1163 if (chap_group == 0) { 1164 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthGroup None\n"); 1165 } else { 1166 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthGroup AuthGroup%d\n", chap_group); 1167 } 1168 1169 val = spdk_conf_section_get_val(sp, "UseDigest"); 1170 if (val != NULL) { 1171 for (i = 0; ; i++) { 1172 val = spdk_conf_section_get_nmval(sp, "UseDigest", 0, i); 1173 if (val == NULL) { 1174 break; 1175 } 1176 if (strcasecmp(val, "Header") == 0) { 1177 header_digest = true; 1178 } else if (strcasecmp(val, "Data") == 0) { 1179 data_digest = true; 1180 } else if (strcasecmp(val, "Auto") == 0) { 1181 header_digest = false; 1182 data_digest = false; 1183 } else { 1184 SPDK_ERRLOG("tgt_node%d: unknown digest\n", target_num); 1185 return -1; 1186 } 1187 } 1188 } 1189 if (!header_digest && !data_digest) { 1190 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "UseDigest Auto\n"); 1191 } else { 1192 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "UseDigest %s %s\n", 1193 header_digest ? "Header" : "", 1194 data_digest ? "Data" : ""); 1195 } 1196 1197 val = spdk_conf_section_get_val(sp, "QueueDepth"); 1198 if (val == NULL) { 1199 queue_depth = g_iscsi.MaxQueueDepth; 1200 } else { 1201 queue_depth = (int) strtol(val, NULL, 10); 1202 } 1203 1204 num_luns = 0; 1205 1206 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 1207 snprintf(buf, sizeof(buf), "LUN%d", i); 1208 val = spdk_conf_section_get_val(sp, buf); 1209 if (val == NULL) { 1210 continue; 1211 } 1212 1213 bdev_name_list[num_luns] = val; 1214 lun_id_list[num_luns] = i; 1215 num_luns++; 1216 } 1217 1218 if (num_luns == 0) { 1219 SPDK_ERRLOG("tgt_node%d: No LUN specified for target %s.\n", target_num, name); 1220 return -1; 1221 } 1222 1223 target = iscsi_tgt_node_construct(target_num, name, alias, 1224 pg_tag_list, ig_tag_list, num_target_maps, 1225 bdev_name_list, lun_id_list, num_luns, queue_depth, 1226 disable_chap, require_chap, mutual_chap, chap_group, 1227 header_digest, data_digest); 1228 1229 if (target == NULL) { 1230 SPDK_ERRLOG("tgt_node%d: add_iscsi_target_node error\n", target_num); 1231 return -1; 1232 } 1233 1234 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 1235 struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i); 1236 1237 if (lun) { 1238 SPDK_INFOLOG(SPDK_LOG_ISCSI, "device %d: LUN%d %s\n", 1239 spdk_scsi_dev_get_id(target->dev), 1240 spdk_scsi_lun_get_id(lun), 1241 spdk_scsi_lun_get_bdev_name(lun)); 1242 } 1243 } 1244 1245 return 0; 1246 } 1247 1248 int iscsi_parse_tgt_nodes(void) 1249 { 1250 struct spdk_conf_section *sp; 1251 int rc; 1252 1253 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "iscsi_parse_tgt_nodes\n"); 1254 1255 sp = spdk_conf_first_section(NULL); 1256 while (sp != NULL) { 1257 if (spdk_conf_section_match_prefix(sp, "TargetNode")) { 1258 int tag = spdk_conf_section_get_num(sp); 1259 1260 if (tag > SPDK_TN_TAG_MAX) { 1261 SPDK_ERRLOG("tag %d is invalid\n", tag); 1262 return -1; 1263 } 1264 rc = iscsi_parse_tgt_node(sp); 1265 if (rc < 0) { 1266 SPDK_ERRLOG("spdk_iscsi_parse_tgt_node() failed\n"); 1267 return -1; 1268 } 1269 } 1270 sp = spdk_conf_next_section(sp); 1271 } 1272 return 0; 1273 } 1274 1275 void 1276 iscsi_shutdown_tgt_nodes(void) 1277 { 1278 struct spdk_iscsi_tgt_node *target; 1279 1280 pthread_mutex_lock(&g_iscsi.mutex); 1281 while (!TAILQ_EMPTY(&g_iscsi.target_head)) { 1282 target = TAILQ_FIRST(&g_iscsi.target_head); 1283 TAILQ_REMOVE(&g_iscsi.target_head, target, tailq); 1284 1285 pthread_mutex_unlock(&g_iscsi.mutex); 1286 1287 iscsi_tgt_node_destruct(target, NULL, NULL); 1288 1289 pthread_mutex_lock(&g_iscsi.mutex); 1290 } 1291 pthread_mutex_unlock(&g_iscsi.mutex); 1292 } 1293 1294 void 1295 iscsi_shutdown_tgt_node_by_name(const char *target_name, 1296 iscsi_tgt_node_destruct_cb cb_fn, void *cb_arg) 1297 { 1298 struct spdk_iscsi_tgt_node *target; 1299 1300 pthread_mutex_lock(&g_iscsi.mutex); 1301 target = iscsi_find_tgt_node(target_name); 1302 if (target != NULL) { 1303 iscsi_tgt_node_unregister(target); 1304 pthread_mutex_unlock(&g_iscsi.mutex); 1305 1306 iscsi_tgt_node_destruct(target, cb_fn, cb_arg); 1307 1308 return; 1309 } 1310 pthread_mutex_unlock(&g_iscsi.mutex); 1311 1312 if (cb_fn) { 1313 cb_fn(cb_arg, -ENOENT); 1314 } 1315 } 1316 1317 bool 1318 iscsi_tgt_node_is_destructed(struct spdk_iscsi_tgt_node *target) 1319 { 1320 return target->destructed; 1321 } 1322 1323 int 1324 iscsi_tgt_node_cleanup_luns(struct spdk_iscsi_conn *conn, 1325 struct spdk_iscsi_tgt_node *target) 1326 { 1327 int i; 1328 struct spdk_iscsi_task *task; 1329 1330 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 1331 struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i); 1332 1333 if (!lun) { 1334 continue; 1335 } 1336 1337 /* we create a fake management task per LUN to cleanup */ 1338 task = iscsi_task_get(conn, NULL, iscsi_task_mgmt_cpl); 1339 if (!task) { 1340 SPDK_ERRLOG("Unable to acquire task\n"); 1341 return -1; 1342 } 1343 1344 task->scsi.target_port = conn->target_port; 1345 task->scsi.initiator_port = conn->initiator_port; 1346 task->scsi.lun = lun; 1347 1348 iscsi_op_abort_task_set(task, SPDK_SCSI_TASK_FUNC_LUN_RESET); 1349 } 1350 1351 return 0; 1352 } 1353 1354 void iscsi_tgt_node_delete_map(struct spdk_iscsi_portal_grp *portal_group, 1355 struct spdk_iscsi_init_grp *initiator_group) 1356 { 1357 struct spdk_iscsi_tgt_node *target; 1358 1359 pthread_mutex_lock(&g_iscsi.mutex); 1360 TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) { 1361 if (portal_group) { 1362 iscsi_tgt_node_delete_pg_map(target, portal_group); 1363 } 1364 if (initiator_group) { 1365 iscsi_tgt_node_delete_ig_maps(target, initiator_group); 1366 } 1367 } 1368 pthread_mutex_unlock(&g_iscsi.mutex); 1369 } 1370 1371 int 1372 iscsi_tgt_node_add_lun(struct spdk_iscsi_tgt_node *target, 1373 const char *bdev_name, int lun_id) 1374 { 1375 struct spdk_scsi_dev *dev; 1376 int rc; 1377 1378 if (target->num_active_conns > 0) { 1379 SPDK_ERRLOG("Target has active connections (count=%d)\n", 1380 target->num_active_conns); 1381 return -1; 1382 } 1383 1384 if (lun_id < -1 || lun_id >= SPDK_SCSI_DEV_MAX_LUN) { 1385 SPDK_ERRLOG("Specified LUN ID (%d) is invalid\n", lun_id); 1386 return -1; 1387 } 1388 1389 dev = target->dev; 1390 if (dev == NULL) { 1391 SPDK_ERRLOG("SCSI device is not found\n"); 1392 return -1; 1393 } 1394 1395 rc = spdk_scsi_dev_add_lun(dev, bdev_name, lun_id, NULL, NULL); 1396 if (rc != 0) { 1397 SPDK_ERRLOG("spdk_scsi_dev_add_lun failed\n"); 1398 return -1; 1399 } 1400 1401 return 0; 1402 } 1403 1404 int 1405 iscsi_tgt_node_set_chap_params(struct spdk_iscsi_tgt_node *target, 1406 bool disable_chap, bool require_chap, 1407 bool mutual_chap, int32_t chap_group) 1408 { 1409 if (!iscsi_check_chap_params(disable_chap, require_chap, 1410 mutual_chap, chap_group)) { 1411 return -EINVAL; 1412 } 1413 1414 pthread_mutex_lock(&target->mutex); 1415 target->disable_chap = disable_chap; 1416 target->require_chap = require_chap; 1417 target->mutual_chap = mutual_chap; 1418 target->chap_group = chap_group; 1419 pthread_mutex_unlock(&target->mutex); 1420 1421 return 0; 1422 } 1423 1424 static const char *target_nodes_section = \ 1425 "\n" 1426 "# Users should change the TargetNode section(s) below to match the\n" 1427 "# desired iSCSI target node configuration.\n" 1428 "# TargetName, Mapping, LUN0 are minimum required\n"; 1429 1430 #define TARGET_NODE_TMPL \ 1431 "[TargetNode%d]\n" \ 1432 " Comment \"Target%d\"\n" \ 1433 " TargetName %s\n" \ 1434 " TargetAlias \"%s\"\n" 1435 1436 #define TARGET_NODE_PGIG_MAPPING_TMPL \ 1437 " Mapping PortalGroup%d InitiatorGroup%d\n" 1438 1439 #define TARGET_NODE_AUTH_TMPL \ 1440 " AuthMethod %s\n" \ 1441 " AuthGroup %s\n" \ 1442 " UseDigest %s\n" 1443 1444 #define TARGET_NODE_QD_TMPL \ 1445 " QueueDepth %d\n\n" 1446 1447 #define TARGET_NODE_LUN_TMPL \ 1448 " LUN%d %s\n" 1449 1450 void 1451 iscsi_tgt_nodes_config_text(FILE *fp) 1452 { 1453 int l = 0; 1454 struct spdk_scsi_dev *dev = NULL; 1455 struct spdk_iscsi_tgt_node *target = NULL; 1456 struct spdk_iscsi_pg_map *pg_map; 1457 struct spdk_iscsi_ig_map *ig_map; 1458 1459 /* Create target nodes section */ 1460 fprintf(fp, "%s", target_nodes_section); 1461 1462 TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) { 1463 int idx; 1464 const char *authmethod = "None"; 1465 char authgroup[32] = "None"; 1466 const char *usedigest = "Auto"; 1467 1468 dev = target->dev; 1469 if (NULL == dev) { continue; } 1470 1471 idx = target->num; 1472 fprintf(fp, TARGET_NODE_TMPL, idx, idx, target->name, spdk_scsi_dev_get_name(dev)); 1473 1474 TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) { 1475 TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) { 1476 fprintf(fp, TARGET_NODE_PGIG_MAPPING_TMPL, 1477 pg_map->pg->tag, 1478 ig_map->ig->tag); 1479 } 1480 } 1481 1482 if (target->disable_chap) { 1483 authmethod = "None"; 1484 } else if (!target->require_chap) { 1485 authmethod = "Auto"; 1486 } else if (target->mutual_chap) { 1487 authmethod = "CHAP Mutual"; 1488 } else { 1489 authmethod = "CHAP"; 1490 } 1491 1492 if (target->chap_group > 0) { 1493 snprintf(authgroup, sizeof(authgroup), "AuthGroup%d", target->chap_group); 1494 } 1495 1496 if (target->header_digest) { 1497 usedigest = "Header"; 1498 } else if (target->data_digest) { 1499 usedigest = "Data"; 1500 } 1501 1502 fprintf(fp, TARGET_NODE_AUTH_TMPL, 1503 authmethod, authgroup, usedigest); 1504 1505 for (l = 0; l < SPDK_SCSI_DEV_MAX_LUN; l++) { 1506 struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(dev, l); 1507 1508 if (!lun) { 1509 continue; 1510 } 1511 1512 fprintf(fp, TARGET_NODE_LUN_TMPL, 1513 spdk_scsi_lun_get_id(lun), 1514 spdk_scsi_lun_get_bdev_name(lun)); 1515 } 1516 1517 fprintf(fp, TARGET_NODE_QD_TMPL, 1518 target->queue_depth); 1519 } 1520 } 1521 1522 static void 1523 iscsi_tgt_node_info_json(struct spdk_iscsi_tgt_node *target, 1524 struct spdk_json_write_ctx *w) 1525 { 1526 struct spdk_iscsi_pg_map *pg_map; 1527 struct spdk_iscsi_ig_map *ig_map; 1528 int i; 1529 1530 spdk_json_write_object_begin(w); 1531 1532 spdk_json_write_named_string(w, "name", target->name); 1533 1534 if (target->alias[0] != '\0') { 1535 spdk_json_write_named_string(w, "alias_name", target->alias); 1536 } 1537 1538 spdk_json_write_named_array_begin(w, "pg_ig_maps"); 1539 TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) { 1540 TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) { 1541 spdk_json_write_object_begin(w); 1542 spdk_json_write_named_int32(w, "pg_tag", pg_map->pg->tag); 1543 spdk_json_write_named_int32(w, "ig_tag", ig_map->ig->tag); 1544 spdk_json_write_object_end(w); 1545 } 1546 } 1547 spdk_json_write_array_end(w); 1548 1549 spdk_json_write_named_array_begin(w, "luns"); 1550 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 1551 struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i); 1552 1553 if (lun) { 1554 spdk_json_write_object_begin(w); 1555 spdk_json_write_named_string(w, "bdev_name", spdk_scsi_lun_get_bdev_name(lun)); 1556 spdk_json_write_named_int32(w, "lun_id", spdk_scsi_lun_get_id(lun)); 1557 spdk_json_write_object_end(w); 1558 } 1559 } 1560 spdk_json_write_array_end(w); 1561 1562 spdk_json_write_named_int32(w, "queue_depth", target->queue_depth); 1563 1564 spdk_json_write_named_bool(w, "disable_chap", target->disable_chap); 1565 spdk_json_write_named_bool(w, "require_chap", target->require_chap); 1566 spdk_json_write_named_bool(w, "mutual_chap", target->mutual_chap); 1567 spdk_json_write_named_int32(w, "chap_group", target->chap_group); 1568 1569 spdk_json_write_named_bool(w, "header_digest", target->header_digest); 1570 spdk_json_write_named_bool(w, "data_digest", target->data_digest); 1571 1572 spdk_json_write_object_end(w); 1573 } 1574 1575 static void 1576 iscsi_tgt_node_config_json(struct spdk_iscsi_tgt_node *target, 1577 struct spdk_json_write_ctx *w) 1578 { 1579 spdk_json_write_object_begin(w); 1580 1581 spdk_json_write_named_string(w, "method", "iscsi_create_target_node"); 1582 1583 spdk_json_write_name(w, "params"); 1584 iscsi_tgt_node_info_json(target, w); 1585 1586 spdk_json_write_object_end(w); 1587 } 1588 1589 void 1590 iscsi_tgt_nodes_info_json(struct spdk_json_write_ctx *w) 1591 { 1592 struct spdk_iscsi_tgt_node *target; 1593 1594 TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) { 1595 iscsi_tgt_node_info_json(target, w); 1596 } 1597 } 1598 1599 void 1600 iscsi_tgt_nodes_config_json(struct spdk_json_write_ctx *w) 1601 { 1602 struct spdk_iscsi_tgt_node *target; 1603 1604 TAILQ_FOREACH(target, &g_iscsi.target_head, tailq) { 1605 iscsi_tgt_node_config_json(target, w); 1606 } 1607 } 1608