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 1024 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 spdk_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->portal->group->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 spdk_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 if (alloc_len - total < 1) { 329 SPDK_ERRLOG("data space small %d\n", alloc_len); 330 return total; 331 } 332 333 pthread_mutex_lock(&g_spdk_iscsi.mutex); 334 TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) { 335 if (strcasecmp(tiqn, "ALL") != 0 336 && strcasecmp(tiqn, target->name) != 0) { 337 continue; 338 } 339 rc = iscsi_tgt_node_allow_iscsi_name(target, iiqn); 340 if (rc == 0) { 341 continue; 342 } 343 344 /* DO SENDTARGETS */ 345 len = snprintf((char *) data + total, alloc_len - total, 346 "TargetName=%s", target->name); 347 total += len + 1; 348 349 /* write to data */ 350 TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) { 351 pg = pg_map->pg; 352 TAILQ_FOREACH(p, &pg->head, per_pg_tailq) { 353 if (alloc_len - total < 1) { 354 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 355 SPDK_ERRLOG("data space small %d\n", alloc_len); 356 return total; 357 } 358 host = p->host; 359 /* wildcard? */ 360 if (strcasecmp(host, "[::]") == 0 361 || strcasecmp(host, "0.0.0.0") == 0) { 362 if (spdk_sock_is_ipv6(conn->sock)) { 363 snprintf(buf, sizeof buf, "[%s]", 364 conn->target_addr); 365 host = buf; 366 } else if (spdk_sock_is_ipv4(conn->sock)) { 367 snprintf(buf, sizeof buf, "%s", 368 conn->target_addr); 369 host = buf; 370 } else { 371 /* skip portal for the family */ 372 continue; 373 } 374 } 375 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, 376 "TargetAddress=%s:%s,%d\n", 377 host, p->port, pg->tag); 378 len = snprintf((char *) data + total, 379 alloc_len - total, 380 "TargetAddress=%s:%s,%d", 381 host, p->port, pg->tag); 382 total += len + 1; 383 } 384 } 385 } 386 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 387 388 return total; 389 } 390 391 struct spdk_iscsi_tgt_node * 392 spdk_iscsi_find_tgt_node(const char *target_name) 393 { 394 struct spdk_iscsi_tgt_node *target; 395 396 if (target_name == NULL) { 397 return NULL; 398 } 399 TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) { 400 if (strcasecmp(target_name, target->name) == 0) { 401 return target; 402 } 403 } 404 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "can't find target %s\n", target_name); 405 return NULL; 406 } 407 408 static int 409 iscsi_tgt_node_register(struct spdk_iscsi_tgt_node *target) 410 { 411 pthread_mutex_lock(&g_spdk_iscsi.mutex); 412 413 if (spdk_iscsi_find_tgt_node(target->name) != NULL) { 414 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 415 return -EEXIST; 416 } 417 418 TAILQ_INSERT_TAIL(&g_spdk_iscsi.target_head, target, tailq); 419 420 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 421 return 0; 422 } 423 424 static int 425 iscsi_tgt_node_unregister(struct spdk_iscsi_tgt_node *target) 426 { 427 struct spdk_iscsi_tgt_node *t; 428 429 TAILQ_FOREACH(t, &g_spdk_iscsi.target_head, tailq) { 430 if (t == target) { 431 TAILQ_REMOVE(&g_spdk_iscsi.target_head, t, tailq); 432 return 0; 433 } 434 } 435 436 return -1; 437 } 438 439 static struct spdk_iscsi_ig_map * 440 iscsi_pg_map_find_ig_map(struct spdk_iscsi_pg_map *pg_map, 441 struct spdk_iscsi_init_grp *ig) 442 { 443 struct spdk_iscsi_ig_map *ig_map; 444 445 TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) { 446 if (ig_map->ig == ig) { 447 return ig_map; 448 } 449 } 450 451 return NULL; 452 } 453 454 static struct spdk_iscsi_ig_map * 455 iscsi_pg_map_add_ig_map(struct spdk_iscsi_pg_map *pg_map, 456 struct spdk_iscsi_init_grp *ig) 457 { 458 struct spdk_iscsi_ig_map *ig_map; 459 460 if (iscsi_pg_map_find_ig_map(pg_map, ig) != NULL) { 461 return NULL; 462 } 463 464 ig_map = malloc(sizeof(*ig_map)); 465 if (ig_map == NULL) { 466 return NULL; 467 } 468 469 ig_map->ig = ig; 470 ig->ref++; 471 pg_map->num_ig_maps++; 472 TAILQ_INSERT_TAIL(&pg_map->ig_map_head, ig_map, tailq); 473 474 return ig_map; 475 } 476 477 static void 478 _iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map, 479 struct spdk_iscsi_ig_map *ig_map) 480 { 481 TAILQ_REMOVE(&pg_map->ig_map_head, ig_map, tailq); 482 pg_map->num_ig_maps--; 483 ig_map->ig->ref--; 484 free(ig_map); 485 } 486 487 static int 488 iscsi_pg_map_delete_ig_map(struct spdk_iscsi_pg_map *pg_map, 489 struct spdk_iscsi_init_grp *ig) 490 { 491 struct spdk_iscsi_ig_map *ig_map; 492 493 ig_map = iscsi_pg_map_find_ig_map(pg_map, ig); 494 if (ig_map == NULL) { 495 return -ENOENT; 496 } 497 498 _iscsi_pg_map_delete_ig_map(pg_map, ig_map); 499 return 0; 500 } 501 502 static void 503 iscsi_pg_map_delete_all_ig_maps(struct spdk_iscsi_pg_map *pg_map) 504 { 505 struct spdk_iscsi_ig_map *ig_map, *tmp; 506 507 TAILQ_FOREACH_SAFE(ig_map, &pg_map->ig_map_head, tailq, tmp) { 508 _iscsi_pg_map_delete_ig_map(pg_map, ig_map); 509 } 510 } 511 512 static struct spdk_iscsi_pg_map * 513 iscsi_tgt_node_find_pg_map(struct spdk_iscsi_tgt_node *target, 514 struct spdk_iscsi_portal_grp *pg) 515 { 516 struct spdk_iscsi_pg_map *pg_map; 517 518 TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) { 519 if (pg_map->pg == pg) { 520 return pg_map; 521 } 522 } 523 524 return NULL; 525 } 526 527 static struct spdk_iscsi_pg_map * 528 iscsi_tgt_node_add_pg_map(struct spdk_iscsi_tgt_node *target, 529 struct spdk_iscsi_portal_grp *pg) 530 { 531 struct spdk_iscsi_pg_map *pg_map; 532 char port_name[MAX_TMPBUF]; 533 int rc; 534 535 if (iscsi_tgt_node_find_pg_map(target, pg) != NULL) { 536 return NULL; 537 } 538 539 if (target->num_pg_maps >= SPDK_SCSI_DEV_MAX_PORTS) { 540 SPDK_ERRLOG("Number of PG maps is more than allowed (max=%d)\n", 541 SPDK_SCSI_DEV_MAX_PORTS); 542 return NULL; 543 } 544 545 pg_map = malloc(sizeof(*pg_map)); 546 if (pg_map == NULL) { 547 return NULL; 548 } 549 550 snprintf(port_name, sizeof(port_name), "%s,t,0x%4.4x", 551 spdk_scsi_dev_get_name(target->dev), pg->tag); 552 rc = spdk_scsi_dev_add_port(target->dev, pg->tag, port_name); 553 if (rc != 0) { 554 free(pg_map); 555 return NULL; 556 } 557 558 TAILQ_INIT(&pg_map->ig_map_head); 559 pg_map->num_ig_maps = 0; 560 pg->ref++; 561 pg_map->pg = pg; 562 target->num_pg_maps++; 563 TAILQ_INSERT_TAIL(&target->pg_map_head, pg_map, tailq); 564 565 return pg_map; 566 } 567 568 static void 569 _iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target, 570 struct spdk_iscsi_pg_map *pg_map) 571 { 572 TAILQ_REMOVE(&target->pg_map_head, pg_map, tailq); 573 target->num_pg_maps--; 574 pg_map->pg->ref--; 575 576 spdk_scsi_dev_delete_port(target->dev, pg_map->pg->tag); 577 578 free(pg_map); 579 } 580 581 static int 582 iscsi_tgt_node_delete_pg_map(struct spdk_iscsi_tgt_node *target, 583 struct spdk_iscsi_portal_grp *pg) 584 { 585 struct spdk_iscsi_pg_map *pg_map; 586 587 pg_map = iscsi_tgt_node_find_pg_map(target, pg); 588 if (pg_map == NULL) { 589 return -ENOENT; 590 } 591 592 if (pg_map->num_ig_maps > 0) { 593 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "delete %d ig_maps forcefully\n", 594 pg_map->num_ig_maps); 595 } 596 597 iscsi_pg_map_delete_all_ig_maps(pg_map); 598 _iscsi_tgt_node_delete_pg_map(target, pg_map); 599 return 0; 600 } 601 602 static void 603 iscsi_tgt_node_delete_ig_maps(struct spdk_iscsi_tgt_node *target, 604 struct spdk_iscsi_init_grp *ig) 605 { 606 struct spdk_iscsi_pg_map *pg_map, *tmp; 607 608 TAILQ_FOREACH_SAFE(pg_map, &target->pg_map_head, tailq, tmp) { 609 iscsi_pg_map_delete_ig_map(pg_map, ig); 610 if (pg_map->num_ig_maps == 0) { 611 _iscsi_tgt_node_delete_pg_map(target, pg_map); 612 } 613 } 614 } 615 616 static void 617 iscsi_tgt_node_delete_all_pg_maps(struct spdk_iscsi_tgt_node *target) 618 { 619 struct spdk_iscsi_pg_map *pg_map, *tmp; 620 621 TAILQ_FOREACH_SAFE(pg_map, &target->pg_map_head, tailq, tmp) { 622 iscsi_pg_map_delete_all_ig_maps(pg_map); 623 _iscsi_tgt_node_delete_pg_map(target, pg_map); 624 } 625 } 626 627 static void 628 _iscsi_tgt_node_destruct(void *cb_arg, int rc) 629 { 630 struct spdk_iscsi_tgt_node *target = cb_arg; 631 iscsi_tgt_node_destruct_cb destruct_cb_fn = target->destruct_cb_fn; 632 void *destruct_cb_arg = target->destruct_cb_arg; 633 634 if (rc != 0) { 635 if (destruct_cb_fn) { 636 destruct_cb_fn(destruct_cb_arg, rc); 637 } 638 return; 639 } 640 641 free(target->name); 642 free(target->alias); 643 644 pthread_mutex_lock(&g_spdk_iscsi.mutex); 645 iscsi_tgt_node_delete_all_pg_maps(target); 646 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 647 648 pthread_mutex_destroy(&target->mutex); 649 free(target); 650 651 if (destruct_cb_fn) { 652 destruct_cb_fn(destruct_cb_arg, 0); 653 } 654 } 655 656 static int 657 iscsi_tgt_node_check_active_conns(void *arg) 658 { 659 struct spdk_iscsi_tgt_node *target = arg; 660 661 if (spdk_iscsi_get_active_conns(target) != 0) { 662 return 1; 663 } 664 665 spdk_poller_unregister(&target->destruct_poller); 666 667 spdk_scsi_dev_destruct(target->dev); 668 _iscsi_tgt_node_destruct(target, 0); 669 670 return 1; 671 } 672 673 static void 674 iscsi_tgt_node_destruct(struct spdk_iscsi_tgt_node *target, 675 iscsi_tgt_node_destruct_cb cb_fn, void *cb_arg) 676 { 677 if (target == NULL) { 678 if (cb_fn) { 679 cb_fn(cb_arg, -ENOENT); 680 } 681 return; 682 } 683 684 if (target->destructed) { 685 SPDK_ERRLOG("Destructing %s is already started\n", target->name); 686 if (cb_fn) { 687 cb_fn(cb_arg, -EBUSY); 688 } 689 return; 690 } 691 692 target->destructed = true; 693 target->destruct_cb_fn = cb_fn; 694 target->destruct_cb_arg = cb_arg; 695 696 spdk_iscsi_conns_start_exit(target); 697 698 if (spdk_iscsi_get_active_conns(target) != 0) { 699 target->destruct_poller = spdk_poller_register(iscsi_tgt_node_check_active_conns, 700 target, 10); 701 } else { 702 spdk_scsi_dev_destruct(target->dev); 703 _iscsi_tgt_node_destruct(target, 0); 704 } 705 706 } 707 708 static int 709 iscsi_tgt_node_delete_pg_ig_map(struct spdk_iscsi_tgt_node *target, 710 int pg_tag, int ig_tag) 711 { 712 struct spdk_iscsi_portal_grp *pg; 713 struct spdk_iscsi_init_grp *ig; 714 struct spdk_iscsi_pg_map *pg_map; 715 struct spdk_iscsi_ig_map *ig_map; 716 717 pg = spdk_iscsi_portal_grp_find_by_tag(pg_tag); 718 if (pg == NULL) { 719 SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag); 720 return -ENOENT; 721 } 722 ig = spdk_iscsi_init_grp_find_by_tag(ig_tag); 723 if (ig == NULL) { 724 SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag); 725 return -ENOENT; 726 } 727 728 pg_map = iscsi_tgt_node_find_pg_map(target, pg); 729 if (pg_map == NULL) { 730 SPDK_ERRLOG("%s: PortalGroup%d is not mapped\n", target->name, pg_tag); 731 return -ENOENT; 732 } 733 ig_map = iscsi_pg_map_find_ig_map(pg_map, ig); 734 if (ig_map == NULL) { 735 SPDK_ERRLOG("%s: InitiatorGroup%d is not mapped\n", target->name, pg_tag); 736 return -ENOENT; 737 } 738 739 _iscsi_pg_map_delete_ig_map(pg_map, ig_map); 740 if (pg_map->num_ig_maps == 0) { 741 _iscsi_tgt_node_delete_pg_map(target, pg_map); 742 } 743 744 return 0; 745 } 746 747 static int 748 iscsi_tgt_node_add_pg_ig_map(struct spdk_iscsi_tgt_node *target, 749 int pg_tag, int ig_tag) 750 { 751 struct spdk_iscsi_portal_grp *pg; 752 struct spdk_iscsi_pg_map *pg_map; 753 struct spdk_iscsi_init_grp *ig; 754 struct spdk_iscsi_ig_map *ig_map; 755 bool new_pg_map = false; 756 757 pg = spdk_iscsi_portal_grp_find_by_tag(pg_tag); 758 if (pg == NULL) { 759 SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag); 760 return -ENOENT; 761 } 762 ig = spdk_iscsi_init_grp_find_by_tag(ig_tag); 763 if (ig == NULL) { 764 SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag); 765 return -ENOENT; 766 } 767 768 /* get existing pg_map or create new pg_map and add it to target */ 769 pg_map = iscsi_tgt_node_find_pg_map(target, pg); 770 if (pg_map == NULL) { 771 pg_map = iscsi_tgt_node_add_pg_map(target, pg); 772 if (pg_map == NULL) { 773 goto failed; 774 } 775 new_pg_map = true; 776 } 777 778 /* create new ig_map and add it to pg_map */ 779 ig_map = iscsi_pg_map_add_ig_map(pg_map, ig); 780 if (ig_map == NULL) { 781 goto failed; 782 } 783 784 return 0; 785 786 failed: 787 if (new_pg_map) { 788 _iscsi_tgt_node_delete_pg_map(target, pg_map); 789 } 790 791 return -1; 792 } 793 794 int 795 spdk_iscsi_tgt_node_add_pg_ig_maps(struct spdk_iscsi_tgt_node *target, 796 int *pg_tag_list, int *ig_tag_list, uint16_t num_maps) 797 { 798 uint16_t i; 799 int rc; 800 801 pthread_mutex_lock(&g_spdk_iscsi.mutex); 802 for (i = 0; i < num_maps; i++) { 803 rc = iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i], 804 ig_tag_list[i]); 805 if (rc != 0) { 806 SPDK_ERRLOG("could not add map to target\n"); 807 goto invalid; 808 } 809 } 810 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 811 return 0; 812 813 invalid: 814 for (; i > 0; --i) { 815 iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i - 1], 816 ig_tag_list[i - 1]); 817 } 818 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 819 return -1; 820 } 821 822 int 823 spdk_iscsi_tgt_node_delete_pg_ig_maps(struct spdk_iscsi_tgt_node *target, 824 int *pg_tag_list, int *ig_tag_list, uint16_t num_maps) 825 { 826 uint16_t i; 827 int rc; 828 829 pthread_mutex_lock(&g_spdk_iscsi.mutex); 830 for (i = 0; i < num_maps; i++) { 831 rc = iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i], 832 ig_tag_list[i]); 833 if (rc != 0) { 834 SPDK_ERRLOG("could not delete map from target\n"); 835 goto invalid; 836 } 837 } 838 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 839 return 0; 840 841 invalid: 842 for (; i > 0; --i) { 843 rc = iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i - 1], 844 ig_tag_list[i - 1]); 845 if (rc != 0) { 846 iscsi_tgt_node_delete_all_pg_maps(target); 847 break; 848 } 849 } 850 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 851 return -1; 852 } 853 854 static int 855 check_iscsi_name(const char *name) 856 { 857 const unsigned char *up = (const unsigned char *) name; 858 size_t n; 859 860 /* valid iSCSI name no larger than 223 bytes */ 861 if (strlen(name) > MAX_TARGET_NAME) { 862 return -1; 863 } 864 865 /* valid iSCSI name? */ 866 for (n = 0; up[n] != 0; n++) { 867 if (up[n] > 0x00U && up[n] <= 0x2cU) { 868 return -1; 869 } 870 if (up[n] == 0x2fU) { 871 return -1; 872 } 873 if (up[n] >= 0x3bU && up[n] <= 0x40U) { 874 return -1; 875 } 876 if (up[n] >= 0x5bU && up[n] <= 0x60U) { 877 return -1; 878 } 879 if (up[n] >= 0x7bU && up[n] <= 0x7fU) { 880 return -1; 881 } 882 if (isspace(up[n])) { 883 return -1; 884 } 885 } 886 /* valid format? */ 887 if (strncasecmp(name, "iqn.", 4) == 0) { 888 /* iqn.YYYY-MM.reversed.domain.name */ 889 if (!isdigit(up[4]) || !isdigit(up[5]) || !isdigit(up[6]) 890 || !isdigit(up[7]) || up[8] != '-' || !isdigit(up[9]) 891 || !isdigit(up[10]) || up[11] != '.') { 892 SPDK_ERRLOG("invalid iqn format. " 893 "expect \"iqn.YYYY-MM.reversed.domain.name\"\n"); 894 return -1; 895 } 896 } else if (strncasecmp(name, "eui.", 4) == 0) { 897 /* EUI-64 -> 16bytes */ 898 /* XXX */ 899 } else if (strncasecmp(name, "naa.", 4) == 0) { 900 /* 64bit -> 16bytes, 128bit -> 32bytes */ 901 /* XXX */ 902 } 903 /* OK */ 904 return 0; 905 } 906 907 bool 908 spdk_iscsi_check_chap_params(bool disable, bool require, bool mutual, int group) 909 { 910 if (group < 0) { 911 SPDK_ERRLOG("Invalid auth group ID (%d)\n", group); 912 return false; 913 } 914 if ((!disable && !require && !mutual) || /* Auto */ 915 (disable && !require && !mutual) || /* None */ 916 (!disable && require && !mutual) || /* CHAP */ 917 (!disable && require && mutual)) { /* CHAP Mutual */ 918 return true; 919 } 920 SPDK_ERRLOG("Invalid combination of CHAP params (d=%d,r=%d,m=%d)\n", 921 disable, require, mutual); 922 return false; 923 } 924 925 _spdk_iscsi_tgt_node * 926 spdk_iscsi_tgt_node_construct(int target_index, 927 const char *name, const char *alias, 928 int *pg_tag_list, int *ig_tag_list, uint16_t num_maps, 929 const char *bdev_name_list[], int *lun_id_list, int num_luns, 930 int queue_depth, 931 bool disable_chap, bool require_chap, bool mutual_chap, int chap_group, 932 bool header_digest, bool data_digest) 933 { 934 char fullname[MAX_TMPBUF]; 935 struct spdk_iscsi_tgt_node *target; 936 int rc; 937 938 if (!spdk_iscsi_check_chap_params(disable_chap, require_chap, 939 mutual_chap, chap_group)) { 940 return NULL; 941 } 942 943 if (num_maps == 0) { 944 SPDK_ERRLOG("num_maps = 0\n"); 945 return NULL; 946 } 947 948 if (name == NULL) { 949 SPDK_ERRLOG("TargetName not found\n"); 950 return NULL; 951 } 952 953 if (strncasecmp(name, "iqn.", 4) != 0 954 && strncasecmp(name, "eui.", 4) != 0 955 && strncasecmp(name, "naa.", 4) != 0) { 956 snprintf(fullname, sizeof(fullname), "%s:%s", g_spdk_iscsi.nodebase, name); 957 } else { 958 snprintf(fullname, sizeof(fullname), "%s", name); 959 } 960 961 if (check_iscsi_name(fullname) != 0) { 962 SPDK_ERRLOG("TargetName %s contains an invalid character or format.\n", 963 name); 964 return NULL; 965 } 966 967 target = malloc(sizeof(*target)); 968 if (!target) { 969 SPDK_ERRLOG("could not allocate target\n"); 970 return NULL; 971 } 972 973 memset(target, 0, sizeof(*target)); 974 975 rc = pthread_mutex_init(&target->mutex, NULL); 976 if (rc != 0) { 977 SPDK_ERRLOG("tgt_node%d: mutex_init() failed\n", target->num); 978 iscsi_tgt_node_destruct(target, NULL, NULL); 979 return NULL; 980 } 981 982 target->num = target_index; 983 984 target->name = strdup(fullname); 985 if (!target->name) { 986 SPDK_ERRLOG("Could not allocate TargetName\n"); 987 iscsi_tgt_node_destruct(target, NULL, NULL); 988 return NULL; 989 } 990 991 if (alias == NULL) { 992 target->alias = NULL; 993 } else { 994 target->alias = strdup(alias); 995 if (!target->alias) { 996 SPDK_ERRLOG("Could not allocate TargetAlias\n"); 997 iscsi_tgt_node_destruct(target, NULL, NULL); 998 return NULL; 999 } 1000 } 1001 1002 target->dev = spdk_scsi_dev_construct(fullname, bdev_name_list, lun_id_list, num_luns, 1003 SPDK_SPC_PROTOCOL_IDENTIFIER_ISCSI, NULL, NULL); 1004 if (!target->dev) { 1005 SPDK_ERRLOG("Could not construct SCSI device\n"); 1006 iscsi_tgt_node_destruct(target, NULL, NULL); 1007 return NULL; 1008 } 1009 1010 TAILQ_INIT(&target->pg_map_head); 1011 rc = spdk_iscsi_tgt_node_add_pg_ig_maps(target, pg_tag_list, ig_tag_list, num_maps); 1012 if (rc != 0) { 1013 SPDK_ERRLOG("could not add map to target\n"); 1014 iscsi_tgt_node_destruct(target, NULL, NULL); 1015 return NULL; 1016 } 1017 1018 target->disable_chap = disable_chap; 1019 target->require_chap = require_chap; 1020 target->mutual_chap = mutual_chap; 1021 target->chap_group = chap_group; 1022 target->header_digest = header_digest; 1023 target->data_digest = data_digest; 1024 1025 if (queue_depth > 0 && ((uint32_t)queue_depth <= g_spdk_iscsi.MaxQueueDepth)) { 1026 target->queue_depth = queue_depth; 1027 } else { 1028 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "QueueDepth %d is invalid and %d is used instead.\n", 1029 queue_depth, g_spdk_iscsi.MaxQueueDepth); 1030 target->queue_depth = g_spdk_iscsi.MaxQueueDepth; 1031 } 1032 1033 rc = iscsi_tgt_node_register(target); 1034 if (rc != 0) { 1035 SPDK_ERRLOG("register target is failed\n"); 1036 iscsi_tgt_node_destruct(target, NULL, NULL); 1037 return NULL; 1038 } 1039 1040 return target; 1041 } 1042 1043 static int 1044 iscsi_parse_tgt_node(struct spdk_conf_section *sp) 1045 { 1046 char buf[MAX_TMPBUF]; 1047 struct spdk_iscsi_tgt_node *target; 1048 int pg_tag_list[MAX_TARGET_MAP], ig_tag_list[MAX_TARGET_MAP]; 1049 int num_target_maps; 1050 const char *alias, *pg_tag, *ig_tag; 1051 const char *ag_tag; 1052 const char *val, *name; 1053 int target_num, chap_group, pg_tag_i, ig_tag_i; 1054 bool header_digest, data_digest; 1055 bool disable_chap, require_chap, mutual_chap; 1056 int i; 1057 int lun_id_list[SPDK_SCSI_DEV_MAX_LUN]; 1058 const char *bdev_name_list[SPDK_SCSI_DEV_MAX_LUN]; 1059 int num_luns, queue_depth; 1060 1061 target_num = spdk_conf_section_get_num(sp); 1062 1063 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "add unit %d\n", target_num); 1064 1065 data_digest = false; 1066 header_digest = false; 1067 1068 name = spdk_conf_section_get_val(sp, "TargetName"); 1069 1070 if (name == NULL) { 1071 SPDK_ERRLOG("tgt_node%d: TargetName not found\n", target_num); 1072 return -1; 1073 } 1074 1075 alias = spdk_conf_section_get_val(sp, "TargetAlias"); 1076 1077 /* Setup initiator and portal group mapping */ 1078 val = spdk_conf_section_get_val(sp, "Mapping"); 1079 if (val == NULL) { 1080 /* no map */ 1081 SPDK_ERRLOG("tgt_node%d: no Mapping\n", target_num); 1082 return -1; 1083 } 1084 1085 for (i = 0; i < MAX_TARGET_MAP; i++) { 1086 val = spdk_conf_section_get_nmval(sp, "Mapping", i, 0); 1087 if (val == NULL) { 1088 break; 1089 } 1090 pg_tag = spdk_conf_section_get_nmval(sp, "Mapping", i, 0); 1091 ig_tag = spdk_conf_section_get_nmval(sp, "Mapping", i, 1); 1092 if (pg_tag == NULL || ig_tag == NULL) { 1093 SPDK_ERRLOG("tgt_node%d: mapping error\n", target_num); 1094 return -1; 1095 } 1096 if (strncasecmp(pg_tag, "PortalGroup", 1097 strlen("PortalGroup")) != 0 1098 || sscanf(pg_tag, "%*[^0-9]%d", &pg_tag_i) != 1) { 1099 SPDK_ERRLOG("tgt_node%d: mapping portal error\n", target_num); 1100 return -1; 1101 } 1102 if (strncasecmp(ig_tag, "InitiatorGroup", 1103 strlen("InitiatorGroup")) != 0 1104 || sscanf(ig_tag, "%*[^0-9]%d", &ig_tag_i) != 1) { 1105 SPDK_ERRLOG("tgt_node%d: mapping initiator error\n", target_num); 1106 return -1; 1107 } 1108 if (pg_tag_i < 1 || ig_tag_i < 1) { 1109 SPDK_ERRLOG("tgt_node%d: invalid group tag\n", target_num); 1110 return -1; 1111 } 1112 pg_tag_list[i] = pg_tag_i; 1113 ig_tag_list[i] = ig_tag_i; 1114 } 1115 1116 num_target_maps = i; 1117 1118 /* Setup AuthMethod */ 1119 val = spdk_conf_section_get_val(sp, "AuthMethod"); 1120 disable_chap = false; 1121 require_chap = false; 1122 mutual_chap = false; 1123 if (val != NULL) { 1124 for (i = 0; ; i++) { 1125 val = spdk_conf_section_get_nmval(sp, "AuthMethod", 0, i); 1126 if (val == NULL) { 1127 break; 1128 } 1129 if (strcasecmp(val, "CHAP") == 0) { 1130 require_chap = true; 1131 } else if (strcasecmp(val, "Mutual") == 0) { 1132 mutual_chap = true; 1133 } else if (strcasecmp(val, "Auto") == 0) { 1134 disable_chap = false; 1135 require_chap = false; 1136 mutual_chap = false; 1137 } else if (strcasecmp(val, "None") == 0) { 1138 disable_chap = true; 1139 require_chap = false; 1140 mutual_chap = false; 1141 } else { 1142 SPDK_ERRLOG("tgt_node%d: unknown auth\n", target_num); 1143 return -1; 1144 } 1145 } 1146 if (mutual_chap && !require_chap) { 1147 SPDK_ERRLOG("tgt_node%d: Mutual but not CHAP\n", target_num); 1148 return -1; 1149 } 1150 } 1151 if (disable_chap) { 1152 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod None\n"); 1153 } else if (!require_chap) { 1154 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod Auto\n"); 1155 } else { 1156 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod CHAP %s\n", 1157 mutual_chap ? "Mutual" : ""); 1158 } 1159 1160 val = spdk_conf_section_get_val(sp, "AuthGroup"); 1161 if (val == NULL) { 1162 chap_group = 0; 1163 } else { 1164 ag_tag = val; 1165 if (strcasecmp(ag_tag, "None") == 0) { 1166 chap_group = 0; 1167 } else { 1168 if (strncasecmp(ag_tag, "AuthGroup", 1169 strlen("AuthGroup")) != 0 1170 || sscanf(ag_tag, "%*[^0-9]%d", &chap_group) != 1) { 1171 SPDK_ERRLOG("tgt_node%d: auth group error\n", target_num); 1172 return -1; 1173 } 1174 if (chap_group == 0) { 1175 SPDK_ERRLOG("tgt_node%d: invalid auth group 0\n", target_num); 1176 return -1; 1177 } 1178 } 1179 } 1180 if (chap_group == 0) { 1181 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthGroup None\n"); 1182 } else { 1183 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthGroup AuthGroup%d\n", chap_group); 1184 } 1185 1186 val = spdk_conf_section_get_val(sp, "UseDigest"); 1187 if (val != NULL) { 1188 for (i = 0; ; i++) { 1189 val = spdk_conf_section_get_nmval(sp, "UseDigest", 0, i); 1190 if (val == NULL) { 1191 break; 1192 } 1193 if (strcasecmp(val, "Header") == 0) { 1194 header_digest = true; 1195 } else if (strcasecmp(val, "Data") == 0) { 1196 data_digest = true; 1197 } else if (strcasecmp(val, "Auto") == 0) { 1198 header_digest = false; 1199 data_digest = false; 1200 } else { 1201 SPDK_ERRLOG("tgt_node%d: unknown digest\n", target_num); 1202 return -1; 1203 } 1204 } 1205 } 1206 if (!header_digest && !data_digest) { 1207 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "UseDigest Auto\n"); 1208 } else { 1209 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "UseDigest %s %s\n", 1210 header_digest ? "Header" : "", 1211 data_digest ? "Data" : ""); 1212 } 1213 1214 val = spdk_conf_section_get_val(sp, "QueueDepth"); 1215 if (val == NULL) { 1216 queue_depth = g_spdk_iscsi.MaxQueueDepth; 1217 } else { 1218 queue_depth = (int) strtol(val, NULL, 10); 1219 } 1220 1221 num_luns = 0; 1222 1223 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 1224 snprintf(buf, sizeof(buf), "LUN%d", i); 1225 val = spdk_conf_section_get_val(sp, buf); 1226 if (val == NULL) { 1227 continue; 1228 } 1229 1230 bdev_name_list[num_luns] = val; 1231 lun_id_list[num_luns] = i; 1232 num_luns++; 1233 } 1234 1235 if (num_luns == 0) { 1236 SPDK_ERRLOG("tgt_node%d: No LUN specified for target %s.\n", target_num, name); 1237 return -1; 1238 } 1239 1240 target = spdk_iscsi_tgt_node_construct(target_num, name, alias, 1241 pg_tag_list, ig_tag_list, num_target_maps, 1242 bdev_name_list, lun_id_list, num_luns, queue_depth, 1243 disable_chap, require_chap, mutual_chap, chap_group, 1244 header_digest, data_digest); 1245 1246 if (target == NULL) { 1247 SPDK_ERRLOG("tgt_node%d: add_iscsi_target_node error\n", target_num); 1248 return -1; 1249 } 1250 1251 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 1252 struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i); 1253 1254 if (lun) { 1255 SPDK_INFOLOG(SPDK_LOG_ISCSI, "device %d: LUN%d %s\n", 1256 spdk_scsi_dev_get_id(target->dev), 1257 spdk_scsi_lun_get_id(lun), 1258 spdk_scsi_lun_get_bdev_name(lun)); 1259 } 1260 } 1261 1262 return 0; 1263 } 1264 1265 int spdk_iscsi_parse_tgt_nodes(void) 1266 { 1267 struct spdk_conf_section *sp; 1268 int rc; 1269 1270 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "spdk_iscsi_parse_tgt_nodes\n"); 1271 1272 sp = spdk_conf_first_section(NULL); 1273 while (sp != NULL) { 1274 if (spdk_conf_section_match_prefix(sp, "TargetNode")) { 1275 int tag = spdk_conf_section_get_num(sp); 1276 1277 if (tag > SPDK_TN_TAG_MAX) { 1278 SPDK_ERRLOG("tag %d is invalid\n", tag); 1279 return -1; 1280 } 1281 rc = iscsi_parse_tgt_node(sp); 1282 if (rc < 0) { 1283 SPDK_ERRLOG("spdk_iscsi_parse_tgt_node() failed\n"); 1284 return -1; 1285 } 1286 } 1287 sp = spdk_conf_next_section(sp); 1288 } 1289 return 0; 1290 } 1291 1292 void 1293 spdk_iscsi_shutdown_tgt_nodes(void) 1294 { 1295 struct spdk_iscsi_tgt_node *target; 1296 1297 pthread_mutex_lock(&g_spdk_iscsi.mutex); 1298 while (!TAILQ_EMPTY(&g_spdk_iscsi.target_head)) { 1299 target = TAILQ_FIRST(&g_spdk_iscsi.target_head); 1300 TAILQ_REMOVE(&g_spdk_iscsi.target_head, target, tailq); 1301 1302 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 1303 1304 iscsi_tgt_node_destruct(target, NULL, NULL); 1305 1306 pthread_mutex_lock(&g_spdk_iscsi.mutex); 1307 } 1308 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 1309 } 1310 1311 void 1312 spdk_iscsi_shutdown_tgt_node_by_name(const char *target_name, 1313 iscsi_tgt_node_destruct_cb cb_fn, void *cb_arg) 1314 { 1315 struct spdk_iscsi_tgt_node *target; 1316 1317 pthread_mutex_lock(&g_spdk_iscsi.mutex); 1318 target = spdk_iscsi_find_tgt_node(target_name); 1319 if (target != NULL) { 1320 iscsi_tgt_node_unregister(target); 1321 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 1322 1323 iscsi_tgt_node_destruct(target, cb_fn, cb_arg); 1324 1325 return; 1326 } 1327 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 1328 1329 if (cb_fn) { 1330 cb_fn(cb_arg, -ENOENT); 1331 } 1332 } 1333 1334 bool 1335 spdk_iscsi_tgt_node_is_destructed(struct spdk_iscsi_tgt_node *target) 1336 { 1337 return target->destructed; 1338 } 1339 1340 int 1341 spdk_iscsi_tgt_node_cleanup_luns(struct spdk_iscsi_conn *conn, 1342 struct spdk_iscsi_tgt_node *target) 1343 { 1344 int i; 1345 struct spdk_iscsi_task *task; 1346 1347 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 1348 struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i); 1349 1350 if (!lun) { 1351 continue; 1352 } 1353 1354 /* we create a fake management task per LUN to cleanup */ 1355 task = spdk_iscsi_task_get(conn, NULL, spdk_iscsi_task_mgmt_cpl); 1356 if (!task) { 1357 SPDK_ERRLOG("Unable to acquire task\n"); 1358 return -1; 1359 } 1360 1361 task->scsi.target_port = conn->target_port; 1362 task->scsi.initiator_port = conn->initiator_port; 1363 task->scsi.lun = lun; 1364 1365 spdk_iscsi_op_abort_task_set(task, SPDK_SCSI_TASK_FUNC_LUN_RESET); 1366 } 1367 1368 return 0; 1369 } 1370 1371 void spdk_iscsi_tgt_node_delete_map(struct spdk_iscsi_portal_grp *portal_group, 1372 struct spdk_iscsi_init_grp *initiator_group) 1373 { 1374 struct spdk_iscsi_tgt_node *target; 1375 1376 pthread_mutex_lock(&g_spdk_iscsi.mutex); 1377 TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) { 1378 if (portal_group) { 1379 iscsi_tgt_node_delete_pg_map(target, portal_group); 1380 } 1381 if (initiator_group) { 1382 iscsi_tgt_node_delete_ig_maps(target, initiator_group); 1383 } 1384 } 1385 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 1386 } 1387 1388 int 1389 spdk_iscsi_tgt_node_add_lun(struct spdk_iscsi_tgt_node *target, 1390 const char *bdev_name, int lun_id) 1391 { 1392 struct spdk_scsi_dev *dev; 1393 int rc; 1394 1395 if (target->num_active_conns > 0) { 1396 SPDK_ERRLOG("Target has active connections (count=%d)\n", 1397 target->num_active_conns); 1398 return -1; 1399 } 1400 1401 if (lun_id < -1 || lun_id >= SPDK_SCSI_DEV_MAX_LUN) { 1402 SPDK_ERRLOG("Specified LUN ID (%d) is invalid\n", lun_id); 1403 return -1; 1404 } 1405 1406 dev = target->dev; 1407 if (dev == NULL) { 1408 SPDK_ERRLOG("SCSI device is not found\n"); 1409 return -1; 1410 } 1411 1412 rc = spdk_scsi_dev_add_lun(dev, bdev_name, lun_id, NULL, NULL); 1413 if (rc != 0) { 1414 SPDK_ERRLOG("spdk_scsi_dev_add_lun failed\n"); 1415 return -1; 1416 } 1417 1418 return 0; 1419 } 1420 1421 int 1422 spdk_iscsi_tgt_node_set_chap_params(struct spdk_iscsi_tgt_node *target, 1423 bool disable_chap, bool require_chap, 1424 bool mutual_chap, int32_t chap_group) 1425 { 1426 if (!spdk_iscsi_check_chap_params(disable_chap, require_chap, 1427 mutual_chap, chap_group)) { 1428 return -EINVAL; 1429 } 1430 1431 pthread_mutex_lock(&target->mutex); 1432 target->disable_chap = disable_chap; 1433 target->require_chap = require_chap; 1434 target->mutual_chap = mutual_chap; 1435 target->chap_group = chap_group; 1436 pthread_mutex_unlock(&target->mutex); 1437 1438 return 0; 1439 } 1440 1441 static const char *target_nodes_section = \ 1442 "\n" 1443 "# Users should change the TargetNode section(s) below to match the\n" 1444 "# desired iSCSI target node configuration.\n" 1445 "# TargetName, Mapping, LUN0 are minimum required\n"; 1446 1447 #define TARGET_NODE_TMPL \ 1448 "[TargetNode%d]\n" \ 1449 " Comment \"Target%d\"\n" \ 1450 " TargetName %s\n" \ 1451 " TargetAlias \"%s\"\n" 1452 1453 #define TARGET_NODE_PGIG_MAPPING_TMPL \ 1454 " Mapping PortalGroup%d InitiatorGroup%d\n" 1455 1456 #define TARGET_NODE_AUTH_TMPL \ 1457 " AuthMethod %s\n" \ 1458 " AuthGroup %s\n" \ 1459 " UseDigest %s\n" 1460 1461 #define TARGET_NODE_QD_TMPL \ 1462 " QueueDepth %d\n\n" 1463 1464 #define TARGET_NODE_LUN_TMPL \ 1465 " LUN%d %s\n" 1466 1467 void 1468 spdk_iscsi_tgt_nodes_config_text(FILE *fp) 1469 { 1470 int l = 0; 1471 struct spdk_scsi_dev *dev = NULL; 1472 struct spdk_iscsi_tgt_node *target = NULL; 1473 struct spdk_iscsi_pg_map *pg_map; 1474 struct spdk_iscsi_ig_map *ig_map; 1475 1476 /* Create target nodes section */ 1477 fprintf(fp, "%s", target_nodes_section); 1478 1479 TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) { 1480 int idx; 1481 const char *authmethod = "None"; 1482 char authgroup[32] = "None"; 1483 const char *usedigest = "Auto"; 1484 1485 dev = target->dev; 1486 if (NULL == dev) { continue; } 1487 1488 idx = target->num; 1489 fprintf(fp, TARGET_NODE_TMPL, idx, idx, target->name, spdk_scsi_dev_get_name(dev)); 1490 1491 TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) { 1492 TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) { 1493 fprintf(fp, TARGET_NODE_PGIG_MAPPING_TMPL, 1494 pg_map->pg->tag, 1495 ig_map->ig->tag); 1496 } 1497 } 1498 1499 if (target->disable_chap) { 1500 authmethod = "None"; 1501 } else if (!target->require_chap) { 1502 authmethod = "Auto"; 1503 } else if (target->mutual_chap) { 1504 authmethod = "CHAP Mutual"; 1505 } else { 1506 authmethod = "CHAP"; 1507 } 1508 1509 if (target->chap_group > 0) { 1510 snprintf(authgroup, sizeof(authgroup), "AuthGroup%d", target->chap_group); 1511 } 1512 1513 if (target->header_digest) { 1514 usedigest = "Header"; 1515 } else if (target->data_digest) { 1516 usedigest = "Data"; 1517 } 1518 1519 fprintf(fp, TARGET_NODE_AUTH_TMPL, 1520 authmethod, authgroup, usedigest); 1521 1522 for (l = 0; l < SPDK_SCSI_DEV_MAX_LUN; l++) { 1523 struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(dev, l); 1524 1525 if (!lun) { 1526 continue; 1527 } 1528 1529 fprintf(fp, TARGET_NODE_LUN_TMPL, 1530 spdk_scsi_lun_get_id(lun), 1531 spdk_scsi_lun_get_bdev_name(lun)); 1532 } 1533 1534 fprintf(fp, TARGET_NODE_QD_TMPL, 1535 target->queue_depth); 1536 } 1537 } 1538 1539 static void 1540 iscsi_tgt_node_info_json(struct spdk_iscsi_tgt_node *target, 1541 struct spdk_json_write_ctx *w) 1542 { 1543 struct spdk_iscsi_pg_map *pg_map; 1544 struct spdk_iscsi_ig_map *ig_map; 1545 int i; 1546 1547 spdk_json_write_object_begin(w); 1548 1549 spdk_json_write_named_string(w, "name", target->name); 1550 1551 if (target->alias) { 1552 spdk_json_write_named_string(w, "alias_name", target->alias); 1553 } 1554 1555 spdk_json_write_named_array_begin(w, "pg_ig_maps"); 1556 TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) { 1557 TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) { 1558 spdk_json_write_object_begin(w); 1559 spdk_json_write_named_int32(w, "pg_tag", pg_map->pg->tag); 1560 spdk_json_write_named_int32(w, "ig_tag", ig_map->ig->tag); 1561 spdk_json_write_object_end(w); 1562 } 1563 } 1564 spdk_json_write_array_end(w); 1565 1566 spdk_json_write_named_array_begin(w, "luns"); 1567 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 1568 struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i); 1569 1570 if (lun) { 1571 spdk_json_write_object_begin(w); 1572 spdk_json_write_named_string(w, "bdev_name", spdk_scsi_lun_get_bdev_name(lun)); 1573 spdk_json_write_named_int32(w, "lun_id", spdk_scsi_lun_get_id(lun)); 1574 spdk_json_write_object_end(w); 1575 } 1576 } 1577 spdk_json_write_array_end(w); 1578 1579 spdk_json_write_named_int32(w, "queue_depth", target->queue_depth); 1580 1581 spdk_json_write_named_bool(w, "disable_chap", target->disable_chap); 1582 spdk_json_write_named_bool(w, "require_chap", target->require_chap); 1583 spdk_json_write_named_bool(w, "mutual_chap", target->mutual_chap); 1584 spdk_json_write_named_int32(w, "chap_group", target->chap_group); 1585 1586 spdk_json_write_named_bool(w, "header_digest", target->header_digest); 1587 spdk_json_write_named_bool(w, "data_digest", target->data_digest); 1588 1589 spdk_json_write_object_end(w); 1590 } 1591 1592 static void 1593 iscsi_tgt_node_config_json(struct spdk_iscsi_tgt_node *target, 1594 struct spdk_json_write_ctx *w) 1595 { 1596 spdk_json_write_object_begin(w); 1597 1598 spdk_json_write_named_string(w, "method", "construct_target_node"); 1599 1600 spdk_json_write_name(w, "params"); 1601 iscsi_tgt_node_info_json(target, w); 1602 1603 spdk_json_write_object_end(w); 1604 } 1605 1606 void 1607 spdk_iscsi_tgt_nodes_info_json(struct spdk_json_write_ctx *w) 1608 { 1609 struct spdk_iscsi_tgt_node *target; 1610 1611 TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) { 1612 iscsi_tgt_node_info_json(target, w); 1613 } 1614 } 1615 1616 void 1617 spdk_iscsi_tgt_nodes_config_json(struct spdk_json_write_ctx *w) 1618 { 1619 struct spdk_iscsi_tgt_node *target; 1620 1621 TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) { 1622 iscsi_tgt_node_config_json(target, w); 1623 } 1624 } 1625