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