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 /* We have our own mock for this */ 36 #define UNIT_TEST_NO_VTOPHYS 37 #include "common/lib/test_env.c" 38 #include "spdk_internal/mock.h" 39 #include "unit/lib/json_mock.c" 40 #include "spdk/reduce.h" 41 42 #include <rte_compressdev.h> 43 44 /* There will be one if the data perfectly matches the chunk size, 45 * or there could be an offset into the data and a remainder after 46 * the data or both for a max of 3. 47 */ 48 #define UT_MBUFS_PER_OP 3 49 /* For testing the crossing of a huge page boundary on address translation, 50 * we'll have an extra one but we only test on the source side. 51 */ 52 #define UT_MBUFS_PER_OP_BOUND_TEST 4 53 54 struct spdk_bdev_io *g_bdev_io; 55 struct spdk_io_channel *g_io_ch; 56 struct rte_comp_op g_comp_op[2]; 57 struct vbdev_compress g_comp_bdev; 58 struct comp_device_qp g_device_qp; 59 struct compress_dev g_device; 60 struct rte_compressdev_capabilities g_cdev_cap; 61 static struct rte_mbuf *g_src_mbufs[UT_MBUFS_PER_OP_BOUND_TEST]; 62 static struct rte_mbuf *g_dst_mbufs[UT_MBUFS_PER_OP]; 63 static struct rte_mbuf g_expected_src_mbufs[UT_MBUFS_PER_OP_BOUND_TEST]; 64 static struct rte_mbuf g_expected_dst_mbufs[UT_MBUFS_PER_OP]; 65 struct comp_bdev_io *g_io_ctx; 66 struct comp_io_channel *g_comp_ch; 67 68 /* Those functions are defined as static inline in DPDK, so we can't 69 * mock them straight away. We use defines to redirect them into 70 * our custom functions. 71 */ 72 73 static void mock_rte_pktmbuf_attach_extbuf(struct rte_mbuf *m, void *buf_addr, rte_iova_t buf_iova, 74 uint16_t buf_len, struct rte_mbuf_ext_shared_info *shinfo); 75 #define rte_pktmbuf_attach_extbuf mock_rte_pktmbuf_attach_extbuf 76 static void mock_rte_pktmbuf_attach_extbuf(struct rte_mbuf *m, void *buf_addr, rte_iova_t buf_iova, 77 uint16_t buf_len, struct rte_mbuf_ext_shared_info *shinfo) 78 { 79 assert(m != NULL); 80 m->buf_addr = buf_addr; 81 m->buf_iova = buf_iova; 82 m->buf_len = buf_len; 83 m->data_len = m->pkt_len = 0; 84 } 85 86 static char *mock_rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len); 87 #define rte_pktmbuf_append mock_rte_pktmbuf_append 88 static char *mock_rte_pktmbuf_append(struct rte_mbuf *m, uint16_t len) 89 { 90 m->pkt_len = m->pkt_len + len; 91 return NULL; 92 } 93 94 static inline int mock_rte_pktmbuf_chain(struct rte_mbuf *head, struct rte_mbuf *tail); 95 #define rte_pktmbuf_chain mock_rte_pktmbuf_chain 96 static inline int mock_rte_pktmbuf_chain(struct rte_mbuf *head, struct rte_mbuf *tail) 97 { 98 struct rte_mbuf *cur_tail; 99 100 cur_tail = rte_pktmbuf_lastseg(head); 101 cur_tail->next = tail; 102 103 return 0; 104 } 105 106 uint16_t ut_max_nb_queue_pairs = 0; 107 void __rte_experimental mock_rte_compressdev_info_get(uint8_t dev_id, 108 struct rte_compressdev_info *dev_info); 109 #define rte_compressdev_info_get mock_rte_compressdev_info_get 110 void __rte_experimental 111 mock_rte_compressdev_info_get(uint8_t dev_id, struct rte_compressdev_info *dev_info) 112 { 113 dev_info->max_nb_queue_pairs = ut_max_nb_queue_pairs; 114 dev_info->capabilities = &g_cdev_cap; 115 dev_info->driver_name = "compress_isal"; 116 } 117 118 int ut_rte_compressdev_configure = 0; 119 int __rte_experimental mock_rte_compressdev_configure(uint8_t dev_id, 120 struct rte_compressdev_config *config); 121 #define rte_compressdev_configure mock_rte_compressdev_configure 122 int __rte_experimental 123 mock_rte_compressdev_configure(uint8_t dev_id, struct rte_compressdev_config *config) 124 { 125 return ut_rte_compressdev_configure; 126 } 127 128 int ut_rte_compressdev_queue_pair_setup = 0; 129 int __rte_experimental mock_rte_compressdev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id, 130 uint32_t max_inflight_ops, int socket_id); 131 #define rte_compressdev_queue_pair_setup mock_rte_compressdev_queue_pair_setup 132 int __rte_experimental 133 mock_rte_compressdev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id, 134 uint32_t max_inflight_ops, int socket_id) 135 { 136 return ut_rte_compressdev_queue_pair_setup; 137 } 138 139 int ut_rte_compressdev_start = 0; 140 int __rte_experimental mock_rte_compressdev_start(uint8_t dev_id); 141 #define rte_compressdev_start mock_rte_compressdev_start 142 int __rte_experimental 143 mock_rte_compressdev_start(uint8_t dev_id) 144 { 145 return ut_rte_compressdev_start; 146 } 147 148 int ut_rte_compressdev_private_xform_create = 0; 149 int __rte_experimental mock_rte_compressdev_private_xform_create(uint8_t dev_id, 150 const struct rte_comp_xform *xform, void **private_xform); 151 #define rte_compressdev_private_xform_create mock_rte_compressdev_private_xform_create 152 int __rte_experimental 153 mock_rte_compressdev_private_xform_create(uint8_t dev_id, 154 const struct rte_comp_xform *xform, void **private_xform) 155 { 156 return ut_rte_compressdev_private_xform_create; 157 } 158 159 uint8_t ut_rte_compressdev_count = 0; 160 uint8_t __rte_experimental mock_rte_compressdev_count(void); 161 #define rte_compressdev_count mock_rte_compressdev_count 162 uint8_t __rte_experimental 163 mock_rte_compressdev_count(void) 164 { 165 return ut_rte_compressdev_count; 166 } 167 168 struct rte_mempool *ut_rte_comp_op_pool_create = NULL; 169 struct rte_mempool *__rte_experimental mock_rte_comp_op_pool_create(const char *name, 170 unsigned int nb_elts, unsigned int cache_size, uint16_t user_size, 171 int socket_id); 172 #define rte_comp_op_pool_create mock_rte_comp_op_pool_create 173 struct rte_mempool *__rte_experimental 174 mock_rte_comp_op_pool_create(const char *name, unsigned int nb_elts, 175 unsigned int cache_size, uint16_t user_size, int socket_id) 176 { 177 return ut_rte_comp_op_pool_create; 178 } 179 180 void mock_rte_pktmbuf_free(struct rte_mbuf *m); 181 #define rte_pktmbuf_free mock_rte_pktmbuf_free 182 void mock_rte_pktmbuf_free(struct rte_mbuf *m) 183 { 184 } 185 186 static bool ut_boundary_alloc = false; 187 static int ut_rte_pktmbuf_alloc_bulk = 0; 188 int mock_rte_pktmbuf_alloc_bulk(struct rte_mempool *pool, struct rte_mbuf **mbufs, 189 unsigned count); 190 #define rte_pktmbuf_alloc_bulk mock_rte_pktmbuf_alloc_bulk 191 int mock_rte_pktmbuf_alloc_bulk(struct rte_mempool *pool, struct rte_mbuf **mbufs, 192 unsigned count) 193 { 194 int i; 195 196 /* This mocked function only supports the alloc of up to 3 src and 3 dst. */ 197 ut_rte_pktmbuf_alloc_bulk += count; 198 199 if (ut_rte_pktmbuf_alloc_bulk == 1) { 200 /* allocation of an extra mbuf for boundary cross test */ 201 ut_boundary_alloc = true; 202 g_src_mbufs[UT_MBUFS_PER_OP_BOUND_TEST - 1]->next = NULL; 203 *mbufs = g_src_mbufs[UT_MBUFS_PER_OP_BOUND_TEST - 1]; 204 ut_rte_pktmbuf_alloc_bulk = 0; 205 } else if (ut_rte_pktmbuf_alloc_bulk == UT_MBUFS_PER_OP) { 206 /* first test allocation, src mbufs */ 207 for (i = 0; i < UT_MBUFS_PER_OP; i++) { 208 g_src_mbufs[i]->next = NULL; 209 *mbufs++ = g_src_mbufs[i]; 210 } 211 } else if (ut_rte_pktmbuf_alloc_bulk == UT_MBUFS_PER_OP * 2) { 212 /* second test allocation, dst mbufs */ 213 for (i = 0; i < UT_MBUFS_PER_OP; i++) { 214 g_dst_mbufs[i]->next = NULL; 215 *mbufs++ = g_dst_mbufs[i]; 216 } 217 ut_rte_pktmbuf_alloc_bulk = 0; 218 } else { 219 return -1; 220 } 221 return 0; 222 } 223 224 struct rte_mempool * 225 rte_pktmbuf_pool_create(const char *name, unsigned n, unsigned cache_size, 226 uint16_t priv_size, uint16_t data_room_size, int socket_id) 227 { 228 struct spdk_mempool *tmp; 229 230 tmp = spdk_mempool_create("mbuf_mp", 1024, sizeof(struct rte_mbuf), 231 SPDK_MEMPOOL_DEFAULT_CACHE_SIZE, 232 SPDK_ENV_SOCKET_ID_ANY); 233 234 return (struct rte_mempool *)tmp; 235 } 236 237 void 238 rte_mempool_free(struct rte_mempool *mp) 239 { 240 if (mp) { 241 spdk_mempool_free((struct spdk_mempool *)mp); 242 } 243 } 244 245 static int ut_spdk_reduce_vol_op_complete_err = 0; 246 void 247 spdk_reduce_vol_writev(struct spdk_reduce_vol *vol, struct iovec *iov, int iovcnt, 248 uint64_t offset, uint64_t length, spdk_reduce_vol_op_complete cb_fn, 249 void *cb_arg) 250 { 251 cb_fn(cb_arg, ut_spdk_reduce_vol_op_complete_err); 252 } 253 254 void 255 spdk_reduce_vol_readv(struct spdk_reduce_vol *vol, struct iovec *iov, int iovcnt, 256 uint64_t offset, uint64_t length, spdk_reduce_vol_op_complete cb_fn, 257 void *cb_arg) 258 { 259 cb_fn(cb_arg, ut_spdk_reduce_vol_op_complete_err); 260 } 261 262 #include "bdev/compress/vbdev_compress.c" 263 264 /* SPDK stubs */ 265 DEFINE_STUB(spdk_bdev_get_aliases, const struct spdk_bdev_aliases_list *, 266 (const struct spdk_bdev *bdev), NULL); 267 DEFINE_STUB_V(spdk_bdev_module_list_add, (struct spdk_bdev_module *bdev_module)); 268 DEFINE_STUB_V(spdk_bdev_free_io, (struct spdk_bdev_io *g_bdev_io)); 269 DEFINE_STUB(spdk_bdev_io_type_supported, bool, (struct spdk_bdev *bdev, 270 enum spdk_bdev_io_type io_type), 0); 271 DEFINE_STUB_V(spdk_bdev_module_release_bdev, (struct spdk_bdev *bdev)); 272 DEFINE_STUB_V(spdk_bdev_close, (struct spdk_bdev_desc *desc)); 273 DEFINE_STUB(spdk_bdev_get_name, const char *, (const struct spdk_bdev *bdev), 0); 274 DEFINE_STUB(spdk_bdev_get_io_channel, struct spdk_io_channel *, (struct spdk_bdev_desc *desc), 0); 275 DEFINE_STUB_V(spdk_bdev_unregister, (struct spdk_bdev *bdev, spdk_bdev_unregister_cb cb_fn, 276 void *cb_arg)); 277 DEFINE_STUB(spdk_bdev_open, int, (struct spdk_bdev *bdev, bool write, 278 spdk_bdev_remove_cb_t remove_cb, 279 void *remove_ctx, struct spdk_bdev_desc **_desc), 0); 280 DEFINE_STUB(spdk_bdev_module_claim_bdev, int, (struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 281 struct spdk_bdev_module *module), 0); 282 DEFINE_STUB_V(spdk_bdev_module_examine_done, (struct spdk_bdev_module *module)); 283 DEFINE_STUB(spdk_bdev_register, int, (struct spdk_bdev *bdev), 0); 284 DEFINE_STUB(spdk_bdev_get_by_name, struct spdk_bdev *, (const char *bdev_name), NULL); 285 DEFINE_STUB(spdk_bdev_io_get_io_channel, struct spdk_io_channel *, (struct spdk_bdev_io *bdev_io), 286 0); 287 DEFINE_STUB(spdk_bdev_queue_io_wait, int, (struct spdk_bdev *bdev, struct spdk_io_channel *ch, 288 struct spdk_bdev_io_wait_entry *entry), 0); 289 DEFINE_STUB_V(spdk_reduce_vol_unload, (struct spdk_reduce_vol *vol, 290 spdk_reduce_vol_op_complete cb_fn, void *cb_arg)); 291 DEFINE_STUB_V(spdk_reduce_vol_load, (struct spdk_reduce_backing_dev *backing_dev, 292 spdk_reduce_vol_op_with_handle_complete cb_fn, void *cb_arg)); 293 DEFINE_STUB(spdk_reduce_vol_get_params, const struct spdk_reduce_vol_params *, 294 (struct spdk_reduce_vol *vol), NULL); 295 296 /* DPDK stubs */ 297 DEFINE_STUB(rte_socket_id, unsigned, (void), 0); 298 DEFINE_STUB(rte_vdev_init, int, (const char *name, const char *args), 0); 299 DEFINE_STUB_V(rte_comp_op_free, (struct rte_comp_op *op)); 300 DEFINE_STUB(rte_comp_op_alloc, struct rte_comp_op *, (struct rte_mempool *mempool), NULL); 301 302 int g_small_size_counter = 0; 303 int g_small_size_modify = 0; 304 uint64_t g_small_size = 0; 305 uint64_t 306 spdk_vtophys(void *buf, uint64_t *size) 307 { 308 g_small_size_counter++; 309 if (g_small_size_counter == g_small_size_modify) { 310 *size = g_small_size; 311 g_small_size_counter = 0; 312 g_small_size_modify = 0; 313 } 314 return (uint64_t)buf; 315 } 316 317 void 318 spdk_bdev_io_get_buf(struct spdk_bdev_io *bdev_io, spdk_bdev_io_get_buf_cb cb, uint64_t len) 319 { 320 cb(g_io_ch, g_bdev_io, true); 321 } 322 323 /* Mock these functions to call the callback and then return the value we require */ 324 int ut_spdk_bdev_readv_blocks = 0; 325 int 326 spdk_bdev_readv_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 327 struct iovec *iov, int iovcnt, 328 uint64_t offset_blocks, uint64_t num_blocks, 329 spdk_bdev_io_completion_cb cb, void *cb_arg) 330 { 331 cb(g_bdev_io, !ut_spdk_bdev_readv_blocks, cb_arg); 332 return ut_spdk_bdev_readv_blocks; 333 } 334 335 int ut_spdk_bdev_writev_blocks = 0; 336 bool ut_spdk_bdev_writev_blocks_mocked = false; 337 int 338 spdk_bdev_writev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 339 struct iovec *iov, int iovcnt, 340 uint64_t offset_blocks, uint64_t num_blocks, 341 spdk_bdev_io_completion_cb cb, void *cb_arg) 342 { 343 cb(g_bdev_io, !ut_spdk_bdev_writev_blocks, cb_arg); 344 return ut_spdk_bdev_writev_blocks; 345 } 346 347 int ut_spdk_bdev_unmap_blocks = 0; 348 bool ut_spdk_bdev_unmap_blocks_mocked = false; 349 int 350 spdk_bdev_unmap_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 351 uint64_t offset_blocks, uint64_t num_blocks, 352 spdk_bdev_io_completion_cb cb, void *cb_arg) 353 { 354 cb(g_bdev_io, !ut_spdk_bdev_unmap_blocks, cb_arg); 355 return ut_spdk_bdev_unmap_blocks; 356 } 357 358 int ut_spdk_bdev_flush_blocks = 0; 359 bool ut_spdk_bdev_flush_blocks_mocked = false; 360 int 361 spdk_bdev_flush_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 362 uint64_t offset_blocks, uint64_t num_blocks, spdk_bdev_io_completion_cb cb, 363 void *cb_arg) 364 { 365 cb(g_bdev_io, !ut_spdk_bdev_flush_blocks, cb_arg); 366 return ut_spdk_bdev_flush_blocks; 367 } 368 369 int ut_spdk_bdev_reset = 0; 370 bool ut_spdk_bdev_reset_mocked = false; 371 int 372 spdk_bdev_reset(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 373 spdk_bdev_io_completion_cb cb, void *cb_arg) 374 { 375 cb(g_bdev_io, !ut_spdk_bdev_reset, cb_arg); 376 return ut_spdk_bdev_reset; 377 } 378 379 bool g_completion_called = false; 380 void 381 spdk_bdev_io_complete(struct spdk_bdev_io *bdev_io, enum spdk_bdev_io_status status) 382 { 383 bdev_io->internal.status = status; 384 g_completion_called = true; 385 } 386 387 static uint16_t ut_rte_compressdev_dequeue_burst = 0; 388 uint16_t 389 rte_compressdev_dequeue_burst(uint8_t dev_id, uint16_t qp_id, struct rte_comp_op **ops, 390 uint16_t nb_op) 391 { 392 if (ut_rte_compressdev_dequeue_burst == 0) { 393 return 0; 394 } 395 396 ops[0] = &g_comp_op[0]; 397 ops[1] = &g_comp_op[1]; 398 399 return ut_rte_compressdev_dequeue_burst; 400 } 401 402 static int ut_compress_done[2]; 403 /* done_count and done_idx together control which expected assertion 404 * value to use when dequeuing 2 operations. 405 */ 406 static uint16_t done_count = 1; 407 static uint16_t done_idx = 0; 408 static void 409 _compress_done(void *_req, int reduce_errno) 410 { 411 if (done_count == 1) { 412 CU_ASSERT(reduce_errno == ut_compress_done[0]); 413 } else if (done_count == 2) { 414 CU_ASSERT(reduce_errno == ut_compress_done[done_idx++]); 415 } 416 } 417 418 static void 419 _get_mbuf_array(struct rte_mbuf *mbuf_array[UT_MBUFS_PER_OP_BOUND_TEST], 420 struct rte_mbuf *mbuf_head, int mbuf_count, bool null_final) 421 { 422 int i; 423 424 for (i = 0; i < mbuf_count; i++) { 425 mbuf_array[i] = mbuf_head; 426 if (mbuf_head) { 427 mbuf_head = mbuf_head->next; 428 } 429 } 430 if (null_final) { 431 mbuf_array[i - 1] = NULL; 432 } 433 } 434 435 #define FAKE_ENQUEUE_SUCCESS 255 436 #define FAKE_ENQUEUE_ERROR 128 437 #define FAKE_ENQUEUE_BUSY 64 438 static uint16_t ut_enqueue_value = FAKE_ENQUEUE_SUCCESS; 439 static struct rte_comp_op ut_expected_op; 440 uint16_t 441 rte_compressdev_enqueue_burst(uint8_t dev_id, uint16_t qp_id, struct rte_comp_op **ops, 442 uint16_t nb_ops) 443 { 444 struct rte_comp_op *op = *ops; 445 struct rte_mbuf *op_mbuf[UT_MBUFS_PER_OP_BOUND_TEST]; 446 struct rte_mbuf *exp_mbuf[UT_MBUFS_PER_OP_BOUND_TEST]; 447 int i, num_src_mbufs = UT_MBUFS_PER_OP; 448 449 switch (ut_enqueue_value) { 450 case FAKE_ENQUEUE_BUSY: 451 op->status = RTE_COMP_OP_STATUS_NOT_PROCESSED; 452 return 0; 453 break; 454 case FAKE_ENQUEUE_SUCCESS: 455 op->status = RTE_COMP_OP_STATUS_SUCCESS; 456 return 1; 457 break; 458 case FAKE_ENQUEUE_ERROR: 459 op->status = RTE_COMP_OP_STATUS_ERROR; 460 return 0; 461 break; 462 default: 463 break; 464 } 465 466 /* by design the compress module will never send more than 1 op at a time */ 467 CU_ASSERT(op->private_xform == ut_expected_op.private_xform); 468 469 /* setup our local pointers to the chained mbufs, those pointed to in the 470 * operation struct and the expected values. 471 */ 472 _get_mbuf_array(op_mbuf, op->m_src, SPDK_COUNTOF(op_mbuf), true); 473 _get_mbuf_array(exp_mbuf, ut_expected_op.m_src, SPDK_COUNTOF(exp_mbuf), true); 474 475 if (ut_boundary_alloc == true) { 476 /* if we crossed a boundary, we need to check the 4th src mbuf and 477 * reset the global that is used to identify whether we crossed 478 * or not 479 */ 480 num_src_mbufs = UT_MBUFS_PER_OP_BOUND_TEST; 481 exp_mbuf[UT_MBUFS_PER_OP_BOUND_TEST - 1] = ut_expected_op.m_src->next->next->next; 482 op_mbuf[UT_MBUFS_PER_OP_BOUND_TEST - 1] = op->m_src->next->next->next; 483 ut_boundary_alloc = false; 484 } 485 486 487 for (i = 0; i < num_src_mbufs; i++) { 488 CU_ASSERT(op_mbuf[i]->buf_addr == exp_mbuf[i]->buf_addr); 489 CU_ASSERT(op_mbuf[i]->buf_iova == exp_mbuf[i]->buf_iova); 490 CU_ASSERT(op_mbuf[i]->buf_len == exp_mbuf[i]->buf_len); 491 CU_ASSERT(op_mbuf[i]->pkt_len == exp_mbuf[i]->pkt_len); 492 } 493 494 /* if only 3 mbufs were used in the test, the 4th should be zeroed */ 495 if (num_src_mbufs == UT_MBUFS_PER_OP) { 496 CU_ASSERT(op_mbuf[UT_MBUFS_PER_OP_BOUND_TEST - 1] == NULL); 497 CU_ASSERT(exp_mbuf[UT_MBUFS_PER_OP_BOUND_TEST - 1] == NULL); 498 } 499 500 CU_ASSERT(op->m_src->userdata == ut_expected_op.m_src->userdata); 501 CU_ASSERT(op->src.offset == ut_expected_op.src.offset); 502 CU_ASSERT(op->src.length == ut_expected_op.src.length); 503 504 /* check dst mbuf values */ 505 _get_mbuf_array(op_mbuf, op->m_dst, SPDK_COUNTOF(op_mbuf), true); 506 _get_mbuf_array(exp_mbuf, ut_expected_op.m_dst, SPDK_COUNTOF(exp_mbuf), true); 507 508 for (i = 0; i < UT_MBUFS_PER_OP; i++) { 509 CU_ASSERT(op_mbuf[i]->buf_addr == exp_mbuf[i]->buf_addr); 510 CU_ASSERT(op_mbuf[i]->buf_iova == exp_mbuf[i]->buf_iova); 511 CU_ASSERT(op_mbuf[i]->buf_len == exp_mbuf[i]->buf_len); 512 CU_ASSERT(op_mbuf[i]->pkt_len == exp_mbuf[i]->pkt_len); 513 } 514 CU_ASSERT(op->dst.offset == ut_expected_op.dst.offset); 515 516 return ut_enqueue_value; 517 } 518 519 /* Global setup for all tests that share a bunch of preparation... */ 520 static int 521 test_setup(void) 522 { 523 struct spdk_thread *thread; 524 int i; 525 526 spdk_thread_lib_init(NULL, 0); 527 528 thread = spdk_thread_create(NULL, NULL); 529 spdk_set_thread(thread); 530 531 g_comp_bdev.reduce_thread = thread; 532 g_comp_bdev.backing_dev.unmap = _comp_reduce_unmap; 533 g_comp_bdev.backing_dev.readv = _comp_reduce_readv; 534 g_comp_bdev.backing_dev.writev = _comp_reduce_writev; 535 g_comp_bdev.backing_dev.compress = _comp_reduce_compress; 536 g_comp_bdev.backing_dev.decompress = _comp_reduce_decompress; 537 g_comp_bdev.backing_dev.blocklen = 512; 538 g_comp_bdev.backing_dev.blockcnt = 1024 * 16; 539 540 g_comp_bdev.device_qp = &g_device_qp; 541 g_comp_bdev.device_qp->device = &g_device; 542 543 TAILQ_INIT(&g_comp_bdev.queued_comp_ops); 544 545 g_comp_xform = (struct rte_comp_xform) { 546 .type = RTE_COMP_COMPRESS, 547 .compress = { 548 .algo = RTE_COMP_ALGO_DEFLATE, 549 .deflate.huffman = RTE_COMP_HUFFMAN_DEFAULT, 550 .level = RTE_COMP_LEVEL_MAX, 551 .window_size = DEFAULT_WINDOW_SIZE, 552 .chksum = RTE_COMP_CHECKSUM_NONE, 553 .hash_algo = RTE_COMP_HASH_ALGO_NONE 554 } 555 }; 556 557 g_decomp_xform = (struct rte_comp_xform) { 558 .type = RTE_COMP_DECOMPRESS, 559 .decompress = { 560 .algo = RTE_COMP_ALGO_DEFLATE, 561 .chksum = RTE_COMP_CHECKSUM_NONE, 562 .window_size = DEFAULT_WINDOW_SIZE, 563 .hash_algo = RTE_COMP_HASH_ALGO_NONE 564 } 565 }; 566 g_device.comp_xform = &g_comp_xform; 567 g_device.decomp_xform = &g_decomp_xform; 568 g_cdev_cap.comp_feature_flags = RTE_COMP_FF_SHAREABLE_PRIV_XFORM; 569 g_device.cdev_info.driver_name = "compress_isal"; 570 g_device.cdev_info.capabilities = &g_cdev_cap; 571 for (i = 0; i < UT_MBUFS_PER_OP_BOUND_TEST; i++) { 572 g_src_mbufs[i] = calloc(1, sizeof(struct rte_mbuf)); 573 } 574 for (i = 0; i < UT_MBUFS_PER_OP; i++) { 575 g_dst_mbufs[i] = calloc(1, sizeof(struct rte_mbuf)); 576 } 577 578 g_bdev_io = calloc(1, sizeof(struct spdk_bdev_io) + sizeof(struct comp_bdev_io)); 579 g_bdev_io->u.bdev.iovs = calloc(128, sizeof(struct iovec)); 580 g_bdev_io->bdev = &g_comp_bdev.comp_bdev; 581 g_io_ch = calloc(1, sizeof(struct spdk_io_channel) + sizeof(struct comp_io_channel)); 582 g_io_ch->thread = thread; 583 g_comp_ch = (struct comp_io_channel *)((uint8_t *)g_io_ch + sizeof(struct spdk_io_channel)); 584 g_io_ctx = (struct comp_bdev_io *)g_bdev_io->driver_ctx; 585 586 g_io_ctx->comp_ch = g_comp_ch; 587 g_io_ctx->comp_bdev = &g_comp_bdev; 588 g_comp_bdev.device_qp = &g_device_qp; 589 590 for (i = 0; i < UT_MBUFS_PER_OP_BOUND_TEST - 1; i++) { 591 g_expected_src_mbufs[i].next = &g_expected_src_mbufs[i + 1]; 592 } 593 g_expected_src_mbufs[UT_MBUFS_PER_OP_BOUND_TEST - 1].next = NULL; 594 595 /* we only test w/4 mbufs on src side */ 596 for (i = 0; i < UT_MBUFS_PER_OP - 1; i++) { 597 g_expected_dst_mbufs[i].next = &g_expected_dst_mbufs[i + 1]; 598 } 599 g_expected_dst_mbufs[UT_MBUFS_PER_OP - 1].next = NULL; 600 601 return 0; 602 } 603 604 /* Global teardown for all tests */ 605 static int 606 test_cleanup(void) 607 { 608 struct spdk_thread *thread; 609 int i; 610 611 for (i = 0; i < UT_MBUFS_PER_OP_BOUND_TEST; i++) { 612 free(g_src_mbufs[i]); 613 } 614 for (i = 0; i < UT_MBUFS_PER_OP; i++) { 615 free(g_dst_mbufs[i]); 616 } 617 free(g_bdev_io->u.bdev.iovs); 618 free(g_bdev_io); 619 free(g_io_ch); 620 621 thread = spdk_get_thread(); 622 spdk_thread_exit(thread); 623 spdk_thread_destroy(thread); 624 625 spdk_thread_lib_fini(); 626 627 return 0; 628 } 629 630 static void 631 test_compress_operation(void) 632 { 633 struct iovec src_iovs[3] = {}; 634 int src_iovcnt; 635 struct iovec dst_iovs[3] = {}; 636 int dst_iovcnt; 637 struct spdk_reduce_vol_cb_args cb_arg; 638 int rc, i; 639 struct vbdev_comp_op *op; 640 struct rte_mbuf *exp_src_mbuf[UT_MBUFS_PER_OP]; 641 struct rte_mbuf *exp_dst_mbuf[UT_MBUFS_PER_OP]; 642 643 src_iovcnt = dst_iovcnt = 3; 644 for (i = 0; i < dst_iovcnt; i++) { 645 src_iovs[i].iov_len = 0x1000; 646 dst_iovs[i].iov_len = 0x1000; 647 src_iovs[i].iov_base = (void *)0x10000000 + 0x1000 * i; 648 dst_iovs[i].iov_base = (void *)0x20000000 + 0x1000 * i; 649 } 650 651 /* test rte_comp_op_alloc failure */ 652 MOCK_SET(rte_comp_op_alloc, NULL); 653 CU_ASSERT(TAILQ_EMPTY(&g_comp_bdev.queued_comp_ops) == true); 654 rc = _compress_operation(&g_comp_bdev.backing_dev, &src_iovs[0], src_iovcnt, 655 &dst_iovs[0], dst_iovcnt, true, &cb_arg); 656 CU_ASSERT(TAILQ_EMPTY(&g_comp_bdev.queued_comp_ops) == false); 657 while (!TAILQ_EMPTY(&g_comp_bdev.queued_comp_ops)) { 658 op = TAILQ_FIRST(&g_comp_bdev.queued_comp_ops); 659 TAILQ_REMOVE(&g_comp_bdev.queued_comp_ops, op, link); 660 free(op); 661 } 662 CU_ASSERT(TAILQ_EMPTY(&g_comp_bdev.queued_comp_ops) == true); 663 CU_ASSERT(rc == 0); 664 MOCK_SET(rte_comp_op_alloc, &g_comp_op[0]); 665 666 /* test mempool get failure */ 667 ut_rte_pktmbuf_alloc_bulk = -1; 668 CU_ASSERT(TAILQ_EMPTY(&g_comp_bdev.queued_comp_ops) == true); 669 rc = _compress_operation(&g_comp_bdev.backing_dev, &src_iovs[0], src_iovcnt, 670 &dst_iovs[0], dst_iovcnt, true, &cb_arg); 671 CU_ASSERT(TAILQ_EMPTY(&g_comp_bdev.queued_comp_ops) == false); 672 while (!TAILQ_EMPTY(&g_comp_bdev.queued_comp_ops)) { 673 op = TAILQ_FIRST(&g_comp_bdev.queued_comp_ops); 674 TAILQ_REMOVE(&g_comp_bdev.queued_comp_ops, op, link); 675 free(op); 676 } 677 CU_ASSERT(TAILQ_EMPTY(&g_comp_bdev.queued_comp_ops) == true); 678 CU_ASSERT(rc == 0); 679 ut_rte_pktmbuf_alloc_bulk = 0; 680 681 /* test enqueue failure busy */ 682 ut_enqueue_value = FAKE_ENQUEUE_BUSY; 683 CU_ASSERT(TAILQ_EMPTY(&g_comp_bdev.queued_comp_ops) == true); 684 rc = _compress_operation(&g_comp_bdev.backing_dev, &src_iovs[0], src_iovcnt, 685 &dst_iovs[0], dst_iovcnt, true, &cb_arg); 686 CU_ASSERT(TAILQ_EMPTY(&g_comp_bdev.queued_comp_ops) == false); 687 while (!TAILQ_EMPTY(&g_comp_bdev.queued_comp_ops)) { 688 op = TAILQ_FIRST(&g_comp_bdev.queued_comp_ops); 689 TAILQ_REMOVE(&g_comp_bdev.queued_comp_ops, op, link); 690 free(op); 691 } 692 CU_ASSERT(TAILQ_EMPTY(&g_comp_bdev.queued_comp_ops) == true); 693 CU_ASSERT(rc == 0); 694 ut_enqueue_value = 1; 695 696 /* test enqueue failure error */ 697 ut_enqueue_value = FAKE_ENQUEUE_ERROR; 698 CU_ASSERT(TAILQ_EMPTY(&g_comp_bdev.queued_comp_ops) == true); 699 rc = _compress_operation(&g_comp_bdev.backing_dev, &src_iovs[0], src_iovcnt, 700 &dst_iovs[0], dst_iovcnt, true, &cb_arg); 701 CU_ASSERT(TAILQ_EMPTY(&g_comp_bdev.queued_comp_ops) == true); 702 CU_ASSERT(rc == -EINVAL); 703 ut_enqueue_value = FAKE_ENQUEUE_SUCCESS; 704 705 /* test success with 3 vector iovec */ 706 ut_expected_op.private_xform = &g_decomp_xform; 707 ut_expected_op.src.offset = 0; 708 ut_expected_op.src.length = src_iovs[0].iov_len + src_iovs[1].iov_len + src_iovs[2].iov_len; 709 710 /* setup the src expected values */ 711 _get_mbuf_array(exp_src_mbuf, &g_expected_src_mbufs[0], SPDK_COUNTOF(exp_src_mbuf), false); 712 ut_expected_op.m_src = exp_src_mbuf[0]; 713 714 for (i = 0; i < UT_MBUFS_PER_OP; i++) { 715 exp_src_mbuf[i]->userdata = &cb_arg; 716 exp_src_mbuf[i]->buf_addr = src_iovs[i].iov_base; 717 exp_src_mbuf[i]->buf_iova = spdk_vtophys(src_iovs[i].iov_base, &src_iovs[i].iov_len); 718 exp_src_mbuf[i]->buf_len = src_iovs[i].iov_len; 719 exp_src_mbuf[i]->pkt_len = src_iovs[i].iov_len; 720 } 721 722 /* setup the dst expected values */ 723 _get_mbuf_array(exp_dst_mbuf, &g_expected_dst_mbufs[0], SPDK_COUNTOF(exp_dst_mbuf), false); 724 ut_expected_op.dst.offset = 0; 725 ut_expected_op.m_dst = exp_dst_mbuf[0]; 726 727 for (i = 0; i < UT_MBUFS_PER_OP; i++) { 728 exp_dst_mbuf[i]->buf_addr = dst_iovs[i].iov_base; 729 exp_dst_mbuf[i]->buf_iova = spdk_vtophys(dst_iovs[i].iov_base, &dst_iovs[i].iov_len); 730 exp_dst_mbuf[i]->buf_len = dst_iovs[i].iov_len; 731 exp_dst_mbuf[i]->pkt_len = dst_iovs[i].iov_len; 732 } 733 734 rc = _compress_operation(&g_comp_bdev.backing_dev, &src_iovs[0], src_iovcnt, 735 &dst_iovs[0], dst_iovcnt, false, &cb_arg); 736 CU_ASSERT(TAILQ_EMPTY(&g_comp_bdev.queued_comp_ops) == true); 737 CU_ASSERT(rc == 0); 738 739 } 740 741 static void 742 test_compress_operation_cross_boundary(void) 743 { 744 struct iovec src_iovs[3] = {}; 745 int src_iovcnt; 746 struct iovec dst_iovs[3] = {}; 747 int dst_iovcnt; 748 struct spdk_reduce_vol_cb_args cb_arg; 749 int rc, i; 750 struct rte_mbuf *exp_src_mbuf[UT_MBUFS_PER_OP_BOUND_TEST]; 751 struct rte_mbuf *exp_dst_mbuf[UT_MBUFS_PER_OP_BOUND_TEST]; 752 753 /* Setup the same basic 3 IOV test as used in the simple success case 754 * but then we'll start testing a vtophy boundary crossing at each 755 * position. 756 */ 757 src_iovcnt = dst_iovcnt = 3; 758 for (i = 0; i < dst_iovcnt; i++) { 759 src_iovs[i].iov_len = 0x1000; 760 dst_iovs[i].iov_len = 0x1000; 761 src_iovs[i].iov_base = (void *)0x10000000 + 0x1000 * i; 762 dst_iovs[i].iov_base = (void *)0x20000000 + 0x1000 * i; 763 } 764 765 ut_expected_op.private_xform = &g_decomp_xform; 766 ut_expected_op.src.offset = 0; 767 ut_expected_op.src.length = src_iovs[0].iov_len + src_iovs[1].iov_len + src_iovs[2].iov_len; 768 769 /* setup the src expected values */ 770 _get_mbuf_array(exp_src_mbuf, &g_expected_src_mbufs[0], SPDK_COUNTOF(exp_src_mbuf), false); 771 ut_expected_op.m_src = exp_src_mbuf[0]; 772 773 for (i = 0; i < UT_MBUFS_PER_OP; i++) { 774 exp_src_mbuf[i]->userdata = &cb_arg; 775 exp_src_mbuf[i]->buf_addr = src_iovs[i].iov_base; 776 exp_src_mbuf[i]->buf_iova = spdk_vtophys(src_iovs[i].iov_base, &src_iovs[i].iov_len); 777 exp_src_mbuf[i]->buf_len = src_iovs[i].iov_len; 778 exp_src_mbuf[i]->pkt_len = src_iovs[i].iov_len; 779 } 780 781 /* setup the dst expected values, we don't test needing a 4th dst mbuf */ 782 _get_mbuf_array(exp_dst_mbuf, &g_expected_dst_mbufs[0], SPDK_COUNTOF(exp_dst_mbuf), false); 783 ut_expected_op.dst.offset = 0; 784 ut_expected_op.m_dst = exp_dst_mbuf[0]; 785 786 for (i = 0; i < UT_MBUFS_PER_OP; i++) { 787 exp_dst_mbuf[i]->buf_addr = dst_iovs[i].iov_base; 788 exp_dst_mbuf[i]->buf_iova = spdk_vtophys(dst_iovs[i].iov_base, &dst_iovs[i].iov_len); 789 exp_dst_mbuf[i]->buf_len = dst_iovs[i].iov_len; 790 exp_dst_mbuf[i]->pkt_len = dst_iovs[i].iov_len; 791 } 792 793 /* force the 1st IOV to get partial length from spdk_vtophys */ 794 g_small_size_counter = 0; 795 g_small_size_modify = 1; 796 g_small_size = 0x800; 797 exp_src_mbuf[3]->userdata = &cb_arg; 798 799 /* first only has shorter length */ 800 exp_src_mbuf[0]->pkt_len = exp_src_mbuf[0]->buf_len = 0x800; 801 802 /* 2nd was inserted by the boundary crossing condition and finishes off 803 * the length from the first */ 804 exp_src_mbuf[1]->buf_addr = (void *)0x10000800; 805 exp_src_mbuf[1]->buf_iova = 0x10000800; 806 exp_src_mbuf[1]->pkt_len = exp_src_mbuf[1]->buf_len = 0x800; 807 808 /* 3rd looks like that the 2nd would have */ 809 exp_src_mbuf[2]->buf_addr = (void *)0x10001000; 810 exp_src_mbuf[2]->buf_iova = 0x10001000; 811 exp_src_mbuf[2]->pkt_len = exp_src_mbuf[2]->buf_len = 0x1000; 812 813 /* a new 4th looks like what the 3rd would have */ 814 exp_src_mbuf[3]->buf_addr = (void *)0x10002000; 815 exp_src_mbuf[3]->buf_iova = 0x10002000; 816 exp_src_mbuf[3]->pkt_len = exp_src_mbuf[3]->buf_len = 0x1000; 817 818 rc = _compress_operation(&g_comp_bdev.backing_dev, &src_iovs[0], src_iovcnt, 819 &dst_iovs[0], dst_iovcnt, false, &cb_arg); 820 CU_ASSERT(TAILQ_EMPTY(&g_comp_bdev.queued_comp_ops) == true); 821 CU_ASSERT(rc == 0); 822 823 /* Now force the 2nd IOV to get partial length from spdk_vtophys */ 824 g_small_size_counter = 0; 825 g_small_size_modify = 2; 826 g_small_size = 0x800; 827 828 /* first is normal */ 829 exp_src_mbuf[0]->buf_addr = (void *)0x10000000; 830 exp_src_mbuf[0]->buf_iova = 0x10000000; 831 exp_src_mbuf[0]->pkt_len = exp_src_mbuf[0]->buf_len = 0x1000; 832 833 /* second only has shorter length */ 834 exp_src_mbuf[1]->buf_addr = (void *)0x10001000; 835 exp_src_mbuf[1]->buf_iova = 0x10001000; 836 exp_src_mbuf[1]->pkt_len = exp_src_mbuf[1]->buf_len = 0x800; 837 838 /* 3rd was inserted by the boundary crossing condition and finishes off 839 * the length from the first */ 840 exp_src_mbuf[2]->buf_addr = (void *)0x10001800; 841 exp_src_mbuf[2]->buf_iova = 0x10001800; 842 exp_src_mbuf[2]->pkt_len = exp_src_mbuf[2]->buf_len = 0x800; 843 844 /* a new 4th looks like what the 3rd would have */ 845 exp_src_mbuf[3]->buf_addr = (void *)0x10002000; 846 exp_src_mbuf[3]->buf_iova = 0x10002000; 847 exp_src_mbuf[3]->pkt_len = exp_src_mbuf[3]->buf_len = 0x1000; 848 849 rc = _compress_operation(&g_comp_bdev.backing_dev, &src_iovs[0], src_iovcnt, 850 &dst_iovs[0], dst_iovcnt, false, &cb_arg); 851 CU_ASSERT(TAILQ_EMPTY(&g_comp_bdev.queued_comp_ops) == true); 852 CU_ASSERT(rc == 0); 853 854 /* Finally force the 3rd IOV to get partial length from spdk_vtophys */ 855 g_small_size_counter = 0; 856 g_small_size_modify = 3; 857 g_small_size = 0x800; 858 859 /* first is normal */ 860 exp_src_mbuf[0]->buf_addr = (void *)0x10000000; 861 exp_src_mbuf[0]->buf_iova = 0x10000000; 862 exp_src_mbuf[0]->pkt_len = exp_src_mbuf[0]->buf_len = 0x1000; 863 864 /* second is normal */ 865 exp_src_mbuf[1]->buf_addr = (void *)0x10001000; 866 exp_src_mbuf[1]->buf_iova = 0x10001000; 867 exp_src_mbuf[1]->pkt_len = exp_src_mbuf[1]->buf_len = 0x1000; 868 869 /* 3rd has shorter length */ 870 exp_src_mbuf[2]->buf_addr = (void *)0x10002000; 871 exp_src_mbuf[2]->buf_iova = 0x10002000; 872 exp_src_mbuf[2]->pkt_len = exp_src_mbuf[2]->buf_len = 0x800; 873 874 /* a new 4th handles the remainder from the 3rd */ 875 exp_src_mbuf[3]->buf_addr = (void *)0x10002800; 876 exp_src_mbuf[3]->buf_iova = 0x10002800; 877 exp_src_mbuf[3]->pkt_len = exp_src_mbuf[3]->buf_len = 0x800; 878 879 rc = _compress_operation(&g_comp_bdev.backing_dev, &src_iovs[0], src_iovcnt, 880 &dst_iovs[0], dst_iovcnt, false, &cb_arg); 881 CU_ASSERT(TAILQ_EMPTY(&g_comp_bdev.queued_comp_ops) == true); 882 CU_ASSERT(rc == 0); 883 } 884 885 static void 886 test_poller(void) 887 { 888 int rc; 889 struct spdk_reduce_vol_cb_args *cb_args; 890 struct rte_mbuf mbuf[4]; /* one src, one dst, 2 ops */ 891 struct vbdev_comp_op *op_to_queue; 892 struct iovec src_iovs[3] = {}; 893 struct iovec dst_iovs[3] = {}; 894 int i; 895 896 cb_args = calloc(1, sizeof(*cb_args)); 897 SPDK_CU_ASSERT_FATAL(cb_args != NULL); 898 cb_args->cb_fn = _compress_done; 899 memset(&g_comp_op[0], 0, sizeof(struct rte_comp_op)); 900 g_comp_op[0].m_src = &mbuf[0]; 901 g_comp_op[1].m_src = &mbuf[1]; 902 g_comp_op[0].m_dst = &mbuf[2]; 903 g_comp_op[1].m_dst = &mbuf[3]; 904 for (i = 0; i < 3; i++) { 905 src_iovs[i].iov_len = 0x1000; 906 dst_iovs[i].iov_len = 0x1000; 907 src_iovs[i].iov_base = (void *)0x10000000 + 0x1000 * i; 908 dst_iovs[i].iov_base = (void *)0x20000000 + 0x1000 * i; 909 } 910 911 /* Error from dequeue, nothing needing to be resubmitted. 912 */ 913 ut_rte_compressdev_dequeue_burst = 1; 914 /* setup what we want dequeue to return for the op */ 915 g_comp_op[0].m_src->userdata = (void *)cb_args; 916 g_comp_op[0].produced = 1; 917 g_comp_op[0].status = 1; 918 /* value asserted in the reduce callback */ 919 ut_compress_done[0] = -EINVAL; 920 CU_ASSERT(TAILQ_EMPTY(&g_comp_bdev.queued_comp_ops) == true); 921 rc = comp_dev_poller((void *)&g_comp_bdev); 922 CU_ASSERT(TAILQ_EMPTY(&g_comp_bdev.queued_comp_ops) == true); 923 CU_ASSERT(rc == 0); 924 925 /* Success from dequeue, 2 ops. nothing needing to be resubmitted. 926 */ 927 ut_rte_compressdev_dequeue_burst = 2; 928 /* setup what we want dequeue to return for the op */ 929 g_comp_op[0].m_src->userdata = (void *)cb_args; 930 g_comp_op[0].produced = 16; 931 g_comp_op[0].status = 0; 932 g_comp_op[1].m_src->userdata = (void *)cb_args; 933 g_comp_op[1].produced = 32; 934 g_comp_op[1].status = 0; 935 /* value asserted in the reduce callback */ 936 ut_compress_done[0] = 16; 937 ut_compress_done[1] = 32; 938 done_count = 2; 939 CU_ASSERT(TAILQ_EMPTY(&g_comp_bdev.queued_comp_ops) == true); 940 rc = comp_dev_poller((void *)&g_comp_bdev); 941 CU_ASSERT(TAILQ_EMPTY(&g_comp_bdev.queued_comp_ops) == true); 942 CU_ASSERT(rc == 0); 943 944 /* Success from dequeue, one op to be resubmitted. 945 */ 946 ut_rte_compressdev_dequeue_burst = 1; 947 /* setup what we want dequeue to return for the op */ 948 g_comp_op[0].m_src->userdata = (void *)cb_args; 949 g_comp_op[0].produced = 16; 950 g_comp_op[0].status = 0; 951 /* value asserted in the reduce callback */ 952 ut_compress_done[0] = 16; 953 done_count = 1; 954 op_to_queue = calloc(1, sizeof(struct vbdev_comp_op)); 955 SPDK_CU_ASSERT_FATAL(op_to_queue != NULL); 956 op_to_queue->backing_dev = &g_comp_bdev.backing_dev; 957 op_to_queue->src_iovs = &src_iovs[0]; 958 op_to_queue->src_iovcnt = 3; 959 op_to_queue->dst_iovs = &dst_iovs[0]; 960 op_to_queue->dst_iovcnt = 3; 961 op_to_queue->compress = true; 962 op_to_queue->cb_arg = cb_args; 963 ut_enqueue_value = FAKE_ENQUEUE_SUCCESS; 964 TAILQ_INSERT_TAIL(&g_comp_bdev.queued_comp_ops, 965 op_to_queue, 966 link); 967 CU_ASSERT(TAILQ_EMPTY(&g_comp_bdev.queued_comp_ops) == false); 968 rc = comp_dev_poller((void *)&g_comp_bdev); 969 CU_ASSERT(TAILQ_EMPTY(&g_comp_bdev.queued_comp_ops) == true); 970 CU_ASSERT(rc == 0); 971 972 /* op_to_queue is freed in code under test */ 973 free(cb_args); 974 } 975 976 static void 977 test_vbdev_compress_submit_request(void) 978 { 979 /* Single element block size write */ 980 g_bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED; 981 g_bdev_io->type = SPDK_BDEV_IO_TYPE_WRITE; 982 g_completion_called = false; 983 vbdev_compress_submit_request(g_io_ch, g_bdev_io); 984 CU_ASSERT(g_bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS); 985 CU_ASSERT(g_completion_called == true); 986 CU_ASSERT(g_io_ctx->orig_io == g_bdev_io); 987 CU_ASSERT(g_io_ctx->comp_bdev == &g_comp_bdev); 988 CU_ASSERT(g_io_ctx->comp_ch == g_comp_ch); 989 990 /* same write but now fail it */ 991 ut_spdk_reduce_vol_op_complete_err = 1; 992 g_completion_called = false; 993 vbdev_compress_submit_request(g_io_ch, g_bdev_io); 994 CU_ASSERT(g_bdev_io->internal.status == SPDK_BDEV_IO_STATUS_FAILED); 995 CU_ASSERT(g_completion_called == true); 996 997 /* test a read success */ 998 g_bdev_io->type = SPDK_BDEV_IO_TYPE_READ; 999 ut_spdk_reduce_vol_op_complete_err = 0; 1000 g_completion_called = false; 1001 vbdev_compress_submit_request(g_io_ch, g_bdev_io); 1002 CU_ASSERT(g_bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS); 1003 CU_ASSERT(g_completion_called == true); 1004 1005 /* test a read failure */ 1006 ut_spdk_reduce_vol_op_complete_err = 1; 1007 g_completion_called = false; 1008 vbdev_compress_submit_request(g_io_ch, g_bdev_io); 1009 CU_ASSERT(g_bdev_io->internal.status == SPDK_BDEV_IO_STATUS_FAILED); 1010 CU_ASSERT(g_completion_called == true); 1011 } 1012 1013 static void 1014 test_passthru(void) 1015 { 1016 1017 } 1018 1019 static void 1020 test_reset(void) 1021 { 1022 /* TODO: There are a few different ways to do this given that 1023 * the code uses spdk_for_each_channel() to implement reset 1024 * handling. SUbmitting w/o UT for this function for now and 1025 * will follow up with something shortly. 1026 */ 1027 } 1028 1029 static void 1030 test_initdrivers(void) 1031 { 1032 int rc; 1033 1034 /* test return values from rte_vdev_init() */ 1035 MOCK_SET(rte_vdev_init, -EEXIST); 1036 rc = vbdev_init_compress_drivers(); 1037 /* This is not an error condition, we already have one */ 1038 CU_ASSERT(rc == 0); 1039 1040 /* error */ 1041 MOCK_SET(rte_vdev_init, -2); 1042 rc = vbdev_init_compress_drivers(); 1043 CU_ASSERT(rc == -EINVAL); 1044 CU_ASSERT(g_mbuf_mp == NULL); 1045 CU_ASSERT(g_comp_op_mp == NULL); 1046 1047 /* compressdev count 0 */ 1048 ut_rte_compressdev_count = 0; 1049 MOCK_SET(rte_vdev_init, 0); 1050 rc = vbdev_init_compress_drivers(); 1051 CU_ASSERT(rc == 0); 1052 1053 /* bogus count */ 1054 ut_rte_compressdev_count = RTE_COMPRESS_MAX_DEVS + 1; 1055 rc = vbdev_init_compress_drivers(); 1056 CU_ASSERT(rc == -EINVAL); 1057 1058 /* can't get mbuf pool */ 1059 ut_rte_compressdev_count = 1; 1060 MOCK_SET(spdk_mempool_create, NULL); 1061 rc = vbdev_init_compress_drivers(); 1062 CU_ASSERT(rc == -ENOMEM); 1063 MOCK_CLEAR(spdk_mempool_create); 1064 1065 /* can't get comp op pool */ 1066 ut_rte_comp_op_pool_create = NULL; 1067 rc = vbdev_init_compress_drivers(); 1068 CU_ASSERT(rc == -ENOMEM); 1069 1070 /* error on create_compress_dev() */ 1071 ut_rte_comp_op_pool_create = (struct rte_mempool *)&test_initdrivers; 1072 ut_rte_compressdev_configure = -1; 1073 rc = vbdev_init_compress_drivers(); 1074 CU_ASSERT(rc == -1); 1075 1076 /* error on create_compress_dev() but coverage for large num queues */ 1077 ut_max_nb_queue_pairs = 99; 1078 rc = vbdev_init_compress_drivers(); 1079 CU_ASSERT(rc == -1); 1080 1081 /* qpair setup fails */ 1082 ut_rte_compressdev_configure = 0; 1083 ut_max_nb_queue_pairs = 0; 1084 ut_rte_compressdev_queue_pair_setup = -1; 1085 rc = vbdev_init_compress_drivers(); 1086 CU_ASSERT(rc == -EINVAL); 1087 1088 /* rte_compressdev_start fails */ 1089 ut_rte_compressdev_queue_pair_setup = 0; 1090 ut_rte_compressdev_start = -1; 1091 rc = vbdev_init_compress_drivers(); 1092 CU_ASSERT(rc == -1); 1093 1094 /* rte_compressdev_private_xform_create() fails */ 1095 ut_rte_compressdev_start = 0; 1096 ut_rte_compressdev_private_xform_create = -2; 1097 rc = vbdev_init_compress_drivers(); 1098 CU_ASSERT(rc == -2); 1099 1100 /* success */ 1101 ut_rte_compressdev_private_xform_create = 0; 1102 rc = vbdev_init_compress_drivers(); 1103 CU_ASSERT(rc == 0); 1104 spdk_mempool_free((struct spdk_mempool *)g_mbuf_mp); 1105 } 1106 1107 static void 1108 test_supported_io(void) 1109 { 1110 1111 } 1112 1113 int 1114 main(int argc, char **argv) 1115 { 1116 CU_pSuite suite = NULL; 1117 unsigned int num_failures; 1118 1119 CU_set_error_action(CUEA_ABORT); 1120 CU_initialize_registry(); 1121 1122 suite = CU_add_suite("compress", test_setup, test_cleanup); 1123 CU_ADD_TEST(suite, test_compress_operation); 1124 CU_ADD_TEST(suite, test_compress_operation_cross_boundary); 1125 CU_ADD_TEST(suite, test_vbdev_compress_submit_request); 1126 CU_ADD_TEST(suite, test_passthru); 1127 CU_ADD_TEST(suite, test_initdrivers); 1128 CU_ADD_TEST(suite, test_supported_io); 1129 CU_ADD_TEST(suite, test_poller); 1130 CU_ADD_TEST(suite, test_reset); 1131 1132 CU_basic_set_mode(CU_BRM_VERBOSE); 1133 CU_basic_run_tests(); 1134 num_failures = CU_get_number_of_failures(); 1135 CU_cleanup_registry(); 1136 return num_failures; 1137 } 1138