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