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