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/string.h" 44 #include "spdk/trace.h" 45 #include "spdk/nvmf_spec.h" 46 47 #include "spdk/log.h" 48 49 void 50 nvmf_update_discovery_log(struct spdk_nvmf_tgt *tgt, const char *hostnqn) 51 { 52 struct spdk_nvmf_subsystem *discovery_subsystem; 53 struct spdk_nvmf_ctrlr *ctrlr; 54 55 tgt->discovery_genctr++; 56 discovery_subsystem = spdk_nvmf_tgt_find_subsystem(tgt, SPDK_NVMF_DISCOVERY_NQN); 57 58 if (discovery_subsystem) { 59 /** There is a change in discovery log for hosts with given hostnqn */ 60 TAILQ_FOREACH(ctrlr, &discovery_subsystem->ctrlrs, link) { 61 if (hostnqn == NULL || strcmp(hostnqn, ctrlr->hostnqn) == 0) { 62 nvmf_ctrlr_async_event_discovery_log_change_notice(ctrlr); 63 } 64 } 65 } 66 } 67 68 static struct spdk_nvmf_discovery_log_page * 69 nvmf_generate_discovery_log(struct spdk_nvmf_tgt *tgt, const char *hostnqn, size_t *log_page_size) 70 { 71 uint64_t numrec = 0; 72 struct spdk_nvmf_subsystem *subsystem; 73 struct spdk_nvmf_subsystem_listener *listener; 74 struct spdk_nvmf_discovery_log_page_entry *entry; 75 struct spdk_nvmf_discovery_log_page *disc_log; 76 size_t cur_size; 77 uint32_t sid; 78 79 SPDK_DEBUGLOG(nvmf, "Generating log page for genctr %" PRIu64 "\n", 80 tgt->discovery_genctr); 81 82 cur_size = sizeof(struct spdk_nvmf_discovery_log_page); 83 disc_log = calloc(1, cur_size); 84 if (disc_log == NULL) { 85 SPDK_ERRLOG("Discovery log page memory allocation error\n"); 86 return NULL; 87 } 88 89 for (sid = 0; sid < tgt->max_subsystems; sid++) { 90 subsystem = tgt->subsystems[sid]; 91 if ((subsystem == NULL) || 92 (subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE) || 93 (subsystem->state == SPDK_NVMF_SUBSYSTEM_DEACTIVATING)) { 94 continue; 95 } 96 97 if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) { 98 continue; 99 } 100 101 if (!spdk_nvmf_subsystem_host_allowed(subsystem, hostnqn)) { 102 continue; 103 } 104 105 for (listener = spdk_nvmf_subsystem_get_first_listener(subsystem); listener != NULL; 106 listener = spdk_nvmf_subsystem_get_next_listener(subsystem, listener)) { 107 size_t new_size = cur_size + sizeof(*entry); 108 void *new_log_page = realloc(disc_log, new_size); 109 110 if (new_log_page == NULL) { 111 SPDK_ERRLOG("Discovery log page memory allocation error\n"); 112 break; 113 } 114 115 disc_log = new_log_page; 116 cur_size = new_size; 117 118 entry = &disc_log->entries[numrec]; 119 memset(entry, 0, sizeof(*entry)); 120 entry->portid = numrec; 121 entry->cntlid = 0xffff; 122 entry->asqsz = listener->transport->opts.max_aq_depth; 123 entry->subtype = subsystem->subtype; 124 snprintf(entry->subnqn, sizeof(entry->subnqn), "%s", subsystem->subnqn); 125 126 nvmf_transport_listener_discover(listener->transport, listener->trid, entry); 127 128 numrec++; 129 } 130 } 131 132 disc_log->numrec = numrec; 133 disc_log->genctr = tgt->discovery_genctr; 134 *log_page_size = cur_size; 135 136 return disc_log; 137 } 138 139 void 140 nvmf_get_discovery_log_page(struct spdk_nvmf_tgt *tgt, const char *hostnqn, struct iovec *iov, 141 uint32_t iovcnt, uint64_t offset, uint32_t length) 142 { 143 size_t copy_len = 0; 144 size_t zero_len = 0; 145 struct iovec *tmp; 146 size_t log_page_size = 0; 147 struct spdk_nvmf_discovery_log_page *discovery_log_page; 148 149 discovery_log_page = nvmf_generate_discovery_log(tgt, hostnqn, &log_page_size); 150 151 /* Copy the valid part of the discovery log page, if any */ 152 if (discovery_log_page) { 153 for (tmp = iov; tmp < iov + iovcnt; tmp++) { 154 copy_len = spdk_min(tmp->iov_len, length); 155 copy_len = spdk_min(log_page_size - offset, copy_len); 156 157 memcpy(tmp->iov_base, (char *)discovery_log_page + offset, copy_len); 158 159 offset += copy_len; 160 length -= copy_len; 161 zero_len = tmp->iov_len - copy_len; 162 if (log_page_size <= offset || length == 0) { 163 break; 164 } 165 } 166 /* Zero out the rest of the payload */ 167 if (zero_len) { 168 memset((char *)tmp->iov_base + copy_len, 0, zero_len); 169 } 170 171 for (++tmp; tmp < iov + iovcnt; tmp++) { 172 memset((char *)tmp->iov_base, 0, tmp->iov_len); 173 } 174 175 free(discovery_log_page); 176 } 177 } 178