xref: /spdk/lib/nvmf/ctrlr_discovery.c (revision a83f91c29a4740e4bea5f9509b7036e9e7dc2788)
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->opts.max_subsystems; sid++) {
73 		subsystem = tgt->subsystems[sid];
74 		if (subsystem == NULL) {
75 			continue;
76 		}
77 
78 		if (subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY) {
79 			continue;
80 		}
81 
82 		for (listener = spdk_nvmf_subsystem_get_first_listener(subsystem); listener != NULL;
83 		     listener = spdk_nvmf_subsystem_get_next_listener(subsystem, listener)) {
84 			size_t new_size = cur_size + sizeof(*entry);
85 			void *new_log_page = realloc(disc_log, new_size);
86 
87 			if (new_log_page == NULL) {
88 				SPDK_ERRLOG("Discovery log page memory allocation error\n");
89 				break;
90 			}
91 
92 			disc_log = new_log_page;
93 			cur_size = new_size;
94 
95 			entry = &disc_log->entries[numrec];
96 			memset(entry, 0, sizeof(*entry));
97 			entry->portid = numrec;
98 			entry->cntlid = 0xffff;
99 			entry->asqsz = tgt->opts.max_queue_depth;
100 			entry->subtype = subsystem->subtype;
101 			snprintf(entry->subnqn, sizeof(entry->subnqn), "%s", subsystem->subnqn);
102 
103 			spdk_nvmf_transport_listener_discover(listener->transport, &listener->trid, entry);
104 
105 			numrec++;
106 		}
107 	}
108 
109 	disc_log->numrec = numrec;
110 	disc_log->genctr = tgt->discovery_genctr;
111 
112 	free(tgt->discovery_log_page);
113 
114 	tgt->discovery_log_page = disc_log;
115 	tgt->discovery_log_page_size = cur_size;
116 }
117 
118 void
119 spdk_nvmf_get_discovery_log_page(struct spdk_nvmf_tgt *tgt, void *buffer,
120 				 uint64_t offset, uint32_t length)
121 {
122 	size_t copy_len = 0;
123 	size_t zero_len = length;
124 
125 	if (tgt->discovery_log_page == NULL ||
126 	    tgt->discovery_log_page->genctr != tgt->discovery_genctr) {
127 		nvmf_update_discovery_log(tgt);
128 	}
129 
130 	/* Copy the valid part of the discovery log page, if any */
131 	if (tgt->discovery_log_page && offset < tgt->discovery_log_page_size) {
132 		copy_len = spdk_min(tgt->discovery_log_page_size - offset, length);
133 		zero_len -= copy_len;
134 		memcpy(buffer, (char *)tgt->discovery_log_page + offset, copy_len);
135 	}
136 
137 	/* Zero out the rest of the buffer */
138 	if (zero_len) {
139 		memset((char *)buffer + copy_len, 0, zero_len);
140 	}
141 
142 	/* We should have copied or zeroed every byte of the output buffer. */
143 	assert(copy_len + zero_len == length);
144 }
145