1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (C) 2018 Intel Corporation. 3 * All rights reserved. 4 * Copyright (c) 2022-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 5 */ 6 7 #include "spdk/stdinc.h" 8 #include "spdk_cunit.h" 9 #include "spdk/env.h" 10 #include "spdk_internal/mock.h" 11 #include "thread/thread_internal.h" 12 #include "bdev/raid/bdev_raid.c" 13 #include "bdev/raid/bdev_raid_rpc.c" 14 #include "bdev/raid/raid0.c" 15 #include "common/lib/ut_multithread.c" 16 17 #define MAX_BASE_DRIVES 32 18 #define MAX_RAIDS 2 19 #define INVALID_IO_SUBMIT 0xFFFF 20 #define MAX_TEST_IO_RANGE (3 * 3 * 3 * (MAX_BASE_DRIVES + 5)) 21 #define BLOCK_CNT (1024ul * 1024ul * 1024ul * 1024ul) 22 23 struct spdk_bdev_channel { 24 struct spdk_io_channel *channel; 25 }; 26 27 struct spdk_bdev_desc { 28 struct spdk_bdev *bdev; 29 }; 30 31 /* Data structure to capture the output of IO for verification */ 32 struct io_output { 33 struct spdk_bdev_desc *desc; 34 struct spdk_io_channel *ch; 35 uint64_t offset_blocks; 36 uint64_t num_blocks; 37 spdk_bdev_io_completion_cb cb; 38 void *cb_arg; 39 enum spdk_bdev_io_type iotype; 40 }; 41 42 struct raid_io_ranges { 43 uint64_t lba; 44 uint64_t nblocks; 45 }; 46 47 /* Globals */ 48 int g_bdev_io_submit_status; 49 struct io_output *g_io_output = NULL; 50 uint32_t g_io_output_index; 51 uint32_t g_io_comp_status; 52 bool g_child_io_status_flag; 53 void *g_rpc_req; 54 uint32_t g_rpc_req_size; 55 TAILQ_HEAD(bdev, spdk_bdev); 56 struct bdev g_bdev_list; 57 TAILQ_HEAD(waitq, spdk_bdev_io_wait_entry); 58 struct waitq g_io_waitq; 59 uint32_t g_block_len; 60 uint32_t g_strip_size; 61 uint32_t g_max_io_size; 62 uint8_t g_max_base_drives; 63 uint8_t g_max_raids; 64 uint8_t g_ignore_io_output; 65 uint8_t g_rpc_err; 66 char *g_get_raids_output[MAX_RAIDS]; 67 uint32_t g_get_raids_count; 68 uint8_t g_json_decode_obj_err; 69 uint8_t g_json_decode_obj_create; 70 uint8_t g_config_level_create = 0; 71 uint8_t g_test_multi_raids; 72 struct raid_io_ranges g_io_ranges[MAX_TEST_IO_RANGE]; 73 uint32_t g_io_range_idx; 74 uint64_t g_lba_offset; 75 struct spdk_io_channel g_io_channel; 76 77 DEFINE_STUB_V(spdk_bdev_module_examine_done, (struct spdk_bdev_module *module)); 78 DEFINE_STUB_V(spdk_bdev_module_list_add, (struct spdk_bdev_module *bdev_module)); 79 DEFINE_STUB(spdk_bdev_register, int, (struct spdk_bdev *bdev), 0); 80 DEFINE_STUB(spdk_bdev_io_type_supported, bool, (struct spdk_bdev *bdev, 81 enum spdk_bdev_io_type io_type), true); 82 DEFINE_STUB_V(spdk_bdev_close, (struct spdk_bdev_desc *desc)); 83 DEFINE_STUB(spdk_bdev_flush_blocks, int, (struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 84 uint64_t offset_blocks, uint64_t num_blocks, spdk_bdev_io_completion_cb cb, 85 void *cb_arg), 0); 86 DEFINE_STUB(spdk_conf_next_section, struct spdk_conf_section *, (struct spdk_conf_section *sp), 87 NULL); 88 DEFINE_STUB_V(spdk_rpc_register_method, (const char *method, spdk_rpc_method_handler func, 89 uint32_t state_mask)); 90 DEFINE_STUB_V(spdk_rpc_register_alias_deprecated, (const char *method, const char *alias)); 91 DEFINE_STUB_V(spdk_jsonrpc_end_result, (struct spdk_jsonrpc_request *request, 92 struct spdk_json_write_ctx *w)); 93 DEFINE_STUB_V(spdk_jsonrpc_send_bool_response, (struct spdk_jsonrpc_request *request, 94 bool value)); 95 DEFINE_STUB(spdk_json_decode_string, int, (const struct spdk_json_val *val, void *out), 0); 96 DEFINE_STUB(spdk_json_decode_uint32, int, (const struct spdk_json_val *val, void *out), 0); 97 DEFINE_STUB(spdk_json_decode_array, int, (const struct spdk_json_val *values, 98 spdk_json_decode_fn decode_func, 99 void *out, size_t max_size, size_t *out_size, size_t stride), 0); 100 DEFINE_STUB(spdk_json_write_name, int, (struct spdk_json_write_ctx *w, const char *name), 0); 101 DEFINE_STUB(spdk_json_write_object_begin, int, (struct spdk_json_write_ctx *w), 0); 102 DEFINE_STUB(spdk_json_write_named_object_begin, int, (struct spdk_json_write_ctx *w, 103 const char *name), 0); 104 DEFINE_STUB(spdk_json_write_string, int, (struct spdk_json_write_ctx *w, const char *val), 0); 105 DEFINE_STUB(spdk_json_write_object_end, int, (struct spdk_json_write_ctx *w), 0); 106 DEFINE_STUB(spdk_json_write_array_begin, int, (struct spdk_json_write_ctx *w), 0); 107 DEFINE_STUB(spdk_json_write_array_end, int, (struct spdk_json_write_ctx *w), 0); 108 DEFINE_STUB(spdk_json_write_named_array_begin, int, (struct spdk_json_write_ctx *w, 109 const char *name), 0); 110 DEFINE_STUB(spdk_json_write_bool, int, (struct spdk_json_write_ctx *w, bool val), 0); 111 DEFINE_STUB(spdk_json_write_null, int, (struct spdk_json_write_ctx *w), 0); 112 DEFINE_STUB(spdk_strerror, const char *, (int errnum), NULL); 113 DEFINE_STUB(spdk_bdev_queue_io_wait, int, (struct spdk_bdev *bdev, struct spdk_io_channel *ch, 114 struct spdk_bdev_io_wait_entry *entry), 0); 115 DEFINE_STUB(spdk_bdev_get_memory_domains, int, (struct spdk_bdev *bdev, 116 struct spdk_memory_domain **domains, int array_size), 0); 117 DEFINE_STUB(spdk_bdev_get_name, const char *, (const struct spdk_bdev *bdev), "test_bdev"); 118 DEFINE_STUB(spdk_bdev_get_md_size, uint32_t, (const struct spdk_bdev *bdev), 0); 119 DEFINE_STUB(spdk_bdev_is_md_interleaved, bool, (const struct spdk_bdev *bdev), false); 120 DEFINE_STUB(spdk_bdev_get_dif_type, enum spdk_dif_type, (const struct spdk_bdev *bdev), 121 SPDK_DIF_DISABLE); 122 DEFINE_STUB(spdk_bdev_is_dif_head_of_md, bool, (const struct spdk_bdev *bdev), false); 123 124 struct spdk_io_channel * 125 spdk_bdev_get_io_channel(struct spdk_bdev_desc *desc) 126 { 127 g_io_channel.thread = spdk_get_thread(); 128 129 return &g_io_channel; 130 } 131 132 static void 133 set_test_opts(void) 134 { 135 136 g_max_base_drives = MAX_BASE_DRIVES; 137 g_max_raids = MAX_RAIDS; 138 g_block_len = 4096; 139 g_strip_size = 64; 140 g_max_io_size = 1024; 141 142 printf("Test Options\n"); 143 printf("blocklen = %u, strip_size = %u, max_io_size = %u, g_max_base_drives = %u, " 144 "g_max_raids = %u\n", 145 g_block_len, g_strip_size, g_max_io_size, g_max_base_drives, g_max_raids); 146 } 147 148 /* Set globals before every test run */ 149 static void 150 set_globals(void) 151 { 152 uint32_t max_splits; 153 154 g_bdev_io_submit_status = 0; 155 if (g_max_io_size < g_strip_size) { 156 max_splits = 2; 157 } else { 158 max_splits = (g_max_io_size / g_strip_size) + 1; 159 } 160 if (max_splits < g_max_base_drives) { 161 max_splits = g_max_base_drives; 162 } 163 164 g_io_output = calloc(max_splits, sizeof(struct io_output)); 165 SPDK_CU_ASSERT_FATAL(g_io_output != NULL); 166 g_io_output_index = 0; 167 memset(g_get_raids_output, 0, sizeof(g_get_raids_output)); 168 g_get_raids_count = 0; 169 g_io_comp_status = 0; 170 g_ignore_io_output = 0; 171 g_config_level_create = 0; 172 g_rpc_err = 0; 173 g_test_multi_raids = 0; 174 g_child_io_status_flag = true; 175 TAILQ_INIT(&g_bdev_list); 176 TAILQ_INIT(&g_io_waitq); 177 g_rpc_req = NULL; 178 g_rpc_req_size = 0; 179 g_json_decode_obj_err = 0; 180 g_json_decode_obj_create = 0; 181 g_lba_offset = 0; 182 } 183 184 static void 185 base_bdevs_cleanup(void) 186 { 187 struct spdk_bdev *bdev; 188 struct spdk_bdev *bdev_next; 189 190 if (!TAILQ_EMPTY(&g_bdev_list)) { 191 TAILQ_FOREACH_SAFE(bdev, &g_bdev_list, internal.link, bdev_next) { 192 free(bdev->name); 193 TAILQ_REMOVE(&g_bdev_list, bdev, internal.link); 194 free(bdev); 195 } 196 } 197 } 198 199 static void 200 check_and_remove_raid_bdev(struct raid_bdev *raid_bdev) 201 { 202 struct raid_base_bdev_info *base_info; 203 204 assert(raid_bdev != NULL); 205 assert(raid_bdev->base_bdev_info != NULL); 206 207 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 208 if (base_info->bdev) { 209 raid_bdev_free_base_bdev_resource(raid_bdev, base_info); 210 } 211 } 212 assert(raid_bdev->num_base_bdevs_discovered == 0); 213 raid_bdev_cleanup_and_free(raid_bdev); 214 } 215 216 /* Reset globals */ 217 static void 218 reset_globals(void) 219 { 220 if (g_io_output) { 221 free(g_io_output); 222 g_io_output = NULL; 223 } 224 g_rpc_req = NULL; 225 g_rpc_req_size = 0; 226 } 227 228 void 229 spdk_bdev_io_get_buf(struct spdk_bdev_io *bdev_io, spdk_bdev_io_get_buf_cb cb, 230 uint64_t len) 231 { 232 cb(bdev_io->internal.ch->channel, bdev_io, true); 233 } 234 235 /* Store the IO completion status in global variable to verify by various tests */ 236 void 237 spdk_bdev_io_complete(struct spdk_bdev_io *bdev_io, enum spdk_bdev_io_status status) 238 { 239 g_io_comp_status = ((status == SPDK_BDEV_IO_STATUS_SUCCESS) ? true : false); 240 } 241 242 static void 243 set_io_output(struct io_output *output, 244 struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 245 uint64_t offset_blocks, uint64_t num_blocks, 246 spdk_bdev_io_completion_cb cb, void *cb_arg, 247 enum spdk_bdev_io_type iotype) 248 { 249 output->desc = desc; 250 output->ch = ch; 251 output->offset_blocks = offset_blocks; 252 output->num_blocks = num_blocks; 253 output->cb = cb; 254 output->cb_arg = cb_arg; 255 output->iotype = iotype; 256 } 257 258 /* It will cache the split IOs for verification */ 259 int 260 spdk_bdev_writev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 261 struct iovec *iov, int iovcnt, 262 uint64_t offset_blocks, uint64_t num_blocks, 263 spdk_bdev_io_completion_cb cb, void *cb_arg) 264 { 265 struct io_output *output = &g_io_output[g_io_output_index]; 266 struct spdk_bdev_io *child_io; 267 268 if (g_ignore_io_output) { 269 return 0; 270 } 271 272 if (g_max_io_size < g_strip_size) { 273 SPDK_CU_ASSERT_FATAL(g_io_output_index < 2); 274 } else { 275 SPDK_CU_ASSERT_FATAL(g_io_output_index < (g_max_io_size / g_strip_size) + 1); 276 } 277 if (g_bdev_io_submit_status == 0) { 278 set_io_output(output, desc, ch, offset_blocks, num_blocks, cb, cb_arg, 279 SPDK_BDEV_IO_TYPE_WRITE); 280 g_io_output_index++; 281 282 child_io = calloc(1, sizeof(struct spdk_bdev_io)); 283 SPDK_CU_ASSERT_FATAL(child_io != NULL); 284 cb(child_io, g_child_io_status_flag, cb_arg); 285 } 286 287 return g_bdev_io_submit_status; 288 } 289 290 int 291 spdk_bdev_writev_blocks_ext(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 292 struct iovec *iov, int iovcnt, 293 uint64_t offset_blocks, uint64_t num_blocks, 294 spdk_bdev_io_completion_cb cb, void *cb_arg, 295 struct spdk_bdev_ext_io_opts *opts) 296 { 297 return spdk_bdev_writev_blocks(desc, ch, iov, iovcnt, offset_blocks, num_blocks, cb, cb_arg); 298 } 299 300 int 301 spdk_bdev_writev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 302 struct iovec *iov, int iovcnt, void *md, 303 uint64_t offset_blocks, uint64_t num_blocks, 304 spdk_bdev_io_completion_cb cb, void *cb_arg) 305 { 306 return spdk_bdev_writev_blocks(desc, ch, iov, iovcnt, offset_blocks, num_blocks, cb, cb_arg); 307 } 308 309 int 310 spdk_bdev_reset(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 311 spdk_bdev_io_completion_cb cb, void *cb_arg) 312 { 313 struct io_output *output = &g_io_output[g_io_output_index]; 314 struct spdk_bdev_io *child_io; 315 316 if (g_ignore_io_output) { 317 return 0; 318 } 319 320 if (g_bdev_io_submit_status == 0) { 321 set_io_output(output, desc, ch, 0, 0, cb, cb_arg, SPDK_BDEV_IO_TYPE_RESET); 322 g_io_output_index++; 323 324 child_io = calloc(1, sizeof(struct spdk_bdev_io)); 325 SPDK_CU_ASSERT_FATAL(child_io != NULL); 326 cb(child_io, g_child_io_status_flag, cb_arg); 327 } 328 329 return g_bdev_io_submit_status; 330 } 331 332 int 333 spdk_bdev_unmap_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 334 uint64_t offset_blocks, uint64_t num_blocks, 335 spdk_bdev_io_completion_cb cb, void *cb_arg) 336 { 337 struct io_output *output = &g_io_output[g_io_output_index]; 338 struct spdk_bdev_io *child_io; 339 340 if (g_ignore_io_output) { 341 return 0; 342 } 343 344 if (g_bdev_io_submit_status == 0) { 345 set_io_output(output, desc, ch, offset_blocks, num_blocks, cb, cb_arg, 346 SPDK_BDEV_IO_TYPE_UNMAP); 347 g_io_output_index++; 348 349 child_io = calloc(1, sizeof(struct spdk_bdev_io)); 350 SPDK_CU_ASSERT_FATAL(child_io != NULL); 351 cb(child_io, g_child_io_status_flag, cb_arg); 352 } 353 354 return g_bdev_io_submit_status; 355 } 356 357 void 358 spdk_bdev_destruct_done(struct spdk_bdev *bdev, int bdeverrno) 359 { 360 CU_ASSERT(bdeverrno == 0); 361 SPDK_CU_ASSERT_FATAL(bdev->internal.unregister_cb != NULL); 362 bdev->internal.unregister_cb(bdev->internal.unregister_ctx, bdeverrno); 363 } 364 365 void 366 spdk_bdev_unregister(struct spdk_bdev *bdev, spdk_bdev_unregister_cb cb_fn, void *cb_arg) 367 { 368 int ret; 369 370 bdev->internal.unregister_cb = cb_fn; 371 bdev->internal.unregister_ctx = cb_arg; 372 373 ret = bdev->fn_table->destruct(bdev->ctxt); 374 CU_ASSERT(ret == 1); 375 376 poll_threads(); 377 } 378 379 int 380 spdk_bdev_open_ext(const char *bdev_name, bool write, spdk_bdev_event_cb_t event_cb, 381 void *event_ctx, struct spdk_bdev_desc **_desc) 382 { 383 struct spdk_bdev *bdev; 384 385 bdev = spdk_bdev_get_by_name(bdev_name); 386 if (bdev == NULL) { 387 return -ENODEV; 388 } 389 390 *_desc = (void *)bdev; 391 return 0; 392 } 393 394 struct spdk_bdev * 395 spdk_bdev_desc_get_bdev(struct spdk_bdev_desc *desc) 396 { 397 return (void *)desc; 398 } 399 400 char * 401 spdk_sprintf_alloc(const char *format, ...) 402 { 403 return strdup(format); 404 } 405 406 int 407 spdk_json_write_named_uint32(struct spdk_json_write_ctx *w, const char *name, uint32_t val) 408 { 409 if (!g_test_multi_raids) { 410 struct rpc_bdev_raid_create *req = g_rpc_req; 411 if (strcmp(name, "strip_size_kb") == 0) { 412 CU_ASSERT(req->strip_size_kb == val); 413 } else if (strcmp(name, "blocklen_shift") == 0) { 414 CU_ASSERT(spdk_u32log2(g_block_len) == val); 415 } else if (strcmp(name, "num_base_bdevs") == 0) { 416 CU_ASSERT(req->base_bdevs.num_base_bdevs == val); 417 } else if (strcmp(name, "state") == 0) { 418 CU_ASSERT(val == RAID_BDEV_STATE_ONLINE); 419 } else if (strcmp(name, "destruct_called") == 0) { 420 CU_ASSERT(val == 0); 421 } else if (strcmp(name, "num_base_bdevs_discovered") == 0) { 422 CU_ASSERT(req->base_bdevs.num_base_bdevs == val); 423 } 424 } 425 return 0; 426 } 427 428 int 429 spdk_json_write_named_string(struct spdk_json_write_ctx *w, const char *name, const char *val) 430 { 431 if (g_test_multi_raids) { 432 if (strcmp(name, "name") == 0) { 433 g_get_raids_output[g_get_raids_count] = strdup(val); 434 SPDK_CU_ASSERT_FATAL(g_get_raids_output[g_get_raids_count] != NULL); 435 g_get_raids_count++; 436 } 437 } else { 438 struct rpc_bdev_raid_create *req = g_rpc_req; 439 if (strcmp(name, "raid_level") == 0) { 440 CU_ASSERT(strcmp(val, raid_bdev_level_to_str(req->level)) == 0); 441 } 442 } 443 return 0; 444 } 445 446 void 447 spdk_bdev_free_io(struct spdk_bdev_io *bdev_io) 448 { 449 if (bdev_io) { 450 free(bdev_io); 451 } 452 } 453 454 /* It will cache split IOs for verification */ 455 int 456 spdk_bdev_readv_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 457 struct iovec *iov, int iovcnt, 458 uint64_t offset_blocks, uint64_t num_blocks, 459 spdk_bdev_io_completion_cb cb, void *cb_arg) 460 { 461 struct io_output *output = &g_io_output[g_io_output_index]; 462 struct spdk_bdev_io *child_io; 463 464 if (g_ignore_io_output) { 465 return 0; 466 } 467 468 SPDK_CU_ASSERT_FATAL(g_io_output_index <= (g_max_io_size / g_strip_size) + 1); 469 if (g_bdev_io_submit_status == 0) { 470 set_io_output(output, desc, ch, offset_blocks, num_blocks, cb, cb_arg, 471 SPDK_BDEV_IO_TYPE_READ); 472 g_io_output_index++; 473 474 child_io = calloc(1, sizeof(struct spdk_bdev_io)); 475 SPDK_CU_ASSERT_FATAL(child_io != NULL); 476 cb(child_io, g_child_io_status_flag, cb_arg); 477 } 478 479 return g_bdev_io_submit_status; 480 } 481 482 int 483 spdk_bdev_readv_blocks_ext(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 484 struct iovec *iov, int iovcnt, 485 uint64_t offset_blocks, uint64_t num_blocks, 486 spdk_bdev_io_completion_cb cb, void *cb_arg, 487 struct spdk_bdev_ext_io_opts *opts) 488 { 489 return spdk_bdev_readv_blocks(desc, ch, iov, iovcnt, offset_blocks, num_blocks, cb, cb_arg); 490 } 491 492 int 493 spdk_bdev_readv_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 494 struct iovec *iov, int iovcnt, void *md, 495 uint64_t offset_blocks, uint64_t num_blocks, 496 spdk_bdev_io_completion_cb cb, void *cb_arg) 497 { 498 return spdk_bdev_readv_blocks(desc, ch, iov, iovcnt, offset_blocks, num_blocks, cb, cb_arg); 499 } 500 501 502 void 503 spdk_bdev_module_release_bdev(struct spdk_bdev *bdev) 504 { 505 CU_ASSERT(bdev->internal.claim_type == SPDK_BDEV_CLAIM_EXCL_WRITE); 506 CU_ASSERT(bdev->internal.claim.v1.module != NULL); 507 bdev->internal.claim_type = SPDK_BDEV_CLAIM_NONE; 508 bdev->internal.claim.v1.module = NULL; 509 } 510 511 int 512 spdk_bdev_module_claim_bdev(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 513 struct spdk_bdev_module *module) 514 { 515 if (bdev->internal.claim_type != SPDK_BDEV_CLAIM_NONE) { 516 CU_ASSERT(bdev->internal.claim.v1.module != NULL); 517 return -1; 518 } 519 CU_ASSERT(bdev->internal.claim.v1.module == NULL); 520 bdev->internal.claim_type = SPDK_BDEV_CLAIM_EXCL_WRITE; 521 bdev->internal.claim.v1.module = module; 522 return 0; 523 } 524 525 int 526 spdk_json_decode_object(const struct spdk_json_val *values, 527 const struct spdk_json_object_decoder *decoders, size_t num_decoders, 528 void *out) 529 { 530 struct rpc_bdev_raid_create *req, *_out; 531 size_t i; 532 533 if (g_json_decode_obj_err) { 534 return -1; 535 } else if (g_json_decode_obj_create) { 536 req = g_rpc_req; 537 _out = out; 538 539 _out->name = strdup(req->name); 540 SPDK_CU_ASSERT_FATAL(_out->name != NULL); 541 _out->strip_size_kb = req->strip_size_kb; 542 _out->level = req->level; 543 _out->base_bdevs.num_base_bdevs = req->base_bdevs.num_base_bdevs; 544 for (i = 0; i < req->base_bdevs.num_base_bdevs; i++) { 545 _out->base_bdevs.base_bdevs[i] = strdup(req->base_bdevs.base_bdevs[i]); 546 SPDK_CU_ASSERT_FATAL(_out->base_bdevs.base_bdevs[i]); 547 } 548 } else { 549 memcpy(out, g_rpc_req, g_rpc_req_size); 550 } 551 552 return 0; 553 } 554 555 struct spdk_json_write_ctx * 556 spdk_jsonrpc_begin_result(struct spdk_jsonrpc_request *request) 557 { 558 return (void *)1; 559 } 560 561 void 562 spdk_jsonrpc_send_error_response(struct spdk_jsonrpc_request *request, 563 int error_code, const char *msg) 564 { 565 g_rpc_err = 1; 566 } 567 568 void 569 spdk_jsonrpc_send_error_response_fmt(struct spdk_jsonrpc_request *request, 570 int error_code, const char *fmt, ...) 571 { 572 g_rpc_err = 1; 573 } 574 575 struct spdk_bdev * 576 spdk_bdev_get_by_name(const char *bdev_name) 577 { 578 struct spdk_bdev *bdev; 579 580 if (!TAILQ_EMPTY(&g_bdev_list)) { 581 TAILQ_FOREACH(bdev, &g_bdev_list, internal.link) { 582 if (strcmp(bdev_name, bdev->name) == 0) { 583 return bdev; 584 } 585 } 586 } 587 588 return NULL; 589 } 590 591 static void 592 bdev_io_cleanup(struct spdk_bdev_io *bdev_io) 593 { 594 if (bdev_io->u.bdev.iovs) { 595 if (bdev_io->u.bdev.iovs->iov_base) { 596 free(bdev_io->u.bdev.iovs->iov_base); 597 } 598 free(bdev_io->u.bdev.iovs); 599 } 600 free(bdev_io); 601 } 602 603 static void 604 bdev_io_initialize(struct spdk_bdev_io *bdev_io, struct spdk_io_channel *ch, struct spdk_bdev *bdev, 605 uint64_t lba, uint64_t blocks, int16_t iotype) 606 { 607 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 608 609 bdev_io->bdev = bdev; 610 bdev_io->u.bdev.offset_blocks = lba; 611 bdev_io->u.bdev.num_blocks = blocks; 612 bdev_io->type = iotype; 613 614 if (bdev_io->type == SPDK_BDEV_IO_TYPE_UNMAP || bdev_io->type == SPDK_BDEV_IO_TYPE_FLUSH) { 615 return; 616 } 617 618 bdev_io->u.bdev.iovcnt = 1; 619 bdev_io->u.bdev.iovs = calloc(1, sizeof(struct iovec)); 620 SPDK_CU_ASSERT_FATAL(bdev_io->u.bdev.iovs != NULL); 621 bdev_io->u.bdev.iovs->iov_base = calloc(1, bdev_io->u.bdev.num_blocks * g_block_len); 622 SPDK_CU_ASSERT_FATAL(bdev_io->u.bdev.iovs->iov_base != NULL); 623 bdev_io->u.bdev.iovs->iov_len = bdev_io->u.bdev.num_blocks * g_block_len; 624 bdev_io->internal.ch = channel; 625 } 626 627 static void 628 verify_reset_io(struct spdk_bdev_io *bdev_io, uint8_t num_base_drives, 629 struct raid_bdev_io_channel *ch_ctx, struct raid_bdev *raid_bdev, uint32_t io_status) 630 { 631 uint8_t index = 0; 632 struct io_output *output; 633 634 SPDK_CU_ASSERT_FATAL(raid_bdev != NULL); 635 SPDK_CU_ASSERT_FATAL(num_base_drives != 0); 636 SPDK_CU_ASSERT_FATAL(io_status != INVALID_IO_SUBMIT); 637 SPDK_CU_ASSERT_FATAL(ch_ctx->base_channel != NULL); 638 639 CU_ASSERT(g_io_output_index == num_base_drives); 640 for (index = 0; index < g_io_output_index; index++) { 641 output = &g_io_output[index]; 642 CU_ASSERT(ch_ctx->base_channel[index] == output->ch); 643 CU_ASSERT(raid_bdev->base_bdev_info[index].desc == output->desc); 644 CU_ASSERT(bdev_io->type == output->iotype); 645 } 646 CU_ASSERT(g_io_comp_status == io_status); 647 } 648 649 static void 650 verify_io(struct spdk_bdev_io *bdev_io, uint8_t num_base_drives, 651 struct raid_bdev_io_channel *ch_ctx, struct raid_bdev *raid_bdev, uint32_t io_status) 652 { 653 uint32_t strip_shift = spdk_u32log2(g_strip_size); 654 uint64_t start_strip = bdev_io->u.bdev.offset_blocks >> strip_shift; 655 uint64_t end_strip = (bdev_io->u.bdev.offset_blocks + bdev_io->u.bdev.num_blocks - 1) >> 656 strip_shift; 657 uint32_t splits_reqd = (end_strip - start_strip + 1); 658 uint32_t strip; 659 uint64_t pd_strip; 660 uint8_t pd_idx; 661 uint32_t offset_in_strip; 662 uint64_t pd_lba; 663 uint64_t pd_blocks; 664 uint32_t index = 0; 665 struct io_output *output; 666 667 if (io_status == INVALID_IO_SUBMIT) { 668 CU_ASSERT(g_io_comp_status == false); 669 return; 670 } 671 SPDK_CU_ASSERT_FATAL(raid_bdev != NULL); 672 SPDK_CU_ASSERT_FATAL(num_base_drives != 0); 673 674 CU_ASSERT(splits_reqd == g_io_output_index); 675 for (strip = start_strip; strip <= end_strip; strip++, index++) { 676 pd_strip = strip / num_base_drives; 677 pd_idx = strip % num_base_drives; 678 if (strip == start_strip) { 679 offset_in_strip = bdev_io->u.bdev.offset_blocks & (g_strip_size - 1); 680 pd_lba = (pd_strip << strip_shift) + offset_in_strip; 681 if (strip == end_strip) { 682 pd_blocks = bdev_io->u.bdev.num_blocks; 683 } else { 684 pd_blocks = g_strip_size - offset_in_strip; 685 } 686 } else if (strip == end_strip) { 687 pd_lba = pd_strip << strip_shift; 688 pd_blocks = ((bdev_io->u.bdev.offset_blocks + bdev_io->u.bdev.num_blocks - 1) & 689 (g_strip_size - 1)) + 1; 690 } else { 691 pd_lba = pd_strip << raid_bdev->strip_size_shift; 692 pd_blocks = raid_bdev->strip_size; 693 } 694 output = &g_io_output[index]; 695 CU_ASSERT(pd_lba == output->offset_blocks); 696 CU_ASSERT(pd_blocks == output->num_blocks); 697 CU_ASSERT(ch_ctx->base_channel[pd_idx] == output->ch); 698 CU_ASSERT(raid_bdev->base_bdev_info[pd_idx].desc == output->desc); 699 CU_ASSERT(bdev_io->type == output->iotype); 700 } 701 CU_ASSERT(g_io_comp_status == io_status); 702 } 703 704 static void 705 verify_io_without_payload(struct spdk_bdev_io *bdev_io, uint8_t num_base_drives, 706 struct raid_bdev_io_channel *ch_ctx, struct raid_bdev *raid_bdev, 707 uint32_t io_status) 708 { 709 uint32_t strip_shift = spdk_u32log2(g_strip_size); 710 uint64_t start_offset_in_strip = bdev_io->u.bdev.offset_blocks % g_strip_size; 711 uint64_t end_offset_in_strip = (bdev_io->u.bdev.offset_blocks + bdev_io->u.bdev.num_blocks - 1) % 712 g_strip_size; 713 uint64_t start_strip = bdev_io->u.bdev.offset_blocks >> strip_shift; 714 uint64_t end_strip = (bdev_io->u.bdev.offset_blocks + bdev_io->u.bdev.num_blocks - 1) >> 715 strip_shift; 716 uint8_t n_disks_involved; 717 uint64_t start_strip_disk_idx; 718 uint64_t end_strip_disk_idx; 719 uint64_t nblocks_in_start_disk; 720 uint64_t offset_in_start_disk; 721 uint8_t disk_idx; 722 uint64_t base_io_idx; 723 uint64_t sum_nblocks = 0; 724 struct io_output *output; 725 726 if (io_status == INVALID_IO_SUBMIT) { 727 CU_ASSERT(g_io_comp_status == false); 728 return; 729 } 730 SPDK_CU_ASSERT_FATAL(raid_bdev != NULL); 731 SPDK_CU_ASSERT_FATAL(num_base_drives != 0); 732 SPDK_CU_ASSERT_FATAL(bdev_io->type != SPDK_BDEV_IO_TYPE_READ); 733 SPDK_CU_ASSERT_FATAL(bdev_io->type != SPDK_BDEV_IO_TYPE_WRITE); 734 735 n_disks_involved = spdk_min(end_strip - start_strip + 1, num_base_drives); 736 CU_ASSERT(n_disks_involved == g_io_output_index); 737 738 start_strip_disk_idx = start_strip % num_base_drives; 739 end_strip_disk_idx = end_strip % num_base_drives; 740 741 offset_in_start_disk = g_io_output[0].offset_blocks; 742 nblocks_in_start_disk = g_io_output[0].num_blocks; 743 744 for (base_io_idx = 0, disk_idx = start_strip_disk_idx; base_io_idx < n_disks_involved; 745 base_io_idx++, disk_idx++) { 746 uint64_t start_offset_in_disk; 747 uint64_t end_offset_in_disk; 748 749 output = &g_io_output[base_io_idx]; 750 751 /* round disk_idx */ 752 if (disk_idx >= num_base_drives) { 753 disk_idx %= num_base_drives; 754 } 755 756 /* start_offset_in_disk aligned in strip check: 757 * The first base io has a same start_offset_in_strip with the whole raid io. 758 * Other base io should have aligned start_offset_in_strip which is 0. 759 */ 760 start_offset_in_disk = output->offset_blocks; 761 if (base_io_idx == 0) { 762 CU_ASSERT(start_offset_in_disk % g_strip_size == start_offset_in_strip); 763 } else { 764 CU_ASSERT(start_offset_in_disk % g_strip_size == 0); 765 } 766 767 /* end_offset_in_disk aligned in strip check: 768 * Base io on disk at which end_strip is located, has a same end_offset_in_strip 769 * with the whole raid io. 770 * Other base io should have aligned end_offset_in_strip. 771 */ 772 end_offset_in_disk = output->offset_blocks + output->num_blocks - 1; 773 if (disk_idx == end_strip_disk_idx) { 774 CU_ASSERT(end_offset_in_disk % g_strip_size == end_offset_in_strip); 775 } else { 776 CU_ASSERT(end_offset_in_disk % g_strip_size == g_strip_size - 1); 777 } 778 779 /* start_offset_in_disk compared with start_disk. 780 * 1. For disk_idx which is larger than start_strip_disk_idx: Its start_offset_in_disk 781 * mustn't be larger than the start offset of start_offset_in_disk; And the gap 782 * must be less than strip size. 783 * 2. For disk_idx which is less than start_strip_disk_idx, Its start_offset_in_disk 784 * must be larger than the start offset of start_offset_in_disk; And the gap mustn't 785 * be less than strip size. 786 */ 787 if (disk_idx > start_strip_disk_idx) { 788 CU_ASSERT(start_offset_in_disk <= offset_in_start_disk); 789 CU_ASSERT(offset_in_start_disk - start_offset_in_disk < g_strip_size); 790 } else if (disk_idx < start_strip_disk_idx) { 791 CU_ASSERT(start_offset_in_disk > offset_in_start_disk); 792 CU_ASSERT(output->offset_blocks - offset_in_start_disk <= g_strip_size); 793 } 794 795 /* nblocks compared with start_disk: 796 * The gap between them must be within a strip size. 797 */ 798 if (output->num_blocks <= nblocks_in_start_disk) { 799 CU_ASSERT(nblocks_in_start_disk - output->num_blocks <= g_strip_size); 800 } else { 801 CU_ASSERT(output->num_blocks - nblocks_in_start_disk < g_strip_size); 802 } 803 804 sum_nblocks += output->num_blocks; 805 806 CU_ASSERT(ch_ctx->base_channel[disk_idx] == output->ch); 807 CU_ASSERT(raid_bdev->base_bdev_info[disk_idx].desc == output->desc); 808 CU_ASSERT(bdev_io->type == output->iotype); 809 } 810 811 /* Sum of each nblocks should be same with raid bdev_io */ 812 CU_ASSERT(bdev_io->u.bdev.num_blocks == sum_nblocks); 813 814 CU_ASSERT(g_io_comp_status == io_status); 815 } 816 817 static void 818 verify_raid_bdev_present(const char *name, bool presence) 819 { 820 struct raid_bdev *pbdev; 821 bool pbdev_found; 822 823 pbdev_found = false; 824 TAILQ_FOREACH(pbdev, &g_raid_bdev_list, global_link) { 825 if (strcmp(pbdev->bdev.name, name) == 0) { 826 pbdev_found = true; 827 break; 828 } 829 } 830 if (presence == true) { 831 CU_ASSERT(pbdev_found == true); 832 } else { 833 CU_ASSERT(pbdev_found == false); 834 } 835 } 836 837 static void 838 verify_raid_bdev(struct rpc_bdev_raid_create *r, bool presence, uint32_t raid_state) 839 { 840 struct raid_bdev *pbdev; 841 struct raid_base_bdev_info *base_info; 842 struct spdk_bdev *bdev = NULL; 843 bool pbdev_found; 844 uint64_t min_blockcnt = 0xFFFFFFFFFFFFFFFF; 845 846 pbdev_found = false; 847 TAILQ_FOREACH(pbdev, &g_raid_bdev_list, global_link) { 848 if (strcmp(pbdev->bdev.name, r->name) == 0) { 849 pbdev_found = true; 850 if (presence == false) { 851 break; 852 } 853 CU_ASSERT(pbdev->base_bdev_info != NULL); 854 CU_ASSERT(pbdev->strip_size == ((r->strip_size_kb * 1024) / g_block_len)); 855 CU_ASSERT(pbdev->strip_size_shift == spdk_u32log2(((r->strip_size_kb * 1024) / 856 g_block_len))); 857 CU_ASSERT(pbdev->blocklen_shift == spdk_u32log2(g_block_len)); 858 CU_ASSERT((uint32_t)pbdev->state == raid_state); 859 CU_ASSERT(pbdev->num_base_bdevs == r->base_bdevs.num_base_bdevs); 860 CU_ASSERT(pbdev->num_base_bdevs_discovered == r->base_bdevs.num_base_bdevs); 861 CU_ASSERT(pbdev->level == r->level); 862 CU_ASSERT(pbdev->base_bdev_info != NULL); 863 RAID_FOR_EACH_BASE_BDEV(pbdev, base_info) { 864 CU_ASSERT(base_info->bdev != NULL); 865 bdev = spdk_bdev_get_by_name(base_info->bdev->name); 866 CU_ASSERT(bdev != NULL); 867 CU_ASSERT(base_info->remove_scheduled == false); 868 869 if (bdev && bdev->blockcnt < min_blockcnt) { 870 min_blockcnt = bdev->blockcnt; 871 } 872 } 873 CU_ASSERT((((min_blockcnt / (r->strip_size_kb * 1024 / g_block_len)) * 874 (r->strip_size_kb * 1024 / g_block_len)) * 875 r->base_bdevs.num_base_bdevs) == pbdev->bdev.blockcnt); 876 CU_ASSERT(strcmp(pbdev->bdev.product_name, "Raid Volume") == 0); 877 CU_ASSERT(pbdev->bdev.write_cache == 0); 878 CU_ASSERT(pbdev->bdev.blocklen == g_block_len); 879 if (pbdev->num_base_bdevs > 1) { 880 CU_ASSERT(pbdev->bdev.optimal_io_boundary == pbdev->strip_size); 881 CU_ASSERT(pbdev->bdev.split_on_optimal_io_boundary == true); 882 } else { 883 CU_ASSERT(pbdev->bdev.optimal_io_boundary == 0); 884 CU_ASSERT(pbdev->bdev.split_on_optimal_io_boundary == false); 885 } 886 CU_ASSERT(pbdev->bdev.ctxt == pbdev); 887 CU_ASSERT(pbdev->bdev.fn_table == &g_raid_bdev_fn_table); 888 CU_ASSERT(pbdev->bdev.module == &g_raid_if); 889 break; 890 } 891 } 892 if (presence == true) { 893 CU_ASSERT(pbdev_found == true); 894 } else { 895 CU_ASSERT(pbdev_found == false); 896 } 897 } 898 899 static void 900 verify_get_raids(struct rpc_bdev_raid_create *construct_req, 901 uint8_t g_max_raids, 902 char **g_get_raids_output, uint32_t g_get_raids_count) 903 { 904 uint8_t i, j; 905 bool found; 906 907 CU_ASSERT(g_max_raids == g_get_raids_count); 908 if (g_max_raids == g_get_raids_count) { 909 for (i = 0; i < g_max_raids; i++) { 910 found = false; 911 for (j = 0; j < g_max_raids; j++) { 912 if (construct_req[i].name && 913 strcmp(construct_req[i].name, g_get_raids_output[i]) == 0) { 914 found = true; 915 break; 916 } 917 } 918 CU_ASSERT(found == true); 919 } 920 } 921 } 922 923 static void 924 create_base_bdevs(uint32_t bbdev_start_idx) 925 { 926 uint8_t i; 927 struct spdk_bdev *base_bdev; 928 char name[16]; 929 930 for (i = 0; i < g_max_base_drives; i++, bbdev_start_idx++) { 931 snprintf(name, 16, "%s%u%s", "Nvme", bbdev_start_idx, "n1"); 932 base_bdev = calloc(1, sizeof(struct spdk_bdev)); 933 SPDK_CU_ASSERT_FATAL(base_bdev != NULL); 934 base_bdev->name = strdup(name); 935 SPDK_CU_ASSERT_FATAL(base_bdev->name != NULL); 936 base_bdev->blocklen = g_block_len; 937 base_bdev->blockcnt = BLOCK_CNT; 938 TAILQ_INSERT_TAIL(&g_bdev_list, base_bdev, internal.link); 939 } 940 } 941 942 static void 943 create_test_req(struct rpc_bdev_raid_create *r, const char *raid_name, 944 uint8_t bbdev_start_idx, bool create_base_bdev) 945 { 946 uint8_t i; 947 char name[16]; 948 uint8_t bbdev_idx = bbdev_start_idx; 949 950 r->name = strdup(raid_name); 951 SPDK_CU_ASSERT_FATAL(r->name != NULL); 952 r->strip_size_kb = (g_strip_size * g_block_len) / 1024; 953 r->level = RAID0; 954 r->base_bdevs.num_base_bdevs = g_max_base_drives; 955 for (i = 0; i < g_max_base_drives; i++, bbdev_idx++) { 956 snprintf(name, 16, "%s%u%s", "Nvme", bbdev_idx, "n1"); 957 r->base_bdevs.base_bdevs[i] = strdup(name); 958 SPDK_CU_ASSERT_FATAL(r->base_bdevs.base_bdevs[i] != NULL); 959 } 960 if (create_base_bdev == true) { 961 create_base_bdevs(bbdev_start_idx); 962 } 963 g_rpc_req = r; 964 g_rpc_req_size = sizeof(*r); 965 } 966 967 static void 968 create_raid_bdev_create_req(struct rpc_bdev_raid_create *r, const char *raid_name, 969 uint8_t bbdev_start_idx, bool create_base_bdev, 970 uint8_t json_decode_obj_err) 971 { 972 create_test_req(r, raid_name, bbdev_start_idx, create_base_bdev); 973 974 g_rpc_err = 0; 975 g_json_decode_obj_create = 1; 976 g_json_decode_obj_err = json_decode_obj_err; 977 g_config_level_create = 0; 978 g_test_multi_raids = 0; 979 } 980 981 static void 982 free_test_req(struct rpc_bdev_raid_create *r) 983 { 984 uint8_t i; 985 986 free(r->name); 987 for (i = 0; i < r->base_bdevs.num_base_bdevs; i++) { 988 free(r->base_bdevs.base_bdevs[i]); 989 } 990 } 991 992 static void 993 create_raid_bdev_delete_req(struct rpc_bdev_raid_delete *r, const char *raid_name, 994 uint8_t json_decode_obj_err) 995 { 996 r->name = strdup(raid_name); 997 SPDK_CU_ASSERT_FATAL(r->name != NULL); 998 999 g_rpc_req = r; 1000 g_rpc_req_size = sizeof(*r); 1001 g_rpc_err = 0; 1002 g_json_decode_obj_create = 0; 1003 g_json_decode_obj_err = json_decode_obj_err; 1004 g_config_level_create = 0; 1005 g_test_multi_raids = 0; 1006 } 1007 1008 static void 1009 create_get_raids_req(struct rpc_bdev_raid_get_bdevs *r, const char *category, 1010 uint8_t json_decode_obj_err) 1011 { 1012 r->category = strdup(category); 1013 SPDK_CU_ASSERT_FATAL(r->category != NULL); 1014 1015 g_rpc_req = r; 1016 g_rpc_req_size = sizeof(*r); 1017 g_rpc_err = 0; 1018 g_json_decode_obj_create = 0; 1019 g_json_decode_obj_err = json_decode_obj_err; 1020 g_config_level_create = 0; 1021 g_test_multi_raids = 1; 1022 g_get_raids_count = 0; 1023 } 1024 1025 static void 1026 test_create_raid(void) 1027 { 1028 struct rpc_bdev_raid_create req; 1029 struct rpc_bdev_raid_delete delete_req; 1030 1031 set_globals(); 1032 CU_ASSERT(raid_bdev_init() == 0); 1033 1034 verify_raid_bdev_present("raid1", false); 1035 create_raid_bdev_create_req(&req, "raid1", 0, true, 0); 1036 rpc_bdev_raid_create(NULL, NULL); 1037 CU_ASSERT(g_rpc_err == 0); 1038 verify_raid_bdev(&req, true, RAID_BDEV_STATE_ONLINE); 1039 free_test_req(&req); 1040 1041 create_raid_bdev_delete_req(&delete_req, "raid1", 0); 1042 rpc_bdev_raid_delete(NULL, NULL); 1043 CU_ASSERT(g_rpc_err == 0); 1044 raid_bdev_exit(); 1045 base_bdevs_cleanup(); 1046 reset_globals(); 1047 } 1048 1049 static void 1050 test_delete_raid(void) 1051 { 1052 struct rpc_bdev_raid_create construct_req; 1053 struct rpc_bdev_raid_delete delete_req; 1054 1055 set_globals(); 1056 CU_ASSERT(raid_bdev_init() == 0); 1057 1058 verify_raid_bdev_present("raid1", false); 1059 create_raid_bdev_create_req(&construct_req, "raid1", 0, true, 0); 1060 rpc_bdev_raid_create(NULL, NULL); 1061 CU_ASSERT(g_rpc_err == 0); 1062 verify_raid_bdev(&construct_req, true, RAID_BDEV_STATE_ONLINE); 1063 free_test_req(&construct_req); 1064 1065 create_raid_bdev_delete_req(&delete_req, "raid1", 0); 1066 rpc_bdev_raid_delete(NULL, NULL); 1067 CU_ASSERT(g_rpc_err == 0); 1068 verify_raid_bdev_present("raid1", false); 1069 1070 raid_bdev_exit(); 1071 base_bdevs_cleanup(); 1072 reset_globals(); 1073 } 1074 1075 static void 1076 test_create_raid_invalid_args(void) 1077 { 1078 struct rpc_bdev_raid_create req; 1079 struct rpc_bdev_raid_delete destroy_req; 1080 struct raid_bdev *raid_bdev; 1081 1082 set_globals(); 1083 CU_ASSERT(raid_bdev_init() == 0); 1084 1085 verify_raid_bdev_present("raid1", false); 1086 create_raid_bdev_create_req(&req, "raid1", 0, true, 0); 1087 req.level = INVALID_RAID_LEVEL; 1088 rpc_bdev_raid_create(NULL, NULL); 1089 CU_ASSERT(g_rpc_err == 1); 1090 free_test_req(&req); 1091 verify_raid_bdev_present("raid1", false); 1092 1093 create_raid_bdev_create_req(&req, "raid1", 0, false, 1); 1094 rpc_bdev_raid_create(NULL, NULL); 1095 CU_ASSERT(g_rpc_err == 1); 1096 free_test_req(&req); 1097 verify_raid_bdev_present("raid1", false); 1098 1099 create_raid_bdev_create_req(&req, "raid1", 0, false, 0); 1100 req.strip_size_kb = 1231; 1101 rpc_bdev_raid_create(NULL, NULL); 1102 CU_ASSERT(g_rpc_err == 1); 1103 free_test_req(&req); 1104 verify_raid_bdev_present("raid1", false); 1105 1106 create_raid_bdev_create_req(&req, "raid1", 0, false, 0); 1107 rpc_bdev_raid_create(NULL, NULL); 1108 CU_ASSERT(g_rpc_err == 0); 1109 verify_raid_bdev(&req, true, RAID_BDEV_STATE_ONLINE); 1110 free_test_req(&req); 1111 1112 create_raid_bdev_create_req(&req, "raid1", 0, false, 0); 1113 rpc_bdev_raid_create(NULL, NULL); 1114 CU_ASSERT(g_rpc_err == 1); 1115 free_test_req(&req); 1116 1117 create_raid_bdev_create_req(&req, "raid2", 0, false, 0); 1118 rpc_bdev_raid_create(NULL, NULL); 1119 CU_ASSERT(g_rpc_err == 1); 1120 free_test_req(&req); 1121 verify_raid_bdev_present("raid2", false); 1122 1123 create_raid_bdev_create_req(&req, "raid2", g_max_base_drives, true, 0); 1124 free(req.base_bdevs.base_bdevs[g_max_base_drives - 1]); 1125 req.base_bdevs.base_bdevs[g_max_base_drives - 1] = strdup("Nvme0n1"); 1126 SPDK_CU_ASSERT_FATAL(req.base_bdevs.base_bdevs[g_max_base_drives - 1] != NULL); 1127 rpc_bdev_raid_create(NULL, NULL); 1128 CU_ASSERT(g_rpc_err == 1); 1129 free_test_req(&req); 1130 verify_raid_bdev_present("raid2", false); 1131 1132 create_raid_bdev_create_req(&req, "raid2", g_max_base_drives, true, 0); 1133 free(req.base_bdevs.base_bdevs[g_max_base_drives - 1]); 1134 req.base_bdevs.base_bdevs[g_max_base_drives - 1] = strdup("Nvme100000n1"); 1135 SPDK_CU_ASSERT_FATAL(req.base_bdevs.base_bdevs[g_max_base_drives - 1] != NULL); 1136 rpc_bdev_raid_create(NULL, NULL); 1137 CU_ASSERT(g_rpc_err == 0); 1138 free_test_req(&req); 1139 verify_raid_bdev_present("raid2", true); 1140 raid_bdev = raid_bdev_find_by_name("raid2"); 1141 SPDK_CU_ASSERT_FATAL(raid_bdev != NULL); 1142 check_and_remove_raid_bdev(raid_bdev); 1143 1144 create_raid_bdev_create_req(&req, "raid2", g_max_base_drives, false, 0); 1145 rpc_bdev_raid_create(NULL, NULL); 1146 CU_ASSERT(g_rpc_err == 0); 1147 free_test_req(&req); 1148 verify_raid_bdev_present("raid2", true); 1149 verify_raid_bdev_present("raid1", true); 1150 1151 create_raid_bdev_delete_req(&destroy_req, "raid1", 0); 1152 rpc_bdev_raid_delete(NULL, NULL); 1153 create_raid_bdev_delete_req(&destroy_req, "raid2", 0); 1154 rpc_bdev_raid_delete(NULL, NULL); 1155 raid_bdev_exit(); 1156 base_bdevs_cleanup(); 1157 reset_globals(); 1158 } 1159 1160 static void 1161 test_delete_raid_invalid_args(void) 1162 { 1163 struct rpc_bdev_raid_create construct_req; 1164 struct rpc_bdev_raid_delete destroy_req; 1165 1166 set_globals(); 1167 CU_ASSERT(raid_bdev_init() == 0); 1168 1169 verify_raid_bdev_present("raid1", false); 1170 create_raid_bdev_create_req(&construct_req, "raid1", 0, true, 0); 1171 rpc_bdev_raid_create(NULL, NULL); 1172 CU_ASSERT(g_rpc_err == 0); 1173 verify_raid_bdev(&construct_req, true, RAID_BDEV_STATE_ONLINE); 1174 free_test_req(&construct_req); 1175 1176 create_raid_bdev_delete_req(&destroy_req, "raid2", 0); 1177 rpc_bdev_raid_delete(NULL, NULL); 1178 CU_ASSERT(g_rpc_err == 1); 1179 1180 create_raid_bdev_delete_req(&destroy_req, "raid1", 1); 1181 rpc_bdev_raid_delete(NULL, NULL); 1182 CU_ASSERT(g_rpc_err == 1); 1183 free(destroy_req.name); 1184 verify_raid_bdev_present("raid1", true); 1185 1186 create_raid_bdev_delete_req(&destroy_req, "raid1", 0); 1187 rpc_bdev_raid_delete(NULL, NULL); 1188 CU_ASSERT(g_rpc_err == 0); 1189 verify_raid_bdev_present("raid1", false); 1190 1191 raid_bdev_exit(); 1192 base_bdevs_cleanup(); 1193 reset_globals(); 1194 } 1195 1196 static void 1197 test_io_channel(void) 1198 { 1199 struct rpc_bdev_raid_create req; 1200 struct rpc_bdev_raid_delete destroy_req; 1201 struct raid_bdev *pbdev; 1202 struct raid_bdev_io_channel *ch_ctx; 1203 uint8_t i; 1204 1205 set_globals(); 1206 CU_ASSERT(raid_bdev_init() == 0); 1207 1208 create_raid_bdev_create_req(&req, "raid1", 0, true, 0); 1209 verify_raid_bdev_present("raid1", false); 1210 rpc_bdev_raid_create(NULL, NULL); 1211 CU_ASSERT(g_rpc_err == 0); 1212 verify_raid_bdev(&req, true, RAID_BDEV_STATE_ONLINE); 1213 1214 TAILQ_FOREACH(pbdev, &g_raid_bdev_list, global_link) { 1215 if (strcmp(pbdev->bdev.name, "raid1") == 0) { 1216 break; 1217 } 1218 } 1219 CU_ASSERT(pbdev != NULL); 1220 ch_ctx = calloc(1, sizeof(struct raid_bdev_io_channel)); 1221 SPDK_CU_ASSERT_FATAL(ch_ctx != NULL); 1222 1223 CU_ASSERT(raid_bdev_create_cb(pbdev, ch_ctx) == 0); 1224 for (i = 0; i < req.base_bdevs.num_base_bdevs; i++) { 1225 CU_ASSERT(ch_ctx->base_channel && ch_ctx->base_channel[i] == &g_io_channel); 1226 } 1227 raid_bdev_destroy_cb(pbdev, ch_ctx); 1228 CU_ASSERT(ch_ctx->base_channel == NULL); 1229 free_test_req(&req); 1230 1231 create_raid_bdev_delete_req(&destroy_req, "raid1", 0); 1232 rpc_bdev_raid_delete(NULL, NULL); 1233 CU_ASSERT(g_rpc_err == 0); 1234 verify_raid_bdev_present("raid1", false); 1235 1236 free(ch_ctx); 1237 raid_bdev_exit(); 1238 base_bdevs_cleanup(); 1239 reset_globals(); 1240 } 1241 1242 static void 1243 test_write_io(void) 1244 { 1245 struct rpc_bdev_raid_create req; 1246 struct rpc_bdev_raid_delete destroy_req; 1247 struct raid_bdev *pbdev; 1248 struct spdk_io_channel *ch; 1249 struct raid_bdev_io_channel *ch_ctx; 1250 uint8_t i; 1251 struct spdk_bdev_io *bdev_io; 1252 uint64_t io_len; 1253 uint64_t lba = 0; 1254 struct spdk_io_channel *ch_b; 1255 struct spdk_bdev_channel *ch_b_ctx; 1256 1257 set_globals(); 1258 CU_ASSERT(raid_bdev_init() == 0); 1259 1260 create_raid_bdev_create_req(&req, "raid1", 0, true, 0); 1261 verify_raid_bdev_present("raid1", false); 1262 rpc_bdev_raid_create(NULL, NULL); 1263 CU_ASSERT(g_rpc_err == 0); 1264 verify_raid_bdev(&req, true, RAID_BDEV_STATE_ONLINE); 1265 TAILQ_FOREACH(pbdev, &g_raid_bdev_list, global_link) { 1266 if (strcmp(pbdev->bdev.name, "raid1") == 0) { 1267 break; 1268 } 1269 } 1270 CU_ASSERT(pbdev != NULL); 1271 ch = calloc(1, sizeof(struct spdk_io_channel) + sizeof(struct raid_bdev_io_channel)); 1272 SPDK_CU_ASSERT_FATAL(ch != NULL); 1273 1274 ch_b = calloc(1, sizeof(struct spdk_io_channel) + sizeof(struct spdk_bdev_channel)); 1275 SPDK_CU_ASSERT_FATAL(ch_b != NULL); 1276 ch_b_ctx = spdk_io_channel_get_ctx(ch_b); 1277 ch_b_ctx->channel = ch; 1278 1279 ch_ctx = spdk_io_channel_get_ctx(ch); 1280 SPDK_CU_ASSERT_FATAL(ch_ctx != NULL); 1281 1282 CU_ASSERT(raid_bdev_create_cb(pbdev, ch_ctx) == 0); 1283 for (i = 0; i < req.base_bdevs.num_base_bdevs; i++) { 1284 CU_ASSERT(ch_ctx->base_channel && ch_ctx->base_channel[i] == &g_io_channel); 1285 } 1286 1287 /* test 2 IO sizes based on global strip size set earlier */ 1288 for (i = 0; i < 2; i++) { 1289 bdev_io = calloc(1, sizeof(struct spdk_bdev_io) + sizeof(struct raid_bdev_io)); 1290 SPDK_CU_ASSERT_FATAL(bdev_io != NULL); 1291 io_len = (g_strip_size / 2) << i; 1292 bdev_io_initialize(bdev_io, ch_b, &pbdev->bdev, lba, io_len, SPDK_BDEV_IO_TYPE_WRITE); 1293 lba += g_strip_size; 1294 memset(g_io_output, 0, ((g_max_io_size / g_strip_size) + 1) * sizeof(struct io_output)); 1295 g_io_output_index = 0; 1296 raid_bdev_submit_request(ch, bdev_io); 1297 verify_io(bdev_io, req.base_bdevs.num_base_bdevs, ch_ctx, pbdev, 1298 g_child_io_status_flag); 1299 bdev_io_cleanup(bdev_io); 1300 } 1301 1302 free_test_req(&req); 1303 raid_bdev_destroy_cb(pbdev, ch_ctx); 1304 CU_ASSERT(ch_ctx->base_channel == NULL); 1305 free(ch); 1306 free(ch_b); 1307 create_raid_bdev_delete_req(&destroy_req, "raid1", 0); 1308 rpc_bdev_raid_delete(NULL, NULL); 1309 CU_ASSERT(g_rpc_err == 0); 1310 verify_raid_bdev_present("raid1", false); 1311 1312 raid_bdev_exit(); 1313 base_bdevs_cleanup(); 1314 reset_globals(); 1315 } 1316 1317 static void 1318 test_read_io(void) 1319 { 1320 struct rpc_bdev_raid_create req; 1321 struct rpc_bdev_raid_delete destroy_req; 1322 struct raid_bdev *pbdev; 1323 struct spdk_io_channel *ch; 1324 struct raid_bdev_io_channel *ch_ctx; 1325 uint8_t i; 1326 struct spdk_bdev_io *bdev_io; 1327 uint64_t io_len; 1328 uint64_t lba; 1329 struct spdk_io_channel *ch_b; 1330 struct spdk_bdev_channel *ch_b_ctx; 1331 1332 set_globals(); 1333 CU_ASSERT(raid_bdev_init() == 0); 1334 1335 verify_raid_bdev_present("raid1", false); 1336 create_raid_bdev_create_req(&req, "raid1", 0, true, 0); 1337 rpc_bdev_raid_create(NULL, NULL); 1338 CU_ASSERT(g_rpc_err == 0); 1339 verify_raid_bdev(&req, true, RAID_BDEV_STATE_ONLINE); 1340 TAILQ_FOREACH(pbdev, &g_raid_bdev_list, global_link) { 1341 if (strcmp(pbdev->bdev.name, "raid1") == 0) { 1342 break; 1343 } 1344 } 1345 CU_ASSERT(pbdev != NULL); 1346 ch = calloc(1, sizeof(struct spdk_io_channel) + sizeof(struct raid_bdev_io_channel)); 1347 SPDK_CU_ASSERT_FATAL(ch != NULL); 1348 1349 ch_b = calloc(1, sizeof(struct spdk_io_channel) + sizeof(struct spdk_bdev_channel)); 1350 SPDK_CU_ASSERT_FATAL(ch_b != NULL); 1351 ch_b_ctx = spdk_io_channel_get_ctx(ch_b); 1352 ch_b_ctx->channel = ch; 1353 1354 ch_ctx = spdk_io_channel_get_ctx(ch); 1355 SPDK_CU_ASSERT_FATAL(ch_ctx != NULL); 1356 1357 CU_ASSERT(raid_bdev_create_cb(pbdev, ch_ctx) == 0); 1358 for (i = 0; i < req.base_bdevs.num_base_bdevs; i++) { 1359 CU_ASSERT(ch_ctx->base_channel && ch_ctx->base_channel[i] == &g_io_channel); 1360 } 1361 free_test_req(&req); 1362 1363 /* test 2 IO sizes based on global strip size set earlier */ 1364 lba = 0; 1365 for (i = 0; i < 2; i++) { 1366 bdev_io = calloc(1, sizeof(struct spdk_bdev_io) + sizeof(struct raid_bdev_io)); 1367 SPDK_CU_ASSERT_FATAL(bdev_io != NULL); 1368 io_len = (g_strip_size / 2) << i; 1369 bdev_io_initialize(bdev_io, ch_b, &pbdev->bdev, lba, io_len, SPDK_BDEV_IO_TYPE_READ); 1370 lba += g_strip_size; 1371 memset(g_io_output, 0, ((g_max_io_size / g_strip_size) + 1) * sizeof(struct io_output)); 1372 g_io_output_index = 0; 1373 raid_bdev_submit_request(ch, bdev_io); 1374 verify_io(bdev_io, req.base_bdevs.num_base_bdevs, ch_ctx, pbdev, 1375 g_child_io_status_flag); 1376 bdev_io_cleanup(bdev_io); 1377 } 1378 1379 raid_bdev_destroy_cb(pbdev, ch_ctx); 1380 CU_ASSERT(ch_ctx->base_channel == NULL); 1381 free(ch); 1382 free(ch_b); 1383 create_raid_bdev_delete_req(&destroy_req, "raid1", 0); 1384 rpc_bdev_raid_delete(NULL, NULL); 1385 CU_ASSERT(g_rpc_err == 0); 1386 verify_raid_bdev_present("raid1", false); 1387 1388 raid_bdev_exit(); 1389 base_bdevs_cleanup(); 1390 reset_globals(); 1391 } 1392 1393 static void 1394 raid_bdev_io_generate_by_strips(uint64_t n_strips) 1395 { 1396 uint64_t lba; 1397 uint64_t nblocks; 1398 uint64_t start_offset; 1399 uint64_t end_offset; 1400 uint64_t offsets_in_strip[3]; 1401 uint64_t start_bdev_idx; 1402 uint64_t start_bdev_offset; 1403 uint64_t start_bdev_idxs[3]; 1404 int i, j, l; 1405 1406 /* 3 different situations of offset in strip */ 1407 offsets_in_strip[0] = 0; 1408 offsets_in_strip[1] = g_strip_size >> 1; 1409 offsets_in_strip[2] = g_strip_size - 1; 1410 1411 /* 3 different situations of start_bdev_idx */ 1412 start_bdev_idxs[0] = 0; 1413 start_bdev_idxs[1] = g_max_base_drives >> 1; 1414 start_bdev_idxs[2] = g_max_base_drives - 1; 1415 1416 /* consider different offset in strip */ 1417 for (i = 0; i < 3; i++) { 1418 start_offset = offsets_in_strip[i]; 1419 for (j = 0; j < 3; j++) { 1420 end_offset = offsets_in_strip[j]; 1421 if (n_strips == 1 && start_offset > end_offset) { 1422 continue; 1423 } 1424 1425 /* consider at which base_bdev lba is started. */ 1426 for (l = 0; l < 3; l++) { 1427 start_bdev_idx = start_bdev_idxs[l]; 1428 start_bdev_offset = start_bdev_idx * g_strip_size; 1429 lba = g_lba_offset + start_bdev_offset + start_offset; 1430 nblocks = (n_strips - 1) * g_strip_size + end_offset - start_offset + 1; 1431 1432 g_io_ranges[g_io_range_idx].lba = lba; 1433 g_io_ranges[g_io_range_idx].nblocks = nblocks; 1434 1435 SPDK_CU_ASSERT_FATAL(g_io_range_idx < MAX_TEST_IO_RANGE); 1436 g_io_range_idx++; 1437 } 1438 } 1439 } 1440 } 1441 1442 static void 1443 raid_bdev_io_generate(void) 1444 { 1445 uint64_t n_strips; 1446 uint64_t n_strips_span = g_max_base_drives; 1447 uint64_t n_strips_times[5] = {g_max_base_drives + 1, g_max_base_drives * 2 - 1, 1448 g_max_base_drives * 2, g_max_base_drives * 3, 1449 g_max_base_drives * 4 1450 }; 1451 uint32_t i; 1452 1453 g_io_range_idx = 0; 1454 1455 /* consider different number of strips from 1 to strips spanned base bdevs, 1456 * and even to times of strips spanned base bdevs 1457 */ 1458 for (n_strips = 1; n_strips < n_strips_span; n_strips++) { 1459 raid_bdev_io_generate_by_strips(n_strips); 1460 } 1461 1462 for (i = 0; i < SPDK_COUNTOF(n_strips_times); i++) { 1463 n_strips = n_strips_times[i]; 1464 raid_bdev_io_generate_by_strips(n_strips); 1465 } 1466 } 1467 1468 static void 1469 test_unmap_io(void) 1470 { 1471 struct rpc_bdev_raid_create req; 1472 struct rpc_bdev_raid_delete destroy_req; 1473 struct raid_bdev *pbdev; 1474 struct spdk_io_channel *ch; 1475 struct raid_bdev_io_channel *ch_ctx; 1476 uint8_t i; 1477 struct spdk_bdev_io *bdev_io; 1478 uint32_t count; 1479 uint64_t io_len; 1480 uint64_t lba; 1481 1482 set_globals(); 1483 CU_ASSERT(raid_bdev_init() == 0); 1484 1485 verify_raid_bdev_present("raid1", false); 1486 create_raid_bdev_create_req(&req, "raid1", 0, true, 0); 1487 rpc_bdev_raid_create(NULL, NULL); 1488 CU_ASSERT(g_rpc_err == 0); 1489 verify_raid_bdev(&req, true, RAID_BDEV_STATE_ONLINE); 1490 TAILQ_FOREACH(pbdev, &g_raid_bdev_list, global_link) { 1491 if (strcmp(pbdev->bdev.name, "raid1") == 0) { 1492 break; 1493 } 1494 } 1495 CU_ASSERT(pbdev != NULL); 1496 ch = calloc(1, sizeof(struct spdk_io_channel) + sizeof(struct raid_bdev_io_channel)); 1497 SPDK_CU_ASSERT_FATAL(ch != NULL); 1498 ch_ctx = spdk_io_channel_get_ctx(ch); 1499 SPDK_CU_ASSERT_FATAL(ch_ctx != NULL); 1500 1501 CU_ASSERT(raid_bdev_create_cb(pbdev, ch_ctx) == 0); 1502 for (i = 0; i < req.base_bdevs.num_base_bdevs; i++) { 1503 SPDK_CU_ASSERT_FATAL(ch_ctx->base_channel && ch_ctx->base_channel[i] == &g_io_channel); 1504 } 1505 1506 CU_ASSERT(raid_bdev_io_type_supported(pbdev, SPDK_BDEV_IO_TYPE_UNMAP) == true); 1507 CU_ASSERT(raid_bdev_io_type_supported(pbdev, SPDK_BDEV_IO_TYPE_FLUSH) == true); 1508 1509 raid_bdev_io_generate(); 1510 for (count = 0; count < g_io_range_idx; count++) { 1511 bdev_io = calloc(1, sizeof(struct spdk_bdev_io) + sizeof(struct raid_bdev_io)); 1512 SPDK_CU_ASSERT_FATAL(bdev_io != NULL); 1513 io_len = g_io_ranges[count].nblocks; 1514 lba = g_io_ranges[count].lba; 1515 bdev_io_initialize(bdev_io, ch, &pbdev->bdev, lba, io_len, SPDK_BDEV_IO_TYPE_UNMAP); 1516 memset(g_io_output, 0, g_max_base_drives * sizeof(struct io_output)); 1517 g_io_output_index = 0; 1518 raid_bdev_submit_request(ch, bdev_io); 1519 verify_io_without_payload(bdev_io, req.base_bdevs.num_base_bdevs, ch_ctx, pbdev, 1520 g_child_io_status_flag); 1521 bdev_io_cleanup(bdev_io); 1522 } 1523 free_test_req(&req); 1524 1525 raid_bdev_destroy_cb(pbdev, ch_ctx); 1526 CU_ASSERT(ch_ctx->base_channel == NULL); 1527 free(ch); 1528 create_raid_bdev_delete_req(&destroy_req, "raid1", 0); 1529 rpc_bdev_raid_delete(NULL, NULL); 1530 CU_ASSERT(g_rpc_err == 0); 1531 verify_raid_bdev_present("raid1", false); 1532 1533 raid_bdev_exit(); 1534 base_bdevs_cleanup(); 1535 reset_globals(); 1536 } 1537 1538 /* Test IO failures */ 1539 static void 1540 test_io_failure(void) 1541 { 1542 struct rpc_bdev_raid_create req; 1543 struct rpc_bdev_raid_delete destroy_req; 1544 struct raid_bdev *pbdev; 1545 struct spdk_io_channel *ch; 1546 struct raid_bdev_io_channel *ch_ctx; 1547 uint8_t i; 1548 struct spdk_bdev_io *bdev_io; 1549 uint32_t count; 1550 uint64_t io_len; 1551 uint64_t lba; 1552 1553 set_globals(); 1554 CU_ASSERT(raid_bdev_init() == 0); 1555 1556 verify_raid_bdev_present("raid1", false); 1557 create_raid_bdev_create_req(&req, "raid1", 0, true, 0); 1558 rpc_bdev_raid_create(NULL, NULL); 1559 CU_ASSERT(g_rpc_err == 0); 1560 verify_raid_bdev(&req, true, RAID_BDEV_STATE_ONLINE); 1561 TAILQ_FOREACH(pbdev, &g_raid_bdev_list, global_link) { 1562 if (strcmp(pbdev->bdev.name, req.name) == 0) { 1563 break; 1564 } 1565 } 1566 CU_ASSERT(pbdev != NULL); 1567 ch = calloc(1, sizeof(struct spdk_io_channel) + sizeof(struct raid_bdev_io_channel)); 1568 SPDK_CU_ASSERT_FATAL(ch != NULL); 1569 ch_ctx = spdk_io_channel_get_ctx(ch); 1570 SPDK_CU_ASSERT_FATAL(ch_ctx != NULL); 1571 1572 CU_ASSERT(raid_bdev_create_cb(pbdev, ch_ctx) == 0); 1573 for (i = 0; i < req.base_bdevs.num_base_bdevs; i++) { 1574 CU_ASSERT(ch_ctx->base_channel && ch_ctx->base_channel[i] == &g_io_channel); 1575 } 1576 free_test_req(&req); 1577 1578 lba = 0; 1579 for (count = 0; count < 1; count++) { 1580 bdev_io = calloc(1, sizeof(struct spdk_bdev_io) + sizeof(struct raid_bdev_io)); 1581 SPDK_CU_ASSERT_FATAL(bdev_io != NULL); 1582 io_len = (g_strip_size / 2) << count; 1583 bdev_io_initialize(bdev_io, ch, &pbdev->bdev, lba, io_len, SPDK_BDEV_IO_TYPE_INVALID); 1584 lba += g_strip_size; 1585 memset(g_io_output, 0, ((g_max_io_size / g_strip_size) + 1) * sizeof(struct io_output)); 1586 g_io_output_index = 0; 1587 raid_bdev_submit_request(ch, bdev_io); 1588 verify_io(bdev_io, req.base_bdevs.num_base_bdevs, ch_ctx, pbdev, 1589 INVALID_IO_SUBMIT); 1590 bdev_io_cleanup(bdev_io); 1591 } 1592 1593 1594 lba = 0; 1595 g_child_io_status_flag = false; 1596 for (count = 0; count < 1; count++) { 1597 bdev_io = calloc(1, sizeof(struct spdk_bdev_io) + sizeof(struct raid_bdev_io)); 1598 SPDK_CU_ASSERT_FATAL(bdev_io != NULL); 1599 io_len = (g_strip_size / 2) << count; 1600 bdev_io_initialize(bdev_io, ch, &pbdev->bdev, lba, io_len, SPDK_BDEV_IO_TYPE_WRITE); 1601 lba += g_strip_size; 1602 memset(g_io_output, 0, ((g_max_io_size / g_strip_size) + 1) * sizeof(struct io_output)); 1603 g_io_output_index = 0; 1604 raid_bdev_submit_request(ch, bdev_io); 1605 verify_io(bdev_io, req.base_bdevs.num_base_bdevs, ch_ctx, pbdev, 1606 g_child_io_status_flag); 1607 bdev_io_cleanup(bdev_io); 1608 } 1609 1610 raid_bdev_destroy_cb(pbdev, ch_ctx); 1611 CU_ASSERT(ch_ctx->base_channel == NULL); 1612 free(ch); 1613 create_raid_bdev_delete_req(&destroy_req, "raid1", 0); 1614 rpc_bdev_raid_delete(NULL, NULL); 1615 CU_ASSERT(g_rpc_err == 0); 1616 verify_raid_bdev_present("raid1", false); 1617 1618 raid_bdev_exit(); 1619 base_bdevs_cleanup(); 1620 reset_globals(); 1621 } 1622 1623 /* Test reset IO */ 1624 static void 1625 test_reset_io(void) 1626 { 1627 struct rpc_bdev_raid_create req; 1628 struct rpc_bdev_raid_delete destroy_req; 1629 struct raid_bdev *pbdev; 1630 struct spdk_io_channel *ch; 1631 struct raid_bdev_io_channel *ch_ctx; 1632 uint8_t i; 1633 struct spdk_bdev_io *bdev_io; 1634 1635 set_globals(); 1636 CU_ASSERT(raid_bdev_init() == 0); 1637 1638 verify_raid_bdev_present("raid1", false); 1639 create_raid_bdev_create_req(&req, "raid1", 0, true, 0); 1640 rpc_bdev_raid_create(NULL, NULL); 1641 CU_ASSERT(g_rpc_err == 0); 1642 verify_raid_bdev(&req, true, RAID_BDEV_STATE_ONLINE); 1643 TAILQ_FOREACH(pbdev, &g_raid_bdev_list, global_link) { 1644 if (strcmp(pbdev->bdev.name, "raid1") == 0) { 1645 break; 1646 } 1647 } 1648 CU_ASSERT(pbdev != NULL); 1649 ch = calloc(1, sizeof(struct spdk_io_channel) + sizeof(struct raid_bdev_io_channel)); 1650 SPDK_CU_ASSERT_FATAL(ch != NULL); 1651 ch_ctx = spdk_io_channel_get_ctx(ch); 1652 SPDK_CU_ASSERT_FATAL(ch_ctx != NULL); 1653 1654 SPDK_CU_ASSERT_FATAL(raid_bdev_create_cb(pbdev, ch_ctx) == 0); 1655 for (i = 0; i < req.base_bdevs.num_base_bdevs; i++) { 1656 CU_ASSERT(ch_ctx->base_channel && ch_ctx->base_channel[i] == &g_io_channel); 1657 } 1658 free_test_req(&req); 1659 1660 g_bdev_io_submit_status = 0; 1661 g_child_io_status_flag = true; 1662 1663 CU_ASSERT(raid_bdev_io_type_supported(pbdev, SPDK_BDEV_IO_TYPE_RESET) == true); 1664 1665 bdev_io = calloc(1, sizeof(struct spdk_bdev_io) + sizeof(struct raid_bdev_io)); 1666 SPDK_CU_ASSERT_FATAL(bdev_io != NULL); 1667 bdev_io_initialize(bdev_io, ch, &pbdev->bdev, 0, 1, SPDK_BDEV_IO_TYPE_RESET); 1668 memset(g_io_output, 0, g_max_base_drives * sizeof(struct io_output)); 1669 g_io_output_index = 0; 1670 raid_bdev_submit_request(ch, bdev_io); 1671 verify_reset_io(bdev_io, req.base_bdevs.num_base_bdevs, ch_ctx, pbdev, 1672 true); 1673 bdev_io_cleanup(bdev_io); 1674 1675 raid_bdev_destroy_cb(pbdev, ch_ctx); 1676 CU_ASSERT(ch_ctx->base_channel == NULL); 1677 free(ch); 1678 create_raid_bdev_delete_req(&destroy_req, "raid1", 0); 1679 rpc_bdev_raid_delete(NULL, NULL); 1680 CU_ASSERT(g_rpc_err == 0); 1681 verify_raid_bdev_present("raid1", false); 1682 1683 raid_bdev_exit(); 1684 base_bdevs_cleanup(); 1685 reset_globals(); 1686 } 1687 1688 /* Create multiple raids, destroy raids without IO, get_raids related tests */ 1689 static void 1690 test_multi_raid_no_io(void) 1691 { 1692 struct rpc_bdev_raid_create *construct_req; 1693 struct rpc_bdev_raid_delete destroy_req; 1694 struct rpc_bdev_raid_get_bdevs get_raids_req; 1695 uint8_t i; 1696 char name[16]; 1697 uint8_t bbdev_idx = 0; 1698 1699 set_globals(); 1700 construct_req = calloc(MAX_RAIDS, sizeof(struct rpc_bdev_raid_create)); 1701 SPDK_CU_ASSERT_FATAL(construct_req != NULL); 1702 CU_ASSERT(raid_bdev_init() == 0); 1703 for (i = 0; i < g_max_raids; i++) { 1704 snprintf(name, 16, "%s%u", "raid", i); 1705 verify_raid_bdev_present(name, false); 1706 create_raid_bdev_create_req(&construct_req[i], name, bbdev_idx, true, 0); 1707 bbdev_idx += g_max_base_drives; 1708 rpc_bdev_raid_create(NULL, NULL); 1709 CU_ASSERT(g_rpc_err == 0); 1710 verify_raid_bdev(&construct_req[i], true, RAID_BDEV_STATE_ONLINE); 1711 } 1712 1713 create_get_raids_req(&get_raids_req, "all", 0); 1714 rpc_bdev_raid_get_bdevs(NULL, NULL); 1715 CU_ASSERT(g_rpc_err == 0); 1716 verify_get_raids(construct_req, g_max_raids, g_get_raids_output, g_get_raids_count); 1717 for (i = 0; i < g_get_raids_count; i++) { 1718 free(g_get_raids_output[i]); 1719 } 1720 1721 create_get_raids_req(&get_raids_req, "online", 0); 1722 rpc_bdev_raid_get_bdevs(NULL, NULL); 1723 CU_ASSERT(g_rpc_err == 0); 1724 verify_get_raids(construct_req, g_max_raids, g_get_raids_output, g_get_raids_count); 1725 for (i = 0; i < g_get_raids_count; i++) { 1726 free(g_get_raids_output[i]); 1727 } 1728 1729 create_get_raids_req(&get_raids_req, "configuring", 0); 1730 rpc_bdev_raid_get_bdevs(NULL, NULL); 1731 CU_ASSERT(g_rpc_err == 0); 1732 CU_ASSERT(g_get_raids_count == 0); 1733 1734 create_get_raids_req(&get_raids_req, "offline", 0); 1735 rpc_bdev_raid_get_bdevs(NULL, NULL); 1736 CU_ASSERT(g_rpc_err == 0); 1737 CU_ASSERT(g_get_raids_count == 0); 1738 1739 create_get_raids_req(&get_raids_req, "invalid_category", 0); 1740 rpc_bdev_raid_get_bdevs(NULL, NULL); 1741 CU_ASSERT(g_rpc_err == 1); 1742 CU_ASSERT(g_get_raids_count == 0); 1743 1744 create_get_raids_req(&get_raids_req, "all", 1); 1745 rpc_bdev_raid_get_bdevs(NULL, NULL); 1746 CU_ASSERT(g_rpc_err == 1); 1747 free(get_raids_req.category); 1748 CU_ASSERT(g_get_raids_count == 0); 1749 1750 create_get_raids_req(&get_raids_req, "all", 0); 1751 rpc_bdev_raid_get_bdevs(NULL, NULL); 1752 CU_ASSERT(g_rpc_err == 0); 1753 CU_ASSERT(g_get_raids_count == g_max_raids); 1754 for (i = 0; i < g_get_raids_count; i++) { 1755 free(g_get_raids_output[i]); 1756 } 1757 1758 for (i = 0; i < g_max_raids; i++) { 1759 SPDK_CU_ASSERT_FATAL(construct_req[i].name != NULL); 1760 snprintf(name, 16, "%s", construct_req[i].name); 1761 create_raid_bdev_delete_req(&destroy_req, name, 0); 1762 rpc_bdev_raid_delete(NULL, NULL); 1763 CU_ASSERT(g_rpc_err == 0); 1764 verify_raid_bdev_present(name, false); 1765 } 1766 raid_bdev_exit(); 1767 for (i = 0; i < g_max_raids; i++) { 1768 free_test_req(&construct_req[i]); 1769 } 1770 free(construct_req); 1771 base_bdevs_cleanup(); 1772 reset_globals(); 1773 } 1774 1775 /* Create multiple raids, fire IOs on raids */ 1776 static void 1777 test_multi_raid_with_io(void) 1778 { 1779 struct rpc_bdev_raid_create *construct_req; 1780 struct rpc_bdev_raid_delete destroy_req; 1781 uint8_t i, j; 1782 char name[16]; 1783 uint8_t bbdev_idx = 0; 1784 struct raid_bdev *pbdev; 1785 struct spdk_io_channel *ch; 1786 struct raid_bdev_io_channel *ch_ctx = NULL; 1787 struct spdk_bdev_io *bdev_io; 1788 uint64_t io_len; 1789 uint64_t lba = 0; 1790 int16_t iotype; 1791 struct spdk_io_channel *ch_b; 1792 struct spdk_bdev_channel *ch_b_ctx; 1793 1794 set_globals(); 1795 construct_req = calloc(g_max_raids, sizeof(struct rpc_bdev_raid_create)); 1796 SPDK_CU_ASSERT_FATAL(construct_req != NULL); 1797 CU_ASSERT(raid_bdev_init() == 0); 1798 ch = calloc(g_max_raids, sizeof(struct spdk_io_channel) + sizeof(struct raid_bdev_io_channel)); 1799 SPDK_CU_ASSERT_FATAL(ch != NULL); 1800 1801 ch_b = calloc(1, sizeof(struct spdk_io_channel) + sizeof(struct spdk_bdev_channel)); 1802 SPDK_CU_ASSERT_FATAL(ch_b != NULL); 1803 ch_b_ctx = spdk_io_channel_get_ctx(ch_b); 1804 ch_b_ctx->channel = ch; 1805 1806 for (i = 0; i < g_max_raids; i++) { 1807 snprintf(name, 16, "%s%u", "raid", i); 1808 verify_raid_bdev_present(name, false); 1809 create_raid_bdev_create_req(&construct_req[i], name, bbdev_idx, true, 0); 1810 bbdev_idx += g_max_base_drives; 1811 rpc_bdev_raid_create(NULL, NULL); 1812 CU_ASSERT(g_rpc_err == 0); 1813 verify_raid_bdev(&construct_req[i], true, RAID_BDEV_STATE_ONLINE); 1814 TAILQ_FOREACH(pbdev, &g_raid_bdev_list, global_link) { 1815 if (strcmp(pbdev->bdev.name, construct_req[i].name) == 0) { 1816 break; 1817 } 1818 } 1819 CU_ASSERT(pbdev != NULL); 1820 ch_ctx = spdk_io_channel_get_ctx(&ch[i]); 1821 SPDK_CU_ASSERT_FATAL(ch_ctx != NULL); 1822 CU_ASSERT(raid_bdev_create_cb(pbdev, ch_ctx) == 0); 1823 SPDK_CU_ASSERT_FATAL(ch_ctx->base_channel != NULL); 1824 for (j = 0; j < construct_req[i].base_bdevs.num_base_bdevs; j++) { 1825 CU_ASSERT(ch_ctx->base_channel[j] == &g_io_channel); 1826 } 1827 } 1828 1829 /* This will perform a write on the first raid and a read on the second. It can be 1830 * expanded in the future to perform r/w on each raid device in the event that 1831 * multiple raid levels are supported. 1832 */ 1833 for (i = 0; i < g_max_raids; i++) { 1834 bdev_io = calloc(1, sizeof(struct spdk_bdev_io) + sizeof(struct raid_bdev_io)); 1835 SPDK_CU_ASSERT_FATAL(bdev_io != NULL); 1836 io_len = g_strip_size; 1837 iotype = (i) ? SPDK_BDEV_IO_TYPE_WRITE : SPDK_BDEV_IO_TYPE_READ; 1838 memset(g_io_output, 0, ((g_max_io_size / g_strip_size) + 1) * sizeof(struct io_output)); 1839 g_io_output_index = 0; 1840 TAILQ_FOREACH(pbdev, &g_raid_bdev_list, global_link) { 1841 if (strcmp(pbdev->bdev.name, construct_req[i].name) == 0) { 1842 break; 1843 } 1844 } 1845 bdev_io_initialize(bdev_io, ch_b, &pbdev->bdev, lba, io_len, iotype); 1846 CU_ASSERT(pbdev != NULL); 1847 raid_bdev_submit_request(ch, bdev_io); 1848 verify_io(bdev_io, g_max_base_drives, ch_ctx, pbdev, 1849 g_child_io_status_flag); 1850 bdev_io_cleanup(bdev_io); 1851 } 1852 1853 for (i = 0; i < g_max_raids; i++) { 1854 TAILQ_FOREACH(pbdev, &g_raid_bdev_list, global_link) { 1855 if (strcmp(pbdev->bdev.name, construct_req[i].name) == 0) { 1856 break; 1857 } 1858 } 1859 CU_ASSERT(pbdev != NULL); 1860 ch_ctx = spdk_io_channel_get_ctx(&ch[i]); 1861 SPDK_CU_ASSERT_FATAL(ch_ctx != NULL); 1862 raid_bdev_destroy_cb(pbdev, ch_ctx); 1863 CU_ASSERT(ch_ctx->base_channel == NULL); 1864 snprintf(name, 16, "%s", construct_req[i].name); 1865 create_raid_bdev_delete_req(&destroy_req, name, 0); 1866 rpc_bdev_raid_delete(NULL, NULL); 1867 CU_ASSERT(g_rpc_err == 0); 1868 verify_raid_bdev_present(name, false); 1869 } 1870 raid_bdev_exit(); 1871 for (i = 0; i < g_max_raids; i++) { 1872 free_test_req(&construct_req[i]); 1873 } 1874 free(construct_req); 1875 free(ch); 1876 free(ch_b); 1877 base_bdevs_cleanup(); 1878 reset_globals(); 1879 } 1880 1881 static void 1882 test_io_type_supported(void) 1883 { 1884 CU_ASSERT(raid_bdev_io_type_supported(NULL, SPDK_BDEV_IO_TYPE_READ) == true); 1885 CU_ASSERT(raid_bdev_io_type_supported(NULL, SPDK_BDEV_IO_TYPE_WRITE) == true); 1886 CU_ASSERT(raid_bdev_io_type_supported(NULL, SPDK_BDEV_IO_TYPE_INVALID) == false); 1887 } 1888 1889 static void 1890 test_raid_json_dump_info(void) 1891 { 1892 struct rpc_bdev_raid_create req; 1893 struct rpc_bdev_raid_delete destroy_req; 1894 struct raid_bdev *pbdev; 1895 1896 set_globals(); 1897 CU_ASSERT(raid_bdev_init() == 0); 1898 1899 verify_raid_bdev_present("raid1", false); 1900 create_raid_bdev_create_req(&req, "raid1", 0, true, 0); 1901 rpc_bdev_raid_create(NULL, NULL); 1902 CU_ASSERT(g_rpc_err == 0); 1903 verify_raid_bdev(&req, true, RAID_BDEV_STATE_ONLINE); 1904 1905 TAILQ_FOREACH(pbdev, &g_raid_bdev_list, global_link) { 1906 if (strcmp(pbdev->bdev.name, "raid1") == 0) { 1907 break; 1908 } 1909 } 1910 CU_ASSERT(pbdev != NULL); 1911 1912 CU_ASSERT(raid_bdev_dump_info_json(pbdev, NULL) == 0); 1913 1914 free_test_req(&req); 1915 1916 create_raid_bdev_delete_req(&destroy_req, "raid1", 0); 1917 rpc_bdev_raid_delete(NULL, NULL); 1918 CU_ASSERT(g_rpc_err == 0); 1919 verify_raid_bdev_present("raid1", false); 1920 1921 raid_bdev_exit(); 1922 base_bdevs_cleanup(); 1923 reset_globals(); 1924 } 1925 1926 static void 1927 test_context_size(void) 1928 { 1929 CU_ASSERT(raid_bdev_get_ctx_size() == sizeof(struct raid_bdev_io)); 1930 } 1931 1932 static void 1933 test_raid_level_conversions(void) 1934 { 1935 const char *raid_str; 1936 1937 CU_ASSERT(raid_bdev_str_to_level("abcd123") == INVALID_RAID_LEVEL); 1938 CU_ASSERT(raid_bdev_str_to_level("0") == RAID0); 1939 CU_ASSERT(raid_bdev_str_to_level("raid0") == RAID0); 1940 CU_ASSERT(raid_bdev_str_to_level("RAID0") == RAID0); 1941 1942 raid_str = raid_bdev_level_to_str(INVALID_RAID_LEVEL); 1943 CU_ASSERT(raid_str != NULL && strlen(raid_str) == 0); 1944 raid_str = raid_bdev_level_to_str(1234); 1945 CU_ASSERT(raid_str != NULL && strlen(raid_str) == 0); 1946 raid_str = raid_bdev_level_to_str(RAID0); 1947 CU_ASSERT(raid_str != NULL && strcmp(raid_str, "raid0") == 0); 1948 } 1949 1950 int 1951 main(int argc, char **argv) 1952 { 1953 CU_pSuite suite = NULL; 1954 unsigned int num_failures; 1955 1956 CU_set_error_action(CUEA_ABORT); 1957 CU_initialize_registry(); 1958 1959 suite = CU_add_suite("raid", NULL, NULL); 1960 1961 CU_ADD_TEST(suite, test_create_raid); 1962 CU_ADD_TEST(suite, test_delete_raid); 1963 CU_ADD_TEST(suite, test_create_raid_invalid_args); 1964 CU_ADD_TEST(suite, test_delete_raid_invalid_args); 1965 CU_ADD_TEST(suite, test_io_channel); 1966 CU_ADD_TEST(suite, test_reset_io); 1967 CU_ADD_TEST(suite, test_write_io); 1968 CU_ADD_TEST(suite, test_read_io); 1969 CU_ADD_TEST(suite, test_unmap_io); 1970 CU_ADD_TEST(suite, test_io_failure); 1971 CU_ADD_TEST(suite, test_multi_raid_no_io); 1972 CU_ADD_TEST(suite, test_multi_raid_with_io); 1973 CU_ADD_TEST(suite, test_io_type_supported); 1974 CU_ADD_TEST(suite, test_raid_json_dump_info); 1975 CU_ADD_TEST(suite, test_context_size); 1976 CU_ADD_TEST(suite, test_raid_level_conversions); 1977 1978 allocate_threads(1); 1979 set_thread(0); 1980 1981 CU_basic_set_mode(CU_BRM_VERBOSE); 1982 set_test_opts(); 1983 CU_basic_run_tests(); 1984 num_failures = CU_get_number_of_failures(); 1985 CU_cleanup_registry(); 1986 1987 free_threads(); 1988 1989 return num_failures; 1990 } 1991