xref: /spdk/test/unit/lib/bdev/mt/bdev.c/bdev_ut.c (revision 674c709733259a0b3763bc859bae4765169b3364)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk_cunit.h"
35 
36 #include "lib/test_env.c"
37 #include "lib/ut_multithread.c"
38 
39 /* HACK: disable VTune integration so the unit test doesn't need VTune headers and libs to build */
40 #undef SPDK_CONFIG_VTUNE
41 
42 #include "bdev.c"
43 
44 #define BDEV_UT_NUM_THREADS 3
45 
46 DEFINE_STUB_V(spdk_scsi_nvme_translate, (const struct spdk_bdev_io *bdev_io,
47 		int *sc, int *sk, int *asc, int *ascq));
48 
49 struct ut_bdev {
50 	struct spdk_bdev	bdev;
51 	int			io_target;
52 };
53 
54 struct ut_bdev g_bdev;
55 struct spdk_bdev_desc *g_desc;
56 
57 static int
58 stub_create_ch(void *io_device, void *ctx_buf)
59 {
60 	return 0;
61 }
62 
63 static void
64 stub_destroy_ch(void *io_device, void *ctx_buf)
65 {
66 }
67 
68 static struct spdk_io_channel *
69 stub_get_io_channel(void *ctx)
70 {
71 	return spdk_get_io_channel(&g_bdev.io_target);
72 }
73 
74 static int
75 stub_destruct(void *ctx)
76 {
77 	return 0;
78 }
79 
80 static struct spdk_bdev_fn_table fn_table = {
81 	.get_io_channel =	stub_get_io_channel,
82 	.destruct =		stub_destruct,
83 };
84 
85 static int
86 module_init(void)
87 {
88 	return 0;
89 }
90 
91 static void
92 module_fini(void)
93 {
94 }
95 
96 SPDK_BDEV_MODULE_REGISTER(bdev_ut, module_init, module_fini, NULL, NULL, NULL)
97 
98 static void
99 register_bdev(void)
100 {
101 	g_bdev.bdev.name = "bdev_ut";
102 	g_bdev.bdev.fn_table = &fn_table;
103 	g_bdev.bdev.module = SPDK_GET_BDEV_MODULE(bdev_ut);
104 
105 	spdk_io_device_register(&g_bdev.io_target, stub_create_ch, stub_destroy_ch, 0);
106 	spdk_bdev_register(&g_bdev.bdev);
107 }
108 
109 static void
110 unregister_bdev(void)
111 {
112 	/* Handle any deferred messages. */
113 	poll_threads();
114 	spdk_bdev_unregister(&g_bdev.bdev);
115 	spdk_io_device_unregister(&g_bdev.io_target, NULL);
116 	memset(&g_bdev, 0, sizeof(g_bdev));
117 }
118 
119 static void
120 bdev_init_cb(void *done, int rc)
121 {
122 	CU_ASSERT(rc == 0);
123 	*(bool *)done = true;
124 }
125 
126 static void
127 setup_test(void)
128 {
129 	bool done = false;
130 
131 	allocate_threads(BDEV_UT_NUM_THREADS);
132 	spdk_bdev_initialize(bdev_init_cb, &done, NULL, NULL);
133 	register_bdev();
134 	spdk_bdev_open(&g_bdev.bdev, true, NULL, NULL, &g_desc);
135 }
136 
137 static void
138 teardown_test(void)
139 {
140 	spdk_bdev_close(g_desc);
141 	g_desc = NULL;
142 	unregister_bdev();
143 	spdk_bdev_finish();
144 	free_threads();
145 }
146 
147 static void
148 test1(void)
149 {
150 	setup_test();
151 
152 	set_thread(0);
153 
154 	g_ut_threads[0].ch = spdk_bdev_get_io_channel(g_desc);
155 	spdk_put_io_channel(g_ut_threads[0].ch);
156 
157 	teardown_test();
158 }
159 
160 int
161 main(int argc, char **argv)
162 {
163 	CU_pSuite	suite = NULL;
164 	unsigned int	num_failures;
165 
166 	if (CU_initialize_registry() != CUE_SUCCESS) {
167 		return CU_get_error();
168 	}
169 
170 	suite = CU_add_suite("bdev", NULL, NULL);
171 	if (suite == NULL) {
172 		CU_cleanup_registry();
173 		return CU_get_error();
174 	}
175 
176 	if (
177 		CU_add_test(suite, "test1", test1) == NULL
178 	) {
179 		CU_cleanup_registry();
180 		return CU_get_error();
181 	}
182 
183 	CU_basic_set_mode(CU_BRM_VERBOSE);
184 	CU_basic_run_tests();
185 	num_failures = CU_get_number_of_failures();
186 	CU_cleanup_registry();
187 	return num_failures;
188 }
189