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 "common/lib/test_env.c" 37 #include "spdk_internal/mock.h" 38 #include "unit/lib/json_mock.c" 39 40 #include <rte_compressdev.h> 41 42 /* Those functions are defined as static inline in DPDK, so we can't 43 * mock them straight away. We use defines to redirect them into 44 * our custom functions. 45 */ 46 47 void __rte_experimental mock_rte_compressdev_info_get(uint8_t dev_id, 48 struct rte_compressdev_info *dev_info); 49 #define rte_compressdev_info_get mock_rte_compressdev_info_get 50 void __rte_experimental 51 mock_rte_compressdev_info_get(uint8_t dev_id, struct rte_compressdev_info *dev_info) 52 { 53 /* TODO return global struct */ 54 } 55 56 int __rte_experimental mock_rte_compressdev_configure(uint8_t dev_id, 57 struct rte_compressdev_config *config); 58 #define rte_compressdev_configure mock_rte_compressdev_configure 59 int __rte_experimental 60 mock_rte_compressdev_configure(uint8_t dev_id, struct rte_compressdev_config *config) 61 { 62 return 0; 63 } 64 65 int __rte_experimental mock_rte_compressdev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id, 66 uint32_t max_inflight_ops, int socket_id); 67 #define rte_compressdev_queue_pair_setup mock_rte_compressdev_queue_pair_setup 68 int __rte_experimental 69 mock_rte_compressdev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id, 70 uint32_t max_inflight_ops, int socket_id) 71 { 72 return 0; 73 } 74 75 int __rte_experimental mock_rte_compressdev_start(uint8_t dev_id); 76 #define rte_compressdev_start mock_rte_compressdev_start 77 int __rte_experimental 78 mock_rte_compressdev_start(uint8_t dev_id) 79 { 80 return 0; 81 } 82 83 int __rte_experimental mock_rte_compressdev_private_xform_create(uint8_t dev_id, 84 const struct rte_comp_xform *xform, void **private_xform); 85 #define rte_compressdev_private_xform_create mock_rte_compressdev_private_xform_create 86 int __rte_experimental 87 mock_rte_compressdev_private_xform_create(uint8_t dev_id, 88 const struct rte_comp_xform *xform, void **private_xform) 89 { 90 return 0; 91 } 92 93 uint8_t __rte_experimental mock_rte_compressdev_count(void); 94 #define rte_compressdev_count mock_rte_compressdev_count 95 uint8_t __rte_experimental 96 mock_rte_compressdev_count(void) 97 { 98 return 0; 99 } 100 101 struct rte_mempool *__rte_experimental mock_rte_comp_op_pool_create(const char *name, 102 unsigned int nb_elts, unsigned int cache_size, uint16_t user_size, 103 int socket_id); 104 #define rte_comp_op_pool_create mock_rte_comp_op_pool_create 105 struct rte_mempool *__rte_experimental 106 mock_rte_comp_op_pool_create(const char *name, unsigned int nb_elts, 107 unsigned int cache_size, uint16_t user_size, int socket_id) 108 { 109 return NULL; 110 } 111 112 #include "bdev/compress/vbdev_compress.c" 113 114 /* SPDK stubs */ 115 DEFINE_STUB_V(spdk_bdev_module_list_add, (struct spdk_bdev_module *bdev_module)); 116 DEFINE_STUB_V(spdk_bdev_free_io, (struct spdk_bdev_io *g_bdev_io)); 117 DEFINE_STUB(spdk_bdev_io_type_supported, bool, (struct spdk_bdev *bdev, 118 enum spdk_bdev_io_type io_type), 0); 119 DEFINE_STUB_V(spdk_bdev_module_release_bdev, (struct spdk_bdev *bdev)); 120 DEFINE_STUB_V(spdk_bdev_close, (struct spdk_bdev_desc *desc)); 121 DEFINE_STUB(spdk_bdev_get_name, const char *, (const struct spdk_bdev *bdev), 0); 122 DEFINE_STUB(spdk_env_get_current_core, uint32_t, (void), 0); 123 DEFINE_STUB(spdk_bdev_get_io_channel, struct spdk_io_channel *, (struct spdk_bdev_desc *desc), 0); 124 DEFINE_STUB_V(spdk_bdev_unregister, (struct spdk_bdev *bdev, spdk_bdev_unregister_cb cb_fn, 125 void *cb_arg)); 126 DEFINE_STUB(spdk_bdev_open, int, (struct spdk_bdev *bdev, bool write, 127 spdk_bdev_remove_cb_t remove_cb, 128 void *remove_ctx, struct spdk_bdev_desc **_desc), 0); 129 DEFINE_STUB(spdk_bdev_module_claim_bdev, int, (struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 130 struct spdk_bdev_module *module), 0); 131 DEFINE_STUB_V(spdk_bdev_module_examine_done, (struct spdk_bdev_module *module)); 132 DEFINE_STUB(spdk_bdev_register, int, (struct spdk_bdev *bdev), 0); 133 DEFINE_STUB(spdk_bdev_get_by_name, struct spdk_bdev *, (const char *bdev_name), NULL); 134 DEFINE_STUB(spdk_env_get_socket_id, uint32_t, (uint32_t core), 0); 135 DEFINE_STUB_V(spdk_reduce_vol_readv, (struct spdk_reduce_vol *vol, 136 struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length, 137 spdk_reduce_vol_op_complete cb_fn, void *cb_arg)); 138 DEFINE_STUB_V(spdk_reduce_vol_writev, (struct spdk_reduce_vol *vol, 139 struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length, 140 spdk_reduce_vol_op_complete cb_fn, void *cb_arg)); 141 DEFINE_STUB(spdk_bdev_io_get_io_channel, struct spdk_io_channel *, (struct spdk_bdev_io *bdev_io), 142 0); 143 DEFINE_STUB(spdk_bdev_queue_io_wait, int, (struct spdk_bdev *bdev, struct spdk_io_channel *ch, 144 struct spdk_bdev_io_wait_entry *entry), 0); 145 DEFINE_STUB_V(spdk_reduce_vol_unload, (struct spdk_reduce_vol *vol, 146 spdk_reduce_vol_op_complete cb_fn, void *cb_arg)); 147 DEFINE_STUB_V(spdk_reduce_vol_load, (struct spdk_reduce_backing_dev *backing_dev, 148 spdk_reduce_vol_op_with_handle_complete cb_fn, void *cb_arg)); 149 150 /* DPDK stubs */ 151 DEFINE_STUB(rte_socket_id, unsigned, (void), 0); 152 DEFINE_STUB(rte_eal_get_configuration, struct rte_config *, (void), NULL); 153 DEFINE_STUB(rte_vdev_init, int, (const char *name, const char *args), 0); 154 DEFINE_STUB_V(rte_mempool_free, (struct rte_mempool *mp)); 155 156 struct spdk_bdev_io *g_bdev_io; 157 struct spdk_io_channel *g_io_ch; 158 159 void 160 spdk_bdev_io_get_buf(struct spdk_bdev_io *bdev_io, spdk_bdev_io_get_buf_cb cb, uint64_t len) 161 { 162 cb(g_io_ch, g_bdev_io, true); 163 } 164 165 /* Mock these functions to call the callback and then return the value we require */ 166 int ut_spdk_bdev_readv_blocks = 0; 167 bool ut_spdk_bdev_readv_blocks_mocked = false; 168 int 169 spdk_bdev_readv_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 170 struct iovec *iov, int iovcnt, 171 uint64_t offset_blocks, uint64_t num_blocks, 172 spdk_bdev_io_completion_cb cb, void *cb_arg) 173 { 174 cb(g_bdev_io, !ut_spdk_bdev_readv_blocks, cb_arg); 175 return ut_spdk_bdev_readv_blocks; 176 } 177 178 int ut_spdk_bdev_writev_blocks = 0; 179 bool ut_spdk_bdev_writev_blocks_mocked = false; 180 int 181 spdk_bdev_writev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 182 struct iovec *iov, int iovcnt, 183 uint64_t offset_blocks, uint64_t num_blocks, 184 spdk_bdev_io_completion_cb cb, void *cb_arg) 185 { 186 cb(g_bdev_io, !ut_spdk_bdev_writev_blocks, cb_arg); 187 return ut_spdk_bdev_writev_blocks; 188 } 189 190 int ut_spdk_bdev_unmap_blocks = 0; 191 bool ut_spdk_bdev_unmap_blocks_mocked = false; 192 int 193 spdk_bdev_unmap_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 194 uint64_t offset_blocks, uint64_t num_blocks, 195 spdk_bdev_io_completion_cb cb, void *cb_arg) 196 { 197 cb(g_bdev_io, !ut_spdk_bdev_unmap_blocks, cb_arg); 198 return ut_spdk_bdev_unmap_blocks; 199 } 200 201 int ut_spdk_bdev_flush_blocks = 0; 202 bool ut_spdk_bdev_flush_blocks_mocked = false; 203 int 204 spdk_bdev_flush_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 205 uint64_t offset_blocks, uint64_t num_blocks, spdk_bdev_io_completion_cb cb, 206 void *cb_arg) 207 { 208 cb(g_bdev_io, !ut_spdk_bdev_flush_blocks, cb_arg); 209 return ut_spdk_bdev_flush_blocks; 210 } 211 212 int ut_spdk_bdev_reset = 0; 213 bool ut_spdk_bdev_reset_mocked = false; 214 int 215 spdk_bdev_reset(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 216 spdk_bdev_io_completion_cb cb, void *cb_arg) 217 { 218 cb(g_bdev_io, !ut_spdk_bdev_reset, cb_arg); 219 return ut_spdk_bdev_reset; 220 } 221 222 bool g_completion_called = false; 223 void 224 spdk_bdev_io_complete(struct spdk_bdev_io *bdev_io, enum spdk_bdev_io_status status) 225 { 226 bdev_io->internal.status = status; 227 g_completion_called = true; 228 } 229 230 231 /* Global setup for all tests that share a bunch of preparation... */ 232 static int 233 test_setup(void) 234 { 235 236 return 0; 237 } 238 239 /* Global teardown for all tests */ 240 static int 241 test_cleanup(void) 242 { 243 244 return 0; 245 } 246 247 static void 248 test_error_paths(void) 249 { 250 251 } 252 253 static void 254 test_simple_write(void) 255 { 256 } 257 258 static void 259 test_simple_read(void) 260 { 261 } 262 263 static void 264 test_large_rw(void) 265 { 266 } 267 268 static void 269 test_passthru(void) 270 { 271 272 } 273 274 static void 275 test_reset(void) 276 { 277 /* TODO: There are a few different ways to do this given that 278 * the code uses spdk_for_each_channel() to implement reset 279 * handling. SUbmitting w/o UT for this function for now and 280 * will follow up with something shortly. 281 */ 282 } 283 284 static void 285 test_initdrivers(void) 286 { 287 } 288 289 static void 290 test_supported_io(void) 291 { 292 293 } 294 295 int 296 main(int argc, char **argv) 297 { 298 CU_pSuite suite = NULL; 299 unsigned int num_failures; 300 301 if (CU_initialize_registry() != CUE_SUCCESS) { 302 return CU_get_error(); 303 } 304 305 suite = CU_add_suite("compress", test_setup, test_cleanup); 306 if (suite == NULL) { 307 CU_cleanup_registry(); 308 return CU_get_error(); 309 } 310 311 if (CU_add_test(suite, "test_error_paths", 312 test_error_paths) == NULL || 313 CU_add_test(suite, "test_simple_write", 314 test_simple_write) == NULL || 315 CU_add_test(suite, "test_simple_read", 316 test_simple_read) == NULL || 317 CU_add_test(suite, "test_large_rw", 318 test_large_rw) == NULL || 319 CU_add_test(suite, "test_passthru", 320 test_passthru) == NULL || 321 CU_add_test(suite, "test_initdrivers", 322 test_initdrivers) == NULL || 323 CU_add_test(suite, "test_supported_io", 324 test_supported_io) == NULL || 325 CU_add_test(suite, "test_reset", 326 test_reset) == NULL 327 ) { 328 CU_cleanup_registry(); 329 return CU_get_error(); 330 } 331 332 CU_basic_set_mode(CU_BRM_VERBOSE); 333 CU_basic_run_tests(); 334 num_failures = CU_get_number_of_failures(); 335 CU_cleanup_registry(); 336 return num_failures; 337 } 338