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 spdk_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 spdk_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 spdk_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 (spdk_iscsi_ipv6_netmask_allow_addr(netmask, addr)) { 177 return true; 178 } 179 } else { 180 /* IPv4 */ 181 if (spdk_iscsi_ipv4_netmask_allow_addr(netmask, addr)) { 182 return true; 183 } 184 } 185 return false; 186 } 187 188 static bool 189 spdk_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 (spdk_iscsi_netmask_allow_addr(imask->mask, addr)) { 198 return true; 199 } 200 } 201 return false; 202 } 203 204 static int 205 spdk_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 spdk_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 = spdk_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 = spdk_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 (spdk_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 spdk_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 = spdk_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 = spdk_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 spdk_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 spdk_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 spdk_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 spdk_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 (spdk_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 _spdk_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 spdk_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 = spdk_iscsi_pg_map_find_ig_map(pg_map, ig); 494 if (ig_map == NULL) { 495 return -ENOENT; 496 } 497 498 _spdk_iscsi_pg_map_delete_ig_map(pg_map, ig_map); 499 return 0; 500 } 501 502 static void 503 spdk_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 _spdk_iscsi_pg_map_delete_ig_map(pg_map, ig_map); 509 } 510 } 511 512 static struct spdk_iscsi_pg_map * 513 spdk_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 spdk_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 (spdk_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 _spdk_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 spdk_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 = spdk_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 spdk_iscsi_pg_map_delete_all_ig_maps(pg_map); 598 _spdk_iscsi_tgt_node_delete_pg_map(target, pg_map); 599 return 0; 600 } 601 602 static void 603 spdk_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 spdk_iscsi_pg_map_delete_ig_map(pg_map, ig); 610 if (pg_map->num_ig_maps == 0) { 611 _spdk_iscsi_tgt_node_delete_pg_map(target, pg_map); 612 } 613 } 614 } 615 616 static void 617 spdk_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 spdk_iscsi_pg_map_delete_all_ig_maps(pg_map); 623 _spdk_iscsi_tgt_node_delete_pg_map(target, pg_map); 624 } 625 } 626 627 static void 628 spdk_iscsi_tgt_node_destruct(struct spdk_iscsi_tgt_node *target) 629 { 630 if (target == NULL) { 631 return; 632 } 633 634 free(target->name); 635 free(target->alias); 636 spdk_iscsi_tgt_node_delete_all_pg_maps(target); 637 spdk_scsi_dev_destruct(target->dev); 638 639 pthread_mutex_destroy(&target->mutex); 640 free(target); 641 } 642 643 static int 644 spdk_iscsi_tgt_node_delete_pg_ig_map(struct spdk_iscsi_tgt_node *target, 645 int pg_tag, int ig_tag) 646 { 647 struct spdk_iscsi_portal_grp *pg; 648 struct spdk_iscsi_init_grp *ig; 649 struct spdk_iscsi_pg_map *pg_map; 650 struct spdk_iscsi_ig_map *ig_map; 651 652 pg = spdk_iscsi_portal_grp_find_by_tag(pg_tag); 653 if (pg == NULL) { 654 SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag); 655 return -ENOENT; 656 } 657 ig = spdk_iscsi_init_grp_find_by_tag(ig_tag); 658 if (ig == NULL) { 659 SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag); 660 return -ENOENT; 661 } 662 663 pg_map = spdk_iscsi_tgt_node_find_pg_map(target, pg); 664 if (pg_map == NULL) { 665 SPDK_ERRLOG("%s: PortalGroup%d is not mapped\n", target->name, pg_tag); 666 return -ENOENT; 667 } 668 ig_map = spdk_iscsi_pg_map_find_ig_map(pg_map, ig); 669 if (ig_map == NULL) { 670 SPDK_ERRLOG("%s: InitiatorGroup%d is not mapped\n", target->name, pg_tag); 671 return -ENOENT; 672 } 673 674 _spdk_iscsi_pg_map_delete_ig_map(pg_map, ig_map); 675 if (pg_map->num_ig_maps == 0) { 676 _spdk_iscsi_tgt_node_delete_pg_map(target, pg_map); 677 } 678 679 return 0; 680 } 681 682 static int 683 spdk_iscsi_tgt_node_add_pg_ig_map(struct spdk_iscsi_tgt_node *target, 684 int pg_tag, int ig_tag) 685 { 686 struct spdk_iscsi_portal_grp *pg; 687 struct spdk_iscsi_pg_map *pg_map; 688 struct spdk_iscsi_init_grp *ig; 689 struct spdk_iscsi_ig_map *ig_map; 690 bool new_pg_map = false; 691 692 pg = spdk_iscsi_portal_grp_find_by_tag(pg_tag); 693 if (pg == NULL) { 694 SPDK_ERRLOG("%s: PortalGroup%d not found\n", target->name, pg_tag); 695 return -ENOENT; 696 } 697 ig = spdk_iscsi_init_grp_find_by_tag(ig_tag); 698 if (ig == NULL) { 699 SPDK_ERRLOG("%s: InitiatorGroup%d not found\n", target->name, ig_tag); 700 return -ENOENT; 701 } 702 703 /* get existing pg_map or create new pg_map and add it to target */ 704 pg_map = spdk_iscsi_tgt_node_find_pg_map(target, pg); 705 if (pg_map == NULL) { 706 pg_map = spdk_iscsi_tgt_node_add_pg_map(target, pg); 707 if (pg_map == NULL) { 708 goto failed; 709 } 710 new_pg_map = true; 711 } 712 713 /* create new ig_map and add it to pg_map */ 714 ig_map = spdk_iscsi_pg_map_add_ig_map(pg_map, ig); 715 if (ig_map == NULL) { 716 goto failed; 717 } 718 719 return 0; 720 721 failed: 722 if (new_pg_map) { 723 _spdk_iscsi_tgt_node_delete_pg_map(target, pg_map); 724 } 725 726 return -1; 727 } 728 729 int 730 spdk_iscsi_tgt_node_add_pg_ig_maps(struct spdk_iscsi_tgt_node *target, 731 int *pg_tag_list, int *ig_tag_list, uint16_t num_maps) 732 { 733 uint16_t i; 734 int rc; 735 736 pthread_mutex_lock(&g_spdk_iscsi.mutex); 737 for (i = 0; i < num_maps; i++) { 738 rc = spdk_iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i], 739 ig_tag_list[i]); 740 if (rc != 0) { 741 SPDK_ERRLOG("could not add map to target\n"); 742 goto invalid; 743 } 744 } 745 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 746 return 0; 747 748 invalid: 749 for (; i > 0; --i) { 750 spdk_iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i - 1], 751 ig_tag_list[i - 1]); 752 } 753 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 754 return -1; 755 } 756 757 int 758 spdk_iscsi_tgt_node_delete_pg_ig_maps(struct spdk_iscsi_tgt_node *target, 759 int *pg_tag_list, int *ig_tag_list, uint16_t num_maps) 760 { 761 uint16_t i; 762 int rc; 763 764 pthread_mutex_lock(&g_spdk_iscsi.mutex); 765 for (i = 0; i < num_maps; i++) { 766 rc = spdk_iscsi_tgt_node_delete_pg_ig_map(target, pg_tag_list[i], 767 ig_tag_list[i]); 768 if (rc != 0) { 769 SPDK_ERRLOG("could not delete map from target\n"); 770 goto invalid; 771 } 772 } 773 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 774 return 0; 775 776 invalid: 777 for (; i > 0; --i) { 778 rc = spdk_iscsi_tgt_node_add_pg_ig_map(target, pg_tag_list[i - 1], 779 ig_tag_list[i - 1]); 780 if (rc != 0) { 781 spdk_iscsi_tgt_node_delete_all_pg_maps(target); 782 break; 783 } 784 } 785 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 786 return -1; 787 } 788 789 static int 790 spdk_check_iscsi_name(const char *name) 791 { 792 const unsigned char *up = (const unsigned char *) name; 793 size_t n; 794 795 /* valid iSCSI name? */ 796 for (n = 0; up[n] != 0; n++) { 797 if (up[n] > 0x00U && up[n] <= 0x2cU) { 798 return -1; 799 } 800 if (up[n] == 0x2fU) { 801 return -1; 802 } 803 if (up[n] >= 0x3bU && up[n] <= 0x40U) { 804 return -1; 805 } 806 if (up[n] >= 0x5bU && up[n] <= 0x60U) { 807 return -1; 808 } 809 if (up[n] >= 0x7bU && up[n] <= 0x7fU) { 810 return -1; 811 } 812 if (isspace(up[n])) { 813 return -1; 814 } 815 } 816 /* valid format? */ 817 if (strncasecmp(name, "iqn.", 4) == 0) { 818 /* iqn.YYYY-MM.reversed.domain.name */ 819 if (!isdigit(up[4]) || !isdigit(up[5]) || !isdigit(up[6]) 820 || !isdigit(up[7]) || up[8] != '-' || !isdigit(up[9]) 821 || !isdigit(up[10]) || up[11] != '.') { 822 SPDK_ERRLOG("invalid iqn format. " 823 "expect \"iqn.YYYY-MM.reversed.domain.name\"\n"); 824 return -1; 825 } 826 } else if (strncasecmp(name, "eui.", 4) == 0) { 827 /* EUI-64 -> 16bytes */ 828 /* XXX */ 829 } else if (strncasecmp(name, "naa.", 4) == 0) { 830 /* 64bit -> 16bytes, 128bit -> 32bytes */ 831 /* XXX */ 832 } 833 /* OK */ 834 return 0; 835 } 836 837 bool 838 spdk_iscsi_check_chap_params(bool disable, bool require, bool mutual, int group) 839 { 840 if (group < 0) { 841 SPDK_ERRLOG("Invalid auth group ID (%d)\n", group); 842 return false; 843 } 844 if ((!disable && !require && !mutual) || /* Auto */ 845 (disable && !require && !mutual) || /* None */ 846 (!disable && require && !mutual) || /* CHAP */ 847 (!disable && require && mutual)) { /* CHAP Mutual */ 848 return true; 849 } 850 SPDK_ERRLOG("Invalid combination of CHAP params (d=%d,r=%d,m=%d)\n", 851 disable, require, mutual); 852 return false; 853 } 854 855 _spdk_iscsi_tgt_node * 856 spdk_iscsi_tgt_node_construct(int target_index, 857 const char *name, const char *alias, 858 int *pg_tag_list, int *ig_tag_list, uint16_t num_maps, 859 const char *bdev_name_list[], int *lun_id_list, int num_luns, 860 int queue_depth, 861 bool disable_chap, bool require_chap, bool mutual_chap, int chap_group, 862 bool header_digest, bool data_digest) 863 { 864 char fullname[MAX_TMPBUF]; 865 struct spdk_iscsi_tgt_node *target; 866 int rc; 867 868 if (!spdk_iscsi_check_chap_params(disable_chap, require_chap, 869 mutual_chap, chap_group)) { 870 return NULL; 871 } 872 873 if (num_maps == 0) { 874 SPDK_ERRLOG("num_maps = 0\n"); 875 return NULL; 876 } 877 878 if (name == NULL) { 879 SPDK_ERRLOG("TargetName not found\n"); 880 return NULL; 881 } 882 883 if (strncasecmp(name, "iqn.", 4) != 0 884 && strncasecmp(name, "eui.", 4) != 0 885 && strncasecmp(name, "naa.", 4) != 0) { 886 snprintf(fullname, sizeof(fullname), "%s:%s", g_spdk_iscsi.nodebase, name); 887 } else { 888 snprintf(fullname, sizeof(fullname), "%s", name); 889 } 890 891 if (spdk_check_iscsi_name(fullname) != 0) { 892 SPDK_ERRLOG("TargetName %s contains an invalid character or format.\n", 893 name); 894 return NULL; 895 } 896 897 target = malloc(sizeof(*target)); 898 if (!target) { 899 SPDK_ERRLOG("could not allocate target\n"); 900 return NULL; 901 } 902 903 memset(target, 0, sizeof(*target)); 904 905 rc = pthread_mutex_init(&target->mutex, NULL); 906 if (rc != 0) { 907 SPDK_ERRLOG("tgt_node%d: mutex_init() failed\n", target->num); 908 spdk_iscsi_tgt_node_destruct(target); 909 return NULL; 910 } 911 912 target->num = target_index; 913 914 target->name = strdup(fullname); 915 if (!target->name) { 916 SPDK_ERRLOG("Could not allocate TargetName\n"); 917 spdk_iscsi_tgt_node_destruct(target); 918 return NULL; 919 } 920 921 if (alias == NULL) { 922 target->alias = NULL; 923 } else { 924 target->alias = strdup(alias); 925 if (!target->alias) { 926 SPDK_ERRLOG("Could not allocate TargetAlias\n"); 927 spdk_iscsi_tgt_node_destruct(target); 928 return NULL; 929 } 930 } 931 932 target->dev = spdk_scsi_dev_construct(fullname, bdev_name_list, lun_id_list, num_luns, 933 SPDK_SPC_PROTOCOL_IDENTIFIER_ISCSI, NULL, NULL); 934 if (!target->dev) { 935 SPDK_ERRLOG("Could not construct SCSI device\n"); 936 spdk_iscsi_tgt_node_destruct(target); 937 return NULL; 938 } 939 940 TAILQ_INIT(&target->pg_map_head); 941 rc = spdk_iscsi_tgt_node_add_pg_ig_maps(target, pg_tag_list, ig_tag_list, num_maps); 942 if (rc != 0) { 943 SPDK_ERRLOG("could not add map to target\n"); 944 spdk_iscsi_tgt_node_destruct(target); 945 return NULL; 946 } 947 948 target->disable_chap = disable_chap; 949 target->require_chap = require_chap; 950 target->mutual_chap = mutual_chap; 951 target->chap_group = chap_group; 952 target->header_digest = header_digest; 953 target->data_digest = data_digest; 954 955 if (queue_depth > 0 && ((uint32_t)queue_depth <= g_spdk_iscsi.MaxQueueDepth)) { 956 target->queue_depth = queue_depth; 957 } else { 958 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "QueueDepth %d is invalid and %d is used instead.\n", 959 queue_depth, g_spdk_iscsi.MaxQueueDepth); 960 target->queue_depth = g_spdk_iscsi.MaxQueueDepth; 961 } 962 963 rc = spdk_iscsi_tgt_node_register(target); 964 if (rc != 0) { 965 SPDK_ERRLOG("register target is failed\n"); 966 spdk_iscsi_tgt_node_destruct(target); 967 return NULL; 968 } 969 970 return target; 971 } 972 973 static int 974 spdk_iscsi_parse_tgt_node(struct spdk_conf_section *sp) 975 { 976 char buf[MAX_TMPBUF]; 977 struct spdk_iscsi_tgt_node *target; 978 int pg_tag_list[MAX_TARGET_MAP], ig_tag_list[MAX_TARGET_MAP]; 979 int num_target_maps; 980 const char *alias, *pg_tag, *ig_tag; 981 const char *ag_tag; 982 const char *val, *name; 983 int target_num, chap_group, pg_tag_i, ig_tag_i; 984 bool header_digest, data_digest; 985 bool disable_chap, require_chap, mutual_chap; 986 int i; 987 int lun_id_list[SPDK_SCSI_DEV_MAX_LUN]; 988 const char *bdev_name_list[SPDK_SCSI_DEV_MAX_LUN]; 989 int num_luns, queue_depth; 990 991 target_num = spdk_conf_section_get_num(sp); 992 993 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "add unit %d\n", target_num); 994 995 data_digest = false; 996 header_digest = false; 997 998 name = spdk_conf_section_get_val(sp, "TargetName"); 999 1000 if (name == NULL) { 1001 SPDK_ERRLOG("tgt_node%d: TargetName not found\n", target_num); 1002 return -1; 1003 } 1004 1005 alias = spdk_conf_section_get_val(sp, "TargetAlias"); 1006 1007 /* Setup initiator and portal group mapping */ 1008 val = spdk_conf_section_get_val(sp, "Mapping"); 1009 if (val == NULL) { 1010 /* no map */ 1011 SPDK_ERRLOG("tgt_node%d: no Mapping\n", target_num); 1012 return -1; 1013 } 1014 1015 for (i = 0; i < MAX_TARGET_MAP; i++) { 1016 val = spdk_conf_section_get_nmval(sp, "Mapping", i, 0); 1017 if (val == NULL) { 1018 break; 1019 } 1020 pg_tag = spdk_conf_section_get_nmval(sp, "Mapping", i, 0); 1021 ig_tag = spdk_conf_section_get_nmval(sp, "Mapping", i, 1); 1022 if (pg_tag == NULL || ig_tag == NULL) { 1023 SPDK_ERRLOG("tgt_node%d: mapping error\n", target_num); 1024 return -1; 1025 } 1026 if (strncasecmp(pg_tag, "PortalGroup", 1027 strlen("PortalGroup")) != 0 1028 || sscanf(pg_tag, "%*[^0-9]%d", &pg_tag_i) != 1) { 1029 SPDK_ERRLOG("tgt_node%d: mapping portal error\n", target_num); 1030 return -1; 1031 } 1032 if (strncasecmp(ig_tag, "InitiatorGroup", 1033 strlen("InitiatorGroup")) != 0 1034 || sscanf(ig_tag, "%*[^0-9]%d", &ig_tag_i) != 1) { 1035 SPDK_ERRLOG("tgt_node%d: mapping initiator error\n", target_num); 1036 return -1; 1037 } 1038 if (pg_tag_i < 1 || ig_tag_i < 1) { 1039 SPDK_ERRLOG("tgt_node%d: invalid group tag\n", target_num); 1040 return -1; 1041 } 1042 pg_tag_list[i] = pg_tag_i; 1043 ig_tag_list[i] = ig_tag_i; 1044 } 1045 1046 num_target_maps = i; 1047 1048 /* Setup AuthMethod */ 1049 val = spdk_conf_section_get_val(sp, "AuthMethod"); 1050 disable_chap = false; 1051 require_chap = false; 1052 mutual_chap = false; 1053 if (val != NULL) { 1054 for (i = 0; ; i++) { 1055 val = spdk_conf_section_get_nmval(sp, "AuthMethod", 0, i); 1056 if (val == NULL) { 1057 break; 1058 } 1059 if (strcasecmp(val, "CHAP") == 0) { 1060 require_chap = true; 1061 } else if (strcasecmp(val, "Mutual") == 0) { 1062 mutual_chap = true; 1063 } else if (strcasecmp(val, "Auto") == 0) { 1064 disable_chap = false; 1065 require_chap = false; 1066 mutual_chap = false; 1067 } else if (strcasecmp(val, "None") == 0) { 1068 disable_chap = true; 1069 require_chap = false; 1070 mutual_chap = false; 1071 } else { 1072 SPDK_ERRLOG("tgt_node%d: unknown auth\n", target_num); 1073 return -1; 1074 } 1075 } 1076 if (mutual_chap && !require_chap) { 1077 SPDK_ERRLOG("tgt_node%d: Mutual but not CHAP\n", target_num); 1078 return -1; 1079 } 1080 } 1081 if (disable_chap) { 1082 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod None\n"); 1083 } else if (!require_chap) { 1084 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod Auto\n"); 1085 } else { 1086 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthMethod CHAP %s\n", 1087 mutual_chap ? "Mutual" : ""); 1088 } 1089 1090 val = spdk_conf_section_get_val(sp, "AuthGroup"); 1091 if (val == NULL) { 1092 chap_group = 0; 1093 } else { 1094 ag_tag = val; 1095 if (strcasecmp(ag_tag, "None") == 0) { 1096 chap_group = 0; 1097 } else { 1098 if (strncasecmp(ag_tag, "AuthGroup", 1099 strlen("AuthGroup")) != 0 1100 || sscanf(ag_tag, "%*[^0-9]%d", &chap_group) != 1) { 1101 SPDK_ERRLOG("tgt_node%d: auth group error\n", target_num); 1102 return -1; 1103 } 1104 if (chap_group == 0) { 1105 SPDK_ERRLOG("tgt_node%d: invalid auth group 0\n", target_num); 1106 return -1; 1107 } 1108 } 1109 } 1110 if (chap_group == 0) { 1111 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthGroup None\n"); 1112 } else { 1113 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "AuthGroup AuthGroup%d\n", chap_group); 1114 } 1115 1116 val = spdk_conf_section_get_val(sp, "UseDigest"); 1117 if (val != NULL) { 1118 for (i = 0; ; i++) { 1119 val = spdk_conf_section_get_nmval(sp, "UseDigest", 0, i); 1120 if (val == NULL) { 1121 break; 1122 } 1123 if (strcasecmp(val, "Header") == 0) { 1124 header_digest = true; 1125 } else if (strcasecmp(val, "Data") == 0) { 1126 data_digest = true; 1127 } else if (strcasecmp(val, "Auto") == 0) { 1128 header_digest = false; 1129 data_digest = false; 1130 } else { 1131 SPDK_ERRLOG("tgt_node%d: unknown digest\n", target_num); 1132 return -1; 1133 } 1134 } 1135 } 1136 if (!header_digest && !data_digest) { 1137 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "UseDigest Auto\n"); 1138 } else { 1139 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "UseDigest %s %s\n", 1140 header_digest ? "Header" : "", 1141 data_digest ? "Data" : ""); 1142 } 1143 1144 val = spdk_conf_section_get_val(sp, "QueueDepth"); 1145 if (val == NULL) { 1146 queue_depth = g_spdk_iscsi.MaxQueueDepth; 1147 } else { 1148 queue_depth = (int) strtol(val, NULL, 10); 1149 } 1150 1151 num_luns = 0; 1152 1153 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 1154 snprintf(buf, sizeof(buf), "LUN%d", i); 1155 val = spdk_conf_section_get_val(sp, buf); 1156 if (val == NULL) { 1157 continue; 1158 } 1159 1160 bdev_name_list[num_luns] = val; 1161 lun_id_list[num_luns] = i; 1162 num_luns++; 1163 } 1164 1165 if (num_luns == 0) { 1166 SPDK_ERRLOG("tgt_node%d: No LUN specified for target %s.\n", target_num, name); 1167 return -1; 1168 } 1169 1170 target = spdk_iscsi_tgt_node_construct(target_num, name, alias, 1171 pg_tag_list, ig_tag_list, num_target_maps, 1172 bdev_name_list, lun_id_list, num_luns, queue_depth, 1173 disable_chap, require_chap, mutual_chap, chap_group, 1174 header_digest, data_digest); 1175 1176 if (target == NULL) { 1177 SPDK_ERRLOG("tgt_node%d: add_iscsi_target_node error\n", target_num); 1178 return -1; 1179 } 1180 1181 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 1182 struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i); 1183 1184 if (lun) { 1185 SPDK_INFOLOG(SPDK_LOG_ISCSI, "device %d: LUN%d %s\n", 1186 spdk_scsi_dev_get_id(target->dev), 1187 spdk_scsi_lun_get_id(lun), 1188 spdk_scsi_lun_get_bdev_name(lun)); 1189 } 1190 } 1191 1192 return 0; 1193 } 1194 1195 int spdk_iscsi_parse_tgt_nodes(void) 1196 { 1197 struct spdk_conf_section *sp; 1198 int rc; 1199 1200 SPDK_DEBUGLOG(SPDK_LOG_ISCSI, "spdk_iscsi_parse_tgt_nodes\n"); 1201 1202 sp = spdk_conf_first_section(NULL); 1203 while (sp != NULL) { 1204 if (spdk_conf_section_match_prefix(sp, "TargetNode")) { 1205 int tag = spdk_conf_section_get_num(sp); 1206 1207 if (tag > SPDK_TN_TAG_MAX) { 1208 SPDK_ERRLOG("tag %d is invalid\n", tag); 1209 return -1; 1210 } 1211 rc = spdk_iscsi_parse_tgt_node(sp); 1212 if (rc < 0) { 1213 SPDK_ERRLOG("spdk_iscsi_parse_tgt_node() failed\n"); 1214 return -1; 1215 } 1216 } 1217 sp = spdk_conf_next_section(sp); 1218 } 1219 return 0; 1220 } 1221 1222 void 1223 spdk_iscsi_shutdown_tgt_nodes(void) 1224 { 1225 struct spdk_iscsi_tgt_node *target, *tmp; 1226 1227 pthread_mutex_lock(&g_spdk_iscsi.mutex); 1228 TAILQ_FOREACH_SAFE(target, &g_spdk_iscsi.target_head, tailq, tmp) { 1229 TAILQ_REMOVE(&g_spdk_iscsi.target_head, target, tailq); 1230 spdk_iscsi_tgt_node_destruct(target); 1231 } 1232 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 1233 } 1234 1235 int 1236 spdk_iscsi_shutdown_tgt_node_by_name(const char *target_name) 1237 { 1238 struct spdk_iscsi_tgt_node *target; 1239 1240 pthread_mutex_lock(&g_spdk_iscsi.mutex); 1241 target = spdk_iscsi_find_tgt_node(target_name); 1242 if (target != NULL) { 1243 spdk_iscsi_tgt_node_unregister(target); 1244 spdk_iscsi_tgt_node_destruct(target); 1245 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 1246 1247 return 0; 1248 } 1249 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 1250 1251 return -ENOENT; 1252 } 1253 1254 int 1255 spdk_iscsi_tgt_node_cleanup_luns(struct spdk_iscsi_conn *conn, 1256 struct spdk_iscsi_tgt_node *target) 1257 { 1258 int i; 1259 struct spdk_iscsi_task *task; 1260 1261 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 1262 struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i); 1263 1264 if (!lun) { 1265 continue; 1266 } 1267 1268 /* we create a fake management task per LUN to cleanup */ 1269 task = spdk_iscsi_task_get(conn, NULL, spdk_iscsi_task_mgmt_cpl); 1270 if (!task) { 1271 SPDK_ERRLOG("Unable to acquire task\n"); 1272 return -1; 1273 } 1274 1275 task->scsi.target_port = conn->target_port; 1276 task->scsi.initiator_port = conn->initiator_port; 1277 task->scsi.lun = lun; 1278 1279 spdk_scsi_dev_queue_mgmt_task(target->dev, &task->scsi, SPDK_SCSI_TASK_FUNC_LUN_RESET); 1280 } 1281 1282 return 0; 1283 } 1284 1285 void spdk_iscsi_tgt_node_delete_map(struct spdk_iscsi_portal_grp *portal_group, 1286 struct spdk_iscsi_init_grp *initiator_group) 1287 { 1288 struct spdk_iscsi_tgt_node *target; 1289 1290 pthread_mutex_lock(&g_spdk_iscsi.mutex); 1291 TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) { 1292 if (portal_group) { 1293 spdk_iscsi_tgt_node_delete_pg_map(target, portal_group); 1294 } 1295 if (initiator_group) { 1296 spdk_iscsi_tgt_node_delete_ig_maps(target, initiator_group); 1297 } 1298 } 1299 pthread_mutex_unlock(&g_spdk_iscsi.mutex); 1300 } 1301 1302 int 1303 spdk_iscsi_tgt_node_add_lun(struct spdk_iscsi_tgt_node *target, 1304 const char *bdev_name, int lun_id) 1305 { 1306 struct spdk_scsi_dev *dev; 1307 int rc; 1308 1309 if (target->num_active_conns > 0) { 1310 SPDK_ERRLOG("Target has active connections (count=%d)\n", 1311 target->num_active_conns); 1312 return -1; 1313 } 1314 1315 if (lun_id < -1 || lun_id >= SPDK_SCSI_DEV_MAX_LUN) { 1316 SPDK_ERRLOG("Specified LUN ID (%d) is invalid\n", lun_id); 1317 return -1; 1318 } 1319 1320 dev = target->dev; 1321 if (dev == NULL) { 1322 SPDK_ERRLOG("SCSI device is not found\n"); 1323 return -1; 1324 } 1325 1326 rc = spdk_scsi_dev_add_lun(dev, bdev_name, lun_id, NULL, NULL); 1327 if (rc != 0) { 1328 SPDK_ERRLOG("spdk_scsi_dev_add_lun failed\n"); 1329 return -1; 1330 } 1331 1332 return 0; 1333 } 1334 1335 static const char *target_nodes_section = \ 1336 "\n" 1337 "# Users should change the TargetNode section(s) below to match the\n" 1338 "# desired iSCSI target node configuration.\n" 1339 "# TargetName, Mapping, LUN0 are minimum required\n"; 1340 1341 #define TARGET_NODE_TMPL \ 1342 "[TargetNode%d]\n" \ 1343 " Comment \"Target%d\"\n" \ 1344 " TargetName %s\n" \ 1345 " TargetAlias \"%s\"\n" 1346 1347 #define TARGET_NODE_PGIG_MAPPING_TMPL \ 1348 " Mapping PortalGroup%d InitiatorGroup%d\n" 1349 1350 #define TARGET_NODE_AUTH_TMPL \ 1351 " AuthMethod %s\n" \ 1352 " AuthGroup %s\n" \ 1353 " UseDigest %s\n" 1354 1355 #define TARGET_NODE_QD_TMPL \ 1356 " QueueDepth %d\n\n" 1357 1358 #define TARGET_NODE_LUN_TMPL \ 1359 " LUN%d %s\n" 1360 1361 void 1362 spdk_iscsi_tgt_nodes_config_text(FILE *fp) 1363 { 1364 int l = 0; 1365 struct spdk_scsi_dev *dev = NULL; 1366 struct spdk_iscsi_tgt_node *target = NULL; 1367 struct spdk_iscsi_pg_map *pg_map; 1368 struct spdk_iscsi_ig_map *ig_map; 1369 1370 /* Create target nodes section */ 1371 fprintf(fp, "%s", target_nodes_section); 1372 1373 TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) { 1374 int idx; 1375 const char *authmethod = "None"; 1376 char authgroup[32] = "None"; 1377 const char *usedigest = "Auto"; 1378 1379 dev = target->dev; 1380 if (NULL == dev) { continue; } 1381 1382 idx = target->num; 1383 fprintf(fp, TARGET_NODE_TMPL, idx, idx, target->name, spdk_scsi_dev_get_name(dev)); 1384 1385 TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) { 1386 TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) { 1387 fprintf(fp, TARGET_NODE_PGIG_MAPPING_TMPL, 1388 pg_map->pg->tag, 1389 ig_map->ig->tag); 1390 } 1391 } 1392 1393 if (target->disable_chap) { 1394 authmethod = "None"; 1395 } else if (!target->require_chap) { 1396 authmethod = "Auto"; 1397 } else if (target->mutual_chap) { 1398 authmethod = "CHAP Mutual"; 1399 } else { 1400 authmethod = "CHAP"; 1401 } 1402 1403 if (target->chap_group > 0) { 1404 snprintf(authgroup, sizeof(authgroup), "AuthGroup%d", target->chap_group); 1405 } 1406 1407 if (target->header_digest) { 1408 usedigest = "Header"; 1409 } else if (target->data_digest) { 1410 usedigest = "Data"; 1411 } 1412 1413 fprintf(fp, TARGET_NODE_AUTH_TMPL, 1414 authmethod, authgroup, usedigest); 1415 1416 for (l = 0; l < SPDK_SCSI_DEV_MAX_LUN; l++) { 1417 struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(dev, l); 1418 1419 if (!lun) { 1420 continue; 1421 } 1422 1423 fprintf(fp, TARGET_NODE_LUN_TMPL, 1424 spdk_scsi_lun_get_id(lun), 1425 spdk_scsi_lun_get_bdev_name(lun)); 1426 } 1427 1428 fprintf(fp, TARGET_NODE_QD_TMPL, 1429 target->queue_depth); 1430 } 1431 } 1432 1433 static void 1434 spdk_iscsi_tgt_node_info_json(struct spdk_iscsi_tgt_node *target, 1435 struct spdk_json_write_ctx *w) 1436 { 1437 struct spdk_iscsi_pg_map *pg_map; 1438 struct spdk_iscsi_ig_map *ig_map; 1439 int i; 1440 1441 spdk_json_write_object_begin(w); 1442 1443 spdk_json_write_named_string(w, "name", target->name); 1444 1445 if (target->alias) { 1446 spdk_json_write_named_string(w, "alias_name", target->alias); 1447 } 1448 1449 spdk_json_write_named_array_begin(w, "pg_ig_maps"); 1450 TAILQ_FOREACH(pg_map, &target->pg_map_head, tailq) { 1451 TAILQ_FOREACH(ig_map, &pg_map->ig_map_head, tailq) { 1452 spdk_json_write_object_begin(w); 1453 spdk_json_write_named_int32(w, "pg_tag", pg_map->pg->tag); 1454 spdk_json_write_named_int32(w, "ig_tag", ig_map->ig->tag); 1455 spdk_json_write_object_end(w); 1456 } 1457 } 1458 spdk_json_write_array_end(w); 1459 1460 spdk_json_write_named_array_begin(w, "luns"); 1461 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 1462 struct spdk_scsi_lun *lun = spdk_scsi_dev_get_lun(target->dev, i); 1463 1464 if (lun) { 1465 spdk_json_write_object_begin(w); 1466 spdk_json_write_named_string(w, "bdev_name", spdk_scsi_lun_get_bdev_name(lun)); 1467 spdk_json_write_named_int32(w, "lun_id", spdk_scsi_lun_get_id(lun)); 1468 spdk_json_write_object_end(w); 1469 } 1470 } 1471 spdk_json_write_array_end(w); 1472 1473 spdk_json_write_named_int32(w, "queue_depth", target->queue_depth); 1474 1475 spdk_json_write_named_bool(w, "disable_chap", target->disable_chap); 1476 spdk_json_write_named_bool(w, "require_chap", target->require_chap); 1477 spdk_json_write_named_bool(w, "mutual_chap", target->mutual_chap); 1478 spdk_json_write_named_int32(w, "chap_group", target->chap_group); 1479 1480 spdk_json_write_named_bool(w, "header_digest", target->header_digest); 1481 spdk_json_write_named_bool(w, "data_digest", target->data_digest); 1482 1483 spdk_json_write_object_end(w); 1484 } 1485 1486 static void 1487 spdk_iscsi_tgt_node_config_json(struct spdk_iscsi_tgt_node *target, 1488 struct spdk_json_write_ctx *w) 1489 { 1490 spdk_json_write_object_begin(w); 1491 1492 spdk_json_write_named_string(w, "method", "construct_target_node"); 1493 1494 spdk_json_write_name(w, "params"); 1495 spdk_iscsi_tgt_node_info_json(target, w); 1496 1497 spdk_json_write_object_end(w); 1498 } 1499 1500 void 1501 spdk_iscsi_tgt_nodes_info_json(struct spdk_json_write_ctx *w) 1502 { 1503 struct spdk_iscsi_tgt_node *target; 1504 1505 TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) { 1506 spdk_iscsi_tgt_node_info_json(target, w); 1507 } 1508 } 1509 1510 void 1511 spdk_iscsi_tgt_nodes_config_json(struct spdk_json_write_ctx *w) 1512 { 1513 struct spdk_iscsi_tgt_node *target; 1514 1515 TAILQ_FOREACH(target, &g_spdk_iscsi.target_head, tailq) { 1516 spdk_iscsi_tgt_node_config_json(target, w); 1517 } 1518 } 1519