]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/test/lib/util/io_channel/io_channel_ut.c
bump version to 12.2.12-pve1
[ceph.git] / ceph / src / spdk / test / lib / util / io_channel / io_channel_ut.c
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 <stdint.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include "spdk_cunit.h"
39
40 #include "util/io_channel.c"
41
42 static void
43 thread_alloc(void)
44 {
45 spdk_allocate_thread();
46 spdk_free_thread();
47 }
48
49 static uint64_t device1;
50 static uint64_t device2;
51 static uint64_t device3;
52
53 static uint64_t ctx1 = 0x1111;
54 static uint64_t ctx2 = 0x2222;
55
56 static int g_create_cb_calls = 0;
57 static int g_destroy_cb_calls = 0;
58
59 static int
60 create_cb_1(void *io_device, uint32_t priority, void *ctx_buf, void *unique_ctx)
61 {
62 CU_ASSERT(io_device == &device1);
63 CU_ASSERT(priority == SPDK_IO_PRIORITY_DEFAULT);
64 *(uint64_t *)ctx_buf = ctx1;
65 g_create_cb_calls++;
66 return 0;
67 }
68
69 static void
70 destroy_cb_1(void *io_device, void *ctx_buf)
71 {
72 CU_ASSERT(io_device == &device1);
73 CU_ASSERT(*(uint64_t *)ctx_buf == ctx1);
74 g_destroy_cb_calls++;
75 }
76
77 static int
78 create_cb_2(void *io_device, uint32_t priority, void *ctx_buf, void *unique_ctx)
79 {
80 CU_ASSERT(io_device == &device2);
81 CU_ASSERT(priority == SPDK_IO_PRIORITY_DEFAULT);
82 *(uint64_t *)ctx_buf = ctx2;
83 g_create_cb_calls++;
84 if (unique_ctx != NULL) {
85 *(int *)unique_ctx = ~(*(int *)unique_ctx);
86 }
87 return 0;
88 }
89
90 static void
91 destroy_cb_2(void *io_device, void *ctx_buf)
92 {
93 CU_ASSERT(io_device == &device2);
94 CU_ASSERT(*(uint64_t *)ctx_buf == ctx2);
95 g_destroy_cb_calls++;
96 }
97
98 static int
99 create_cb_null(void *io_device, uint32_t priority, void *ctx_buf, void *unique_ctx)
100 {
101 return -1;
102 }
103
104 static void
105 channel(void)
106 {
107 struct spdk_io_channel *ch1, *ch2, *ch3;
108 int tmp;
109 void *ctx;
110
111 spdk_allocate_thread();
112 spdk_io_device_register(&device1, create_cb_1, destroy_cb_1, sizeof(ctx1));
113 spdk_io_device_register(&device2, create_cb_2, destroy_cb_2, sizeof(ctx2));
114 spdk_io_device_register(&device3, create_cb_null, NULL, 0);
115
116 g_create_cb_calls = 0;
117 ch1 = spdk_get_io_channel(&device1, SPDK_IO_PRIORITY_DEFAULT, false, NULL);
118 CU_ASSERT(g_create_cb_calls == 1);
119 SPDK_CU_ASSERT_FATAL(ch1 != NULL);
120
121 g_create_cb_calls = 0;
122 ch2 = spdk_get_io_channel(&device1, SPDK_IO_PRIORITY_DEFAULT, false, NULL);
123 CU_ASSERT(g_create_cb_calls == 0);
124 CU_ASSERT(ch1 == ch2);
125 SPDK_CU_ASSERT_FATAL(ch2 != NULL);
126
127 g_destroy_cb_calls = 0;
128 spdk_put_io_channel(ch2);
129 CU_ASSERT(g_destroy_cb_calls == 0);
130
131 g_create_cb_calls = 0;
132 ch2 = spdk_get_io_channel(&device2, SPDK_IO_PRIORITY_DEFAULT, false, NULL);
133 CU_ASSERT(g_create_cb_calls == 1);
134 CU_ASSERT(ch1 != ch2);
135 SPDK_CU_ASSERT_FATAL(ch2 != NULL);
136
137 ctx = spdk_io_channel_get_ctx(ch2);
138 CU_ASSERT(*(uint64_t *)ctx == ctx2);
139
140 /*
141 * Confirm that specifying unique==true will generate a new I/O channel,
142 * and reuse ch2.
143 */
144 g_create_cb_calls = 0;
145 tmp = 0x5a5a;
146 ch3 = spdk_get_io_channel(&device2, SPDK_IO_PRIORITY_DEFAULT, true, &tmp);
147 CU_ASSERT(g_create_cb_calls == 1);
148 CU_ASSERT(ch2 != ch3);
149 SPDK_CU_ASSERT_FATAL(ch3 != NULL);
150 CU_ASSERT(tmp == ~0x5a5a);
151
152 g_destroy_cb_calls = 0;
153 spdk_put_io_channel(ch1);
154 CU_ASSERT(g_destroy_cb_calls == 1);
155
156 g_destroy_cb_calls = 0;
157 spdk_put_io_channel(ch2);
158 CU_ASSERT(g_destroy_cb_calls == 1);
159
160 g_destroy_cb_calls = 0;
161 spdk_put_io_channel(ch3);
162 CU_ASSERT(g_destroy_cb_calls == 1);
163
164 ch1 = spdk_get_io_channel(&device3, SPDK_IO_PRIORITY_DEFAULT, false, NULL);
165 CU_ASSERT(ch1 == NULL);
166
167 /* Confirm failure if user specifies an invalid I/O priority. */
168 ch1 = spdk_get_io_channel(&device1, SPDK_IO_PRIORITY_DEFAULT + 1, false, NULL);
169 CU_ASSERT(ch1 == NULL);
170
171 /* Confirm failure if user specifies non-NULL unique_ctx for a shared channel. */
172 ch1 = spdk_get_io_channel(&device1, SPDK_IO_PRIORITY_DEFAULT, false, &tmp);
173 CU_ASSERT(ch1 == NULL);
174
175 spdk_io_device_unregister(&device1);
176 spdk_io_device_unregister(&device2);
177 spdk_io_device_unregister(&device3);
178 CU_ASSERT(TAILQ_EMPTY(&g_io_devices));
179 CU_ASSERT(TAILQ_EMPTY(&g_io_channels));
180 spdk_free_thread();
181 }
182
183 int
184 main(int argc, char **argv)
185 {
186 CU_pSuite suite = NULL;
187 unsigned int num_failures;
188
189 if (CU_initialize_registry() != CUE_SUCCESS) {
190 return CU_get_error();
191 }
192
193 suite = CU_add_suite("io_channel", NULL, NULL);
194 if (suite == NULL) {
195 CU_cleanup_registry();
196 return CU_get_error();
197 }
198
199 if (
200 CU_add_test(suite, "thread_alloc", thread_alloc) == NULL ||
201 CU_add_test(suite, "channel", channel) == NULL
202 ) {
203 CU_cleanup_registry();
204 return CU_get_error();
205 }
206
207 CU_basic_set_mode(CU_BRM_VERBOSE);
208 CU_basic_run_tests();
209 num_failures = CU_get_number_of_failures();
210 CU_cleanup_registry();
211 return num_failures;
212 }