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