1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * NVMe over Fabrics discovery service 36 */ 37 38 #include "spdk/stdinc.h" 39 40 #include "nvmf_internal.h" 41 #include "transport.h" 42 43 #include "spdk/event.h" 44 #include "spdk/string.h" 45 #include "spdk/trace.h" 46 #include "spdk/nvmf_spec.h" 47 48 #include "spdk/bdev_module.h" 49 #include "spdk_internal/log.h" 50 51 static void 52 nvmf_update_discovery_log(struct spdk_nvmf_tgt *tgt) 53 { 54 uint64_t numrec = 0; 55 struct spdk_nvmf_subsystem *subsystem; 56 struct spdk_nvmf_listener *listener; 57 struct spdk_nvmf_discovery_log_page_entry *entry; 58 struct spdk_nvmf_discovery_log_page *disc_log; 59 size_t cur_size; 60 uint32_t sid; 61 62 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Generating log page for genctr %" PRIu64 "\n", 63 tgt->discovery_genctr); 64 65 cur_size = sizeof(struct spdk_nvmf_discovery_log_page); 66 disc_log = calloc(1, cur_size); 67 if (disc_log == NULL) { 68 SPDK_ERRLOG("Discovery log page memory allocation error\n"); 69 return; 70 } 71 72 for (sid = 0; sid < tgt->max_subsystems; sid++) { 73 subsystem = tgt->subsystems[sid]; 74 if ((subsystem == NULL) || 75 (subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE) || 76 (subsystem->state == SPDK_NVMF_SUBSYSTEM_DEACTIVATING)) { 77 continue; 78 } 79 80 if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) { 81 continue; 82 } 83 84 for (listener = spdk_nvmf_subsystem_get_first_listener(subsystem); listener != NULL; 85 listener = spdk_nvmf_subsystem_get_next_listener(subsystem, listener)) { 86 size_t new_size = cur_size + sizeof(*entry); 87 void *new_log_page = realloc(disc_log, new_size); 88 89 if (new_log_page == NULL) { 90 SPDK_ERRLOG("Discovery log page memory allocation error\n"); 91 break; 92 } 93 94 disc_log = new_log_page; 95 cur_size = new_size; 96 97 entry = &disc_log->entries[numrec]; 98 memset(entry, 0, sizeof(*entry)); 99 entry->portid = numrec; 100 entry->cntlid = 0xffff; 101 entry->asqsz = listener->transport->opts.max_aq_depth; 102 entry->subtype = subsystem->subtype; 103 snprintf(entry->subnqn, sizeof(entry->subnqn), "%s", subsystem->subnqn); 104 105 spdk_nvmf_transport_listener_discover(listener->transport, &listener->trid, entry); 106 107 numrec++; 108 } 109 } 110 111 disc_log->numrec = numrec; 112 disc_log->genctr = tgt->discovery_genctr; 113 114 free(tgt->discovery_log_page); 115 116 tgt->discovery_log_page = disc_log; 117 tgt->discovery_log_page_size = cur_size; 118 } 119 120 void 121 spdk_nvmf_get_discovery_log_page(struct spdk_nvmf_tgt *tgt, struct iovec *iov, 122 uint32_t iovcnt, uint64_t offset, uint32_t length) 123 { 124 size_t copy_len = 0; 125 size_t zero_len = 0; 126 struct iovec *tmp; 127 128 if (tgt->discovery_log_page == NULL || 129 tgt->discovery_log_page->genctr != tgt->discovery_genctr) { 130 nvmf_update_discovery_log(tgt); 131 } 132 133 /* Copy the valid part of the discovery log page, if any */ 134 if (tgt->discovery_log_page) { 135 for (tmp = iov; tmp < iov + iovcnt; tmp++) { 136 copy_len = spdk_min(tmp->iov_len, length); 137 copy_len = spdk_min(tgt->discovery_log_page_size - offset, copy_len); 138 139 memcpy(tmp->iov_base, (char *)tgt->discovery_log_page + offset, copy_len); 140 141 offset += copy_len; 142 length -= copy_len; 143 zero_len = tmp->iov_len - copy_len; 144 if (tgt->discovery_log_page_size <= offset || length == 0) { 145 break; 146 } 147 } 148 /* Zero out the rest of the payload */ 149 if (zero_len) { 150 memset((char *)tmp->iov_base + copy_len, 0, zero_len); 151 } 152 153 for (++tmp; tmp < iov + iovcnt; tmp++) { 154 memset((char *)tmp->iov_base, 0, tmp->iov_len); 155 } 156 } 157 } 158