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