]>
git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/test/lib/util/io_channel/io_channel_ut.c
4 * Copyright (c) Intel Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
38 #include "spdk_cunit.h"
40 #include "util/io_channel.c"
45 spdk_allocate_thread();
49 static uint64_t device1
;
50 static uint64_t device2
;
51 static uint64_t device3
;
53 static uint64_t ctx1
= 0x1111;
54 static uint64_t ctx2
= 0x2222;
56 static int g_create_cb_calls
= 0;
57 static int g_destroy_cb_calls
= 0;
60 create_cb_1(void *io_device
, uint32_t priority
, void *ctx_buf
, void *unique_ctx
)
62 CU_ASSERT(io_device
== &device1
);
63 CU_ASSERT(priority
== SPDK_IO_PRIORITY_DEFAULT
);
64 *(uint64_t *)ctx_buf
= ctx1
;
70 destroy_cb_1(void *io_device
, void *ctx_buf
)
72 CU_ASSERT(io_device
== &device1
);
73 CU_ASSERT(*(uint64_t *)ctx_buf
== ctx1
);
78 create_cb_2(void *io_device
, uint32_t priority
, void *ctx_buf
, void *unique_ctx
)
80 CU_ASSERT(io_device
== &device2
);
81 CU_ASSERT(priority
== SPDK_IO_PRIORITY_DEFAULT
);
82 *(uint64_t *)ctx_buf
= ctx2
;
84 if (unique_ctx
!= NULL
) {
85 *(int *)unique_ctx
= ~(*(int *)unique_ctx
);
91 destroy_cb_2(void *io_device
, void *ctx_buf
)
93 CU_ASSERT(io_device
== &device2
);
94 CU_ASSERT(*(uint64_t *)ctx_buf
== ctx2
);
99 create_cb_null(void *io_device
, uint32_t priority
, void *ctx_buf
, void *unique_ctx
)
107 struct spdk_io_channel
*ch1
, *ch2
, *ch3
;
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);
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
);
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
);
127 g_destroy_cb_calls
= 0;
128 spdk_put_io_channel(ch2
);
129 CU_ASSERT(g_destroy_cb_calls
== 0);
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
);
137 ctx
= spdk_io_channel_get_ctx(ch2
);
138 CU_ASSERT(*(uint64_t *)ctx
== ctx2
);
141 * Confirm that specifying unique==true will generate a new I/O channel,
144 g_create_cb_calls
= 0;
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);
152 g_destroy_cb_calls
= 0;
153 spdk_put_io_channel(ch1
);
154 CU_ASSERT(g_destroy_cb_calls
== 1);
156 g_destroy_cb_calls
= 0;
157 spdk_put_io_channel(ch2
);
158 CU_ASSERT(g_destroy_cb_calls
== 1);
160 g_destroy_cb_calls
= 0;
161 spdk_put_io_channel(ch3
);
162 CU_ASSERT(g_destroy_cb_calls
== 1);
164 ch1
= spdk_get_io_channel(&device3
, SPDK_IO_PRIORITY_DEFAULT
, false, NULL
);
165 CU_ASSERT(ch1
== NULL
);
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
);
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
);
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
));
184 main(int argc
, char **argv
)
186 CU_pSuite suite
= NULL
;
187 unsigned int num_failures
;
189 if (CU_initialize_registry() != CUE_SUCCESS
) {
190 return CU_get_error();
193 suite
= CU_add_suite("io_channel", NULL
, NULL
);
195 CU_cleanup_registry();
196 return CU_get_error();
200 CU_add_test(suite
, "thread_alloc", thread_alloc
) == NULL
||
201 CU_add_test(suite
, "channel", channel
) == NULL
203 CU_cleanup_registry();
204 return CU_get_error();
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();