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