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_internal/bdev.h" 48 #include "spdk_internal/log.h" 49 50 static void 51 nvmf_update_discovery_log(struct spdk_nvmf_tgt *tgt) 52 { 53 uint64_t numrec = 0; 54 struct spdk_nvmf_subsystem *subsystem; 55 struct spdk_nvmf_listener *listener; 56 struct spdk_nvmf_discovery_log_page_entry *entry; 57 struct spdk_nvmf_discovery_log_page *disc_log; 58 size_t cur_size; 59 60 SPDK_DEBUGLOG(SPDK_TRACE_NVMF, "Generating log page for genctr %" PRIu64 "\n", 61 tgt->discovery_genctr); 62 63 cur_size = sizeof(struct spdk_nvmf_discovery_log_page); 64 disc_log = calloc(1, cur_size); 65 if (disc_log == NULL) { 66 SPDK_ERRLOG("Discovery log page memory allocation error\n"); 67 return; 68 } 69 70 TAILQ_FOREACH(subsystem, &tgt->subsystems, entries) { 71 if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) { 72 continue; 73 } 74 75 for (listener = spdk_nvmf_subsystem_get_first_listener(subsystem); listener != NULL; 76 listener = spdk_nvmf_subsystem_get_next_listener(subsystem, listener)) { 77 size_t new_size = cur_size + sizeof(*entry); 78 void *new_log_page = realloc(disc_log, new_size); 79 80 if (new_log_page == NULL) { 81 SPDK_ERRLOG("Discovery log page memory allocation error\n"); 82 break; 83 } 84 85 disc_log = new_log_page; 86 cur_size = new_size; 87 88 entry = &disc_log->entries[numrec]; 89 memset(entry, 0, sizeof(*entry)); 90 entry->portid = numrec; 91 entry->cntlid = 0xffff; 92 entry->asqsz = tgt->opts.max_queue_depth; 93 entry->subtype = subsystem->subtype; 94 snprintf(entry->subnqn, sizeof(entry->subnqn), "%s", subsystem->subnqn); 95 96 spdk_nvmf_transport_listener_discover(listener->transport, &listener->trid, entry); 97 98 numrec++; 99 } 100 } 101 102 disc_log->numrec = numrec; 103 disc_log->genctr = tgt->discovery_genctr; 104 105 free(tgt->discovery_log_page); 106 107 tgt->discovery_log_page = disc_log; 108 tgt->discovery_log_page_size = cur_size; 109 } 110 111 void 112 spdk_nvmf_get_discovery_log_page(struct spdk_nvmf_tgt *tgt, void *buffer, 113 uint64_t offset, uint32_t length) 114 { 115 size_t copy_len = 0; 116 size_t zero_len = length; 117 118 if (tgt->discovery_log_page == NULL || 119 tgt->discovery_log_page->genctr != tgt->discovery_genctr) { 120 nvmf_update_discovery_log(tgt); 121 } 122 123 /* Copy the valid part of the discovery log page, if any */ 124 if (tgt->discovery_log_page && offset < tgt->discovery_log_page_size) { 125 copy_len = spdk_min(tgt->discovery_log_page_size - offset, length); 126 zero_len -= copy_len; 127 memcpy(buffer, (char *)tgt->discovery_log_page + offset, copy_len); 128 } 129 130 /* Zero out the rest of the buffer */ 131 if (zero_len) { 132 memset((char *)buffer + copy_len, 0, zero_len); 133 } 134 135 /* We should have copied or zeroed every byte of the output buffer. */ 136 assert(copy_len + zero_len == length); 137 } 138