xref: /spdk/test/unit/lib/nvmf/vfio_user.c/vfio_user_ut.c (revision cc6920a4763d4b9a43aa40583c8397d8f14fa100)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Intel Corporation nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "spdk/stdinc.h"
34 #include "spdk_cunit.h"
35 #include "common/lib/test_env.c"
36 #include "nvmf/vfio_user.c"
37 #include "nvmf/transport.c"
38 
39 DEFINE_STUB(spdk_nvmf_ctrlr_get_regs, const struct spdk_nvmf_registers *,
40 	    (struct spdk_nvmf_ctrlr *ctrlr), NULL);
41 DEFINE_STUB(spdk_mem_register, int, (void *vaddr, size_t len), 0);
42 DEFINE_STUB(spdk_mem_unregister, int, (void *vaddr, size_t len), 0);
43 DEFINE_STUB_V(spdk_nvmf_request_exec, (struct spdk_nvmf_request *req));
44 DEFINE_STUB_V(spdk_nvmf_request_exec_fabrics, (struct spdk_nvmf_request *req));
45 DEFINE_STUB(spdk_nvmf_request_complete, int, (struct spdk_nvmf_request *req), 0);
46 DEFINE_STUB_V(spdk_nvmf_tgt_new_qpair, (struct spdk_nvmf_tgt *tgt, struct spdk_nvmf_qpair *qpair));
47 DEFINE_STUB(nvmf_ctrlr_abort_request, int, (struct spdk_nvmf_request *req), 0);
48 DEFINE_STUB(spdk_nvmf_qpair_disconnect, int, (struct spdk_nvmf_qpair *qpair,
49 		nvmf_qpair_disconnect_cb cb_fn, void *ctx), 0);
50 DEFINE_STUB(spdk_nvmf_subsystem_get_nqn, const char *,
51 	    (const struct spdk_nvmf_subsystem *subsystem), NULL);
52 DEFINE_STUB(spdk_bdev_get_block_size, uint32_t, (const struct spdk_bdev *bdev), 512);
53 DEFINE_STUB_V(nvmf_ctrlr_abort_aer, (struct spdk_nvmf_ctrlr *ctrlr));
54 DEFINE_STUB(nvmf_ctrlr_async_event_error_event, int, (struct spdk_nvmf_ctrlr *ctrlr,
55 		union spdk_nvme_async_event_completion event), 0);
56 
57 static void *
58 gpa_to_vva(void *prv, uint64_t addr, uint64_t len, int prot)
59 {
60 	return (void *)(uintptr_t)addr;
61 }
62 
63 static void
64 test_nvme_cmd_map_prps(void)
65 {
66 	struct spdk_nvme_cmd cmd = {};
67 	struct iovec iovs[33];
68 	uint64_t phy_addr, *prp;
69 	uint32_t len;
70 	void *buf, *prps;
71 	int i, ret;
72 	size_t mps = 4096;
73 
74 	buf = spdk_zmalloc(132 * 1024, 4096, &phy_addr, 0, 0);
75 	CU_ASSERT(buf != NULL);
76 	prps = spdk_zmalloc(4096, 4096, &phy_addr, 0, 0);
77 	CU_ASSERT(prps != NULL);
78 
79 	/* test case 1: 4KiB with PRP1 only */
80 	cmd.dptr.prp.prp1 = (uint64_t)(uintptr_t)buf;
81 	len = 4096;
82 	ret = nvme_cmd_map_prps(NULL, &cmd, iovs, 33, len, mps, gpa_to_vva);
83 	CU_ASSERT(ret == 1);
84 	CU_ASSERT(iovs[0].iov_base == (void *)(uintptr_t)cmd.dptr.prp.prp1);
85 	CU_ASSERT(iovs[0].iov_len == len);
86 
87 	/* test case 2: 4KiB with PRP1 and PRP2, 1KiB in first iov, and 3KiB in second iov */
88 	cmd.dptr.prp.prp1 = (uint64_t)(uintptr_t)buf + 1024 * 3;
89 	cmd.dptr.prp.prp2 = (uint64_t)(uintptr_t)buf + 4096;
90 	len = 4096;
91 	ret = nvme_cmd_map_prps(NULL, &cmd, iovs, 1, len, mps, gpa_to_vva);
92 	CU_ASSERT(ret == -ERANGE);
93 	ret = nvme_cmd_map_prps(NULL, &cmd, iovs, 33, len, mps, gpa_to_vva);
94 	CU_ASSERT(ret == 2);
95 	CU_ASSERT(iovs[0].iov_base == (void *)(uintptr_t)cmd.dptr.prp.prp1);
96 	CU_ASSERT(iovs[0].iov_len == 1024);
97 	CU_ASSERT(iovs[1].iov_base == (void *)(uintptr_t)cmd.dptr.prp.prp2);
98 	CU_ASSERT(iovs[1].iov_len == 1024 * 3);
99 
100 	/* test case 3: 128KiB with PRP list, 1KiB in first iov, 3KiB in last iov */
101 	cmd.dptr.prp.prp1 = (uint64_t)(uintptr_t)buf + 1024 * 3;
102 	cmd.dptr.prp.prp2 = (uint64_t)(uintptr_t)prps;
103 	len = 128 * 1024;
104 	prp = prps;
105 	for (i = 1; i < 33; i++) {
106 		*prp = (uint64_t)(uintptr_t)buf + i * 4096;
107 		prp++;
108 	}
109 	ret = nvme_cmd_map_prps(NULL, &cmd, iovs, 33, len, mps, gpa_to_vva);
110 	CU_ASSERT(ret == 33);
111 	CU_ASSERT(iovs[0].iov_base == (void *)(uintptr_t)cmd.dptr.prp.prp1);
112 	CU_ASSERT(iovs[0].iov_len == 1024);
113 	for (i = 1; i < 32; i++) {
114 		CU_ASSERT(iovs[i].iov_base == (void *)((uintptr_t)buf + i * 4096));
115 		CU_ASSERT(iovs[i].iov_len == 4096);
116 	}
117 	CU_ASSERT(iovs[32].iov_base == (void *)((uintptr_t)buf + 32 * 4096));
118 	CU_ASSERT(iovs[32].iov_len == 1024 * 3);
119 
120 	/* test case 4: 256KiB with PRP list, not enough iovs */
121 	cmd.dptr.prp.prp1 = (uint64_t)(uintptr_t)buf + 1024 * 3;
122 	cmd.dptr.prp.prp2 = (uint64_t)(uintptr_t)prps;
123 	len = 256 * 1024;
124 	ret = nvme_cmd_map_prps(NULL, &cmd, iovs, 33, len, mps, gpa_to_vva);
125 	CU_ASSERT(ret == -ERANGE);
126 
127 	spdk_free(buf);
128 	spdk_free(prps);
129 }
130 
131 static void
132 test_nvme_cmd_map_sgls(void)
133 {
134 	struct spdk_nvme_cmd cmd = {};
135 	struct iovec iovs[33];
136 	uint64_t phy_addr;
137 	uint32_t len;
138 	void *buf, *sgls;
139 	struct spdk_nvme_sgl_descriptor *sgl;
140 	int i, ret;
141 	size_t mps = 4096;
142 
143 	buf = spdk_zmalloc(132 * 1024, 4096, &phy_addr, 0, 0);
144 	CU_ASSERT(buf != NULL);
145 	sgls = spdk_zmalloc(4096, 4096, &phy_addr, 0, 0);
146 	CU_ASSERT(sgls != NULL);
147 
148 	/* test case 1: 8KiB with 1 data block */
149 	len = 8192;
150 	cmd.dptr.sgl1.unkeyed.type = SPDK_NVME_SGL_TYPE_DATA_BLOCK;
151 	cmd.dptr.sgl1.unkeyed.length = len;
152 	cmd.dptr.sgl1.address = (uint64_t)(uintptr_t)buf;
153 
154 	ret = nvme_cmd_map_sgls(NULL, &cmd, iovs, 33, len, mps, gpa_to_vva);
155 	CU_ASSERT(ret == 1);
156 	CU_ASSERT(iovs[0].iov_base == buf);
157 	CU_ASSERT(iovs[0].iov_len == 8192);
158 
159 	/* test case 2: 8KiB with 2 data blocks and 1 last segment */
160 	sgl = (struct spdk_nvme_sgl_descriptor *)sgls;
161 	sgl[0].unkeyed.type = SPDK_NVME_SGL_TYPE_DATA_BLOCK;
162 	sgl[0].unkeyed.length = 2048;
163 	sgl[0].address = (uint64_t)(uintptr_t)buf;
164 	sgl[1].unkeyed.type = SPDK_NVME_SGL_TYPE_DATA_BLOCK;
165 	sgl[1].unkeyed.length = len - 2048;
166 	sgl[1].address = (uint64_t)(uintptr_t)buf + 16 * 1024;
167 
168 	cmd.dptr.sgl1.unkeyed.type = SPDK_NVME_SGL_TYPE_LAST_SEGMENT;
169 	cmd.dptr.sgl1.unkeyed.length = 2 * sizeof(*sgl);
170 	cmd.dptr.sgl1.address = (uint64_t)(uintptr_t)sgls;
171 
172 	ret = nvme_cmd_map_sgls(NULL, &cmd, iovs, 33, len, mps, gpa_to_vva);
173 	CU_ASSERT(ret == 2);
174 	CU_ASSERT(iovs[0].iov_base == (void *)(uintptr_t)buf);
175 	CU_ASSERT(iovs[0].iov_len == 2048);
176 	CU_ASSERT(iovs[1].iov_base == (void *)((uintptr_t)buf + 16 * 1024));
177 	CU_ASSERT(iovs[1].iov_len == len - 2048);
178 
179 	/* test case 3: 8KiB with 1 segment, 1 last segment and 3 data blocks */
180 	sgl[0].unkeyed.type = SPDK_NVME_SGL_TYPE_DATA_BLOCK;
181 	sgl[0].unkeyed.length = 2048;
182 	sgl[0].address = (uint64_t)(uintptr_t)buf;
183 	sgl[1].unkeyed.type = SPDK_NVME_SGL_TYPE_LAST_SEGMENT;
184 	sgl[1].unkeyed.length = 2 * sizeof(*sgl);
185 	sgl[1].address = (uint64_t)(uintptr_t)&sgl[9];
186 
187 	sgl[9].unkeyed.type = SPDK_NVME_SGL_TYPE_DATA_BLOCK;
188 	sgl[9].unkeyed.length = 4096;
189 	sgl[9].address = (uint64_t)(uintptr_t)buf + 4 * 1024;
190 	sgl[10].unkeyed.type = SPDK_NVME_SGL_TYPE_DATA_BLOCK;
191 	sgl[10].unkeyed.length = 2048;
192 	sgl[10].address = (uint64_t)(uintptr_t)buf + 16 * 1024;
193 
194 	cmd.dptr.sgl1.unkeyed.type = SPDK_NVME_SGL_TYPE_SEGMENT;
195 	cmd.dptr.sgl1.unkeyed.length = 2 * sizeof(*sgl);
196 	cmd.dptr.sgl1.address = (uint64_t)(uintptr_t)&sgl[0];
197 
198 	ret = nvme_cmd_map_sgls(NULL, &cmd, iovs, 33, len, mps, gpa_to_vva);
199 	CU_ASSERT(ret == 3);
200 	CU_ASSERT(iovs[0].iov_base == (void *)(uintptr_t)buf);
201 	CU_ASSERT(iovs[0].iov_len == 2048);
202 	CU_ASSERT(iovs[1].iov_base == (void *)((uintptr_t)buf + 4 * 1024));
203 	CU_ASSERT(iovs[1].iov_len == 4096);
204 	CU_ASSERT(iovs[2].iov_base == (void *)((uintptr_t)buf + 16 * 1024));
205 	CU_ASSERT(iovs[2].iov_len == 2048);
206 
207 	/* test case 4: not enough iovs */
208 	len = 12 * 1024;
209 	for (i = 0; i < 6; i++) {
210 		sgl[0].unkeyed.type = SPDK_NVME_SGL_TYPE_DATA_BLOCK;
211 		sgl[0].unkeyed.length = 2048;
212 		sgl[0].address = (uint64_t)(uintptr_t)buf + i * 4096;
213 	}
214 
215 	cmd.dptr.sgl1.unkeyed.type = SPDK_NVME_SGL_TYPE_LAST_SEGMENT;
216 	cmd.dptr.sgl1.unkeyed.length = 6 * sizeof(*sgl);
217 	cmd.dptr.sgl1.address = (uint64_t)(uintptr_t)sgls;
218 
219 	ret = nvme_cmd_map_sgls(NULL, &cmd, iovs, 4, len, mps, gpa_to_vva);
220 	CU_ASSERT(ret == -ERANGE);
221 
222 	spdk_free(buf);
223 	spdk_free(sgls);
224 }
225 
226 static void
227 ut_transport_destroy_done_cb(void *cb_arg)
228 {
229 	int *done = cb_arg;
230 	*done = 1;
231 }
232 
233 static void
234 test_nvmf_vfio_user_create_destroy(void)
235 {
236 	struct spdk_nvmf_transport *transport = NULL;
237 	struct nvmf_vfio_user_transport *vu_transport = NULL;
238 	struct nvmf_vfio_user_endpoint *endpoint = NULL;
239 	struct spdk_nvmf_transport_opts opts = {};
240 	int rc;
241 	int done;
242 
243 	/* Initialize transport_specific NULL to avoid decoding json */
244 	opts.transport_specific = NULL;
245 
246 	transport = nvmf_vfio_user_create(&opts);
247 	CU_ASSERT(transport != NULL);
248 
249 	vu_transport = SPDK_CONTAINEROF(transport, struct nvmf_vfio_user_transport,
250 					transport);
251 	/* Allocate a endpoint for destroy */
252 	endpoint = calloc(1, sizeof(*endpoint));
253 	pthread_mutex_init(&endpoint->lock, NULL);
254 	TAILQ_INSERT_TAIL(&vu_transport->endpoints, endpoint, link);
255 	done = 0;
256 
257 	rc = nvmf_vfio_user_destroy(transport, ut_transport_destroy_done_cb, &done);
258 	CU_ASSERT(rc == 0);
259 	CU_ASSERT(done == 1);
260 }
261 
262 int main(int argc, char **argv)
263 {
264 	CU_pSuite	suite = NULL;
265 	unsigned int	num_failures;
266 
267 	CU_set_error_action(CUEA_ABORT);
268 	CU_initialize_registry();
269 
270 	suite = CU_add_suite("vfio_user", NULL, NULL);
271 
272 	CU_ADD_TEST(suite, test_nvme_cmd_map_prps);
273 	CU_ADD_TEST(suite, test_nvme_cmd_map_sgls);
274 	CU_ADD_TEST(suite, test_nvmf_vfio_user_create_destroy);
275 
276 	CU_basic_set_mode(CU_BRM_VERBOSE);
277 	CU_basic_run_tests();
278 	num_failures = CU_get_number_of_failures();
279 	CU_cleanup_registry();
280 	return num_failures;
281 }
282