xref: /spdk/test/unit/lib/ftl/ftl_io.c/ftl_io_ut.c (revision fecffda6ecf8853b82edccde429b68252f0a62c5)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2019 Intel Corporation.
3  *   All rights reserved.
4  */
5 
6 #include "spdk/stdinc.h"
7 
8 #include "spdk_cunit.h"
9 #include "common/lib/ut_multithread.c"
10 
11 #include "ftl/ftl_io.c"
12 #include "ftl/utils/ftl_conf.c"
13 
14 DEFINE_STUB(spdk_bdev_io_get_append_location, uint64_t, (struct spdk_bdev_io *bdev_io), 0);
15 DEFINE_STUB_V(spdk_bdev_close, (struct spdk_bdev_desc *desc));
16 DEFINE_STUB(spdk_bdev_desc_get_bdev, struct spdk_bdev *, (struct spdk_bdev_desc *desc), NULL);
17 DEFINE_STUB(spdk_bdev_get_optimal_open_zones, uint32_t, (const struct spdk_bdev *b), 1);
18 DEFINE_STUB(spdk_bdev_get_by_name, struct spdk_bdev *, (const char *bdev_name), NULL);
19 DEFINE_STUB(spdk_bdev_is_md_separate, bool, (const struct spdk_bdev *bdev), false);
20 DEFINE_STUB(spdk_bdev_is_zoned, bool, (const struct spdk_bdev *bdev), false);
21 DEFINE_STUB(spdk_bdev_zone_appendv, int, (struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
22 		struct iovec *iov, int iovcnt, uint64_t zone_id, uint64_t num_blocks,
23 		spdk_bdev_io_completion_cb cb, void *cb_arg), 0);
24 DEFINE_STUB(spdk_bdev_get_zone_size, uint64_t, (const struct spdk_bdev *b), 1024);
25 DEFINE_STUB_V(spdk_bdev_free_io, (struct spdk_bdev_io *bdev_io));
26 DEFINE_STUB(spdk_bdev_get_buf_align, size_t, (const struct spdk_bdev *bdev), 64);
27 DEFINE_STUB(spdk_bdev_get_dif_type, enum spdk_dif_type,
28 	    (const struct spdk_bdev *bdev), 0);
29 DEFINE_STUB(spdk_bdev_get_name, const char *, (const struct spdk_bdev *bdev), "test");
30 DEFINE_STUB(spdk_bdev_get_write_unit_size, uint32_t,
31 	    (const struct spdk_bdev *bdev), 0);
32 DEFINE_STUB(spdk_bdev_io_type_supported, bool, (struct spdk_bdev *bdev,
33 		enum spdk_bdev_io_type io_type), true);
34 DEFINE_STUB(spdk_bdev_open_ext, int,
35 	    (const char *bdev_name, bool write, spdk_bdev_event_cb_t event_cb,
36 	     void *event_ctx, struct spdk_bdev_desc **desc), 0);
37 DEFINE_STUB(spdk_bdev_read_blocks, int, (struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
38 		void *buf, uint64_t offset_blocks, uint64_t num_blocks,
39 		spdk_bdev_io_completion_cb cb, void *cb_arg), 0);
40 DEFINE_STUB(spdk_bdev_write_blocks, int, (struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
41 		void *buf, uint64_t offset_blocks, uint64_t num_blocks, spdk_bdev_io_completion_cb cb,
42 		void *cb_arg), 0);
43 DEFINE_STUB(spdk_bdev_write_blocks_with_md, int, (struct spdk_bdev_desc *desc,
44 		struct spdk_io_channel *ch, void *buf, void *md, uint64_t offset_blocks,
45 		uint64_t num_blocks, spdk_bdev_io_completion_cb cb, void *cb_arg), 0);
46 DEFINE_STUB(spdk_bdev_writev_blocks, int, (struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
47 		struct iovec *iov, int iovcnt, uint64_t offset_blocks, uint64_t num_blocks,
48 		spdk_bdev_io_completion_cb cb, void *cb_arg), 0);
49 DEFINE_STUB(spdk_bdev_get_num_blocks, uint64_t, (const struct spdk_bdev *bdev), 1024);
50 DEFINE_STUB(spdk_bdev_get_md_size, uint32_t, (const struct spdk_bdev *bdev), 0);
51 DEFINE_STUB(spdk_bdev_get_block_size, uint32_t, (const struct spdk_bdev *bdev), 4096);
52 DEFINE_STUB_V(spdk_bdev_module_release_bdev, (struct spdk_bdev *bdev));
53 DEFINE_STUB(spdk_bdev_write_zeroes_blocks, int,
54 	    (struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
55 	     uint64_t offset_blocks, uint64_t num_blocks,
56 	     spdk_bdev_io_completion_cb cb, void *cb_arg), 0);
57 DEFINE_STUB(spdk_mempool_create_ctor, struct spdk_mempool *,
58 	    (const char *name, size_t count, size_t ele_size, size_t cache_size,
59 	     int socket_id, spdk_mempool_obj_cb_t *obj_init, void *obj_init_arg), NULL);
60 DEFINE_STUB(spdk_mempool_obj_iter, uint32_t,
61 	    (struct spdk_mempool *mp, spdk_mempool_obj_cb_t obj_cb, void *obj_cb_arg), 0);
62 DEFINE_STUB_V(ftl_reloc, (struct ftl_reloc *reloc));
63 DEFINE_STUB_V(ftl_reloc_add, (struct ftl_reloc *reloc, struct ftl_band *band, size_t offset,
64 			      size_t num_blocks, int prio, bool defrag));
65 DEFINE_STUB_V(ftl_reloc_free, (struct ftl_reloc *reloc));
66 DEFINE_STUB_V(ftl_reloc_halt, (struct ftl_reloc *reloc));
67 DEFINE_STUB(ftl_reloc_init, struct ftl_reloc *, (struct spdk_ftl_dev *dev), NULL);
68 DEFINE_STUB(ftl_reloc_is_defrag_active, bool, (const struct ftl_reloc *reloc), false);
69 DEFINE_STUB(ftl_reloc_is_halted, bool, (const struct ftl_reloc *reloc), false);
70 DEFINE_STUB_V(ftl_reloc_resume, (struct ftl_reloc *reloc));
71 DEFINE_STUB_V(ftl_l2p_unpin, (struct spdk_ftl_dev *dev, uint64_t lba, uint64_t count));
72 DEFINE_STUB(ftl_p2l_ckpt_acquire, struct ftl_p2l_ckpt *, (struct spdk_ftl_dev *dev), NULL);
73 DEFINE_STUB_V(ftl_p2l_ckpt_release, (struct spdk_ftl_dev *dev, struct ftl_p2l_ckpt *ckpt));
74 DEFINE_STUB(ftl_l2p_get, ftl_addr, (struct spdk_ftl_dev *dev, uint64_t lba), 0);
75 DEFINE_STUB_V(ftl_mempool_put, (struct ftl_mempool *mpool, void *element));
76 
77 #if defined(DEBUG)
78 DEFINE_STUB_V(ftl_trace_defrag_band, (struct spdk_ftl_dev *dev, const struct ftl_band *band));
79 DEFINE_STUB_V(ftl_trace_submission, (struct spdk_ftl_dev *dev, const struct ftl_io *io,
80 				     ftl_addr addr, size_t addr_cnt));
81 DEFINE_STUB_V(ftl_trace_lba_io_init, (struct spdk_ftl_dev *dev, const struct ftl_io *io));
82 DEFINE_STUB_V(ftl_trace_limits, (struct spdk_ftl_dev *dev, int limit, size_t num_free));
83 DEFINE_STUB(ftl_trace_alloc_id, uint64_t, (struct spdk_ftl_dev *dev), 0);
84 DEFINE_STUB_V(ftl_trace_completion, (struct spdk_ftl_dev *dev, const struct ftl_io *io,
85 				     enum ftl_trace_completion type));
86 DEFINE_STUB_V(ftl_trace_wbuf_fill, (struct spdk_ftl_dev *dev, const struct ftl_io *io));
87 DEFINE_STUB_V(ftl_trace_wbuf_pop, (struct spdk_ftl_dev *dev, const struct ftl_wbuf_entry *entry));
88 DEFINE_STUB_V(ftl_trace_write_band, (struct spdk_ftl_dev *dev, const struct ftl_band *band));
89 #endif
90 
91 #if defined(FTL_DUMP_STATS)
92 DEFINE_STUB_V(ftl_dev_dump_stats, (const struct spdk_ftl_dev *dev));
93 #endif
94 
95 struct ftl_io_channel_ctx {
96 	struct ftl_io_channel *ioch;
97 };
98 
99 struct ftl_io_channel *
100 ftl_io_channel_get_ctx(struct spdk_io_channel *ioch)
101 {
102 	struct ftl_io_channel_ctx *ctx = spdk_io_channel_get_ctx(ioch);
103 
104 	return ctx->ioch;
105 }
106 
107 struct spdk_io_channel *
108 spdk_bdev_get_io_channel(struct spdk_bdev_desc *bdev_desc)
109 {
110 	return spdk_get_io_channel(bdev_desc);
111 }
112 
113 static int
114 channel_create_cb(void *io_device, void *ctx)
115 {
116 	return 0;
117 }
118 
119 static void
120 channel_destroy_cb(void *io_device, void *ctx)
121 {
122 }
123 
124 static struct spdk_ftl_dev *
125 setup_device(uint32_t num_threads, uint32_t xfer_size)
126 {
127 	struct spdk_ftl_dev *dev;
128 	struct ftl_io_channel *ioch;
129 	struct ftl_io_channel_ctx *ctx;
130 
131 	allocate_threads(num_threads);
132 	set_thread(0);
133 
134 	dev = calloc(1, sizeof(*dev));
135 	SPDK_CU_ASSERT_FATAL(dev != NULL);
136 
137 	dev->core_thread = spdk_get_thread();
138 	dev->ioch = calloc(1, SPDK_IO_CHANNEL_STRUCT_SIZE + sizeof(struct ftl_io_channel_ctx));
139 	SPDK_CU_ASSERT_FATAL(dev->ioch != NULL);
140 
141 	ctx = spdk_io_channel_get_ctx(dev->ioch);
142 	ctx->ioch = calloc(1, sizeof(*ctx->ioch));
143 
144 	ioch = ftl_io_channel_get_ctx(dev->ioch);
145 	SPDK_CU_ASSERT_FATAL(ioch != NULL);
146 
147 	ioch->cq = spdk_ring_create(0, 1024, 0);
148 
149 	dev->conf = g_default_conf;
150 	dev->xfer_size = xfer_size;
151 	dev->base_bdev_desc = (struct spdk_bdev_desc *)0xdeadbeef;
152 	dev->nv_cache.bdev_desc = (struct spdk_bdev_desc *)0xdead1234;
153 	spdk_io_device_register(dev, channel_create_cb, channel_destroy_cb, 0, NULL);
154 	spdk_io_device_register(dev->base_bdev_desc, channel_create_cb, channel_destroy_cb, 0, NULL);
155 	spdk_io_device_register(dev->nv_cache.bdev_desc, channel_create_cb, channel_destroy_cb, 0, NULL);
156 
157 	TAILQ_INIT(&dev->ioch_queue);
158 
159 	return dev;
160 }
161 
162 static void
163 free_device(struct spdk_ftl_dev *dev)
164 {
165 	struct ftl_io_channel *ioch;
166 
167 	ioch = ftl_io_channel_get_ctx(dev->ioch);
168 	spdk_ring_free(ioch->cq);
169 	free(ioch);
170 
171 	spdk_io_device_unregister(dev, NULL);
172 	spdk_io_device_unregister(dev->base_bdev_desc, NULL);
173 	spdk_io_device_unregister(dev->nv_cache.bdev_desc, NULL);
174 
175 	while (!TAILQ_EMPTY(&dev->ioch_queue)) {
176 		TAILQ_REMOVE(&dev->ioch_queue, TAILQ_FIRST(&dev->ioch_queue), entry);
177 	}
178 
179 	free_threads();
180 
181 	free(dev->ioch);
182 	free(dev->sb);
183 	free(dev);
184 }
185 
186 static void
187 setup_io(struct ftl_io *io, struct spdk_ftl_dev *dev, spdk_ftl_fn cb, void *ctx)
188 {
189 	io->dev = dev;
190 	io->user_fn = cb;
191 	io->cb_ctx = ctx;
192 	io->flags = 0;
193 	io->ioch = dev->ioch;
194 }
195 
196 static void
197 io_complete_cb(void *ctx, int status)
198 {
199 	*(int *)ctx = status;
200 }
201 
202 static void
203 test_completion(void)
204 {
205 	struct spdk_ftl_dev *dev;
206 	struct ftl_io_channel *ioch;
207 	struct ftl_io io = { 0 }, *io_ring;
208 	int req, status = 0;
209 
210 	dev = setup_device(1, FTL_NUM_LBA_IN_BLOCK);
211 	ioch = ftl_io_channel_get_ctx(dev->ioch);
212 
213 	/* Setup IO and 'send' NUM_REQUESTS subrequests */
214 	setup_io(&io, dev, io_complete_cb, &status);
215 	io.status = -EIO;
216 
217 #define NUM_REQUESTS 16
218 	for (req = 0; req < NUM_REQUESTS; ++req) {
219 		ftl_io_inc_req(&io);
220 		CU_ASSERT_FALSE(ftl_io_done(&io));
221 	}
222 
223 	CU_ASSERT_EQUAL(io.req_cnt, NUM_REQUESTS);
224 
225 	/* Complete all but one subrequest, make sure io still not marked as done */
226 	for (req = 0; req < (NUM_REQUESTS - 1); ++req) {
227 		ftl_io_dec_req(&io);
228 		CU_ASSERT_FALSE(ftl_io_done(&io));
229 	}
230 
231 	CU_ASSERT_EQUAL(io.req_cnt, 1);
232 
233 	/* Complete last subrequest, make sure it appears on the completion queue */
234 	ftl_io_dec_req(&io);
235 	CU_ASSERT_TRUE(ftl_io_done(&io));
236 
237 	ftl_io_complete(&io);
238 
239 	CU_ASSERT_EQUAL(spdk_ring_count(ioch->cq), 1);
240 
241 	/* Dequeue and check if the completion callback changes the status, this is usually done via poller */
242 	spdk_ring_dequeue(ioch->cq, (void **)&io_ring, 1);
243 	io_ring->user_fn(io_ring->cb_ctx, io_ring->status);
244 
245 	CU_ASSERT_EQUAL(status, -EIO);
246 
247 	free_device(dev);
248 }
249 
250 static void
251 test_multiple_ios(void)
252 {
253 	struct spdk_ftl_dev *dev;
254 	struct ftl_io_channel *ioch;
255 	struct ftl_io io[2] = { 0 }, *io_ring[2];
256 	int status = -1;
257 
258 	dev = setup_device(1, FTL_NUM_LBA_IN_BLOCK);
259 	ioch = ftl_io_channel_get_ctx(dev->ioch);
260 
261 	/* Send t2o IOs and check if both are in the completion queue */
262 	setup_io(&io[0], dev, io_complete_cb, &status);
263 	CU_ASSERT_EQUAL(spdk_ring_count(ioch->cq), 0);
264 
265 	ftl_io_complete(io);
266 	CU_ASSERT_EQUAL(spdk_ring_count(ioch->cq), 1);
267 
268 	setup_io(&io[1], dev, io_complete_cb, &status);
269 
270 	ftl_io_complete(&io[1]);
271 	CU_ASSERT_EQUAL(spdk_ring_count(ioch->cq), 2);
272 
273 	/* Dequeue and check if the completion callback changes the status, this is usually done via poller */
274 	spdk_ring_dequeue(ioch->cq, (void **)io_ring, 2);
275 	status = -1;
276 	io_ring[0]->user_fn(io_ring[0]->cb_ctx, io_ring[0]->status);
277 	CU_ASSERT_EQUAL(status, 0);
278 	status = -1;
279 	io_ring[1]->user_fn(io_ring[1]->cb_ctx, io_ring[1]->status);
280 	CU_ASSERT_EQUAL(status, 0);
281 
282 	free_device(dev);
283 }
284 
285 int
286 main(int argc, char **argv)
287 {
288 	CU_pSuite suite;
289 	unsigned int num_failures;
290 
291 	CU_set_error_action(CUEA_ABORT);
292 	CU_initialize_registry();
293 
294 	suite = CU_add_suite("ftl_io_suite", NULL, NULL);
295 
296 
297 	CU_ADD_TEST(suite, test_completion);
298 	CU_ADD_TEST(suite, test_multiple_ios);
299 
300 	CU_basic_set_mode(CU_BRM_VERBOSE);
301 	CU_basic_run_tests();
302 	num_failures = CU_get_number_of_failures();
303 	CU_cleanup_registry();
304 
305 	return num_failures;
306 }
307