xref: /spdk/test/unit/lib/nvmf/ctrlr_bdev.c/ctrlr_bdev_ut.c (revision 3ef479ab163d96d6fd7f28b256d2a93ab42afd8e)
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 #include "spdk/stdinc.h"
35 
36 #include "spdk_cunit.h"
37 
38 #include "ctrlr_bdev.c"
39 
40 
41 SPDK_LOG_REGISTER_COMPONENT("nvmf", SPDK_LOG_NVMF)
42 
43 struct spdk_nvmf_qpair *
44 spdk_nvmf_ctrlr_get_qpair(struct spdk_nvmf_ctrlr *ctrlr, uint16_t qid)
45 {
46 	return NULL;
47 }
48 
49 int
50 spdk_nvmf_request_complete(struct spdk_nvmf_request *req)
51 {
52 	return -1;
53 }
54 
55 const char *
56 spdk_bdev_get_name(const struct spdk_bdev *bdev)
57 {
58 	return "test";
59 }
60 
61 uint32_t
62 spdk_bdev_get_block_size(const struct spdk_bdev *bdev)
63 {
64 	abort();
65 	return 0;
66 }
67 
68 uint64_t
69 spdk_bdev_get_num_blocks(const struct spdk_bdev *bdev)
70 {
71 	abort();
72 	return 0;
73 }
74 
75 uint32_t
76 spdk_bdev_get_optimal_io_boundary(const struct spdk_bdev *bdev)
77 {
78 	abort();
79 	return 0;
80 }
81 
82 struct spdk_io_channel *
83 spdk_bdev_get_io_channel(struct spdk_bdev_desc *desc)
84 {
85 	return NULL;
86 }
87 
88 int
89 spdk_bdev_flush_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
90 		       uint64_t offset_blocks, uint64_t num_blocks,
91 		       spdk_bdev_io_completion_cb cb, void *cb_arg)
92 {
93 	return 0;
94 }
95 
96 int
97 spdk_bdev_unmap_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
98 		       uint64_t offset_blocks, uint64_t num_blocks,
99 		       spdk_bdev_io_completion_cb cb, void *cb_arg)
100 {
101 	return 0;
102 }
103 
104 void
105 spdk_trace_record(uint16_t tpoint_id, uint16_t poller_id, uint32_t size, uint64_t object_id,
106 		  uint64_t arg1)
107 {
108 }
109 
110 bool
111 spdk_bdev_io_type_supported(struct spdk_bdev *bdev, enum spdk_bdev_io_type io_type)
112 {
113 	return false;
114 }
115 
116 int
117 spdk_bdev_write_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, void *buf,
118 		       uint64_t offset_blocks, uint64_t num_blocks,
119 		       spdk_bdev_io_completion_cb cb, void *cb_arg)
120 {
121 	return 0;
122 }
123 
124 int
125 spdk_bdev_read_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, void *buf,
126 		      uint64_t offset_blocks, uint64_t num_blocks,
127 		      spdk_bdev_io_completion_cb cb, void *cb_arg)
128 {
129 	return 0;
130 }
131 
132 int
133 spdk_bdev_write_zeroes_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
134 			      uint64_t offset_blocks, uint64_t num_blocks,
135 			      spdk_bdev_io_completion_cb cb, void *cb_arg)
136 {
137 	return 0;
138 }
139 
140 int
141 spdk_bdev_nvme_io_passthru(struct spdk_bdev_desc *desc,
142 			   struct spdk_io_channel *ch,
143 			   const struct spdk_nvme_cmd *cmd,
144 			   void *buf, size_t nbytes,
145 			   spdk_bdev_io_completion_cb cb, void *cb_arg)
146 {
147 	return 0;
148 }
149 
150 int spdk_bdev_free_io(struct spdk_bdev_io *bdev_io)
151 {
152 	return -1;
153 }
154 
155 void spdk_bdev_close(struct spdk_bdev_desc *desc)
156 {
157 }
158 
159 const char *spdk_nvmf_subsystem_get_nqn(struct spdk_nvmf_subsystem *subsystem)
160 {
161 	return NULL;
162 }
163 
164 struct spdk_nvmf_ns *
165 spdk_nvmf_subsystem_get_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid)
166 {
167 	abort();
168 	return NULL;
169 }
170 
171 struct spdk_nvmf_ns *
172 spdk_nvmf_subsystem_get_first_ns(struct spdk_nvmf_subsystem *subsystem)
173 {
174 	abort();
175 	return NULL;
176 }
177 
178 struct spdk_nvmf_ns *
179 spdk_nvmf_subsystem_get_next_ns(struct spdk_nvmf_subsystem *subsystem, struct spdk_nvmf_ns *prev_ns)
180 {
181 	abort();
182 	return NULL;
183 }
184 
185 void spdk_bdev_io_get_nvme_status(const struct spdk_bdev_io *bdev_io, int *sct, int *sc)
186 {
187 }
188 
189 static void
190 test_get_rw_params(void)
191 {
192 	struct spdk_nvme_cmd cmd = {0};
193 	uint64_t lba;
194 	uint64_t count;
195 
196 	lba = 0;
197 	count = 0;
198 	to_le64(&cmd.cdw10, 0x1234567890ABCDEF);
199 	to_le32(&cmd.cdw12, 0x9875 | SPDK_NVME_IO_FLAGS_FORCE_UNIT_ACCESS);
200 	nvmf_bdev_ctrlr_get_rw_params(&cmd, &lba, &count);
201 	CU_ASSERT(lba == 0x1234567890ABCDEF);
202 	CU_ASSERT(count == 0x9875 + 1); /* NOTE: this field is 0's based, hence the +1 */
203 }
204 
205 static void
206 test_lba_in_range(void)
207 {
208 	/* Trivial cases (no overflow) */
209 	CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 0, 1) == true);
210 	CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 0, 1000) == true);
211 	CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 0, 1001) == false);
212 	CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 1, 999) == true);
213 	CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 1, 1000) == false);
214 	CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 999, 1) == true);
215 	CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 1000, 1) == false);
216 	CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 1001, 1) == false);
217 
218 	/* Overflow edge cases */
219 	CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(UINT64_MAX, 0, UINT64_MAX) == true);
220 	CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(UINT64_MAX, 1, UINT64_MAX) == false)
221 	CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(UINT64_MAX, UINT64_MAX - 1, 1) == true);
222 	CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(UINT64_MAX, UINT64_MAX, 1) == false);
223 }
224 
225 int main(int argc, char **argv)
226 {
227 	CU_pSuite	suite = NULL;
228 	unsigned int	num_failures;
229 
230 	if (CU_initialize_registry() != CUE_SUCCESS) {
231 		return CU_get_error();
232 	}
233 
234 	suite = CU_add_suite("nvmf", NULL, NULL);
235 	if (suite == NULL) {
236 		CU_cleanup_registry();
237 		return CU_get_error();
238 	}
239 
240 	if (
241 		CU_add_test(suite, "get_rw_params", test_get_rw_params) == NULL ||
242 		CU_add_test(suite, "lba_in_range", test_lba_in_range) == NULL
243 	) {
244 		CU_cleanup_registry();
245 		return CU_get_error();
246 	}
247 
248 	CU_basic_set_mode(CU_BRM_VERBOSE);
249 	CU_basic_run_tests();
250 	num_failures = CU_get_number_of_failures();
251 	CU_cleanup_registry();
252 	return num_failures;
253 }
254