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