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