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