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