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