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