1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (C) 2018 Intel Corporation. 3 * Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. 4 * All rights reserved. 5 */ 6 7 #include "accel_dpdk_cryptodev.h" 8 9 #include "spdk/accel.h" 10 #include "spdk_internal/accel_module.h" 11 #include "spdk/env.h" 12 #include "spdk/likely.h" 13 #include "spdk/thread.h" 14 #include "spdk/util.h" 15 #include "spdk/log.h" 16 #include "spdk/json.h" 17 #include "spdk_internal/sgl.h" 18 19 #include <rte_bus_vdev.h> 20 #include <rte_crypto.h> 21 #include <rte_cryptodev.h> 22 #include <rte_mbuf_dyn.h> 23 #include <rte_version.h> 24 25 /* The VF spread is the number of queue pairs between virtual functions, we use this to 26 * load balance the QAT device. 27 */ 28 #define ACCEL_DPDK_CRYPTODEV_QAT_VF_SPREAD 32 29 30 /* This controls how many ops will be dequeued from the crypto driver in one run 31 * of the poller. It is mainly a performance knob as it effectively determines how 32 * much work the poller has to do. However even that can vary between crypto drivers 33 * as the ACCEL_DPDK_CRYPTODEV_AESNI_MB driver for example does all the crypto work on dequeue whereas the 34 * QAT driver just dequeues what has been completed already. 35 */ 36 #define ACCEL_DPDK_CRYPTODEV_MAX_DEQUEUE_BURST_SIZE 64 37 38 #define ACCEL_DPDK_CRYPTODEV_MAX_ENQUEUE_ARRAY_SIZE (128) 39 40 /* The number of MBUFS we need must be a power of two and to support other small IOs 41 * in addition to the limits mentioned above, we go to the next power of two. It is 42 * big number because it is one mempool for source and destination mbufs. It may 43 * need to be bigger to support multiple crypto drivers at once. 44 */ 45 #define ACCEL_DPDK_CRYPTODEV_NUM_MBUFS 32768 46 #define ACCEL_DPDK_CRYPTODEV_POOL_CACHE_SIZE 256 47 #define ACCEL_DPDK_CRYPTODEV_MAX_CRYPTO_VOLUMES 128 48 #define ACCEL_DPDK_CRYPTODEV_NUM_SESSIONS (2 * ACCEL_DPDK_CRYPTODEV_MAX_CRYPTO_VOLUMES) 49 #define ACCEL_DPDK_CRYPTODEV_SESS_MEMPOOL_CACHE_SIZE 0 50 51 /* This is the max number of IOs we can supply to any crypto device QP at one time. 52 * It can vary between drivers. 53 */ 54 #define ACCEL_DPDK_CRYPTODEV_QP_DESCRIPTORS 2048 55 56 /* At this moment DPDK descriptors allocation for mlx5 has some issues. We use 512 57 * as a compromise value between performance and the time spent for initialization. */ 58 #define ACCEL_DPDK_CRYPTODEV_QP_DESCRIPTORS_MLX5 512 59 60 #define ACCEL_DPDK_CRYPTODEV_AESNI_MB_NUM_QP 64 61 62 /* Common for suported devices. */ 63 #define ACCEL_DPDK_CRYPTODEV_DEFAULT_NUM_XFORMS 2 64 #define ACCEL_DPDK_CRYPTODEV_IV_OFFSET (sizeof(struct rte_crypto_op) + \ 65 sizeof(struct rte_crypto_sym_op) + \ 66 (ACCEL_DPDK_CRYPTODEV_DEFAULT_NUM_XFORMS * \ 67 sizeof(struct rte_crypto_sym_xform))) 68 #define ACCEL_DPDK_CRYPTODEV_IV_LENGTH 16 69 #define ACCEL_DPDK_CRYPTODEV_QUEUED_OP_OFFSET (ACCEL_DPDK_CRYPTODEV_IV_OFFSET + ACCEL_DPDK_CRYPTODEV_IV_LENGTH) 70 71 /* Driver names */ 72 #define ACCEL_DPDK_CRYPTODEV_AESNI_MB "crypto_aesni_mb" 73 #define ACCEL_DPDK_CRYPTODEV_QAT "crypto_qat" 74 #define ACCEL_DPDK_CRYPTODEV_QAT_ASYM "crypto_qat_asym" 75 #define ACCEL_DPDK_CRYPTODEV_MLX5 "mlx5_pci" 76 77 /* Supported ciphers */ 78 #define ACCEL_DPDK_CRYPTODEV_AES_CBC "AES_CBC" /* QAT and ACCEL_DPDK_CRYPTODEV_AESNI_MB */ 79 #define ACCEL_DPDK_CRYPTODEV_AES_XTS "AES_XTS" /* QAT and MLX5 */ 80 81 /* Specific to AES_CBC. */ 82 #define ACCEL_DPDK_CRYPTODEV_AES_CBC_KEY_LENGTH 16 83 #define ACCEL_DPDK_CRYPTODEV_AES_XTS_128_BLOCK_KEY_LENGTH 16 /* AES-XTS-128 block key size. */ 84 #define ACCEL_DPDK_CRYPTODEV_AES_XTS_256_BLOCK_KEY_LENGTH 32 /* AES-XTS-256 block key size. */ 85 #define ACCEL_DPDK_CRYPTODEV_AES_XTS_512_BLOCK_KEY_LENGTH 64 /* AES-XTS-512 block key size. */ 86 87 #define ACCEL_DPDK_CRYPTODEV_AES_XTS_TWEAK_KEY_LENGTH 16 /* XTS part key size is always 128 bit. */ 88 89 /* Limit of the max memory len attached to mbuf - rte_pktmbuf_attach_extbuf has uint16_t `buf_len` 90 * parameter, we use closes aligned value 32768 for better performance */ 91 #define ACCEL_DPDK_CRYPTODEV_MAX_MBUF_LEN 32768 92 93 /* Used to store IO context in mbuf */ 94 static const struct rte_mbuf_dynfield rte_mbuf_dynfield_io_context = { 95 .name = "context_accel_dpdk_cryptodev", 96 .size = sizeof(uint64_t), 97 .align = __alignof__(uint64_t), 98 .flags = 0, 99 }; 100 101 struct accel_dpdk_cryptodev_device; 102 103 enum accel_dpdk_cryptodev_driver_type { 104 ACCEL_DPDK_CRYPTODEV_DRIVER_AESNI_MB = 0, 105 ACCEL_DPDK_CRYPTODEV_DRIVER_QAT, 106 ACCEL_DPDK_CRYPTODEV_DRIVER_MLX5_PCI, 107 ACCEL_DPDK_CRYPTODEV_DRIVER_LAST 108 }; 109 110 enum accel_dpdk_crypto_dev_cipher_type { 111 ACCEL_DPDK_CRYPTODEV_CIPHER_AES_CBC, 112 ACCEL_DPDK_CRYPTODEV_CIPHER_AES_XTS 113 }; 114 115 struct accel_dpdk_cryptodev_qp { 116 struct accel_dpdk_cryptodev_device *device; /* ptr to crypto device */ 117 uint32_t num_enqueued_ops; /* Used to decide whether to poll the qp or not */ 118 uint8_t qp; /* queue identifier */ 119 bool in_use; /* whether this node is in use or not */ 120 uint8_t index; /* used by QAT to load balance placement of qpairs */ 121 TAILQ_ENTRY(accel_dpdk_cryptodev_qp) link; 122 }; 123 124 struct accel_dpdk_cryptodev_device { 125 enum accel_dpdk_cryptodev_driver_type type; 126 struct rte_cryptodev_info cdev_info; /* includes DPDK device friendly name */ 127 uint32_t qp_desc_nr; /* max number of qp descriptors to be enqueued in burst */ 128 uint8_t cdev_id; /* identifier for the device */ 129 TAILQ_HEAD(, accel_dpdk_cryptodev_qp) qpairs; 130 TAILQ_ENTRY(accel_dpdk_cryptodev_device) link; 131 }; 132 133 struct accel_dpdk_cryptodev_key_handle { 134 struct accel_dpdk_cryptodev_device *device; 135 TAILQ_ENTRY(accel_dpdk_cryptodev_key_handle) link; 136 void *session_encrypt; /* encryption session for this key */ 137 void *session_decrypt; /* decryption session for this key */ 138 struct rte_crypto_sym_xform cipher_xform; /* crypto control struct for this key */ 139 }; 140 141 struct accel_dpdk_cryptodev_key_priv { 142 enum accel_dpdk_cryptodev_driver_type driver; 143 enum accel_dpdk_crypto_dev_cipher_type cipher; 144 char *xts_key; 145 TAILQ_HEAD(, accel_dpdk_cryptodev_key_handle) dev_keys; 146 }; 147 148 /* For queueing up crypto operations that we can't submit for some reason */ 149 struct accel_dpdk_cryptodev_queued_op { 150 struct accel_dpdk_cryptodev_qp *qp; 151 struct rte_crypto_op *crypto_op; 152 struct accel_dpdk_cryptodev_task *task; 153 TAILQ_ENTRY(accel_dpdk_cryptodev_queued_op) link; 154 }; 155 #define ACCEL_DPDK_CRYPTODEV_QUEUED_OP_LENGTH (sizeof(struct accel_dpdk_cryptodev_queued_op)) 156 157 /* The crypto channel struct. It is allocated and freed on my behalf by the io channel code. 158 * We store things in here that are needed on per thread basis like the base_channel for this thread, 159 * and the poller for this thread. 160 */ 161 struct accel_dpdk_cryptodev_io_channel { 162 /* completion poller */ 163 struct spdk_poller *poller; 164 /* Array of qpairs for each available device. The specific device will be selected depending on the crypto key */ 165 struct accel_dpdk_cryptodev_qp *device_qp[ACCEL_DPDK_CRYPTODEV_DRIVER_LAST]; 166 /* queued for re-submission to CryptoDev. Used when for some reason crypto op was not processed by the driver */ 167 TAILQ_HEAD(, accel_dpdk_cryptodev_queued_op) queued_cry_ops; 168 /* Used to queue tasks when qpair is full. No crypto operation was submitted to the driver by the task */ 169 TAILQ_HEAD(, accel_dpdk_cryptodev_task) queued_tasks; 170 }; 171 172 struct accel_dpdk_cryptodev_task { 173 struct spdk_accel_task base; 174 uint32_t cryop_completed; /* The number of crypto operations completed by HW */ 175 uint32_t cryop_submitted; /* The number of crypto operations submitted to HW */ 176 uint32_t cryop_total; /* Total number of crypto operations in this task */ 177 bool is_failed; 178 bool inplace; 179 TAILQ_ENTRY(accel_dpdk_cryptodev_task) link; 180 }; 181 182 /* Shared mempools between all devices on this system */ 183 static struct rte_mempool *g_session_mp = NULL; 184 static struct rte_mempool *g_session_mp_priv = NULL; 185 static struct rte_mempool *g_mbuf_mp = NULL; /* mbuf mempool */ 186 static int g_mbuf_offset; 187 static struct rte_mempool *g_crypto_op_mp = NULL; /* crypto operations, must be rte* mempool */ 188 189 static struct rte_mbuf_ext_shared_info g_shinfo = {}; /* used by DPDK mbuf macro */ 190 191 static uint8_t g_qat_total_qp = 0; 192 static uint8_t g_next_qat_index; 193 194 static const char *g_driver_names[] = { 195 [ACCEL_DPDK_CRYPTODEV_DRIVER_AESNI_MB] = ACCEL_DPDK_CRYPTODEV_AESNI_MB, 196 [ACCEL_DPDK_CRYPTODEV_DRIVER_QAT] = ACCEL_DPDK_CRYPTODEV_QAT, 197 [ACCEL_DPDK_CRYPTODEV_DRIVER_MLX5_PCI] = ACCEL_DPDK_CRYPTODEV_MLX5 198 }; 199 static const char *g_cipher_names[] = { 200 [ACCEL_DPDK_CRYPTODEV_CIPHER_AES_CBC] = ACCEL_DPDK_CRYPTODEV_AES_CBC, 201 [ACCEL_DPDK_CRYPTODEV_CIPHER_AES_XTS] = ACCEL_DPDK_CRYPTODEV_AES_XTS, 202 }; 203 204 static enum accel_dpdk_cryptodev_driver_type g_dpdk_cryptodev_driver = 205 ACCEL_DPDK_CRYPTODEV_DRIVER_AESNI_MB; 206 207 /* Global list of all crypto devices */ 208 static TAILQ_HEAD(, accel_dpdk_cryptodev_device) g_crypto_devices = TAILQ_HEAD_INITIALIZER( 209 g_crypto_devices); 210 static pthread_mutex_t g_device_lock = PTHREAD_MUTEX_INITIALIZER; 211 212 static struct spdk_accel_module_if g_accel_dpdk_cryptodev_module; 213 214 static int accel_dpdk_cryptodev_process_task(struct accel_dpdk_cryptodev_io_channel *crypto_ch, 215 struct accel_dpdk_cryptodev_task *task); 216 217 void 218 accel_dpdk_cryptodev_enable(void) 219 { 220 spdk_accel_module_list_add(&g_accel_dpdk_cryptodev_module); 221 } 222 223 int 224 accel_dpdk_cryptodev_set_driver(const char *driver_name) 225 { 226 if (strcmp(driver_name, ACCEL_DPDK_CRYPTODEV_QAT) == 0) { 227 g_dpdk_cryptodev_driver = ACCEL_DPDK_CRYPTODEV_DRIVER_QAT; 228 } else if (strcmp(driver_name, ACCEL_DPDK_CRYPTODEV_AESNI_MB) == 0) { 229 g_dpdk_cryptodev_driver = ACCEL_DPDK_CRYPTODEV_DRIVER_AESNI_MB; 230 } else if (strcmp(driver_name, ACCEL_DPDK_CRYPTODEV_MLX5) == 0) { 231 g_dpdk_cryptodev_driver = ACCEL_DPDK_CRYPTODEV_DRIVER_MLX5_PCI; 232 } else { 233 SPDK_ERRLOG("Unsupported driver %s\n", driver_name); 234 return -EINVAL; 235 } 236 237 SPDK_NOTICELOG("Using driver %s\n", driver_name); 238 239 return 0; 240 } 241 242 const char * 243 accel_dpdk_cryptodev_get_driver(void) 244 { 245 return g_driver_names[g_dpdk_cryptodev_driver]; 246 } 247 248 static void 249 cancel_queued_crypto_ops(struct accel_dpdk_cryptodev_io_channel *crypto_ch, 250 struct accel_dpdk_cryptodev_task *task) 251 { 252 struct rte_mbuf *mbufs_to_free[2 * ACCEL_DPDK_CRYPTODEV_MAX_DEQUEUE_BURST_SIZE]; 253 struct rte_crypto_op *cancelled_ops[ACCEL_DPDK_CRYPTODEV_MAX_DEQUEUE_BURST_SIZE]; 254 struct accel_dpdk_cryptodev_queued_op *op_to_cancel, *tmp_op; 255 struct rte_crypto_op *crypto_op; 256 int num_mbufs = 0, num_dequeued_ops = 0; 257 258 /* Remove all ops from the failed IO. Since we don't know the 259 * order we have to check them all. */ 260 TAILQ_FOREACH_SAFE(op_to_cancel, &crypto_ch->queued_cry_ops, link, tmp_op) { 261 /* Checking if this is our op. One IO contains multiple ops. */ 262 if (task == op_to_cancel->task) { 263 crypto_op = op_to_cancel->crypto_op; 264 TAILQ_REMOVE(&crypto_ch->queued_cry_ops, op_to_cancel, link); 265 266 /* Populating lists for freeing mbufs and ops. */ 267 mbufs_to_free[num_mbufs++] = (void *)crypto_op->sym->m_src; 268 if (crypto_op->sym->m_dst) { 269 mbufs_to_free[num_mbufs++] = (void *)crypto_op->sym->m_dst; 270 } 271 cancelled_ops[num_dequeued_ops++] = crypto_op; 272 } 273 } 274 275 /* Now bulk free both mbufs and crypto operations. */ 276 if (num_dequeued_ops > 0) { 277 rte_mempool_put_bulk(g_crypto_op_mp, (void **)cancelled_ops, 278 num_dequeued_ops); 279 assert(num_mbufs > 0); 280 /* This also releases chained mbufs if any. */ 281 rte_pktmbuf_free_bulk(mbufs_to_free, num_mbufs); 282 } 283 } 284 285 static inline uint16_t 286 accel_dpdk_cryptodev_poll_qp(struct accel_dpdk_cryptodev_qp *qp, 287 struct accel_dpdk_cryptodev_io_channel *crypto_ch) 288 { 289 struct rte_crypto_op *dequeued_ops[ACCEL_DPDK_CRYPTODEV_MAX_DEQUEUE_BURST_SIZE]; 290 struct rte_mbuf *mbufs_to_free[2 * ACCEL_DPDK_CRYPTODEV_MAX_DEQUEUE_BURST_SIZE]; 291 struct accel_dpdk_cryptodev_task *task; 292 uint32_t num_mbufs = 0; 293 int i; 294 uint16_t num_dequeued_ops; 295 296 /* Each run of the poller will get just what the device has available 297 * at the moment we call it, we don't check again after draining the 298 * first batch. 299 */ 300 num_dequeued_ops = rte_cryptodev_dequeue_burst(qp->device->cdev_id, qp->qp, 301 dequeued_ops, ACCEL_DPDK_CRYPTODEV_MAX_DEQUEUE_BURST_SIZE); 302 /* Check if operation was processed successfully */ 303 for (i = 0; i < num_dequeued_ops; i++) { 304 305 /* We don't know the order or association of the crypto ops wrt any 306 * particular task so need to look at each and determine if it's 307 * the last one for it's task or not. 308 */ 309 task = (struct accel_dpdk_cryptodev_task *)*RTE_MBUF_DYNFIELD(dequeued_ops[i]->sym->m_src, 310 g_mbuf_offset, uint64_t *); 311 assert(task != NULL); 312 313 if (dequeued_ops[i]->status != RTE_CRYPTO_OP_STATUS_SUCCESS) { 314 SPDK_ERRLOG("error with op %d status %u\n", i, dequeued_ops[i]->status); 315 /* Update the task status to error, we'll still process the 316 * rest of the crypto ops for this task though so they 317 * aren't left hanging. 318 */ 319 task->is_failed = true; 320 } 321 322 /* Return the associated src and dst mbufs by collecting them into 323 * an array that we can use the bulk API to free after the loop. 324 */ 325 *RTE_MBUF_DYNFIELD(dequeued_ops[i]->sym->m_src, g_mbuf_offset, uint64_t *) = 0; 326 mbufs_to_free[num_mbufs++] = (void *)dequeued_ops[i]->sym->m_src; 327 if (dequeued_ops[i]->sym->m_dst) { 328 mbufs_to_free[num_mbufs++] = (void *)dequeued_ops[i]->sym->m_dst; 329 } 330 331 task->cryop_completed++; 332 if (task->cryop_completed == task->cryop_total) { 333 /* Complete the IO */ 334 spdk_accel_task_complete(&task->base, task->is_failed ? -EINVAL : 0); 335 } else if (task->cryop_completed == task->cryop_submitted) { 336 /* submit remaining crypto ops */ 337 int rc = accel_dpdk_cryptodev_process_task(crypto_ch, task); 338 339 if (spdk_unlikely(rc)) { 340 if (rc == -ENOMEM) { 341 TAILQ_INSERT_TAIL(&crypto_ch->queued_tasks, task, link); 342 continue; 343 } 344 spdk_accel_task_complete(&task->base, rc); 345 } 346 } 347 } 348 349 /* Now bulk free both mbufs and crypto operations. */ 350 if (num_dequeued_ops > 0) { 351 rte_mempool_put_bulk(g_crypto_op_mp, (void **)dequeued_ops, num_dequeued_ops); 352 assert(num_mbufs > 0); 353 /* This also releases chained mbufs if any. */ 354 rte_pktmbuf_free_bulk(mbufs_to_free, num_mbufs); 355 } 356 357 assert(qp->num_enqueued_ops >= num_dequeued_ops); 358 qp->num_enqueued_ops -= num_dequeued_ops; 359 360 return num_dequeued_ops; 361 } 362 363 /* This is the poller for the crypto module. It uses a single API to dequeue whatever is ready at 364 * the device. Then we need to decide if what we've got so far (including previous poller 365 * runs) totals up to one or more complete task */ 366 static int 367 accel_dpdk_cryptodev_poller(void *args) 368 { 369 struct accel_dpdk_cryptodev_io_channel *crypto_ch = args; 370 struct accel_dpdk_cryptodev_qp *qp; 371 struct accel_dpdk_cryptodev_task *task, *task_tmp; 372 struct accel_dpdk_cryptodev_queued_op *op_to_resubmit, *op_to_resubmit_tmp; 373 TAILQ_HEAD(, accel_dpdk_cryptodev_task) queued_tasks_tmp; 374 uint32_t num_dequeued_ops = 0, num_enqueued_ops = 0; 375 uint16_t enqueued; 376 int i, rc; 377 378 for (i = 0; i < ACCEL_DPDK_CRYPTODEV_DRIVER_LAST; i++) { 379 qp = crypto_ch->device_qp[i]; 380 /* Avoid polling "idle" qps since it may affect performance */ 381 if (qp && qp->num_enqueued_ops) { 382 num_dequeued_ops += accel_dpdk_cryptodev_poll_qp(qp, crypto_ch); 383 } 384 } 385 386 /* Check if there are any queued crypto ops to process */ 387 TAILQ_FOREACH_SAFE(op_to_resubmit, &crypto_ch->queued_cry_ops, link, op_to_resubmit_tmp) { 388 task = op_to_resubmit->task; 389 qp = op_to_resubmit->qp; 390 if (qp->num_enqueued_ops == qp->device->qp_desc_nr) { 391 continue; 392 } 393 enqueued = rte_cryptodev_enqueue_burst(qp->device->cdev_id, 394 qp->qp, 395 &op_to_resubmit->crypto_op, 396 1); 397 if (enqueued == 1) { 398 TAILQ_REMOVE(&crypto_ch->queued_cry_ops, op_to_resubmit, link); 399 qp->num_enqueued_ops++; 400 num_enqueued_ops++; 401 } else { 402 if (op_to_resubmit->crypto_op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED) { 403 /* If we couldn't get one, just break and try again later. */ 404 break; 405 } else { 406 /* Something is really wrong with the op. Most probably the 407 * mbuf is broken or the HW is not able to process the request. 408 * Fail the IO and remove its ops from the queued ops list. */ 409 task->is_failed = true; 410 411 cancel_queued_crypto_ops(crypto_ch, task); 412 413 task->cryop_completed++; 414 /* Fail the IO if there is nothing left on device. */ 415 if (task->cryop_completed == task->cryop_submitted) { 416 spdk_accel_task_complete(&task->base, -EFAULT); 417 } 418 } 419 } 420 } 421 422 if (!TAILQ_EMPTY(&crypto_ch->queued_tasks)) { 423 TAILQ_INIT(&queued_tasks_tmp); 424 425 TAILQ_FOREACH_SAFE(task, &crypto_ch->queued_tasks, link, task_tmp) { 426 TAILQ_REMOVE(&crypto_ch->queued_tasks, task, link); 427 rc = accel_dpdk_cryptodev_process_task(crypto_ch, task); 428 if (spdk_unlikely(rc)) { 429 if (rc == -ENOMEM) { 430 TAILQ_INSERT_TAIL(&queued_tasks_tmp, task, link); 431 /* Other queued tasks may belong to other qpairs, 432 * so process the whole list */ 433 continue; 434 } 435 spdk_accel_task_complete(&task->base, rc); 436 } else { 437 num_enqueued_ops++; 438 } 439 } 440 441 TAILQ_SWAP(&crypto_ch->queued_tasks, &queued_tasks_tmp, accel_dpdk_cryptodev_task, link); 442 } 443 444 return !!(num_dequeued_ops + num_enqueued_ops); 445 } 446 447 /* Allocate the new mbuf of @remainder size with data pointed by @addr and attach 448 * it to the @orig_mbuf. */ 449 static inline int 450 accel_dpdk_cryptodev_mbuf_chain_remainder(struct accel_dpdk_cryptodev_task *task, 451 struct rte_mbuf *orig_mbuf, uint8_t *addr, uint64_t *_remainder) 452 { 453 uint64_t phys_addr, phys_len, remainder = *_remainder; 454 struct rte_mbuf *chain_mbuf; 455 int rc; 456 457 phys_len = remainder; 458 phys_addr = spdk_vtophys((void *)addr, &phys_len); 459 if (spdk_unlikely(phys_addr == SPDK_VTOPHYS_ERROR)) { 460 return -EFAULT; 461 } 462 remainder = spdk_min(remainder, phys_len); 463 remainder = spdk_min(remainder, ACCEL_DPDK_CRYPTODEV_MAX_MBUF_LEN); 464 rc = rte_pktmbuf_alloc_bulk(g_mbuf_mp, (struct rte_mbuf **)&chain_mbuf, 1); 465 if (spdk_unlikely(rc)) { 466 return -ENOMEM; 467 } 468 /* Store context in every mbuf as we don't know anything about completion order */ 469 *RTE_MBUF_DYNFIELD(chain_mbuf, g_mbuf_offset, uint64_t *) = (uint64_t)task; 470 rte_pktmbuf_attach_extbuf(chain_mbuf, addr, phys_addr, remainder, &g_shinfo); 471 rte_pktmbuf_append(chain_mbuf, remainder); 472 473 /* Chained buffer is released by rte_pktbuf_free_bulk() automagicaly. */ 474 rte_pktmbuf_chain(orig_mbuf, chain_mbuf); 475 *_remainder = remainder; 476 477 return 0; 478 } 479 480 /* Attach data buffer pointed by @addr to @mbuf. Return utilized len of the 481 * contiguous space that was physically available. */ 482 static inline uint64_t 483 accel_dpdk_cryptodev_mbuf_attach_buf(struct accel_dpdk_cryptodev_task *task, struct rte_mbuf *mbuf, 484 uint8_t *addr, uint32_t len) 485 { 486 uint64_t phys_addr, phys_len; 487 488 /* Store context in every mbuf as we don't know anything about completion order */ 489 *RTE_MBUF_DYNFIELD(mbuf, g_mbuf_offset, uint64_t *) = (uint64_t)task; 490 491 phys_len = len; 492 phys_addr = spdk_vtophys((void *)addr, &phys_len); 493 if (spdk_unlikely(phys_addr == SPDK_VTOPHYS_ERROR || phys_len == 0)) { 494 return 0; 495 } 496 assert(phys_len <= len); 497 phys_len = spdk_min(phys_len, ACCEL_DPDK_CRYPTODEV_MAX_MBUF_LEN); 498 499 /* Set the mbuf elements address and length. */ 500 rte_pktmbuf_attach_extbuf(mbuf, addr, phys_addr, phys_len, &g_shinfo); 501 rte_pktmbuf_append(mbuf, phys_len); 502 503 return phys_len; 504 } 505 506 static inline struct accel_dpdk_cryptodev_key_handle * 507 accel_dpdk_find_key_handle_in_channel(struct accel_dpdk_cryptodev_io_channel *crypto_ch, 508 struct accel_dpdk_cryptodev_key_priv *key) 509 { 510 struct accel_dpdk_cryptodev_key_handle *key_handle; 511 512 if (key->driver == ACCEL_DPDK_CRYPTODEV_DRIVER_MLX5_PCI) { 513 /* Crypto key is registered on all available devices while io_channel opens CQ/QP on a single device. 514 * We need to iterate a list of key entries to find a suitable device */ 515 TAILQ_FOREACH(key_handle, &key->dev_keys, link) { 516 if (key_handle->device->cdev_id == 517 crypto_ch->device_qp[ACCEL_DPDK_CRYPTODEV_DRIVER_MLX5_PCI]->device->cdev_id) { 518 return key_handle; 519 } 520 } 521 return NULL; 522 } else { 523 return TAILQ_FIRST(&key->dev_keys); 524 } 525 } 526 527 static inline int 528 accel_dpdk_cryptodev_task_alloc_resources(struct rte_mbuf **src_mbufs, struct rte_mbuf **dst_mbufs, 529 struct rte_crypto_op **crypto_ops, int count) 530 { 531 int rc; 532 533 /* Get the number of source mbufs that we need. These will always be 1:1 because we 534 * don't support chaining. The reason we don't is because of our decision to use 535 * LBA as IV, there can be no case where we'd need >1 mbuf per crypto op or the 536 * op would be > 1 LBA. 537 */ 538 rc = rte_pktmbuf_alloc_bulk(g_mbuf_mp, src_mbufs, count); 539 if (rc) { 540 SPDK_ERRLOG("Failed to get src_mbufs!\n"); 541 return -ENOMEM; 542 } 543 544 /* Get the same amount to describe destination. If crypto operation is inline then we don't just skip it */ 545 if (dst_mbufs) { 546 rc = rte_pktmbuf_alloc_bulk(g_mbuf_mp, dst_mbufs, count); 547 if (rc) { 548 SPDK_ERRLOG("Failed to get dst_mbufs!\n"); 549 goto err_free_src; 550 } 551 } 552 553 #ifdef __clang_analyzer__ 554 /* silence scan-build false positive */ 555 SPDK_CLANG_ANALYZER_PREINIT_PTR_ARRAY(crypto_ops, ACCEL_DPDK_CRYPTODEV_MAX_ENQUEUE_ARRAY_SIZE, 556 0x1000); 557 #endif 558 /* Allocate crypto operations. */ 559 rc = rte_crypto_op_bulk_alloc(g_crypto_op_mp, 560 RTE_CRYPTO_OP_TYPE_SYMMETRIC, 561 crypto_ops, count); 562 if (rc < count) { 563 SPDK_ERRLOG("Failed to allocate crypto ops! rc %d\n", rc); 564 goto err_free_ops; 565 } 566 567 return 0; 568 569 err_free_ops: 570 if (rc > 0) { 571 rte_mempool_put_bulk(g_crypto_op_mp, (void **)crypto_ops, rc); 572 } 573 if (dst_mbufs) { 574 /* This also releases chained mbufs if any. */ 575 rte_pktmbuf_free_bulk(dst_mbufs, count); 576 } 577 err_free_src: 578 /* This also releases chained mbufs if any. */ 579 rte_pktmbuf_free_bulk(src_mbufs, count); 580 581 return -ENOMEM; 582 } 583 584 static inline int 585 accel_dpdk_cryptodev_mbuf_add_single_block(struct spdk_iov_sgl *sgl, struct rte_mbuf *mbuf, 586 struct accel_dpdk_cryptodev_task *task) 587 { 588 int rc; 589 uint8_t *buf_addr; 590 uint64_t phys_len; 591 uint64_t remainder; 592 uint64_t buf_len = spdk_min(task->base.block_size, sgl->iov->iov_len - sgl->iov_offset); 593 594 buf_addr = sgl->iov->iov_base + sgl->iov_offset; 595 phys_len = accel_dpdk_cryptodev_mbuf_attach_buf(task, mbuf, buf_addr, buf_len); 596 if (spdk_unlikely(phys_len == 0)) { 597 return -EFAULT; 598 } 599 buf_len = spdk_min(buf_len, phys_len); 600 spdk_iov_sgl_advance(sgl, buf_len); 601 602 /* Handle the case of page boundary. */ 603 assert(task->base.block_size >= buf_len); 604 remainder = task->base.block_size - buf_len; 605 while (remainder) { 606 buf_len = spdk_min(remainder, sgl->iov->iov_len - sgl->iov_offset); 607 buf_addr = sgl->iov->iov_base + sgl->iov_offset; 608 rc = accel_dpdk_cryptodev_mbuf_chain_remainder(task, mbuf, buf_addr, &buf_len); 609 if (spdk_unlikely(rc)) { 610 return rc; 611 } 612 spdk_iov_sgl_advance(sgl, buf_len); 613 remainder -= buf_len; 614 } 615 616 return 0; 617 } 618 619 static inline void 620 accel_dpdk_cryptodev_op_set_iv(struct rte_crypto_op *crypto_op, uint64_t iv) 621 { 622 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(crypto_op, uint8_t *, ACCEL_DPDK_CRYPTODEV_IV_OFFSET); 623 624 /* Set the IV - we use the LBA of the crypto_op */ 625 memset(iv_ptr, 0, ACCEL_DPDK_CRYPTODEV_IV_LENGTH); 626 rte_memcpy(iv_ptr, &iv, sizeof(uint64_t)); 627 } 628 629 static int 630 accel_dpdk_cryptodev_process_task(struct accel_dpdk_cryptodev_io_channel *crypto_ch, 631 struct accel_dpdk_cryptodev_task *task) 632 { 633 uint16_t num_enqueued_ops; 634 uint32_t cryop_cnt; 635 uint32_t crypto_len = task->base.block_size; 636 uint64_t dst_length, total_length; 637 uint32_t sgl_offset; 638 uint32_t qp_capacity; 639 uint64_t iv_start; 640 struct accel_dpdk_cryptodev_queued_op *op_to_queue; 641 uint32_t i, crypto_index; 642 struct rte_crypto_op *crypto_ops[ACCEL_DPDK_CRYPTODEV_MAX_ENQUEUE_ARRAY_SIZE]; 643 struct rte_mbuf *src_mbufs[ACCEL_DPDK_CRYPTODEV_MAX_ENQUEUE_ARRAY_SIZE]; 644 struct rte_mbuf *dst_mbufs[ACCEL_DPDK_CRYPTODEV_MAX_ENQUEUE_ARRAY_SIZE]; 645 void *session; 646 struct accel_dpdk_cryptodev_key_priv *priv; 647 struct accel_dpdk_cryptodev_key_handle *key_handle; 648 struct accel_dpdk_cryptodev_qp *qp; 649 struct accel_dpdk_cryptodev_device *dev; 650 struct spdk_iov_sgl src, dst = {}; 651 int rc; 652 653 if (spdk_unlikely(!task->base.crypto_key || 654 task->base.crypto_key->module_if != &g_accel_dpdk_cryptodev_module)) { 655 return -EINVAL; 656 } 657 658 priv = task->base.crypto_key->priv; 659 assert(priv->driver < ACCEL_DPDK_CRYPTODEV_DRIVER_LAST); 660 661 if (task->cryop_completed) { 662 /* We continue to process remaining blocks */ 663 assert(task->cryop_submitted == task->cryop_completed); 664 assert(task->cryop_total > task->cryop_completed); 665 cryop_cnt = task->cryop_total - task->cryop_completed; 666 sgl_offset = task->cryop_completed * crypto_len; 667 iv_start = task->base.iv + task->cryop_completed; 668 } else { 669 /* That is a new task */ 670 total_length = 0; 671 for (i = 0; i < task->base.s.iovcnt; i++) { 672 total_length += task->base.s.iovs[i].iov_len; 673 } 674 dst_length = 0; 675 for (i = 0; i < task->base.d.iovcnt; i++) { 676 dst_length += task->base.d.iovs[i].iov_len; 677 } 678 679 if (spdk_unlikely(total_length != dst_length || !total_length)) { 680 return -ERANGE; 681 } 682 if (spdk_unlikely(total_length % task->base.block_size != 0)) { 683 return -EINVAL; 684 } 685 686 cryop_cnt = total_length / task->base.block_size; 687 task->cryop_total = cryop_cnt; 688 sgl_offset = 0; 689 iv_start = task->base.iv; 690 } 691 692 /* Limit the number of crypto ops that we can process once */ 693 cryop_cnt = spdk_min(cryop_cnt, ACCEL_DPDK_CRYPTODEV_MAX_ENQUEUE_ARRAY_SIZE); 694 695 qp = crypto_ch->device_qp[priv->driver]; 696 assert(qp); 697 dev = qp->device; 698 assert(dev); 699 assert(dev->qp_desc_nr >= qp->num_enqueued_ops); 700 701 qp_capacity = dev->qp_desc_nr - qp->num_enqueued_ops; 702 cryop_cnt = spdk_min(cryop_cnt, qp_capacity); 703 if (spdk_unlikely(cryop_cnt == 0)) { 704 /* QP is full */ 705 return -ENOMEM; 706 } 707 708 key_handle = accel_dpdk_find_key_handle_in_channel(crypto_ch, priv); 709 if (spdk_unlikely(!key_handle)) { 710 SPDK_ERRLOG("Failed to find a key handle, driver %s, cipher %s\n", g_driver_names[priv->driver], 711 g_cipher_names[priv->cipher]); 712 return -EINVAL; 713 } 714 /* mlx5_pci binds keys to a specific device, we can't use a key with any device */ 715 assert(dev == key_handle->device || priv->driver != ACCEL_DPDK_CRYPTODEV_DRIVER_MLX5_PCI); 716 717 if (task->base.op_code == ACCEL_OPC_ENCRYPT) { 718 session = key_handle->session_encrypt; 719 } else if (task->base.op_code == ACCEL_OPC_DECRYPT) { 720 session = key_handle->session_decrypt; 721 } else { 722 return -EINVAL; 723 } 724 725 rc = accel_dpdk_cryptodev_task_alloc_resources(src_mbufs, task->inplace ? NULL : dst_mbufs, 726 crypto_ops, cryop_cnt); 727 if (rc) { 728 return rc; 729 } 730 /* This value is used in the completion callback to determine when the accel task is complete. */ 731 task->cryop_submitted += cryop_cnt; 732 733 /* As we don't support chaining because of a decision to use LBA as IV, construction 734 * of crypto operations is straightforward. We build both the op, the mbuf and the 735 * dst_mbuf in our local arrays by looping through the length of the accel task and 736 * picking off LBA sized blocks of memory from the IOVs as we walk through them. Each 737 * LBA sized chunk of memory will correspond 1:1 to a crypto operation and a single 738 * mbuf per crypto operation. 739 */ 740 spdk_iov_sgl_init(&src, task->base.s.iovs, task->base.s.iovcnt, sgl_offset); 741 if (!task->inplace) { 742 spdk_iov_sgl_init(&dst, task->base.d.iovs, task->base.d.iovcnt, sgl_offset); 743 } 744 745 for (crypto_index = 0; crypto_index < cryop_cnt; crypto_index++) { 746 rc = accel_dpdk_cryptodev_mbuf_add_single_block(&src, src_mbufs[crypto_index], task); 747 if (spdk_unlikely(rc)) { 748 goto err_free_ops; 749 } 750 accel_dpdk_cryptodev_op_set_iv(crypto_ops[crypto_index], iv_start); 751 iv_start++; 752 753 /* Set the data to encrypt/decrypt length */ 754 crypto_ops[crypto_index]->sym->cipher.data.length = crypto_len; 755 crypto_ops[crypto_index]->sym->cipher.data.offset = 0; 756 rte_crypto_op_attach_sym_session(crypto_ops[crypto_index], session); 757 758 /* link the mbuf to the crypto op. */ 759 crypto_ops[crypto_index]->sym->m_src = src_mbufs[crypto_index]; 760 761 if (task->inplace) { 762 crypto_ops[crypto_index]->sym->m_dst = NULL; 763 } else { 764 #ifndef __clang_analyzer__ 765 /* scan-build thinks that dst_mbufs is not initialized */ 766 rc = accel_dpdk_cryptodev_mbuf_add_single_block(&dst, dst_mbufs[crypto_index], task); 767 if (spdk_unlikely(rc)) { 768 goto err_free_ops; 769 } 770 crypto_ops[crypto_index]->sym->m_dst = dst_mbufs[crypto_index]; 771 #endif 772 } 773 } 774 775 /* Enqueue everything we've got but limit by the max number of descriptors we 776 * configured the crypto device for. 777 */ 778 num_enqueued_ops = rte_cryptodev_enqueue_burst(dev->cdev_id, qp->qp, crypto_ops, cryop_cnt); 779 780 qp->num_enqueued_ops += num_enqueued_ops; 781 /* We were unable to enqueue everything but did get some, so need to decide what 782 * to do based on the status of the last op. 783 */ 784 if (num_enqueued_ops < cryop_cnt) { 785 switch (crypto_ops[num_enqueued_ops]->status) { 786 case RTE_CRYPTO_OP_STATUS_NOT_PROCESSED: 787 /* Queue them up on a linked list to be resubmitted via the poller. */ 788 for (crypto_index = num_enqueued_ops; crypto_index < cryop_cnt; crypto_index++) { 789 op_to_queue = (struct accel_dpdk_cryptodev_queued_op *)rte_crypto_op_ctod_offset( 790 crypto_ops[crypto_index], 791 uint8_t *, ACCEL_DPDK_CRYPTODEV_QUEUED_OP_OFFSET); 792 op_to_queue->qp = qp; 793 op_to_queue->crypto_op = crypto_ops[crypto_index]; 794 op_to_queue->task = task; 795 TAILQ_INSERT_TAIL(&crypto_ch->queued_cry_ops, op_to_queue, link); 796 } 797 break; 798 default: 799 /* For all other statuses, mark task as failed so that the poller will pick 800 * the failure up for the overall task status. 801 */ 802 task->is_failed = true; 803 if (num_enqueued_ops == 0) { 804 /* If nothing was enqueued, but the last one wasn't because of 805 * busy, fail it now as the poller won't know anything about it. 806 */ 807 rc = -EINVAL; 808 goto err_free_ops; 809 } 810 break; 811 } 812 } 813 814 return 0; 815 816 /* Error cleanup paths. */ 817 err_free_ops: 818 if (!task->inplace) { 819 /* This also releases chained mbufs if any. */ 820 rte_pktmbuf_free_bulk(dst_mbufs, cryop_cnt); 821 } 822 rte_mempool_put_bulk(g_crypto_op_mp, (void **)crypto_ops, cryop_cnt); 823 /* This also releases chained mbufs if any. */ 824 rte_pktmbuf_free_bulk(src_mbufs, cryop_cnt); 825 return rc; 826 } 827 828 static inline struct accel_dpdk_cryptodev_qp * 829 accel_dpdk_cryptodev_get_next_device_qpair(enum accel_dpdk_cryptodev_driver_type type) 830 { 831 struct accel_dpdk_cryptodev_device *device, *device_tmp; 832 struct accel_dpdk_cryptodev_qp *qpair; 833 834 TAILQ_FOREACH_SAFE(device, &g_crypto_devices, link, device_tmp) { 835 if (device->type != type) { 836 continue; 837 } 838 TAILQ_FOREACH(qpair, &device->qpairs, link) { 839 if (!qpair->in_use) { 840 qpair->in_use = true; 841 return qpair; 842 } 843 } 844 } 845 846 return NULL; 847 } 848 849 /* Helper function for the channel creation callback. 850 * Returns the number of drivers assigned to the channel */ 851 static uint32_t 852 accel_dpdk_cryptodev_assign_device_qps(struct accel_dpdk_cryptodev_io_channel *crypto_ch) 853 { 854 struct accel_dpdk_cryptodev_device *device; 855 struct accel_dpdk_cryptodev_qp *device_qp; 856 uint32_t num_drivers = 0; 857 bool qat_found = false; 858 859 pthread_mutex_lock(&g_device_lock); 860 861 TAILQ_FOREACH(device, &g_crypto_devices, link) { 862 if (device->type == ACCEL_DPDK_CRYPTODEV_DRIVER_QAT && !qat_found) { 863 /* For some QAT devices, the optimal qp to use is every 32nd as this spreads the 864 * workload out over the multiple virtual functions in the device. For the devices 865 * where this isn't the case, it doesn't hurt. 866 */ 867 TAILQ_FOREACH(device_qp, &device->qpairs, link) { 868 if (device_qp->index != g_next_qat_index) { 869 continue; 870 } 871 if (device_qp->in_use == false) { 872 assert(crypto_ch->device_qp[ACCEL_DPDK_CRYPTODEV_DRIVER_QAT] == NULL); 873 crypto_ch->device_qp[ACCEL_DPDK_CRYPTODEV_DRIVER_QAT] = device_qp; 874 device_qp->in_use = true; 875 g_next_qat_index = (g_next_qat_index + ACCEL_DPDK_CRYPTODEV_QAT_VF_SPREAD) % g_qat_total_qp; 876 qat_found = true; 877 num_drivers++; 878 break; 879 } else { 880 /* if the preferred index is used, skip to the next one in this set. */ 881 g_next_qat_index = (g_next_qat_index + 1) % g_qat_total_qp; 882 } 883 } 884 } 885 } 886 887 /* For ACCEL_DPDK_CRYPTODEV_AESNI_MB and MLX5_PCI select devices in round-robin manner */ 888 device_qp = accel_dpdk_cryptodev_get_next_device_qpair(ACCEL_DPDK_CRYPTODEV_DRIVER_AESNI_MB); 889 if (device_qp) { 890 assert(crypto_ch->device_qp[ACCEL_DPDK_CRYPTODEV_DRIVER_AESNI_MB] == NULL); 891 crypto_ch->device_qp[ACCEL_DPDK_CRYPTODEV_DRIVER_AESNI_MB] = device_qp; 892 num_drivers++; 893 } 894 895 device_qp = accel_dpdk_cryptodev_get_next_device_qpair(ACCEL_DPDK_CRYPTODEV_DRIVER_MLX5_PCI); 896 if (device_qp) { 897 assert(crypto_ch->device_qp[ACCEL_DPDK_CRYPTODEV_DRIVER_MLX5_PCI] == NULL); 898 crypto_ch->device_qp[ACCEL_DPDK_CRYPTODEV_DRIVER_MLX5_PCI] = device_qp; 899 num_drivers++; 900 } 901 902 pthread_mutex_unlock(&g_device_lock); 903 904 return num_drivers; 905 } 906 907 static void 908 _accel_dpdk_cryptodev_destroy_cb(void *io_device, void *ctx_buf) 909 { 910 struct accel_dpdk_cryptodev_io_channel *crypto_ch = (struct accel_dpdk_cryptodev_io_channel *) 911 ctx_buf; 912 int i; 913 914 pthread_mutex_lock(&g_device_lock); 915 for (i = 0; i < ACCEL_DPDK_CRYPTODEV_DRIVER_LAST; i++) { 916 if (crypto_ch->device_qp[i]) { 917 crypto_ch->device_qp[i]->in_use = false; 918 } 919 } 920 pthread_mutex_unlock(&g_device_lock); 921 922 spdk_poller_unregister(&crypto_ch->poller); 923 } 924 925 static int 926 _accel_dpdk_cryptodev_create_cb(void *io_device, void *ctx_buf) 927 { 928 struct accel_dpdk_cryptodev_io_channel *crypto_ch = (struct accel_dpdk_cryptodev_io_channel *) 929 ctx_buf; 930 931 crypto_ch->poller = SPDK_POLLER_REGISTER(accel_dpdk_cryptodev_poller, crypto_ch, 0); 932 if (!accel_dpdk_cryptodev_assign_device_qps(crypto_ch)) { 933 SPDK_ERRLOG("No crypto drivers assigned\n"); 934 spdk_poller_unregister(&crypto_ch->poller); 935 return -EINVAL; 936 } 937 938 /* We use this to queue up crypto ops when the device is busy. */ 939 TAILQ_INIT(&crypto_ch->queued_cry_ops); 940 /* We use this to queue tasks when qpair is full or no resources in pools */ 941 TAILQ_INIT(&crypto_ch->queued_tasks); 942 943 return 0; 944 } 945 946 static struct spdk_io_channel * 947 accel_dpdk_cryptodev_get_io_channel(void) 948 { 949 return spdk_get_io_channel(&g_accel_dpdk_cryptodev_module); 950 } 951 952 static size_t 953 accel_dpdk_cryptodev_ctx_size(void) 954 { 955 return sizeof(struct accel_dpdk_cryptodev_task); 956 } 957 958 static bool 959 accel_dpdk_cryptodev_supports_opcode(enum accel_opcode opc) 960 { 961 switch (opc) { 962 case ACCEL_OPC_ENCRYPT: 963 case ACCEL_OPC_DECRYPT: 964 return true; 965 default: 966 return false; 967 } 968 } 969 970 static int 971 accel_dpdk_cryptodev_submit_tasks(struct spdk_io_channel *_ch, struct spdk_accel_task *_task) 972 { 973 struct accel_dpdk_cryptodev_task *task = SPDK_CONTAINEROF(_task, struct accel_dpdk_cryptodev_task, 974 base); 975 struct accel_dpdk_cryptodev_io_channel *ch = spdk_io_channel_get_ctx(_ch); 976 int rc; 977 978 task->cryop_completed = 0; 979 task->cryop_submitted = 0; 980 task->cryop_total = 0; 981 task->inplace = true; 982 task->is_failed = false; 983 984 /* Check if crypto operation is inplace: no destination or source == destination */ 985 if (task->base.s.iovcnt == task->base.d.iovcnt) { 986 if (memcmp(task->base.s.iovs, task->base.d.iovs, sizeof(struct iovec) * task->base.s.iovcnt) != 0) { 987 task->inplace = false; 988 } 989 } else if (task->base.d.iovcnt != 0) { 990 task->inplace = false; 991 } 992 993 rc = accel_dpdk_cryptodev_process_task(ch, task); 994 if (spdk_unlikely(rc == -ENOMEM)) { 995 TAILQ_INSERT_TAIL(&ch->queued_tasks, task, link); 996 rc = 0; 997 } 998 999 return rc; 1000 } 1001 1002 /* Dummy function used by DPDK to free ext attached buffers to mbufs, we free them ourselves but 1003 * this callback has to be here. */ 1004 static void 1005 shinfo_free_cb(void *arg1, void *arg2) 1006 { 1007 } 1008 1009 static int 1010 accel_dpdk_cryptodev_create(uint8_t index, uint16_t num_lcores) 1011 { 1012 struct rte_cryptodev_qp_conf qp_conf = { 1013 .mp_session = g_session_mp, 1014 #if RTE_VERSION < RTE_VERSION_NUM(22, 11, 0, 0) 1015 .mp_session_private = g_session_mp_priv 1016 #endif 1017 }; 1018 /* Setup queue pairs. */ 1019 struct rte_cryptodev_config conf = { .socket_id = SPDK_ENV_SOCKET_ID_ANY }; 1020 struct accel_dpdk_cryptodev_device *device; 1021 uint8_t j, cdev_id, cdrv_id; 1022 struct accel_dpdk_cryptodev_qp *dev_qp; 1023 int rc; 1024 1025 device = calloc(1, sizeof(*device)); 1026 if (!device) { 1027 return -ENOMEM; 1028 } 1029 1030 /* Get details about this device. */ 1031 rte_cryptodev_info_get(index, &device->cdev_info); 1032 cdrv_id = device->cdev_info.driver_id; 1033 cdev_id = device->cdev_id = index; 1034 1035 if (strcmp(device->cdev_info.driver_name, ACCEL_DPDK_CRYPTODEV_QAT) == 0) { 1036 device->qp_desc_nr = ACCEL_DPDK_CRYPTODEV_QP_DESCRIPTORS; 1037 device->type = ACCEL_DPDK_CRYPTODEV_DRIVER_QAT; 1038 } else if (strcmp(device->cdev_info.driver_name, ACCEL_DPDK_CRYPTODEV_AESNI_MB) == 0) { 1039 device->qp_desc_nr = ACCEL_DPDK_CRYPTODEV_QP_DESCRIPTORS; 1040 device->type = ACCEL_DPDK_CRYPTODEV_DRIVER_AESNI_MB; 1041 } else if (strcmp(device->cdev_info.driver_name, ACCEL_DPDK_CRYPTODEV_MLX5) == 0) { 1042 device->qp_desc_nr = ACCEL_DPDK_CRYPTODEV_QP_DESCRIPTORS_MLX5; 1043 device->type = ACCEL_DPDK_CRYPTODEV_DRIVER_MLX5_PCI; 1044 } else if (strcmp(device->cdev_info.driver_name, ACCEL_DPDK_CRYPTODEV_QAT_ASYM) == 0) { 1045 /* ACCEL_DPDK_CRYPTODEV_QAT_ASYM devices are not supported at this time. */ 1046 rc = 0; 1047 goto err; 1048 } else { 1049 SPDK_ERRLOG("Failed to start device %u. Invalid driver name \"%s\"\n", 1050 cdev_id, device->cdev_info.driver_name); 1051 rc = -EINVAL; 1052 goto err; 1053 } 1054 1055 /* Before going any further, make sure we have enough resources for this 1056 * device type to function. We need a unique queue pair per core accross each 1057 * device type to remain lockless.... 1058 */ 1059 if ((rte_cryptodev_device_count_by_driver(cdrv_id) * 1060 device->cdev_info.max_nb_queue_pairs) < num_lcores) { 1061 SPDK_ERRLOG("Insufficient unique queue pairs available for %s\n", 1062 device->cdev_info.driver_name); 1063 SPDK_ERRLOG("Either add more crypto devices or decrease core count\n"); 1064 rc = -EINVAL; 1065 goto err; 1066 } 1067 1068 conf.nb_queue_pairs = device->cdev_info.max_nb_queue_pairs; 1069 rc = rte_cryptodev_configure(cdev_id, &conf); 1070 if (rc < 0) { 1071 SPDK_ERRLOG("Failed to configure cryptodev %u: error %d\n", 1072 cdev_id, rc); 1073 rc = -EINVAL; 1074 goto err; 1075 } 1076 1077 /* Pre-setup all potential qpairs now and assign them in the channel 1078 * callback. If we were to create them there, we'd have to stop the 1079 * entire device affecting all other threads that might be using it 1080 * even on other queue pairs. 1081 */ 1082 qp_conf.nb_descriptors = device->qp_desc_nr; 1083 for (j = 0; j < device->cdev_info.max_nb_queue_pairs; j++) { 1084 rc = rte_cryptodev_queue_pair_setup(cdev_id, j, &qp_conf, SOCKET_ID_ANY); 1085 if (rc < 0) { 1086 SPDK_ERRLOG("Failed to setup queue pair %u on " 1087 "cryptodev %u: error %d\n", j, cdev_id, rc); 1088 rc = -EINVAL; 1089 goto err_qp_setup; 1090 } 1091 } 1092 1093 rc = rte_cryptodev_start(cdev_id); 1094 if (rc < 0) { 1095 SPDK_ERRLOG("Failed to start device %u: error %d\n", cdev_id, rc); 1096 rc = -EINVAL; 1097 goto err_dev_start; 1098 } 1099 1100 TAILQ_INIT(&device->qpairs); 1101 /* Build up lists of device/qp combinations per PMD */ 1102 for (j = 0; j < device->cdev_info.max_nb_queue_pairs; j++) { 1103 dev_qp = calloc(1, sizeof(*dev_qp)); 1104 if (!dev_qp) { 1105 rc = -ENOMEM; 1106 goto err_qp_alloc; 1107 } 1108 dev_qp->device = device; 1109 dev_qp->qp = j; 1110 dev_qp->in_use = false; 1111 TAILQ_INSERT_TAIL(&device->qpairs, dev_qp, link); 1112 if (device->type == ACCEL_DPDK_CRYPTODEV_DRIVER_QAT) { 1113 dev_qp->index = g_qat_total_qp++; 1114 } 1115 } 1116 /* Add to our list of available crypto devices. */ 1117 TAILQ_INSERT_TAIL(&g_crypto_devices, device, link); 1118 1119 return 0; 1120 1121 err_qp_alloc: 1122 TAILQ_FOREACH(dev_qp, &device->qpairs, link) { 1123 if (dev_qp->device->cdev_id != device->cdev_id) { 1124 continue; 1125 } 1126 free(dev_qp); 1127 if (device->type == ACCEL_DPDK_CRYPTODEV_DRIVER_QAT) { 1128 assert(g_qat_total_qp); 1129 g_qat_total_qp--; 1130 } 1131 } 1132 rte_cryptodev_stop(cdev_id); 1133 err_dev_start: 1134 err_qp_setup: 1135 rte_cryptodev_close(cdev_id); 1136 err: 1137 free(device); 1138 1139 return rc; 1140 } 1141 1142 static void 1143 accel_dpdk_cryptodev_release(struct accel_dpdk_cryptodev_device *device) 1144 { 1145 struct accel_dpdk_cryptodev_qp *dev_qp, *tmp; 1146 1147 assert(device); 1148 1149 TAILQ_FOREACH_SAFE(dev_qp, &device->qpairs, link, tmp) { 1150 free(dev_qp); 1151 } 1152 if (device->type == ACCEL_DPDK_CRYPTODEV_DRIVER_QAT) { 1153 assert(g_qat_total_qp >= device->cdev_info.max_nb_queue_pairs); 1154 g_qat_total_qp -= device->cdev_info.max_nb_queue_pairs; 1155 } 1156 rte_cryptodev_stop(device->cdev_id); 1157 rte_cryptodev_close(device->cdev_id); 1158 free(device); 1159 } 1160 1161 static int 1162 accel_dpdk_cryptodev_init(void) 1163 { 1164 uint8_t cdev_count; 1165 uint8_t cdev_id; 1166 int i, rc; 1167 struct accel_dpdk_cryptodev_device *device, *tmp_dev; 1168 unsigned int max_sess_size = 0, sess_size; 1169 uint16_t num_lcores = rte_lcore_count(); 1170 char aesni_args[32]; 1171 1172 /* Only the first call via module init should init the crypto drivers. */ 1173 if (g_session_mp != NULL) { 1174 return 0; 1175 } 1176 1177 /* We always init ACCEL_DPDK_CRYPTODEV_AESNI_MB */ 1178 snprintf(aesni_args, sizeof(aesni_args), "max_nb_queue_pairs=%d", 1179 ACCEL_DPDK_CRYPTODEV_AESNI_MB_NUM_QP); 1180 rc = rte_vdev_init(ACCEL_DPDK_CRYPTODEV_AESNI_MB, aesni_args); 1181 if (rc) { 1182 SPDK_NOTICELOG("Failed to create virtual PMD %s: error %d. " 1183 "Possibly %s is not supported by DPDK library. " 1184 "Keep going...\n", ACCEL_DPDK_CRYPTODEV_AESNI_MB, rc, ACCEL_DPDK_CRYPTODEV_AESNI_MB); 1185 } 1186 1187 /* If we have no crypto devices, there's no reason to continue. */ 1188 cdev_count = rte_cryptodev_count(); 1189 SPDK_NOTICELOG("Found crypto devices: %d\n", (int)cdev_count); 1190 if (cdev_count == 0) { 1191 return 0; 1192 } 1193 1194 g_mbuf_offset = rte_mbuf_dynfield_register(&rte_mbuf_dynfield_io_context); 1195 if (g_mbuf_offset < 0) { 1196 SPDK_ERRLOG("error registering dynamic field with DPDK\n"); 1197 return -EINVAL; 1198 } 1199 1200 /* Create global mempools, shared by all devices regardless of type */ 1201 /* First determine max session size, most pools are shared by all the devices, 1202 * so we need to find the global max sessions size. */ 1203 for (cdev_id = 0; cdev_id < cdev_count; cdev_id++) { 1204 sess_size = rte_cryptodev_sym_get_private_session_size(cdev_id); 1205 if (sess_size > max_sess_size) { 1206 max_sess_size = sess_size; 1207 } 1208 } 1209 1210 #if RTE_VERSION < RTE_VERSION_NUM(22, 11, 0, 0) 1211 g_session_mp_priv = rte_mempool_create("dpdk_crypto_ses_mp_priv", 1212 ACCEL_DPDK_CRYPTODEV_NUM_SESSIONS, max_sess_size, ACCEL_DPDK_CRYPTODEV_SESS_MEMPOOL_CACHE_SIZE, 0, 1213 NULL, NULL, NULL, NULL, SOCKET_ID_ANY, 0); 1214 if (g_session_mp_priv == NULL) { 1215 SPDK_ERRLOG("Cannot create private session pool max size 0x%x\n", max_sess_size); 1216 return -ENOMEM; 1217 } 1218 1219 /* When session private data mempool allocated, the element size for the session mempool 1220 * should be 0. */ 1221 max_sess_size = 0; 1222 #endif 1223 1224 g_session_mp = rte_cryptodev_sym_session_pool_create("dpdk_crypto_ses_mp", 1225 ACCEL_DPDK_CRYPTODEV_NUM_SESSIONS, max_sess_size, ACCEL_DPDK_CRYPTODEV_SESS_MEMPOOL_CACHE_SIZE, 0, 1226 SOCKET_ID_ANY); 1227 if (g_session_mp == NULL) { 1228 SPDK_ERRLOG("Cannot create session pool max size 0x%x\n", max_sess_size); 1229 rc = -ENOMEM; 1230 goto error_create_session_mp; 1231 } 1232 1233 g_mbuf_mp = rte_pktmbuf_pool_create("dpdk_crypto_mbuf_mp", ACCEL_DPDK_CRYPTODEV_NUM_MBUFS, 1234 ACCEL_DPDK_CRYPTODEV_POOL_CACHE_SIZE, 1235 0, 0, SPDK_ENV_SOCKET_ID_ANY); 1236 if (g_mbuf_mp == NULL) { 1237 SPDK_ERRLOG("Cannot create mbuf pool\n"); 1238 rc = -ENOMEM; 1239 goto error_create_mbuf; 1240 } 1241 1242 /* We use per op private data as suggested by DPDK and to store the IV and 1243 * our own struct for queueing ops. */ 1244 g_crypto_op_mp = rte_crypto_op_pool_create("dpdk_crypto_op_mp", 1245 RTE_CRYPTO_OP_TYPE_SYMMETRIC, ACCEL_DPDK_CRYPTODEV_NUM_MBUFS, ACCEL_DPDK_CRYPTODEV_POOL_CACHE_SIZE, 1246 (ACCEL_DPDK_CRYPTODEV_DEFAULT_NUM_XFORMS * sizeof(struct rte_crypto_sym_xform)) + 1247 ACCEL_DPDK_CRYPTODEV_IV_LENGTH + ACCEL_DPDK_CRYPTODEV_QUEUED_OP_LENGTH, rte_socket_id()); 1248 if (g_crypto_op_mp == NULL) { 1249 SPDK_ERRLOG("Cannot create op pool\n"); 1250 rc = -ENOMEM; 1251 goto error_create_op; 1252 } 1253 1254 /* Init all devices */ 1255 for (i = 0; i < cdev_count; i++) { 1256 rc = accel_dpdk_cryptodev_create(i, num_lcores); 1257 if (rc) { 1258 goto err; 1259 } 1260 } 1261 1262 g_shinfo.free_cb = shinfo_free_cb; 1263 1264 spdk_io_device_register(&g_accel_dpdk_cryptodev_module, _accel_dpdk_cryptodev_create_cb, 1265 _accel_dpdk_cryptodev_destroy_cb, sizeof(struct accel_dpdk_cryptodev_io_channel), 1266 "accel_dpdk_cryptodev"); 1267 1268 return 0; 1269 1270 /* Error cleanup paths. */ 1271 err: 1272 TAILQ_FOREACH_SAFE(device, &g_crypto_devices, link, tmp_dev) { 1273 TAILQ_REMOVE(&g_crypto_devices, device, link); 1274 accel_dpdk_cryptodev_release(device); 1275 } 1276 rte_mempool_free(g_crypto_op_mp); 1277 g_crypto_op_mp = NULL; 1278 error_create_op: 1279 rte_mempool_free(g_mbuf_mp); 1280 g_mbuf_mp = NULL; 1281 error_create_mbuf: 1282 rte_mempool_free(g_session_mp); 1283 g_session_mp = NULL; 1284 error_create_session_mp: 1285 if (g_session_mp_priv != NULL) { 1286 rte_mempool_free(g_session_mp_priv); 1287 g_session_mp_priv = NULL; 1288 } 1289 return rc; 1290 } 1291 1292 static void 1293 accel_dpdk_cryptodev_fini_cb(void *io_device) 1294 { 1295 struct accel_dpdk_cryptodev_device *device, *tmp; 1296 1297 TAILQ_FOREACH_SAFE(device, &g_crypto_devices, link, tmp) { 1298 TAILQ_REMOVE(&g_crypto_devices, device, link); 1299 accel_dpdk_cryptodev_release(device); 1300 } 1301 rte_vdev_uninit(ACCEL_DPDK_CRYPTODEV_AESNI_MB); 1302 1303 rte_mempool_free(g_crypto_op_mp); 1304 rte_mempool_free(g_mbuf_mp); 1305 rte_mempool_free(g_session_mp); 1306 if (g_session_mp_priv != NULL) { 1307 rte_mempool_free(g_session_mp_priv); 1308 } 1309 1310 spdk_accel_module_finish(); 1311 } 1312 1313 /* Called when the entire module is being torn down. */ 1314 static void 1315 accel_dpdk_cryptodev_fini(void *ctx) 1316 { 1317 if (g_crypto_op_mp) { 1318 spdk_io_device_unregister(&g_accel_dpdk_cryptodev_module, accel_dpdk_cryptodev_fini_cb); 1319 } 1320 } 1321 1322 static void 1323 accel_dpdk_cryptodev_key_handle_session_free(struct accel_dpdk_cryptodev_device *device, 1324 void *session) 1325 { 1326 #if RTE_VERSION >= RTE_VERSION_NUM(22, 11, 0, 0) 1327 assert(device != NULL); 1328 1329 rte_cryptodev_sym_session_free(device->cdev_id, session); 1330 #else 1331 rte_cryptodev_sym_session_free(session); 1332 #endif 1333 } 1334 1335 static void * 1336 accel_dpdk_cryptodev_key_handle_session_create(struct accel_dpdk_cryptodev_device *device, 1337 struct rte_crypto_sym_xform *cipher_xform) 1338 { 1339 void *session; 1340 1341 #if RTE_VERSION >= RTE_VERSION_NUM(22, 11, 0, 0) 1342 session = rte_cryptodev_sym_session_create(device->cdev_id, cipher_xform, g_session_mp); 1343 #else 1344 session = rte_cryptodev_sym_session_create(g_session_mp); 1345 if (!session) { 1346 return NULL; 1347 } 1348 1349 if (rte_cryptodev_sym_session_init(device->cdev_id, session, cipher_xform, g_session_mp_priv) < 0) { 1350 accel_dpdk_cryptodev_key_handle_session_free(device, session); 1351 return NULL; 1352 } 1353 #endif 1354 1355 return session; 1356 } 1357 1358 static int 1359 accel_dpdk_cryptodev_key_handle_configure(struct spdk_accel_crypto_key *key, 1360 struct accel_dpdk_cryptodev_key_handle *key_handle) 1361 { 1362 struct accel_dpdk_cryptodev_key_priv *priv = key->priv; 1363 1364 key_handle->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER; 1365 key_handle->cipher_xform.cipher.iv.offset = ACCEL_DPDK_CRYPTODEV_IV_OFFSET; 1366 key_handle->cipher_xform.cipher.iv.length = ACCEL_DPDK_CRYPTODEV_IV_LENGTH; 1367 1368 switch (priv->cipher) { 1369 case ACCEL_DPDK_CRYPTODEV_CIPHER_AES_CBC: 1370 key_handle->cipher_xform.cipher.key.data = key->key; 1371 key_handle->cipher_xform.cipher.key.length = key->key_size; 1372 key_handle->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC; 1373 break; 1374 case ACCEL_DPDK_CRYPTODEV_CIPHER_AES_XTS: 1375 key_handle->cipher_xform.cipher.key.data = priv->xts_key; 1376 key_handle->cipher_xform.cipher.key.length = key->key_size + key->key2_size; 1377 key_handle->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_XTS; 1378 break; 1379 default: 1380 SPDK_ERRLOG("Invalid cipher name %s.\n", key->param.cipher); 1381 return -EINVAL; 1382 } 1383 1384 key_handle->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT; 1385 key_handle->session_encrypt = accel_dpdk_cryptodev_key_handle_session_create(key_handle->device, 1386 &key_handle->cipher_xform); 1387 if (!key_handle->session_encrypt) { 1388 SPDK_ERRLOG("Failed to init encrypt session\n"); 1389 return -EINVAL; 1390 } 1391 1392 key_handle->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT; 1393 key_handle->session_decrypt = accel_dpdk_cryptodev_key_handle_session_create(key_handle->device, 1394 &key_handle->cipher_xform); 1395 if (!key_handle->session_decrypt) { 1396 SPDK_ERRLOG("Failed to init decrypt session:"); 1397 accel_dpdk_cryptodev_key_handle_session_free(key_handle->device, key_handle->session_encrypt); 1398 return -EINVAL; 1399 } 1400 1401 return 0; 1402 } 1403 1404 static int 1405 accel_dpdk_cryptodev_validate_parameters(enum accel_dpdk_cryptodev_driver_type driver, 1406 enum accel_dpdk_crypto_dev_cipher_type cipher, struct spdk_accel_crypto_key *key) 1407 { 1408 /* Check that all required parameters exist */ 1409 switch (cipher) { 1410 case ACCEL_DPDK_CRYPTODEV_CIPHER_AES_CBC: 1411 if (!key->key || !key->key_size) { 1412 SPDK_ERRLOG("ACCEL_DPDK_CRYPTODEV_AES_CBC requires a key\n"); 1413 return -1; 1414 } 1415 if (key->key2 || key->key2_size) { 1416 SPDK_ERRLOG("ACCEL_DPDK_CRYPTODEV_AES_CBC doesn't use key2\n"); 1417 return -1; 1418 } 1419 break; 1420 case ACCEL_DPDK_CRYPTODEV_CIPHER_AES_XTS: 1421 if (!key->key || !key->key_size || !key->key2 || !key->key2_size) { 1422 SPDK_ERRLOG("ACCEL_DPDK_CRYPTODEV_AES_XTS requires both key and key2\n"); 1423 return -1; 1424 } 1425 break; 1426 default: 1427 return -1; 1428 } 1429 1430 /* Check driver/cipher combinations and key lengths */ 1431 switch (cipher) { 1432 case ACCEL_DPDK_CRYPTODEV_CIPHER_AES_CBC: 1433 if (driver == ACCEL_DPDK_CRYPTODEV_DRIVER_MLX5_PCI) { 1434 SPDK_ERRLOG("Driver %s only supports cipher %s\n", 1435 g_driver_names[ACCEL_DPDK_CRYPTODEV_DRIVER_MLX5_PCI], 1436 g_cipher_names[ACCEL_DPDK_CRYPTODEV_CIPHER_AES_XTS]); 1437 return -1; 1438 } 1439 if (key->key_size != ACCEL_DPDK_CRYPTODEV_AES_CBC_KEY_LENGTH) { 1440 SPDK_ERRLOG("Invalid key size %zu for cipher %s, should be %d\n", key->key_size, 1441 g_cipher_names[ACCEL_DPDK_CRYPTODEV_CIPHER_AES_CBC], ACCEL_DPDK_CRYPTODEV_AES_CBC_KEY_LENGTH); 1442 return -1; 1443 } 1444 break; 1445 case ACCEL_DPDK_CRYPTODEV_CIPHER_AES_XTS: 1446 switch (driver) { 1447 case ACCEL_DPDK_CRYPTODEV_DRIVER_MLX5_PCI: 1448 if (key->key_size != ACCEL_DPDK_CRYPTODEV_AES_XTS_256_BLOCK_KEY_LENGTH && 1449 key->key_size != ACCEL_DPDK_CRYPTODEV_AES_XTS_512_BLOCK_KEY_LENGTH) { 1450 SPDK_ERRLOG("Invalid key size %zu for driver %s, cipher %s, supported %d or %d\n", 1451 key->key_size, g_driver_names[ACCEL_DPDK_CRYPTODEV_DRIVER_MLX5_PCI], 1452 g_cipher_names[ACCEL_DPDK_CRYPTODEV_CIPHER_AES_XTS], 1453 ACCEL_DPDK_CRYPTODEV_AES_XTS_256_BLOCK_KEY_LENGTH, 1454 ACCEL_DPDK_CRYPTODEV_AES_XTS_512_BLOCK_KEY_LENGTH); 1455 return -1; 1456 } 1457 break; 1458 case ACCEL_DPDK_CRYPTODEV_DRIVER_QAT: 1459 case ACCEL_DPDK_CRYPTODEV_DRIVER_AESNI_MB: 1460 if (key->key_size != ACCEL_DPDK_CRYPTODEV_AES_XTS_128_BLOCK_KEY_LENGTH) { 1461 SPDK_ERRLOG("Invalid key size %zu, supported %d\n", key->key_size, 1462 ACCEL_DPDK_CRYPTODEV_AES_XTS_128_BLOCK_KEY_LENGTH); 1463 return -1; 1464 } 1465 break; 1466 default: 1467 SPDK_ERRLOG("Incorrect driver type %d\n", driver); 1468 assert(0); 1469 return -1; 1470 } 1471 if (key->key2_size != ACCEL_DPDK_CRYPTODEV_AES_XTS_TWEAK_KEY_LENGTH) { 1472 SPDK_ERRLOG("Cipher %s requires key2 size %d\n", 1473 g_cipher_names[ACCEL_DPDK_CRYPTODEV_CIPHER_AES_CBC], ACCEL_DPDK_CRYPTODEV_AES_XTS_TWEAK_KEY_LENGTH); 1474 return -1; 1475 } 1476 break; 1477 } 1478 1479 return 0; 1480 } 1481 1482 static void 1483 accel_dpdk_cryptodev_key_deinit(struct spdk_accel_crypto_key *key) 1484 { 1485 struct accel_dpdk_cryptodev_key_handle *key_handle, *key_handle_tmp; 1486 struct accel_dpdk_cryptodev_key_priv *priv = key->priv; 1487 1488 TAILQ_FOREACH_SAFE(key_handle, &priv->dev_keys, link, key_handle_tmp) { 1489 accel_dpdk_cryptodev_key_handle_session_free(key_handle->device, key_handle->session_encrypt); 1490 accel_dpdk_cryptodev_key_handle_session_free(key_handle->device, key_handle->session_decrypt); 1491 TAILQ_REMOVE(&priv->dev_keys, key_handle, link); 1492 spdk_memset_s(key_handle, sizeof(*key_handle), 0, sizeof(*key_handle)); 1493 free(key_handle); 1494 } 1495 1496 if (priv->xts_key) { 1497 spdk_memset_s(priv->xts_key, key->key_size + key->key2_size, 0, key->key_size + key->key2_size); 1498 } 1499 free(priv->xts_key); 1500 free(priv); 1501 } 1502 1503 static int 1504 accel_dpdk_cryptodev_key_init(struct spdk_accel_crypto_key *key) 1505 { 1506 struct accel_dpdk_cryptodev_device *device; 1507 struct accel_dpdk_cryptodev_key_priv *priv; 1508 struct accel_dpdk_cryptodev_key_handle *key_handle; 1509 enum accel_dpdk_cryptodev_driver_type driver; 1510 enum accel_dpdk_crypto_dev_cipher_type cipher; 1511 int rc; 1512 1513 if (!key->param.cipher) { 1514 SPDK_ERRLOG("Cipher is missing\n"); 1515 return -EINVAL; 1516 } 1517 1518 if (strcmp(key->param.cipher, ACCEL_DPDK_CRYPTODEV_AES_CBC) == 0) { 1519 cipher = ACCEL_DPDK_CRYPTODEV_CIPHER_AES_CBC; 1520 } else if (strcmp(key->param.cipher, ACCEL_DPDK_CRYPTODEV_AES_XTS) == 0) { 1521 cipher = ACCEL_DPDK_CRYPTODEV_CIPHER_AES_XTS; 1522 } else { 1523 SPDK_ERRLOG("Unsupported cipher name %s.\n", key->param.cipher); 1524 return -EINVAL; 1525 } 1526 1527 driver = g_dpdk_cryptodev_driver; 1528 1529 if (accel_dpdk_cryptodev_validate_parameters(driver, cipher, key)) { 1530 return -EINVAL; 1531 } 1532 1533 priv = calloc(1, sizeof(*priv)); 1534 if (!priv) { 1535 SPDK_ERRLOG("Memory allocation failed\n"); 1536 return -ENOMEM; 1537 } 1538 key->priv = priv; 1539 priv->driver = driver; 1540 priv->cipher = cipher; 1541 TAILQ_INIT(&priv->dev_keys); 1542 1543 if (cipher == ACCEL_DPDK_CRYPTODEV_CIPHER_AES_XTS) { 1544 /* DPDK expects the keys to be concatenated together. */ 1545 priv->xts_key = calloc(key->key_size + key->key2_size + 1, sizeof(char)); 1546 if (!priv->xts_key) { 1547 SPDK_ERRLOG("Memory allocation failed\n"); 1548 accel_dpdk_cryptodev_key_deinit(key); 1549 return -ENOMEM; 1550 } 1551 memcpy(priv->xts_key, key->key, key->key_size); 1552 memcpy(priv->xts_key + key->key_size, key->key2, key->key2_size); 1553 } 1554 1555 pthread_mutex_lock(&g_device_lock); 1556 TAILQ_FOREACH(device, &g_crypto_devices, link) { 1557 if (device->type != driver) { 1558 continue; 1559 } 1560 key_handle = calloc(1, sizeof(*key_handle)); 1561 if (!key_handle) { 1562 pthread_mutex_unlock(&g_device_lock); 1563 accel_dpdk_cryptodev_key_deinit(key); 1564 return -ENOMEM; 1565 } 1566 key_handle->device = device; 1567 TAILQ_INSERT_TAIL(&priv->dev_keys, key_handle, link); 1568 rc = accel_dpdk_cryptodev_key_handle_configure(key, key_handle); 1569 if (rc) { 1570 pthread_mutex_unlock(&g_device_lock); 1571 accel_dpdk_cryptodev_key_deinit(key); 1572 return rc; 1573 } 1574 if (driver != ACCEL_DPDK_CRYPTODEV_DRIVER_MLX5_PCI) { 1575 /* For MLX5_PCI we need to register a key on each device since 1576 * the key is bound to a specific Protection Domain, 1577 * so don't break the loop */ 1578 break; 1579 } 1580 } 1581 pthread_mutex_unlock(&g_device_lock); 1582 1583 if (TAILQ_EMPTY(&priv->dev_keys)) { 1584 free(priv); 1585 return -ENODEV; 1586 } 1587 1588 return 0; 1589 } 1590 1591 static void 1592 accel_dpdk_cryptodev_write_config_json(struct spdk_json_write_ctx *w) 1593 { 1594 spdk_json_write_object_begin(w); 1595 spdk_json_write_named_string(w, "method", "dpdk_cryptodev_scan_accel_module"); 1596 spdk_json_write_object_end(w); 1597 1598 spdk_json_write_object_begin(w); 1599 spdk_json_write_named_string(w, "method", "dpdk_cryptodev_set_driver"); 1600 spdk_json_write_named_object_begin(w, "params"); 1601 spdk_json_write_named_string(w, "driver_name", g_driver_names[g_dpdk_cryptodev_driver]); 1602 spdk_json_write_object_end(w); 1603 spdk_json_write_object_end(w); 1604 } 1605 1606 static struct spdk_accel_module_if g_accel_dpdk_cryptodev_module = { 1607 .module_init = accel_dpdk_cryptodev_init, 1608 .module_fini = accel_dpdk_cryptodev_fini, 1609 .write_config_json = accel_dpdk_cryptodev_write_config_json, 1610 .get_ctx_size = accel_dpdk_cryptodev_ctx_size, 1611 .name = "dpdk_cryptodev", 1612 .supports_opcode = accel_dpdk_cryptodev_supports_opcode, 1613 .get_io_channel = accel_dpdk_cryptodev_get_io_channel, 1614 .submit_tasks = accel_dpdk_cryptodev_submit_tasks, 1615 .crypto_key_init = accel_dpdk_cryptodev_key_init, 1616 .crypto_key_deinit = accel_dpdk_cryptodev_key_deinit, 1617 }; 1618