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 INTERRUcryptoION) 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 "vbdev_crypto.h" 35 36 #include "spdk/env.h" 37 #include "spdk/conf.h" 38 #include "spdk/endian.h" 39 #include "spdk/io_channel.h" 40 #include "spdk/bdev_module.h" 41 #include "spdk_internal/log.h" 42 43 #include <rte_config.h> 44 #include <rte_version.h> 45 #include <rte_bus_vdev.h> 46 #include <rte_crypto.h> 47 #include <rte_cryptodev.h> 48 #include <rte_cryptodev_pmd.h> 49 50 /* To add support for new device types, follow the examples of the following... 51 * Note that the string names are defined by the DPDK PMD in question so be 52 * sure to use the exact names. 53 */ 54 #define MAX_NUM_DRV_TYPES 2 55 #define AESNI_MB "crypto_aesni_mb" 56 #define QAT "crypto_qat" 57 const char *g_driver_names[MAX_NUM_DRV_TYPES] = { AESNI_MB, QAT }; 58 59 /* Global list of available crypto devices. */ 60 struct vbdev_dev { 61 struct rte_cryptodev_info cdev_info; /* includes device friendly name */ 62 uint8_t cdev_id; /* identifier for the device */ 63 TAILQ_ENTRY(vbdev_dev) link; 64 }; 65 static TAILQ_HEAD(, vbdev_dev) g_vbdev_devs = TAILQ_HEAD_INITIALIZER(g_vbdev_devs); 66 67 /* Global list and lock for unique device/queue pair combos */ 68 struct device_qp { 69 struct vbdev_dev *device; /* ptr to crypto device */ 70 uint8_t qp; /* queue pair for this node */ 71 bool in_use; /* whether this node is in use or not */ 72 TAILQ_ENTRY(device_qp) link; 73 }; 74 static TAILQ_HEAD(, device_qp) g_device_qp = TAILQ_HEAD_INITIALIZER(g_device_qp); 75 static pthread_mutex_t g_device_qp_lock = PTHREAD_MUTEX_INITIALIZER; 76 77 78 /* In order to limit the number of resources we need to do one crypto 79 * operation per LBA (we use LBA as IV), we tell the bdev layer that 80 * our max IO size is something reasonable. Units here are in bytes. 81 */ 82 #define CRYPTO_MAX_IO (64 * 1024) 83 84 /* This controls how many ops will be dequeued from the crypto driver in one run 85 * of the poller. It is mainly a performance knob as it effectively determines how 86 * much work the poller has to do. However even that can vary between crypto drivers 87 * as the AESNI_MB driver for example does all the crypto work on dequeue whereas the 88 * QAT driver just dequeues what has been completed already. 89 */ 90 #define MAX_DEQUEUE_BURST_SIZE 64 91 92 /* When enqueueing, we need to supply the crypto driver with an array of pointers to 93 * operation structs. As each of these can be max 512B, we can adjust the CRYPTO_MAX_IO 94 * value in conjunction with the other defines to make sure we're not using crazy amounts 95 * of memory. All of these numbers can and probably should be adjusted based on the 96 * workload. By default we'll use the worst case (smallest) block size for the 97 * minimum number of array entries. As an example, a CRYPTO_MAX_IO size of 64K with 512B 98 * blocks would give us an enqueue array size of 128. 99 */ 100 #define MAX_ENQUEUE_ARRAY_SIZE (CRYPTO_MAX_IO / 512) 101 102 /* The number of MBUFS we need must be a power of two and to support other small IOs 103 * in addition to the limits mentioned above, we go to the next power of two. It is 104 * big number because it is one mempool for source and destination mbufs. It may 105 * need to be bigger to support multiple crypto drivers at once. 106 */ 107 #define NUM_MBUFS 32768 108 #define POOL_CACHE_SIZE 256 109 #define NUM_SESSIONS 2 110 #define SESS_MEMPOOL_CACHE_SIZE 0 111 112 /* This is the max number of IOs we can supply to any crypto device QP at one time. 113 * It can vary between drivers. 114 */ 115 #define CRYPTO_QP_DESCRIPTORS 2048 116 117 /* Specific to AES_CBC. */ 118 #define AES_CBC_IV_LENGTH 16 119 #define AES_CBC_KEY_LENGTH 16 120 121 /* Common for suported devices. */ 122 #define IV_OFFSET (sizeof(struct rte_crypto_op) + \ 123 sizeof(struct rte_crypto_sym_op)) 124 125 static void _complete_internal_io(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg); 126 static void _complete_internal_read(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg); 127 static void _complete_internal_write(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg); 128 static void vbdev_crypto_examine(struct spdk_bdev *bdev); 129 static int vbdev_crypto_claim(struct spdk_bdev *bdev); 130 static void vbdev_crypto_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io); 131 132 /* List of crypto_bdev names and their base bdevs via configuration file. */ 133 struct bdev_names { 134 char *vbdev_name; /* name of the vbdev to create */ 135 char *bdev_name; /* base bdev name */ 136 137 /* Note, for dev/test we allow use of key in the config file, for production 138 * use, you must use an RPC to specify the key for security reasons. 139 */ 140 uint8_t *key; /* key per bdev */ 141 char *drv_name; /* name of the crypto device driver */ 142 TAILQ_ENTRY(bdev_names) link; 143 }; 144 static TAILQ_HEAD(, bdev_names) g_bdev_names = TAILQ_HEAD_INITIALIZER(g_bdev_names); 145 146 /* List of virtual bdevs and associated info for each. We keep the device friendly name here even 147 * though its also in the device struct because we use it early on. 148 */ 149 struct vbdev_crypto { 150 struct spdk_bdev *base_bdev; /* the thing we're attaching to */ 151 struct spdk_bdev_desc *base_desc; /* its descriptor we get from open */ 152 struct spdk_bdev crypto_bdev; /* the crypto virtual bdev */ 153 uint8_t *key; /* key per bdev */ 154 char *drv_name; /* name of the crypto device driver */ 155 struct rte_cryptodev_sym_session *session_encrypt; /* encryption session for this bdev */ 156 struct rte_cryptodev_sym_session *session_decrypt; /* decryption session for this bdev */ 157 struct rte_crypto_sym_xform cipher_xform; /* crypto control struct for this bdev */ 158 TAILQ_ENTRY(vbdev_crypto) link; 159 }; 160 static TAILQ_HEAD(, vbdev_crypto) g_vbdev_crypto = TAILQ_HEAD_INITIALIZER(g_vbdev_crypto); 161 162 /* Shared mempools between all devices on this system */ 163 static struct rte_mempool *g_session_mp = NULL; 164 static struct rte_mempool *g_session_mp_priv = NULL; 165 static struct spdk_mempool *g_mbuf_mp = NULL; /* mbuf mempool */ 166 static struct rte_mempool *g_crypto_op_mp = NULL; /* crypto operations, must be rte* mempool */ 167 168 /* The crypto vbdev channel struct. It is allocated and freed on my behalf by the io channel code. 169 * We store things in here that are needed on per thread basis like the base_channel for this thread, 170 * and the poller for this thread. 171 */ 172 struct crypto_io_channel { 173 struct spdk_io_channel *base_ch; /* IO channel of base device */ 174 struct spdk_poller *poller; /* completion poller */ 175 struct device_qp *device_qp; /* unique device/qp combination for this channel */ 176 TAILQ_HEAD(, spdk_bdev_io) pending_cry_ios; /* outstanding operations to the crypto device */ 177 struct spdk_io_channel_iter *iter; /* used with for_each_channel in reset */ 178 }; 179 180 /* This is the crypto per IO context that the bdev layer allocates for us opaquely and attaches to 181 * each IO for us. 182 */ 183 struct crypto_bdev_io { 184 int cryop_cnt_remaining; /* counter used when completing crypto ops */ 185 struct crypto_io_channel *crypto_ch; /* need to store for crypto completion handling */ 186 struct vbdev_crypto *crypto_bdev; /* the crypto node struct associated with this IO */ 187 struct spdk_bdev_io *orig_io; /* the original IO */ 188 struct spdk_bdev_io *read_io; /* the read IO we issued */ 189 190 /* Used for the single contiguous buffer that serves as the crypto destination target for writes */ 191 uint64_t cry_num_blocks; /* num of blocks for the contiguous buffer */ 192 uint64_t cry_offset_blocks; /* block offset on media */ 193 struct iovec cry_iov; /* iov representing contig write buffer */ 194 195 /* for bdev_io_wait */ 196 struct spdk_bdev_io_wait_entry bdev_io_wait; 197 struct spdk_io_channel *ch; 198 }; 199 200 /* Called by vbdev_crypto_init_crypto_drivers() to init each discovered crypto device */ 201 static int 202 create_vbdev_dev(uint8_t index, uint16_t num_lcores) 203 { 204 struct vbdev_dev *device; 205 uint8_t j, cdev_id, cdrv_id; 206 struct device_qp *dev_qp; 207 struct device_qp *tmp_qp; 208 int rc; 209 210 device = calloc(1, sizeof(struct vbdev_dev)); 211 if (!device) { 212 return -ENOMEM; 213 } 214 215 /* Get details about this device. */ 216 rte_cryptodev_info_get(index, &device->cdev_info); 217 cdrv_id = device->cdev_info.driver_id; 218 cdev_id = device->cdev_id = index; 219 220 /* Before going any further, make sure we have enough resources for this 221 * device type to function. We need a unique queue pair per core accross each 222 * device type to remain lockless.... 223 */ 224 if ((rte_cryptodev_device_count_by_driver(cdrv_id) * 225 device->cdev_info.max_nb_queue_pairs) < num_lcores) { 226 SPDK_ERRLOG("Insufficient unique queue pairs available for %s\n", 227 device->cdev_info.driver_name); 228 SPDK_ERRLOG("Either add more crypto devices or decrease core count\n"); 229 rc = -EINVAL; 230 goto err; 231 } 232 233 /* Setup queue pairs. */ 234 struct rte_cryptodev_config conf = { 235 .nb_queue_pairs = device->cdev_info.max_nb_queue_pairs, 236 .socket_id = SPDK_ENV_SOCKET_ID_ANY 237 }; 238 239 rc = rte_cryptodev_configure(cdev_id, &conf); 240 if (rc < 0) { 241 SPDK_ERRLOG("Failed to configure cryptodev %u\n", cdev_id); 242 rc = -EINVAL; 243 goto err; 244 } 245 246 struct rte_cryptodev_qp_conf qp_conf = { 247 .nb_descriptors = CRYPTO_QP_DESCRIPTORS, 248 #if RTE_VERSION >= RTE_VERSION_NUM(19, 02, 0, 0) 249 .mp_session = g_session_mp, 250 .mp_session_private = g_session_mp_priv, 251 #endif 252 }; 253 254 /* Pre-setup all potential qpairs now and assign them in the channel 255 * callback. If we were to create them there, we'd have to stop the 256 * entire device affecting all other threads that might be using it 257 * even on other queue pairs. 258 */ 259 for (j = 0; j < device->cdev_info.max_nb_queue_pairs; j++) { 260 #if RTE_VERSION >= RTE_VERSION_NUM(19, 02, 0, 0) 261 rc = rte_cryptodev_queue_pair_setup(cdev_id, j, &qp_conf, SOCKET_ID_ANY); 262 #else 263 rc = rte_cryptodev_queue_pair_setup(cdev_id, j, &qp_conf, SOCKET_ID_ANY, 264 g_session_mp); 265 #endif 266 267 if (rc < 0) { 268 SPDK_ERRLOG("Failed to setup queue pair %u on " 269 "cryptodev %u\n", j, cdev_id); 270 rc = -EINVAL; 271 goto err; 272 } 273 } 274 275 rc = rte_cryptodev_start(cdev_id); 276 if (rc < 0) { 277 SPDK_ERRLOG("Failed to start device %u: error %d\n", 278 cdev_id, rc); 279 rc = -EINVAL; 280 goto err; 281 } 282 283 /* Build up list of device/qp combinations */ 284 for (j = 0; j < device->cdev_info.max_nb_queue_pairs; j++) { 285 dev_qp = calloc(1, sizeof(struct device_qp)); 286 if (!dev_qp) { 287 rc = -ENOMEM; 288 goto err; 289 } 290 dev_qp->device = device; 291 dev_qp->qp = j; 292 dev_qp->in_use = false; 293 TAILQ_INSERT_TAIL(&g_device_qp, dev_qp, link); 294 } 295 296 /* Add to our list of available crypto devices. */ 297 TAILQ_INSERT_TAIL(&g_vbdev_devs, device, link); 298 299 return 0; 300 err: 301 TAILQ_FOREACH_SAFE(dev_qp, &g_device_qp, link, tmp_qp) { 302 TAILQ_REMOVE(&g_device_qp, dev_qp, link); 303 free(dev_qp); 304 } 305 free(device); 306 307 return rc; 308 309 } 310 311 /* This is called from the module's init function. We setup all crypto devices early on as we are unable 312 * to easily dynamically configure queue pairs after the drivers are up and running. So, here, we 313 * configure the max capabilities of each device and assign threads to queue pairs as channels are 314 * requested. 315 */ 316 static int 317 vbdev_crypto_init_crypto_drivers(void) 318 { 319 uint8_t cdev_count; 320 uint8_t cdev_id, i; 321 int rc = 0; 322 struct vbdev_dev *device; 323 struct vbdev_dev *tmp_dev; 324 unsigned int max_sess_size = 0, sess_size; 325 uint16_t num_lcores = rte_lcore_count(); 326 327 /* Only the first call, via RPC or module init should init the crypto drivers. */ 328 if (g_session_mp != NULL) { 329 return 0; 330 } 331 332 /* We always init AESNI_MB */ 333 rc = rte_vdev_init(AESNI_MB, NULL); 334 if (rc) { 335 SPDK_ERRLOG("error creating virtual PMD %s\n", AESNI_MB); 336 return -EINVAL; 337 } 338 339 /* If we have no crypto devices, there's no reason to continue. */ 340 cdev_count = rte_cryptodev_count(); 341 if (cdev_count == 0) { 342 return 0; 343 } 344 345 /* 346 * Create global mempools, shared by all devices regardless of type. 347 */ 348 349 /* First determine max session size, most pools are shared by all the devices, 350 * so we need to find the global max sessions size. 351 */ 352 for (cdev_id = 0; cdev_id < cdev_count; cdev_id++) { 353 sess_size = rte_cryptodev_sym_get_private_session_size(cdev_id); 354 if (sess_size > max_sess_size) { 355 max_sess_size = sess_size; 356 } 357 } 358 359 #if RTE_VERSION >= RTE_VERSION_NUM(19, 02, 0, 0) 360 g_session_mp_priv = rte_mempool_create("session_mp_priv", NUM_SESSIONS, max_sess_size, 361 SESS_MEMPOOL_CACHE_SIZE, 0, NULL, NULL, NULL, 362 NULL, SOCKET_ID_ANY, 0); 363 if (g_session_mp_priv == NULL) { 364 SPDK_ERRLOG("Cannot create private session pool max size 0x%x\n", max_sess_size); 365 return -ENOMEM; 366 } 367 368 g_session_mp = rte_cryptodev_sym_session_pool_create( 369 "session_mp", 370 NUM_SESSIONS, 0, SESS_MEMPOOL_CACHE_SIZE, 0, 371 SOCKET_ID_ANY); 372 #else 373 g_session_mp = rte_mempool_create("session_mp", NUM_SESSIONS, max_sess_size, 374 SESS_MEMPOOL_CACHE_SIZE, 375 0, NULL, NULL, NULL, NULL, SOCKET_ID_ANY, 0); 376 #endif 377 if (g_session_mp == NULL) { 378 SPDK_ERRLOG("Cannot create session pool max size 0x%x\n", max_sess_size); 379 goto error_create_session_mp; 380 return -ENOMEM; 381 } 382 383 g_mbuf_mp = spdk_mempool_create("mbuf_mp", NUM_MBUFS, sizeof(struct rte_mbuf), 384 SPDK_MEMPOOL_DEFAULT_CACHE_SIZE, 385 SPDK_ENV_SOCKET_ID_ANY); 386 if (g_mbuf_mp == NULL) { 387 SPDK_ERRLOG("Cannot create mbuf pool\n"); 388 rc = -ENOMEM; 389 goto error_create_mbuf; 390 } 391 392 g_crypto_op_mp = rte_crypto_op_pool_create("op_mp", 393 RTE_CRYPTO_OP_TYPE_SYMMETRIC, 394 NUM_MBUFS, 395 POOL_CACHE_SIZE, 396 AES_CBC_IV_LENGTH, 397 rte_socket_id()); 398 if (g_crypto_op_mp == NULL) { 399 SPDK_ERRLOG("Cannot create op pool\n"); 400 rc = -ENOMEM; 401 goto error_create_op; 402 } 403 404 /* Init all devices */ 405 for (i = 0; i < cdev_count; i++) { 406 rc = create_vbdev_dev(i, num_lcores); 407 if (rc) { 408 goto err; 409 } 410 } 411 return 0; 412 413 /* Error cleanup paths. */ 414 err: 415 TAILQ_FOREACH_SAFE(device, &g_vbdev_devs, link, tmp_dev) { 416 TAILQ_REMOVE(&g_vbdev_devs, device, link); 417 free(device); 418 } 419 rte_mempool_free(g_crypto_op_mp); 420 g_crypto_op_mp = NULL; 421 error_create_op: 422 spdk_mempool_free(g_mbuf_mp); 423 g_mbuf_mp = NULL; 424 error_create_mbuf: 425 rte_mempool_free(g_session_mp); 426 g_session_mp = NULL; 427 error_create_session_mp: 428 if (g_session_mp_priv != NULL) { 429 rte_mempool_free(g_session_mp_priv); 430 g_session_mp_priv = NULL; 431 } 432 return rc; 433 } 434 435 /* Following an encrypt or decrypt we need to then either write the encrypted data or finish 436 * the read on decrypted data. Do that here. 437 */ 438 static void 439 _crypto_operation_complete(struct spdk_bdev_io *bdev_io) 440 { 441 struct vbdev_crypto *crypto_bdev = SPDK_CONTAINEROF(bdev_io->bdev, struct vbdev_crypto, 442 crypto_bdev); 443 struct crypto_bdev_io *io_ctx = (struct crypto_bdev_io *)bdev_io->driver_ctx; 444 struct crypto_io_channel *crypto_ch = io_ctx->crypto_ch; 445 struct spdk_bdev_io *free_me = io_ctx->read_io; 446 int rc = 0; 447 448 TAILQ_REMOVE(&crypto_ch->pending_cry_ios, bdev_io, module_link); 449 450 if (bdev_io->type == SPDK_BDEV_IO_TYPE_READ) { 451 452 /* Complete the original IO and then free the one that we created 453 * as a result of issuing an IO via submit_request. 454 */ 455 if (bdev_io->internal.status != SPDK_BDEV_IO_STATUS_FAILED) { 456 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_SUCCESS); 457 } else { 458 SPDK_ERRLOG("Issue with decryption on bdev_io %p\n", bdev_io); 459 rc = -EINVAL; 460 } 461 spdk_bdev_free_io(free_me); 462 463 } else if (bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE) { 464 465 if (bdev_io->internal.status != SPDK_BDEV_IO_STATUS_FAILED) { 466 /* Write the encrypted data. */ 467 rc = spdk_bdev_writev_blocks(crypto_bdev->base_desc, crypto_ch->base_ch, 468 &io_ctx->cry_iov, 1, io_ctx->cry_offset_blocks, 469 io_ctx->cry_num_blocks, _complete_internal_write, 470 bdev_io); 471 } else { 472 SPDK_ERRLOG("Issue with encryption on bdev_io %p\n", bdev_io); 473 rc = -EINVAL; 474 } 475 476 } else { 477 SPDK_ERRLOG("Unknown bdev type %u on crypto operation completion\n", 478 bdev_io->type); 479 rc = -EINVAL; 480 } 481 482 if (rc) { 483 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 484 } 485 } 486 487 /* This is the poller for the crypto device. It uses a single API to dequeue whatever is ready at 488 * the device. Then we need to decide if what we've got so far (including previous poller 489 * runs) totals up to one or more complete bdev_ios and if so continue with the bdev_io 490 * accordingly. This means either completing a read or issuing a new write. 491 */ 492 static int 493 crypto_dev_poller(void *args) 494 { 495 struct crypto_io_channel *crypto_ch = args; 496 uint8_t cdev_id = crypto_ch->device_qp->device->cdev_id; 497 int i, num_dequeued_ops; 498 struct spdk_bdev_io *bdev_io = NULL; 499 struct crypto_bdev_io *io_ctx = NULL; 500 struct rte_crypto_op *dequeued_ops[MAX_DEQUEUE_BURST_SIZE]; 501 struct rte_crypto_op *mbufs_to_free[2 * MAX_DEQUEUE_BURST_SIZE]; 502 int num_mbufs = 0; 503 504 /* Each run of the poller will get just what the device has available 505 * at the moment we call it, we don't check again after draining the 506 * first batch. 507 */ 508 num_dequeued_ops = rte_cryptodev_dequeue_burst(cdev_id, crypto_ch->device_qp->qp, 509 dequeued_ops, MAX_DEQUEUE_BURST_SIZE); 510 511 /* Check if operation was processed successfully */ 512 for (i = 0; i < num_dequeued_ops; i++) { 513 514 /* We don't know the order or association of the crypto ops wrt any 515 * partiular bdev_io so need to look at each and determine if it's 516 * the last one for it's bdev_io or not. 517 */ 518 bdev_io = (struct spdk_bdev_io *)dequeued_ops[i]->sym->m_src->userdata; 519 assert(bdev_io != NULL); 520 521 if (dequeued_ops[i]->status != RTE_CRYPTO_OP_STATUS_SUCCESS) { 522 SPDK_ERRLOG("error with op %d status %u\n", i, 523 dequeued_ops[i]->status); 524 /* Update the bdev status to error, we'll still process the 525 * rest of the crypto ops for this bdev_io though so they 526 * aren't left hanging. 527 */ 528 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED; 529 } 530 531 io_ctx = (struct crypto_bdev_io *)bdev_io->driver_ctx; 532 assert(io_ctx->cryop_cnt_remaining > 0); 533 534 /* Return the associated src and dst mbufs by collecting them into 535 * an array that we can use the bulk API to free after the loop. 536 */ 537 dequeued_ops[i]->sym->m_src->userdata = NULL; 538 mbufs_to_free[num_mbufs++] = (void *)dequeued_ops[i]->sym->m_src; 539 if (dequeued_ops[i]->sym->m_dst) { 540 mbufs_to_free[num_mbufs++] = (void *)dequeued_ops[i]->sym->m_dst; 541 } 542 543 /* done encrypting, complete the bdev_io */ 544 if (--io_ctx->cryop_cnt_remaining == 0) { 545 546 /* If we're completing this with an outstanding reset we need 547 * to fail it. 548 */ 549 if (crypto_ch->iter) { 550 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED; 551 } 552 553 /* Complete the IO */ 554 _crypto_operation_complete(bdev_io); 555 } 556 } 557 558 /* Now bulk free both mbufs and crypto operations. */ 559 if (num_dequeued_ops > 0) { 560 rte_mempool_put_bulk(g_crypto_op_mp, 561 (void **)dequeued_ops, 562 num_dequeued_ops); 563 assert(num_mbufs > 0); 564 spdk_mempool_put_bulk(g_mbuf_mp, 565 (void **)mbufs_to_free, 566 num_mbufs); 567 } 568 569 /* If the channel iter is not NULL, we need to continue to poll 570 * until the pending list is empty, then we can move on to the 571 * next channel. 572 */ 573 if (crypto_ch->iter && TAILQ_EMPTY(&crypto_ch->pending_cry_ios)) { 574 SPDK_NOTICELOG("Channel %p has been quiesced.\n", crypto_ch); 575 spdk_for_each_channel_continue(crypto_ch->iter, 0); 576 crypto_ch->iter = NULL; 577 } 578 579 return num_dequeued_ops; 580 } 581 582 /* We're either encrypting on the way down or decrypting on the way back. */ 583 static int 584 _crypto_operation(struct spdk_bdev_io *bdev_io, enum rte_crypto_cipher_operation crypto_op) 585 { 586 uint16_t num_enqueued_ops = 0; 587 uint32_t cryop_cnt = bdev_io->u.bdev.num_blocks; 588 struct crypto_bdev_io *io_ctx = (struct crypto_bdev_io *)bdev_io->driver_ctx; 589 struct crypto_io_channel *crypto_ch = io_ctx->crypto_ch; 590 uint8_t cdev_id = crypto_ch->device_qp->device->cdev_id; 591 uint32_t crypto_len = io_ctx->crypto_bdev->crypto_bdev.blocklen; 592 uint64_t total_length = bdev_io->u.bdev.num_blocks * crypto_len; 593 int rc; 594 uint32_t enqueued = 0; 595 uint32_t iov_index = 0; 596 uint32_t allocated = 0; 597 uint8_t *current_iov = NULL; 598 uint64_t total_remaining = 0; 599 uint64_t updated_length, current_iov_remaining = 0; 600 int completed = 0; 601 int crypto_index = 0; 602 uint32_t en_offset = 0; 603 struct rte_crypto_op *crypto_ops[MAX_ENQUEUE_ARRAY_SIZE]; 604 struct rte_mbuf *src_mbufs[MAX_ENQUEUE_ARRAY_SIZE]; 605 struct rte_mbuf *dst_mbufs[MAX_ENQUEUE_ARRAY_SIZE]; 606 int burst; 607 608 assert((bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen) <= CRYPTO_MAX_IO); 609 610 /* Get the number of source mbufs that we need. These will always be 1:1 because we 611 * don't support chaining. The reason we don't is because of our decision to use 612 * LBA as IV, there can be no case where we'd need >1 mbuf per crypto op or the 613 * op would be > 1 LBA. 614 */ 615 rc = spdk_mempool_get_bulk(g_mbuf_mp, (void **)&src_mbufs[0], cryop_cnt); 616 if (rc) { 617 SPDK_ERRLOG("ERROR trying to get src_mbufs!\n"); 618 return -ENOMEM; 619 } 620 621 /* Get the same amount but these buffers to describe the encrypted data location (dst). */ 622 if (crypto_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) { 623 rc = spdk_mempool_get_bulk(g_mbuf_mp, (void **)&dst_mbufs[0], cryop_cnt); 624 if (rc) { 625 SPDK_ERRLOG("ERROR trying to get dst_mbufs!\n"); 626 rc = -ENOMEM; 627 goto error_get_dst; 628 } 629 } 630 631 #ifdef __clang_analyzer__ 632 /* silence scan-build false positive */ 633 SPDK_CLANG_ANALYZER_PREINIT_PTR_ARRAY(crypto_ops, MAX_ENQUEUE_ARRAY_SIZE, 0x1000); 634 #endif 635 /* Allocate crypto operations. */ 636 allocated = rte_crypto_op_bulk_alloc(g_crypto_op_mp, 637 RTE_CRYPTO_OP_TYPE_SYMMETRIC, 638 crypto_ops, cryop_cnt); 639 if (allocated < cryop_cnt) { 640 SPDK_ERRLOG("ERROR trying to get crypto ops!\n"); 641 rc = -ENOMEM; 642 goto error_get_ops; 643 } 644 645 /* For encryption, we need to prepare a single contiguous buffer as the encryption 646 * destination, we'll then pass that along for the write after encryption is done. 647 * This is done to avoiding encrypting the provided write buffer which may be 648 * undesirable in some use cases. 649 */ 650 if (crypto_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) { 651 io_ctx->cry_iov.iov_len = total_length; 652 /* For now just allocate in the I/O path, not optimal but the current bdev API 653 * for getting a buffer from the pool won't work if the bdev_io passed in 654 * has a buffer, which ours always will. So, until we modify that API 655 * or better yet the current ZCOPY work lands, this is the best we can do. 656 */ 657 io_ctx->cry_iov.iov_base = spdk_malloc(total_length, 658 spdk_bdev_get_buf_align(bdev_io->bdev), NULL, 659 SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA); 660 if (!io_ctx->cry_iov.iov_base) { 661 SPDK_ERRLOG("ERROR trying to allocate write buffer for encryption!\n"); 662 rc = -ENOMEM; 663 goto error_get_write_buffer; 664 } 665 io_ctx->cry_offset_blocks = bdev_io->u.bdev.offset_blocks; 666 io_ctx->cry_num_blocks = bdev_io->u.bdev.num_blocks; 667 } 668 669 /* This value is used in the completion callback to determine when the bdev_io is 670 * complete. 671 */ 672 io_ctx->cryop_cnt_remaining = cryop_cnt; 673 674 /* As we don't support chaining because of a decision to use LBA as IV, construction 675 * of crypto operations is straightforward. We build both the op, the mbuf and the 676 * dst_mbuf in our local arrays by looping through the length of the bdev IO and 677 * picking off LBA sized blocks of memory from the IOVs as we walk through them. Each 678 * LBA sized chunk of memory will correspond 1:1 to a crypto operation and a single 679 * mbuf per crypto operation. 680 */ 681 total_remaining = total_length; 682 current_iov = bdev_io->u.bdev.iovs[iov_index].iov_base; 683 current_iov_remaining = bdev_io->u.bdev.iovs[iov_index].iov_len; 684 do { 685 uint8_t *iv_ptr; 686 uint64_t op_block_offset; 687 688 /* Set the mbuf elements address and length. Null out the next pointer. */ 689 src_mbufs[crypto_index]->buf_addr = current_iov; 690 src_mbufs[crypto_index]->data_len = updated_length = crypto_len; 691 src_mbufs[crypto_index]->buf_iova = spdk_vtophys((void *)current_iov, &updated_length); 692 assert(updated_length == crypto_len); 693 src_mbufs[crypto_index]->next = NULL; 694 /* Store context in every mbuf as we don't know anything about completion order */ 695 src_mbufs[crypto_index]->userdata = bdev_io; 696 697 /* Set the IV - we use the LBA of the crypto_op */ 698 iv_ptr = rte_crypto_op_ctod_offset(crypto_ops[crypto_index], uint8_t *, 699 IV_OFFSET); 700 memset(iv_ptr, 0, AES_CBC_IV_LENGTH); 701 op_block_offset = bdev_io->u.bdev.offset_blocks + crypto_index; 702 rte_memcpy(iv_ptr, &op_block_offset, sizeof(uint64_t)); 703 704 /* Set the data to encrypt/decrypt length */ 705 crypto_ops[crypto_index]->sym->cipher.data.length = crypto_len; 706 crypto_ops[crypto_index]->sym->cipher.data.offset = 0; 707 708 /* link the mbuf to the crypto op. */ 709 crypto_ops[crypto_index]->sym->m_src = src_mbufs[crypto_index]; 710 if (crypto_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) { 711 crypto_ops[crypto_index]->sym->m_dst = src_mbufs[crypto_index]; 712 } else { 713 crypto_ops[crypto_index]->sym->m_dst = NULL; 714 } 715 716 /* For encrypt, point the destination to a buffer we allocate and redirect the bdev_io 717 * that will be used to process the write on completion to the same buffer. Setting 718 * up the en_buffer is a little simpler as we know the destination buffer is single IOV. 719 */ 720 if (crypto_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) { 721 722 /* Set the relevant destination en_mbuf elements. */ 723 dst_mbufs[crypto_index]->buf_addr = io_ctx->cry_iov.iov_base + en_offset; 724 dst_mbufs[crypto_index]->data_len = updated_length = crypto_len; 725 dst_mbufs[crypto_index]->buf_iova = spdk_vtophys(dst_mbufs[crypto_index]->buf_addr, 726 &updated_length); 727 assert(updated_length == crypto_len); 728 crypto_ops[crypto_index]->sym->m_dst = dst_mbufs[crypto_index]; 729 en_offset += crypto_len; 730 dst_mbufs[crypto_index]->next = NULL; 731 732 /* Attach the crypto session to the operation */ 733 rc = rte_crypto_op_attach_sym_session(crypto_ops[crypto_index], 734 io_ctx->crypto_bdev->session_encrypt); 735 if (rc) { 736 rc = -EINVAL; 737 goto error_attach_session; 738 } 739 740 } else { 741 /* Attach the crypto session to the operation */ 742 rc = rte_crypto_op_attach_sym_session(crypto_ops[crypto_index], 743 io_ctx->crypto_bdev->session_decrypt); 744 if (rc) { 745 rc = -EINVAL; 746 goto error_attach_session; 747 } 748 749 750 } 751 752 /* Subtract our running totals for the op in progress and the overall bdev io */ 753 total_remaining -= crypto_len; 754 current_iov_remaining -= crypto_len; 755 756 /* move our current IOV pointer accordingly. */ 757 current_iov += crypto_len; 758 759 /* move on to the next crypto operation */ 760 crypto_index++; 761 762 /* If we're done with this IOV, move to the next one. */ 763 if (current_iov_remaining == 0 && total_remaining > 0) { 764 iov_index++; 765 current_iov = bdev_io->u.bdev.iovs[iov_index].iov_base; 766 current_iov_remaining = bdev_io->u.bdev.iovs[iov_index].iov_len; 767 } 768 } while (total_remaining > 0); 769 770 /* Enqueue everything we've got but limit by the max number of descriptors we 771 * configured the crypto device for. 772 */ 773 do { 774 burst = spdk_min((cryop_cnt - enqueued), CRYPTO_QP_DESCRIPTORS); 775 num_enqueued_ops = rte_cryptodev_enqueue_burst(cdev_id, crypto_ch->device_qp->qp, 776 &crypto_ops[enqueued], 777 burst); 778 enqueued += num_enqueued_ops; 779 780 /* Dequeue all inline if the device is full. We don't defer anything simply 781 * because of the complexity involved as we're building 1 or more crypto 782 * ops per IO. Dequeue will free up space for more enqueue. 783 */ 784 if (enqueued < cryop_cnt) { 785 786 /* Dequeue everything, this may include ops that were already 787 * in the device before this submission.... 788 */ 789 do { 790 completed = crypto_dev_poller(crypto_ch); 791 } while (completed > 0); 792 } 793 } while (enqueued < cryop_cnt); 794 795 /* Add this bdev_io to our outstanding list. */ 796 TAILQ_INSERT_TAIL(&crypto_ch->pending_cry_ios, bdev_io, module_link); 797 798 return rc; 799 800 /* Error cleanup paths. */ 801 error_attach_session: 802 error_get_write_buffer: 803 rte_mempool_put_bulk(g_crypto_op_mp, (void **)crypto_ops, cryop_cnt); 804 allocated = 0; 805 error_get_ops: 806 if (crypto_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) { 807 spdk_mempool_put_bulk(g_mbuf_mp, (void **)&dst_mbufs[0], 808 cryop_cnt); 809 } 810 if (allocated > 0) { 811 rte_mempool_put_bulk(g_crypto_op_mp, (void **)crypto_ops, 812 allocated); 813 } 814 error_get_dst: 815 spdk_mempool_put_bulk(g_mbuf_mp, (void **)&src_mbufs[0], 816 cryop_cnt); 817 return rc; 818 } 819 820 /* This function is called after all channels have been quiesced following 821 * a bdev reset. 822 */ 823 static void 824 _ch_quiesce_done(struct spdk_io_channel_iter *i, int status) 825 { 826 struct crypto_bdev_io *io_ctx = spdk_io_channel_iter_get_ctx(i); 827 828 assert(TAILQ_EMPTY(&io_ctx->crypto_ch->pending_cry_ios)); 829 assert(io_ctx->orig_io != NULL); 830 831 spdk_bdev_io_complete(io_ctx->orig_io, SPDK_BDEV_IO_STATUS_SUCCESS); 832 } 833 834 /* This function is called per channel to quiesce IOs before completing a 835 * bdev reset that we received. 836 */ 837 static void 838 _ch_quiesce(struct spdk_io_channel_iter *i) 839 { 840 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 841 struct crypto_io_channel *crypto_ch = spdk_io_channel_get_ctx(ch); 842 843 crypto_ch->iter = i; 844 /* When the poller runs, it will see the non-NULL iter and handle 845 * the quiesce. 846 */ 847 } 848 849 /* Completion callback for IO that were issued from this bdev other than read/write. 850 * They have their own for readability. 851 */ 852 static void 853 _complete_internal_io(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 854 { 855 struct spdk_bdev_io *orig_io = cb_arg; 856 int status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED; 857 858 if (bdev_io->type == SPDK_BDEV_IO_TYPE_RESET) { 859 struct crypto_bdev_io *orig_ctx = (struct crypto_bdev_io *)orig_io->driver_ctx; 860 861 assert(orig_io == orig_ctx->orig_io); 862 863 spdk_bdev_free_io(bdev_io); 864 865 spdk_for_each_channel(orig_ctx->crypto_bdev, 866 _ch_quiesce, 867 orig_ctx, 868 _ch_quiesce_done); 869 return; 870 } 871 872 spdk_bdev_io_complete(orig_io, status); 873 spdk_bdev_free_io(bdev_io); 874 } 875 876 /* Completion callback for writes that were issued from this bdev. */ 877 static void 878 _complete_internal_write(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 879 { 880 struct spdk_bdev_io *orig_io = cb_arg; 881 int status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED; 882 struct crypto_bdev_io *orig_ctx = (struct crypto_bdev_io *)orig_io->driver_ctx; 883 884 spdk_free(orig_ctx->cry_iov.iov_base); 885 spdk_bdev_io_complete(orig_io, status); 886 spdk_bdev_free_io(bdev_io); 887 } 888 889 /* Completion callback for reads that were issued from this bdev. */ 890 static void 891 _complete_internal_read(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 892 { 893 struct spdk_bdev_io *orig_io = cb_arg; 894 struct crypto_bdev_io *orig_ctx = (struct crypto_bdev_io *)orig_io->driver_ctx; 895 896 if (success) { 897 898 /* Save off this bdev_io so it can be freed after decryption. */ 899 orig_ctx->read_io = bdev_io; 900 901 if (!_crypto_operation(orig_io, RTE_CRYPTO_CIPHER_OP_DECRYPT)) { 902 return; 903 } else { 904 SPDK_ERRLOG("ERROR decrypting\n"); 905 } 906 } else { 907 SPDK_ERRLOG("ERROR on read prior to decrypting\n"); 908 } 909 910 spdk_bdev_io_complete(orig_io, SPDK_BDEV_IO_STATUS_FAILED); 911 spdk_bdev_free_io(bdev_io); 912 } 913 914 static void 915 vbdev_crypto_resubmit_io(void *arg) 916 { 917 struct spdk_bdev_io *bdev_io = (struct spdk_bdev_io *)arg; 918 struct crypto_bdev_io *io_ctx = (struct crypto_bdev_io *)bdev_io->driver_ctx; 919 920 vbdev_crypto_submit_request(io_ctx->ch, bdev_io); 921 } 922 923 static void 924 vbdev_crypto_queue_io(struct spdk_bdev_io *bdev_io) 925 { 926 struct crypto_bdev_io *io_ctx = (struct crypto_bdev_io *)bdev_io->driver_ctx; 927 int rc; 928 929 io_ctx->bdev_io_wait.bdev = bdev_io->bdev; 930 io_ctx->bdev_io_wait.cb_fn = vbdev_crypto_resubmit_io; 931 io_ctx->bdev_io_wait.cb_arg = bdev_io; 932 933 rc = spdk_bdev_queue_io_wait(bdev_io->bdev, io_ctx->ch, &io_ctx->bdev_io_wait); 934 if (rc != 0) { 935 SPDK_ERRLOG("Queue io failed in vbdev_crypto_queue_io, rc=%d.\n", rc); 936 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 937 } 938 } 939 940 /* Callback for getting a buf from the bdev pool in the event that the caller passed 941 * in NULL, we need to own the buffer so it doesn't get freed by another vbdev module 942 * beneath us before we're done with it. 943 */ 944 static void 945 crypto_read_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, 946 bool success) 947 { 948 struct vbdev_crypto *crypto_bdev = SPDK_CONTAINEROF(bdev_io->bdev, struct vbdev_crypto, 949 crypto_bdev); 950 struct crypto_io_channel *crypto_ch = spdk_io_channel_get_ctx(ch); 951 struct crypto_bdev_io *io_ctx = (struct crypto_bdev_io *)bdev_io->driver_ctx; 952 int rc; 953 954 if (!success) { 955 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 956 return; 957 } 958 959 rc = spdk_bdev_readv_blocks(crypto_bdev->base_desc, crypto_ch->base_ch, bdev_io->u.bdev.iovs, 960 bdev_io->u.bdev.iovcnt, bdev_io->u.bdev.offset_blocks, 961 bdev_io->u.bdev.num_blocks, _complete_internal_read, 962 bdev_io); 963 if (rc != 0) { 964 if (rc == -ENOMEM) { 965 SPDK_DEBUGLOG(SPDK_LOG_CRYPTO, "No memory, queue the IO.\n"); 966 io_ctx->ch = ch; 967 vbdev_crypto_queue_io(bdev_io); 968 } else { 969 SPDK_ERRLOG("ERROR on bdev_io submission!\n"); 970 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 971 } 972 } 973 } 974 975 /* Called when someone submits IO to this crypto vbdev. For IO's not relevant to crypto, 976 * we're simply passing it on here via SPDK IO calls which in turn allocate another bdev IO 977 * and call our cpl callback provided below along with the original bdev_io so that we can 978 * complete it once this IO completes. For crypto operations, we'll either encrypt it first 979 * (writes) then call back into bdev to submit it or we'll submit a read and then catch it 980 * on the way back for decryption. 981 */ 982 static void 983 vbdev_crypto_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io) 984 { 985 struct vbdev_crypto *crypto_bdev = SPDK_CONTAINEROF(bdev_io->bdev, struct vbdev_crypto, 986 crypto_bdev); 987 struct crypto_io_channel *crypto_ch = spdk_io_channel_get_ctx(ch); 988 struct crypto_bdev_io *io_ctx = (struct crypto_bdev_io *)bdev_io->driver_ctx; 989 int rc = 0; 990 991 memset(io_ctx, 0, sizeof(struct crypto_bdev_io)); 992 io_ctx->crypto_bdev = crypto_bdev; 993 io_ctx->crypto_ch = crypto_ch; 994 io_ctx->orig_io = bdev_io; 995 996 switch (bdev_io->type) { 997 case SPDK_BDEV_IO_TYPE_READ: 998 spdk_bdev_io_get_buf(bdev_io, crypto_read_get_buf_cb, 999 bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen); 1000 break; 1001 case SPDK_BDEV_IO_TYPE_WRITE: 1002 rc = _crypto_operation(bdev_io, RTE_CRYPTO_CIPHER_OP_ENCRYPT); 1003 break; 1004 case SPDK_BDEV_IO_TYPE_UNMAP: 1005 rc = spdk_bdev_unmap_blocks(crypto_bdev->base_desc, crypto_ch->base_ch, 1006 bdev_io->u.bdev.offset_blocks, 1007 bdev_io->u.bdev.num_blocks, 1008 _complete_internal_io, bdev_io); 1009 break; 1010 case SPDK_BDEV_IO_TYPE_FLUSH: 1011 rc = spdk_bdev_flush_blocks(crypto_bdev->base_desc, crypto_ch->base_ch, 1012 bdev_io->u.bdev.offset_blocks, 1013 bdev_io->u.bdev.num_blocks, 1014 _complete_internal_io, bdev_io); 1015 break; 1016 case SPDK_BDEV_IO_TYPE_RESET: 1017 rc = spdk_bdev_reset(crypto_bdev->base_desc, crypto_ch->base_ch, 1018 _complete_internal_io, bdev_io); 1019 break; 1020 case SPDK_BDEV_IO_TYPE_WRITE_ZEROES: 1021 default: 1022 SPDK_ERRLOG("crypto: unknown I/O type %d\n", bdev_io->type); 1023 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 1024 return; 1025 } 1026 1027 if (rc != 0) { 1028 if (rc == -ENOMEM) { 1029 SPDK_DEBUGLOG(SPDK_LOG_CRYPTO, "No memory, queue the IO.\n"); 1030 io_ctx->ch = ch; 1031 vbdev_crypto_queue_io(bdev_io); 1032 } else { 1033 SPDK_ERRLOG("ERROR on bdev_io submission!\n"); 1034 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 1035 } 1036 } 1037 } 1038 1039 /* We'll just call the base bdev and let it answer except for WZ command which 1040 * we always say we don't support so that the bdev layer will actually send us 1041 * real writes that we can encrypt. 1042 */ 1043 static bool 1044 vbdev_crypto_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type) 1045 { 1046 struct vbdev_crypto *crypto_bdev = (struct vbdev_crypto *)ctx; 1047 1048 switch (io_type) { 1049 case SPDK_BDEV_IO_TYPE_WRITE: 1050 case SPDK_BDEV_IO_TYPE_UNMAP: 1051 case SPDK_BDEV_IO_TYPE_RESET: 1052 case SPDK_BDEV_IO_TYPE_READ: 1053 case SPDK_BDEV_IO_TYPE_FLUSH: 1054 return spdk_bdev_io_type_supported(crypto_bdev->base_bdev, io_type); 1055 case SPDK_BDEV_IO_TYPE_WRITE_ZEROES: 1056 /* Force the bdev layer to issue actual writes of zeroes so we can 1057 * encrypt them as regular writes. 1058 */ 1059 default: 1060 return false; 1061 } 1062 } 1063 1064 /* Callback for unregistering the IO device. */ 1065 static void 1066 _device_unregister_cb(void *io_device) 1067 { 1068 struct vbdev_crypto *crypto_bdev = io_device; 1069 1070 /* Done with this crypto_bdev. */ 1071 rte_cryptodev_sym_session_free(crypto_bdev->session_decrypt); 1072 rte_cryptodev_sym_session_free(crypto_bdev->session_encrypt); 1073 free(crypto_bdev->drv_name); 1074 free(crypto_bdev->key); 1075 free(crypto_bdev->crypto_bdev.name); 1076 free(crypto_bdev); 1077 } 1078 1079 /* Called after we've unregistered following a hot remove callback. 1080 * Our finish entry point will be called next. 1081 */ 1082 static int 1083 vbdev_crypto_destruct(void *ctx) 1084 { 1085 struct vbdev_crypto *crypto_bdev = (struct vbdev_crypto *)ctx; 1086 1087 /* Remove this device from the internal list */ 1088 TAILQ_REMOVE(&g_vbdev_crypto, crypto_bdev, link); 1089 1090 /* Unclaim the underlying bdev. */ 1091 spdk_bdev_module_release_bdev(crypto_bdev->base_bdev); 1092 1093 /* Close the underlying bdev. */ 1094 spdk_bdev_close(crypto_bdev->base_desc); 1095 1096 /* Unregister the io_device. */ 1097 spdk_io_device_unregister(crypto_bdev, _device_unregister_cb); 1098 1099 return 0; 1100 } 1101 1102 /* We supplied this as an entry point for upper layers who want to communicate to this 1103 * bdev. This is how they get a channel. We are passed the same context we provided when 1104 * we created our crypto vbdev in examine() which, for this bdev, is the address of one of 1105 * our context nodes. From here we'll ask the SPDK channel code to fill out our channel 1106 * struct and we'll keep it in our crypto node. 1107 */ 1108 static struct spdk_io_channel * 1109 vbdev_crypto_get_io_channel(void *ctx) 1110 { 1111 struct vbdev_crypto *crypto_bdev = (struct vbdev_crypto *)ctx; 1112 1113 /* The IO channel code will allocate a channel for us which consists of 1114 * the SPDK channel structure plus the size of our crypto_io_channel struct 1115 * that we passed in when we registered our IO device. It will then call 1116 * our channel create callback to populate any elements that we need to 1117 * update. 1118 */ 1119 return spdk_get_io_channel(crypto_bdev); 1120 } 1121 1122 /* This is the output for bdev_get_bdevs() for this vbdev */ 1123 static int 1124 vbdev_crypto_dump_info_json(void *ctx, struct spdk_json_write_ctx *w) 1125 { 1126 struct vbdev_crypto *crypto_bdev = (struct vbdev_crypto *)ctx; 1127 1128 spdk_json_write_name(w, "crypto"); 1129 spdk_json_write_object_begin(w); 1130 spdk_json_write_named_string(w, "base_bdev_name", spdk_bdev_get_name(crypto_bdev->base_bdev)); 1131 spdk_json_write_named_string(w, "name", spdk_bdev_get_name(&crypto_bdev->crypto_bdev)); 1132 spdk_json_write_named_string(w, "crypto_pmd", crypto_bdev->drv_name); 1133 spdk_json_write_named_string(w, "key", crypto_bdev->key); 1134 spdk_json_write_object_end(w); 1135 return 0; 1136 } 1137 1138 static int 1139 vbdev_crypto_config_json(struct spdk_json_write_ctx *w) 1140 { 1141 struct vbdev_crypto *crypto_bdev; 1142 1143 TAILQ_FOREACH(crypto_bdev, &g_vbdev_crypto, link) { 1144 spdk_json_write_object_begin(w); 1145 spdk_json_write_named_string(w, "method", "bdev_crypto_create"); 1146 spdk_json_write_named_object_begin(w, "params"); 1147 spdk_json_write_named_string(w, "base_bdev_name", spdk_bdev_get_name(crypto_bdev->base_bdev)); 1148 spdk_json_write_named_string(w, "name", spdk_bdev_get_name(&crypto_bdev->crypto_bdev)); 1149 spdk_json_write_named_string(w, "crypto_pmd", crypto_bdev->drv_name); 1150 spdk_json_write_named_string(w, "key", crypto_bdev->key); 1151 spdk_json_write_object_end(w); 1152 spdk_json_write_object_end(w); 1153 } 1154 return 0; 1155 } 1156 1157 /* We provide this callback for the SPDK channel code to create a channel using 1158 * the channel struct we provided in our module get_io_channel() entry point. Here 1159 * we get and save off an underlying base channel of the device below us so that 1160 * we can communicate with the base bdev on a per channel basis. We also register the 1161 * poller used to complete crypto operations from the device. 1162 */ 1163 static int 1164 crypto_bdev_ch_create_cb(void *io_device, void *ctx_buf) 1165 { 1166 struct crypto_io_channel *crypto_ch = ctx_buf; 1167 struct vbdev_crypto *crypto_bdev = io_device; 1168 struct device_qp *device_qp; 1169 1170 crypto_ch->base_ch = spdk_bdev_get_io_channel(crypto_bdev->base_desc); 1171 crypto_ch->poller = spdk_poller_register(crypto_dev_poller, crypto_ch, 0); 1172 crypto_ch->device_qp = NULL; 1173 1174 pthread_mutex_lock(&g_device_qp_lock); 1175 TAILQ_FOREACH(device_qp, &g_device_qp, link) { 1176 if ((strcmp(device_qp->device->cdev_info.driver_name, crypto_bdev->drv_name) == 0) && 1177 (device_qp->in_use == false)) { 1178 crypto_ch->device_qp = device_qp; 1179 device_qp->in_use = true; 1180 break; 1181 } 1182 } 1183 pthread_mutex_unlock(&g_device_qp_lock); 1184 assert(crypto_ch->device_qp); 1185 1186 /* We use this queue to track outstanding IO in our layer. */ 1187 TAILQ_INIT(&crypto_ch->pending_cry_ios); 1188 1189 return 0; 1190 } 1191 1192 /* We provide this callback for the SPDK channel code to destroy a channel 1193 * created with our create callback. We just need to undo anything we did 1194 * when we created. 1195 */ 1196 static void 1197 crypto_bdev_ch_destroy_cb(void *io_device, void *ctx_buf) 1198 { 1199 struct crypto_io_channel *crypto_ch = ctx_buf; 1200 1201 pthread_mutex_lock(&g_device_qp_lock); 1202 crypto_ch->device_qp->in_use = false; 1203 pthread_mutex_unlock(&g_device_qp_lock); 1204 1205 spdk_poller_unregister(&crypto_ch->poller); 1206 spdk_put_io_channel(crypto_ch->base_ch); 1207 } 1208 1209 /* Create the association from the bdev and vbdev name and insert 1210 * on the global list. */ 1211 static int 1212 vbdev_crypto_insert_name(const char *bdev_name, const char *vbdev_name, 1213 const char *crypto_pmd, const char *key) 1214 { 1215 struct bdev_names *name; 1216 int rc, j; 1217 bool found = false; 1218 1219 TAILQ_FOREACH(name, &g_bdev_names, link) { 1220 if (strcmp(vbdev_name, name->vbdev_name) == 0) { 1221 SPDK_ERRLOG("crypto bdev %s already exists\n", vbdev_name); 1222 return -EEXIST; 1223 } 1224 } 1225 1226 name = calloc(1, sizeof(struct bdev_names)); 1227 if (!name) { 1228 SPDK_ERRLOG("could not allocate bdev_names\n"); 1229 return -ENOMEM; 1230 } 1231 1232 name->bdev_name = strdup(bdev_name); 1233 if (!name->bdev_name) { 1234 SPDK_ERRLOG("could not allocate name->bdev_name\n"); 1235 rc = -ENOMEM; 1236 goto error_alloc_bname; 1237 } 1238 1239 name->vbdev_name = strdup(vbdev_name); 1240 if (!name->vbdev_name) { 1241 SPDK_ERRLOG("could not allocate name->vbdev_name\n"); 1242 rc = -ENOMEM; 1243 goto error_alloc_vname; 1244 } 1245 1246 name->drv_name = strdup(crypto_pmd); 1247 if (!name->drv_name) { 1248 SPDK_ERRLOG("could not allocate name->drv_name\n"); 1249 rc = -ENOMEM; 1250 goto error_alloc_dname; 1251 } 1252 for (j = 0; j < MAX_NUM_DRV_TYPES ; j++) { 1253 if (strcmp(crypto_pmd, g_driver_names[j]) == 0) { 1254 found = true; 1255 break; 1256 } 1257 } 1258 if (!found) { 1259 SPDK_ERRLOG("invalid crypto PMD type %s\n", crypto_pmd); 1260 rc = -EINVAL; 1261 goto error_invalid_pmd; 1262 } 1263 1264 name->key = strdup(key); 1265 if (!name->key) { 1266 SPDK_ERRLOG("could not allocate name->key\n"); 1267 rc = -ENOMEM; 1268 goto error_alloc_key; 1269 } 1270 if (strlen(name->key) != AES_CBC_KEY_LENGTH) { 1271 SPDK_ERRLOG("invalid AES_CCB key length\n"); 1272 rc = -EINVAL; 1273 goto error_invalid_key; 1274 } 1275 1276 TAILQ_INSERT_TAIL(&g_bdev_names, name, link); 1277 1278 return 0; 1279 1280 /* Error cleanup paths. */ 1281 error_invalid_key: 1282 error_alloc_key: 1283 error_invalid_pmd: 1284 free(name->drv_name); 1285 error_alloc_dname: 1286 free(name->vbdev_name); 1287 error_alloc_vname: 1288 free(name->bdev_name); 1289 error_alloc_bname: 1290 free(name); 1291 return rc; 1292 } 1293 1294 /* RPC entry point for crypto creation. */ 1295 int 1296 create_crypto_disk(const char *bdev_name, const char *vbdev_name, 1297 const char *crypto_pmd, const char *key) 1298 { 1299 struct spdk_bdev *bdev = NULL; 1300 int rc = 0; 1301 1302 bdev = spdk_bdev_get_by_name(bdev_name); 1303 1304 rc = vbdev_crypto_insert_name(bdev_name, vbdev_name, crypto_pmd, key); 1305 if (rc) { 1306 return rc; 1307 } 1308 1309 if (!bdev) { 1310 SPDK_NOTICELOG("vbdev creation deferred pending base bdev arrival\n"); 1311 return 0; 1312 } 1313 1314 rc = vbdev_crypto_claim(bdev); 1315 if (rc) { 1316 return rc; 1317 } 1318 1319 return rc; 1320 } 1321 1322 /* Called at driver init time, parses config file to prepare for examine calls, 1323 * also fully initializes the crypto drivers. 1324 */ 1325 static int 1326 vbdev_crypto_init(void) 1327 { 1328 struct spdk_conf_section *sp = NULL; 1329 const char *conf_bdev_name = NULL; 1330 const char *conf_vbdev_name = NULL; 1331 const char *crypto_pmd = NULL; 1332 int i; 1333 int rc = 0; 1334 const char *key = NULL; 1335 1336 /* Fully configure both SW and HW drivers. */ 1337 rc = vbdev_crypto_init_crypto_drivers(); 1338 if (rc) { 1339 SPDK_ERRLOG("Error setting up crypto devices\n"); 1340 return rc; 1341 } 1342 1343 sp = spdk_conf_find_section(NULL, "crypto"); 1344 if (sp == NULL) { 1345 return 0; 1346 } 1347 1348 for (i = 0; ; i++) { 1349 1350 if (!spdk_conf_section_get_nval(sp, "CRY", i)) { 1351 break; 1352 } 1353 1354 conf_bdev_name = spdk_conf_section_get_nmval(sp, "CRY", i, 0); 1355 if (!conf_bdev_name) { 1356 SPDK_ERRLOG("crypto configuration missing bdev name\n"); 1357 return -EINVAL; 1358 } 1359 1360 conf_vbdev_name = spdk_conf_section_get_nmval(sp, "CRY", i, 1); 1361 if (!conf_vbdev_name) { 1362 SPDK_ERRLOG("crypto configuration missing crypto_bdev name\n"); 1363 return -EINVAL; 1364 } 1365 1366 key = spdk_conf_section_get_nmval(sp, "CRY", i, 2); 1367 if (!key) { 1368 SPDK_ERRLOG("crypto configuration missing crypto_bdev key\n"); 1369 return -EINVAL; 1370 } 1371 SPDK_NOTICELOG("WARNING: You are storing your key in a plain text file!!\n"); 1372 1373 crypto_pmd = spdk_conf_section_get_nmval(sp, "CRY", i, 3); 1374 if (!crypto_pmd) { 1375 SPDK_ERRLOG("crypto configuration missing driver type\n"); 1376 return -EINVAL; 1377 } 1378 1379 rc = vbdev_crypto_insert_name(conf_bdev_name, conf_vbdev_name, 1380 crypto_pmd, key); 1381 if (rc != 0) { 1382 return rc; 1383 } 1384 } 1385 1386 return rc; 1387 } 1388 1389 /* Called when the entire module is being torn down. */ 1390 static void 1391 vbdev_crypto_finish(void) 1392 { 1393 struct bdev_names *name; 1394 struct vbdev_dev *device; 1395 struct device_qp *dev_qp; 1396 unsigned i; 1397 int rc; 1398 1399 while ((name = TAILQ_FIRST(&g_bdev_names))) { 1400 TAILQ_REMOVE(&g_bdev_names, name, link); 1401 free(name->drv_name); 1402 free(name->key); 1403 free(name->bdev_name); 1404 free(name->vbdev_name); 1405 free(name); 1406 } 1407 1408 while ((device = TAILQ_FIRST(&g_vbdev_devs))) { 1409 struct rte_cryptodev *rte_dev; 1410 1411 TAILQ_REMOVE(&g_vbdev_devs, device, link); 1412 rte_cryptodev_stop(device->cdev_id); 1413 1414 assert(device->cdev_id < RTE_CRYPTO_MAX_DEVS); 1415 rte_dev = &rte_cryptodevs[device->cdev_id]; 1416 1417 if (rte_dev->dev_ops->queue_pair_release != NULL) { 1418 for (i = 0; i < device->cdev_info.max_nb_queue_pairs; i++) { 1419 rte_dev->dev_ops->queue_pair_release(rte_dev, i); 1420 } 1421 } 1422 free(device); 1423 } 1424 rc = rte_vdev_uninit(AESNI_MB); 1425 if (rc) { 1426 SPDK_ERRLOG("%d from rte_vdev_uninit\n", rc); 1427 } 1428 1429 while ((dev_qp = TAILQ_FIRST(&g_device_qp))) { 1430 TAILQ_REMOVE(&g_device_qp, dev_qp, link); 1431 free(dev_qp); 1432 } 1433 1434 rte_mempool_free(g_crypto_op_mp); 1435 spdk_mempool_free(g_mbuf_mp); 1436 rte_mempool_free(g_session_mp); 1437 if (g_session_mp_priv != NULL) { 1438 rte_mempool_free(g_session_mp_priv); 1439 } 1440 } 1441 1442 /* During init we'll be asked how much memory we'd like passed to us 1443 * in bev_io structures as context. Here's where we specify how 1444 * much context we want per IO. 1445 */ 1446 static int 1447 vbdev_crypto_get_ctx_size(void) 1448 { 1449 return sizeof(struct crypto_bdev_io); 1450 } 1451 1452 /* Called when SPDK wants to save the current config of this vbdev module to 1453 * a file. 1454 */ 1455 static void 1456 vbdev_crypto_get_spdk_running_config(FILE *fp) 1457 { 1458 struct bdev_names *names = NULL; 1459 fprintf(fp, "\n[crypto]\n"); 1460 TAILQ_FOREACH(names, &g_bdev_names, link) { 1461 fprintf(fp, " crypto %s %s ", names->bdev_name, names->vbdev_name); 1462 fprintf(fp, "\n"); 1463 } 1464 1465 fprintf(fp, "\n"); 1466 } 1467 1468 /* Called when the underlying base bdev goes away. */ 1469 static void 1470 vbdev_crypto_examine_hotremove_cb(void *ctx) 1471 { 1472 struct vbdev_crypto *crypto_bdev, *tmp; 1473 struct spdk_bdev *bdev_find = ctx; 1474 1475 TAILQ_FOREACH_SAFE(crypto_bdev, &g_vbdev_crypto, link, tmp) { 1476 if (bdev_find == crypto_bdev->base_bdev) { 1477 spdk_bdev_unregister(&crypto_bdev->crypto_bdev, NULL, NULL); 1478 } 1479 } 1480 } 1481 1482 static void 1483 vbdev_crypto_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w) 1484 { 1485 /* No config per bdev needed */ 1486 } 1487 1488 /* When we register our bdev this is how we specify our entry points. */ 1489 static const struct spdk_bdev_fn_table vbdev_crypto_fn_table = { 1490 .destruct = vbdev_crypto_destruct, 1491 .submit_request = vbdev_crypto_submit_request, 1492 .io_type_supported = vbdev_crypto_io_type_supported, 1493 .get_io_channel = vbdev_crypto_get_io_channel, 1494 .dump_info_json = vbdev_crypto_dump_info_json, 1495 .write_config_json = vbdev_crypto_write_config_json 1496 }; 1497 1498 static struct spdk_bdev_module crypto_if = { 1499 .name = "crypto", 1500 .module_init = vbdev_crypto_init, 1501 .config_text = vbdev_crypto_get_spdk_running_config, 1502 .get_ctx_size = vbdev_crypto_get_ctx_size, 1503 .examine_config = vbdev_crypto_examine, 1504 .module_fini = vbdev_crypto_finish, 1505 .config_json = vbdev_crypto_config_json 1506 }; 1507 1508 SPDK_BDEV_MODULE_REGISTER(crypto, &crypto_if) 1509 1510 static int 1511 vbdev_crypto_claim(struct spdk_bdev *bdev) 1512 { 1513 struct bdev_names *name; 1514 struct vbdev_crypto *vbdev; 1515 struct vbdev_dev *device; 1516 bool found = false; 1517 int rc = 0; 1518 1519 /* Check our list of names from config versus this bdev and if 1520 * there's a match, create the crypto_bdev & bdev accordingly. 1521 */ 1522 TAILQ_FOREACH(name, &g_bdev_names, link) { 1523 if (strcmp(name->bdev_name, bdev->name) != 0) { 1524 continue; 1525 } 1526 SPDK_DEBUGLOG(SPDK_LOG_CRYPTO, "Match on %s\n", bdev->name); 1527 1528 vbdev = calloc(1, sizeof(struct vbdev_crypto)); 1529 if (!vbdev) { 1530 SPDK_ERRLOG("could not allocate crypto_bdev\n"); 1531 rc = -ENOMEM; 1532 goto error_vbdev_alloc; 1533 } 1534 1535 /* The base bdev that we're attaching to. */ 1536 vbdev->base_bdev = bdev; 1537 vbdev->crypto_bdev.name = strdup(name->vbdev_name); 1538 if (!vbdev->crypto_bdev.name) { 1539 SPDK_ERRLOG("could not allocate crypto_bdev name\n"); 1540 rc = -ENOMEM; 1541 goto error_bdev_name; 1542 } 1543 1544 vbdev->key = strdup(name->key); 1545 if (!vbdev->key) { 1546 SPDK_ERRLOG("could not allocate crypto_bdev key\n"); 1547 rc = -ENOMEM; 1548 goto error_alloc_key; 1549 } 1550 1551 vbdev->drv_name = strdup(name->drv_name); 1552 if (!vbdev->drv_name) { 1553 SPDK_ERRLOG("could not allocate crypto_bdev drv_name\n"); 1554 rc = -ENOMEM; 1555 goto error_drv_name; 1556 } 1557 1558 vbdev->crypto_bdev.product_name = "crypto"; 1559 vbdev->crypto_bdev.write_cache = bdev->write_cache; 1560 if (strcmp(vbdev->drv_name, QAT) == 0) { 1561 vbdev->crypto_bdev.required_alignment = 1562 spdk_max(spdk_u32log2(bdev->blocklen), bdev->required_alignment); 1563 SPDK_NOTICELOG("QAT in use: Required alignment set to %u\n", 1564 vbdev->crypto_bdev.required_alignment); 1565 } else { 1566 vbdev->crypto_bdev.required_alignment = bdev->required_alignment; 1567 } 1568 /* Note: CRYPTO_MAX_IO is in units of bytes, optimal_io_boundary is 1569 * in units of blocks. 1570 */ 1571 if (bdev->optimal_io_boundary > 0) { 1572 vbdev->crypto_bdev.optimal_io_boundary = 1573 spdk_min((CRYPTO_MAX_IO / bdev->blocklen), bdev->optimal_io_boundary); 1574 } else { 1575 vbdev->crypto_bdev.optimal_io_boundary = (CRYPTO_MAX_IO / bdev->blocklen); 1576 } 1577 vbdev->crypto_bdev.split_on_optimal_io_boundary = true; 1578 vbdev->crypto_bdev.blocklen = bdev->blocklen; 1579 vbdev->crypto_bdev.blockcnt = bdev->blockcnt; 1580 1581 /* This is the context that is passed to us when the bdev 1582 * layer calls in so we'll save our crypto_bdev node here. 1583 */ 1584 vbdev->crypto_bdev.ctxt = vbdev; 1585 vbdev->crypto_bdev.fn_table = &vbdev_crypto_fn_table; 1586 vbdev->crypto_bdev.module = &crypto_if; 1587 TAILQ_INSERT_TAIL(&g_vbdev_crypto, vbdev, link); 1588 1589 spdk_io_device_register(vbdev, crypto_bdev_ch_create_cb, crypto_bdev_ch_destroy_cb, 1590 sizeof(struct crypto_io_channel), vbdev->crypto_bdev.name); 1591 1592 rc = spdk_bdev_open(bdev, true, vbdev_crypto_examine_hotremove_cb, 1593 bdev, &vbdev->base_desc); 1594 if (rc) { 1595 SPDK_ERRLOG("could not open bdev %s\n", spdk_bdev_get_name(bdev)); 1596 goto error_open; 1597 } 1598 1599 rc = spdk_bdev_module_claim_bdev(bdev, vbdev->base_desc, vbdev->crypto_bdev.module); 1600 if (rc) { 1601 SPDK_ERRLOG("could not claim bdev %s\n", spdk_bdev_get_name(bdev)); 1602 goto error_claim; 1603 } 1604 1605 /* To init the session we have to get the cryptoDev device ID for this vbdev */ 1606 TAILQ_FOREACH(device, &g_vbdev_devs, link) { 1607 if (strcmp(device->cdev_info.driver_name, vbdev->drv_name) == 0) { 1608 found = true; 1609 break; 1610 } 1611 } 1612 if (found == false) { 1613 SPDK_ERRLOG("ERROR can't match crypto device driver to crypto vbdev!\n"); 1614 rc = -EINVAL; 1615 goto error_cant_find_devid; 1616 } 1617 1618 /* Get sessions. */ 1619 vbdev->session_encrypt = rte_cryptodev_sym_session_create(g_session_mp); 1620 if (NULL == vbdev->session_encrypt) { 1621 SPDK_ERRLOG("ERROR trying to create crypto session!\n"); 1622 rc = -EINVAL; 1623 goto error_session_en_create; 1624 } 1625 1626 vbdev->session_decrypt = rte_cryptodev_sym_session_create(g_session_mp); 1627 if (NULL == vbdev->session_decrypt) { 1628 SPDK_ERRLOG("ERROR trying to create crypto session!\n"); 1629 rc = -EINVAL; 1630 goto error_session_de_create; 1631 } 1632 1633 /* Init our per vbdev xform with the desired cipher options. */ 1634 vbdev->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER; 1635 vbdev->cipher_xform.cipher.key.data = vbdev->key; 1636 vbdev->cipher_xform.cipher.iv.offset = IV_OFFSET; 1637 vbdev->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC; 1638 vbdev->cipher_xform.cipher.key.length = AES_CBC_KEY_LENGTH; 1639 vbdev->cipher_xform.cipher.iv.length = AES_CBC_IV_LENGTH; 1640 1641 vbdev->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT; 1642 rc = rte_cryptodev_sym_session_init(device->cdev_id, vbdev->session_encrypt, 1643 &vbdev->cipher_xform, 1644 g_session_mp_priv ? g_session_mp_priv : g_session_mp); 1645 if (rc < 0) { 1646 SPDK_ERRLOG("ERROR trying to init encrypt session!\n"); 1647 rc = -EINVAL; 1648 goto error_session_init; 1649 } 1650 1651 vbdev->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT; 1652 rc = rte_cryptodev_sym_session_init(device->cdev_id, vbdev->session_decrypt, 1653 &vbdev->cipher_xform, 1654 g_session_mp_priv ? g_session_mp_priv : g_session_mp); 1655 if (rc < 0) { 1656 SPDK_ERRLOG("ERROR trying to init decrypt session!\n"); 1657 rc = -EINVAL; 1658 goto error_session_init; 1659 } 1660 1661 rc = spdk_bdev_register(&vbdev->crypto_bdev); 1662 if (rc < 0) { 1663 SPDK_ERRLOG("ERROR trying to register bdev\n"); 1664 rc = -EINVAL; 1665 goto error_bdev_register; 1666 } 1667 SPDK_DEBUGLOG(SPDK_LOG_CRYPTO, "registered io_device and virtual bdev for: %s\n", 1668 name->vbdev_name); 1669 break; 1670 } 1671 1672 return rc; 1673 1674 /* Error cleanup paths. */ 1675 error_bdev_register: 1676 error_session_init: 1677 rte_cryptodev_sym_session_free(vbdev->session_decrypt); 1678 error_session_de_create: 1679 rte_cryptodev_sym_session_free(vbdev->session_encrypt); 1680 error_session_en_create: 1681 error_cant_find_devid: 1682 error_claim: 1683 spdk_bdev_close(vbdev->base_desc); 1684 error_open: 1685 TAILQ_REMOVE(&g_vbdev_crypto, vbdev, link); 1686 spdk_io_device_unregister(vbdev, NULL); 1687 free(vbdev->drv_name); 1688 error_drv_name: 1689 free(vbdev->key); 1690 error_alloc_key: 1691 free(vbdev->crypto_bdev.name); 1692 error_bdev_name: 1693 free(vbdev); 1694 error_vbdev_alloc: 1695 return rc; 1696 } 1697 1698 /* RPC entry for deleting a crypto vbdev. */ 1699 void 1700 delete_crypto_disk(struct spdk_bdev *bdev, spdk_delete_crypto_complete cb_fn, 1701 void *cb_arg) 1702 { 1703 struct bdev_names *name; 1704 1705 if (!bdev || bdev->module != &crypto_if) { 1706 cb_fn(cb_arg, -ENODEV); 1707 return; 1708 } 1709 1710 /* Remove the association (vbdev, bdev) from g_bdev_names. This is required so that the 1711 * vbdev does not get re-created if the same bdev is constructed at some other time, 1712 * unless the underlying bdev was hot-removed. 1713 */ 1714 TAILQ_FOREACH(name, &g_bdev_names, link) { 1715 if (strcmp(name->vbdev_name, bdev->name) == 0) { 1716 TAILQ_REMOVE(&g_bdev_names, name, link); 1717 free(name->bdev_name); 1718 free(name->vbdev_name); 1719 free(name->drv_name); 1720 free(name->key); 1721 free(name); 1722 break; 1723 } 1724 } 1725 1726 /* Additional cleanup happens in the destruct callback. */ 1727 spdk_bdev_unregister(bdev, cb_fn, cb_arg); 1728 } 1729 1730 /* Because we specified this function in our crypto bdev function table when we 1731 * registered our crypto bdev, we'll get this call anytime a new bdev shows up. 1732 * Here we need to decide if we care about it and if so what to do. We 1733 * parsed the config file at init so we check the new bdev against the list 1734 * we built up at that time and if the user configured us to attach to this 1735 * bdev, here's where we do it. 1736 */ 1737 static void 1738 vbdev_crypto_examine(struct spdk_bdev *bdev) 1739 { 1740 vbdev_crypto_claim(bdev); 1741 spdk_bdev_module_examine_done(&crypto_if); 1742 } 1743 1744 SPDK_LOG_REGISTER_COMPONENT("vbdev_crypto", SPDK_LOG_CRYPTO) 1745