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