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