xref: /spdk/test/unit/lib/nvmf/ctrlr_bdev.c/ctrlr_bdev_ut.c (revision 22898a91b9b6f289933db19b0175821cfb7e7820)
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 "nvmf/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 const char *spdk_nvmf_subsystem_get_nqn(struct spdk_nvmf_subsystem *subsystem)
156 {
157 	return NULL;
158 }
159 
160 struct spdk_nvmf_ns *
161 spdk_nvmf_subsystem_get_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid)
162 {
163 	abort();
164 	return NULL;
165 }
166 
167 struct spdk_nvmf_ns *
168 spdk_nvmf_subsystem_get_first_ns(struct spdk_nvmf_subsystem *subsystem)
169 {
170 	abort();
171 	return NULL;
172 }
173 
174 struct spdk_nvmf_ns *
175 spdk_nvmf_subsystem_get_next_ns(struct spdk_nvmf_subsystem *subsystem, struct spdk_nvmf_ns *prev_ns)
176 {
177 	abort();
178 	return NULL;
179 }
180 
181 void spdk_bdev_io_get_nvme_status(const struct spdk_bdev_io *bdev_io, int *sct, int *sc)
182 {
183 }
184 
185 static void
186 test_get_rw_params(void)
187 {
188 	struct spdk_nvme_cmd cmd = {0};
189 	uint64_t lba;
190 	uint64_t count;
191 
192 	lba = 0;
193 	count = 0;
194 	to_le64(&cmd.cdw10, 0x1234567890ABCDEF);
195 	to_le32(&cmd.cdw12, 0x9875 | SPDK_NVME_IO_FLAGS_FORCE_UNIT_ACCESS);
196 	nvmf_bdev_ctrlr_get_rw_params(&cmd, &lba, &count);
197 	CU_ASSERT(lba == 0x1234567890ABCDEF);
198 	CU_ASSERT(count == 0x9875 + 1); /* NOTE: this field is 0's based, hence the +1 */
199 }
200 
201 static void
202 test_lba_in_range(void)
203 {
204 	/* Trivial cases (no overflow) */
205 	CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 0, 1) == true);
206 	CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 0, 1000) == true);
207 	CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 0, 1001) == false);
208 	CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 1, 999) == true);
209 	CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 1, 1000) == false);
210 	CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 999, 1) == true);
211 	CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 1000, 1) == false);
212 	CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(1000, 1001, 1) == false);
213 
214 	/* Overflow edge cases */
215 	CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(UINT64_MAX, 0, UINT64_MAX) == true);
216 	CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(UINT64_MAX, 1, UINT64_MAX) == false)
217 	CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(UINT64_MAX, UINT64_MAX - 1, 1) == true);
218 	CU_ASSERT(nvmf_bdev_ctrlr_lba_in_range(UINT64_MAX, UINT64_MAX, 1) == false);
219 }
220 
221 int main(int argc, char **argv)
222 {
223 	CU_pSuite	suite = NULL;
224 	unsigned int	num_failures;
225 
226 	if (CU_initialize_registry() != CUE_SUCCESS) {
227 		return CU_get_error();
228 	}
229 
230 	suite = CU_add_suite("nvmf", NULL, NULL);
231 	if (suite == NULL) {
232 		CU_cleanup_registry();
233 		return CU_get_error();
234 	}
235 
236 	if (
237 		CU_add_test(suite, "get_rw_params", test_get_rw_params) == NULL ||
238 		CU_add_test(suite, "lba_in_range", test_lba_in_range) == NULL
239 	) {
240 		CU_cleanup_registry();
241 		return CU_get_error();
242 	}
243 
244 	CU_basic_set_mode(CU_BRM_VERBOSE);
245 	CU_basic_run_tests();
246 	num_failures = CU_get_number_of_failures();
247 	CU_cleanup_registry();
248 	return num_failures;
249 }
250