1 /*- 2 * BSD LICENSE 3 * 4 * Copyright(c) 2016 Intel Corporation. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in 14 * the documentation and/or other materials provided with the 15 * distribution. 16 * * Neither the name of Intel Corporation nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <string.h> 34 35 #include <rte_common.h> 36 #include <rte_malloc.h> 37 #include <rte_cryptodev_pmd.h> 38 39 #include "rte_openssl_pmd_private.h" 40 41 42 static const struct rte_cryptodev_capabilities openssl_pmd_capabilities[] = { 43 { /* MD5 HMAC */ 44 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 45 {.sym = { 46 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 47 {.auth = { 48 .algo = RTE_CRYPTO_AUTH_MD5_HMAC, 49 .block_size = 64, 50 .key_size = { 51 .min = 64, 52 .max = 64, 53 .increment = 0 54 }, 55 .digest_size = { 56 .min = 16, 57 .max = 16, 58 .increment = 0 59 }, 60 .aad_size = { 0 } 61 }, } 62 }, } 63 }, 64 { /* MD5 */ 65 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 66 {.sym = { 67 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 68 {.auth = { 69 .algo = RTE_CRYPTO_AUTH_MD5, 70 .block_size = 64, 71 .key_size = { 72 .min = 0, 73 .max = 0, 74 .increment = 0 75 }, 76 .digest_size = { 77 .min = 16, 78 .max = 16, 79 .increment = 0 80 }, 81 .aad_size = { 0 } 82 }, } 83 }, } 84 }, 85 { /* SHA1 HMAC */ 86 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 87 {.sym = { 88 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 89 {.auth = { 90 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC, 91 .block_size = 64, 92 .key_size = { 93 .min = 64, 94 .max = 64, 95 .increment = 0 96 }, 97 .digest_size = { 98 .min = 20, 99 .max = 20, 100 .increment = 0 101 }, 102 .aad_size = { 0 } 103 }, } 104 }, } 105 }, 106 { /* SHA1 */ 107 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 108 {.sym = { 109 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 110 {.auth = { 111 .algo = RTE_CRYPTO_AUTH_SHA1, 112 .block_size = 64, 113 .key_size = { 114 .min = 0, 115 .max = 0, 116 .increment = 0 117 }, 118 .digest_size = { 119 .min = 20, 120 .max = 20, 121 .increment = 0 122 }, 123 .aad_size = { 0 } 124 }, } 125 }, } 126 }, 127 { /* SHA224 HMAC */ 128 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 129 {.sym = { 130 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 131 {.auth = { 132 .algo = RTE_CRYPTO_AUTH_SHA224_HMAC, 133 .block_size = 64, 134 .key_size = { 135 .min = 64, 136 .max = 64, 137 .increment = 0 138 }, 139 .digest_size = { 140 .min = 28, 141 .max = 28, 142 .increment = 0 143 }, 144 .aad_size = { 0 } 145 }, } 146 }, } 147 }, 148 { /* SHA224 */ 149 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 150 {.sym = { 151 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 152 {.auth = { 153 .algo = RTE_CRYPTO_AUTH_SHA224, 154 .block_size = 64, 155 .key_size = { 156 .min = 0, 157 .max = 0, 158 .increment = 0 159 }, 160 .digest_size = { 161 .min = 28, 162 .max = 28, 163 .increment = 0 164 }, 165 .aad_size = { 0 } 166 }, } 167 }, } 168 }, 169 { /* SHA256 HMAC */ 170 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 171 {.sym = { 172 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 173 {.auth = { 174 .algo = RTE_CRYPTO_AUTH_SHA256_HMAC, 175 .block_size = 64, 176 .key_size = { 177 .min = 64, 178 .max = 64, 179 .increment = 0 180 }, 181 .digest_size = { 182 .min = 32, 183 .max = 32, 184 .increment = 0 185 }, 186 .aad_size = { 0 } 187 }, } 188 }, } 189 }, 190 { /* SHA256 */ 191 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 192 {.sym = { 193 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 194 {.auth = { 195 .algo = RTE_CRYPTO_AUTH_SHA256, 196 .block_size = 64, 197 .key_size = { 198 .min = 0, 199 .max = 0, 200 .increment = 0 201 }, 202 .digest_size = { 203 .min = 32, 204 .max = 32, 205 .increment = 0 206 }, 207 .aad_size = { 0 } 208 }, } 209 }, } 210 }, 211 { /* SHA384 HMAC */ 212 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 213 {.sym = { 214 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 215 {.auth = { 216 .algo = RTE_CRYPTO_AUTH_SHA384_HMAC, 217 .block_size = 128, 218 .key_size = { 219 .min = 128, 220 .max = 128, 221 .increment = 0 222 }, 223 .digest_size = { 224 .min = 48, 225 .max = 48, 226 .increment = 0 227 }, 228 .aad_size = { 0 } 229 }, } 230 }, } 231 }, 232 { /* SHA384 */ 233 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 234 {.sym = { 235 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 236 {.auth = { 237 .algo = RTE_CRYPTO_AUTH_SHA384, 238 .block_size = 128, 239 .key_size = { 240 .min = 0, 241 .max = 0, 242 .increment = 0 243 }, 244 .digest_size = { 245 .min = 48, 246 .max = 48, 247 .increment = 0 248 }, 249 .aad_size = { 0 } 250 }, } 251 }, } 252 }, 253 { /* SHA512 HMAC */ 254 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 255 {.sym = { 256 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 257 {.auth = { 258 .algo = RTE_CRYPTO_AUTH_SHA512_HMAC, 259 .block_size = 128, 260 .key_size = { 261 .min = 128, 262 .max = 128, 263 .increment = 0 264 }, 265 .digest_size = { 266 .min = 64, 267 .max = 64, 268 .increment = 0 269 }, 270 .aad_size = { 0 } 271 }, } 272 }, } 273 }, 274 { /* SHA512 */ 275 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 276 {.sym = { 277 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 278 {.auth = { 279 .algo = RTE_CRYPTO_AUTH_SHA512, 280 .block_size = 128, 281 .key_size = { 282 .min = 0, 283 .max = 0, 284 .increment = 0 285 }, 286 .digest_size = { 287 .min = 64, 288 .max = 64, 289 .increment = 0 290 }, 291 .aad_size = { 0 } 292 }, } 293 }, } 294 }, 295 { /* AES CBC */ 296 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 297 {.sym = { 298 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER, 299 {.cipher = { 300 .algo = RTE_CRYPTO_CIPHER_AES_CBC, 301 .block_size = 16, 302 .key_size = { 303 .min = 16, 304 .max = 32, 305 .increment = 8 306 }, 307 .iv_size = { 308 .min = 16, 309 .max = 16, 310 .increment = 0 311 } 312 }, } 313 }, } 314 }, 315 { /* AES CTR */ 316 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 317 {.sym = { 318 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER, 319 {.cipher = { 320 .algo = RTE_CRYPTO_CIPHER_AES_CTR, 321 .block_size = 16, 322 .key_size = { 323 .min = 16, 324 .max = 32, 325 .increment = 8 326 }, 327 .iv_size = { 328 .min = 16, 329 .max = 16, 330 .increment = 0 331 } 332 }, } 333 }, } 334 }, 335 { /* AES GCM (AUTH) */ 336 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 337 {.sym = { 338 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 339 {.auth = { 340 .algo = RTE_CRYPTO_AUTH_AES_GCM, 341 .block_size = 16, 342 .key_size = { 343 .min = 16, 344 .max = 32, 345 .increment = 8 346 }, 347 .digest_size = { 348 .min = 16, 349 .max = 16, 350 .increment = 0 351 }, 352 .aad_size = { 353 .min = 8, 354 .max = 12, 355 .increment = 4 356 } 357 }, } 358 }, } 359 }, 360 { /* AES GCM (CIPHER) */ 361 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 362 {.sym = { 363 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER, 364 {.cipher = { 365 .algo = RTE_CRYPTO_CIPHER_AES_GCM, 366 .block_size = 16, 367 .key_size = { 368 .min = 16, 369 .max = 16, 370 .increment = 0 371 }, 372 .iv_size = { 373 .min = 12, 374 .max = 16, 375 .increment = 4 376 } 377 }, } 378 }, } 379 }, 380 { /* AES GMAC (AUTH) */ 381 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 382 {.sym = { 383 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH, 384 {.auth = { 385 .algo = RTE_CRYPTO_AUTH_AES_GMAC, 386 .block_size = 16, 387 .key_size = { 388 .min = 16, 389 .max = 32, 390 .increment = 8 391 }, 392 .digest_size = { 393 .min = 16, 394 .max = 16, 395 .increment = 0 396 }, 397 .aad_size = { 398 .min = 8, 399 .max = 65532, 400 .increment = 4 401 } 402 }, } 403 }, } 404 }, 405 { /* 3DES CBC */ 406 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 407 {.sym = { 408 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER, 409 {.cipher = { 410 .algo = RTE_CRYPTO_CIPHER_3DES_CBC, 411 .block_size = 8, 412 .key_size = { 413 .min = 16, 414 .max = 24, 415 .increment = 8 416 }, 417 .iv_size = { 418 .min = 8, 419 .max = 8, 420 .increment = 0 421 } 422 }, } 423 }, } 424 }, 425 { /* 3DES CTR */ 426 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC, 427 {.sym = { 428 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER, 429 {.cipher = { 430 .algo = RTE_CRYPTO_CIPHER_3DES_CTR, 431 .block_size = 8, 432 .key_size = { 433 .min = 16, 434 .max = 24, 435 .increment = 8 436 }, 437 .iv_size = { 438 .min = 8, 439 .max = 8, 440 .increment = 0 441 } 442 }, } 443 }, } 444 }, 445 446 RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST() 447 }; 448 449 450 /** Configure device */ 451 static int 452 openssl_pmd_config(__rte_unused struct rte_cryptodev *dev, 453 __rte_unused struct rte_cryptodev_config *config) 454 { 455 return 0; 456 } 457 458 /** Start device */ 459 static int 460 openssl_pmd_start(__rte_unused struct rte_cryptodev *dev) 461 { 462 return 0; 463 } 464 465 /** Stop device */ 466 static void 467 openssl_pmd_stop(__rte_unused struct rte_cryptodev *dev) 468 { 469 } 470 471 /** Close device */ 472 static int 473 openssl_pmd_close(__rte_unused struct rte_cryptodev *dev) 474 { 475 return 0; 476 } 477 478 479 /** Get device statistics */ 480 static void 481 openssl_pmd_stats_get(struct rte_cryptodev *dev, 482 struct rte_cryptodev_stats *stats) 483 { 484 int qp_id; 485 486 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) { 487 struct openssl_qp *qp = dev->data->queue_pairs[qp_id]; 488 489 stats->enqueued_count += qp->stats.enqueued_count; 490 stats->dequeued_count += qp->stats.dequeued_count; 491 492 stats->enqueue_err_count += qp->stats.enqueue_err_count; 493 stats->dequeue_err_count += qp->stats.dequeue_err_count; 494 } 495 } 496 497 /** Reset device statistics */ 498 static void 499 openssl_pmd_stats_reset(struct rte_cryptodev *dev) 500 { 501 int qp_id; 502 503 for (qp_id = 0; qp_id < dev->data->nb_queue_pairs; qp_id++) { 504 struct openssl_qp *qp = dev->data->queue_pairs[qp_id]; 505 506 memset(&qp->stats, 0, sizeof(qp->stats)); 507 } 508 } 509 510 511 /** Get device info */ 512 static void 513 openssl_pmd_info_get(struct rte_cryptodev *dev, 514 struct rte_cryptodev_info *dev_info) 515 { 516 struct openssl_private *internals = dev->data->dev_private; 517 518 if (dev_info != NULL) { 519 dev_info->dev_type = dev->dev_type; 520 dev_info->feature_flags = dev->feature_flags; 521 dev_info->capabilities = openssl_pmd_capabilities; 522 dev_info->max_nb_queue_pairs = internals->max_nb_qpairs; 523 dev_info->sym.max_nb_sessions = internals->max_nb_sessions; 524 } 525 } 526 527 /** Release queue pair */ 528 static int 529 openssl_pmd_qp_release(struct rte_cryptodev *dev, uint16_t qp_id) 530 { 531 if (dev->data->queue_pairs[qp_id] != NULL) { 532 rte_free(dev->data->queue_pairs[qp_id]); 533 dev->data->queue_pairs[qp_id] = NULL; 534 } 535 return 0; 536 } 537 538 /** set a unique name for the queue pair based on it's name, dev_id and qp_id */ 539 static int 540 openssl_pmd_qp_set_unique_name(struct rte_cryptodev *dev, 541 struct openssl_qp *qp) 542 { 543 unsigned int n = snprintf(qp->name, sizeof(qp->name), 544 "openssl_pmd_%u_qp_%u", 545 dev->data->dev_id, qp->id); 546 547 if (n > sizeof(qp->name)) 548 return -1; 549 550 return 0; 551 } 552 553 554 /** Create a ring to place processed operations on */ 555 static struct rte_ring * 556 openssl_pmd_qp_create_processed_ops_ring(struct openssl_qp *qp, 557 unsigned int ring_size, int socket_id) 558 { 559 struct rte_ring *r; 560 561 r = rte_ring_lookup(qp->name); 562 if (r) { 563 if (rte_ring_get_size(r) >= ring_size) { 564 OPENSSL_LOG_INFO( 565 "Reusing existing ring %s for processed ops", 566 qp->name); 567 return r; 568 } 569 570 OPENSSL_LOG_ERR( 571 "Unable to reuse existing ring %s for processed ops", 572 qp->name); 573 return NULL; 574 } 575 576 return rte_ring_create(qp->name, ring_size, socket_id, 577 RING_F_SP_ENQ | RING_F_SC_DEQ); 578 } 579 580 581 /** Setup a queue pair */ 582 static int 583 openssl_pmd_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id, 584 const struct rte_cryptodev_qp_conf *qp_conf, 585 int socket_id) 586 { 587 struct openssl_qp *qp = NULL; 588 589 /* Free memory prior to re-allocation if needed. */ 590 if (dev->data->queue_pairs[qp_id] != NULL) 591 openssl_pmd_qp_release(dev, qp_id); 592 593 /* Allocate the queue pair data structure. */ 594 qp = rte_zmalloc_socket("OPENSSL PMD Queue Pair", sizeof(*qp), 595 RTE_CACHE_LINE_SIZE, socket_id); 596 if (qp == NULL) 597 return -ENOMEM; 598 599 qp->id = qp_id; 600 dev->data->queue_pairs[qp_id] = qp; 601 602 if (openssl_pmd_qp_set_unique_name(dev, qp)) 603 goto qp_setup_cleanup; 604 605 qp->processed_ops = openssl_pmd_qp_create_processed_ops_ring(qp, 606 qp_conf->nb_descriptors, socket_id); 607 if (qp->processed_ops == NULL) 608 goto qp_setup_cleanup; 609 610 qp->sess_mp = dev->data->session_pool; 611 612 memset(&qp->stats, 0, sizeof(qp->stats)); 613 614 return 0; 615 616 qp_setup_cleanup: 617 if (qp) 618 rte_free(qp); 619 620 return -1; 621 } 622 623 /** Start queue pair */ 624 static int 625 openssl_pmd_qp_start(__rte_unused struct rte_cryptodev *dev, 626 __rte_unused uint16_t queue_pair_id) 627 { 628 return -ENOTSUP; 629 } 630 631 /** Stop queue pair */ 632 static int 633 openssl_pmd_qp_stop(__rte_unused struct rte_cryptodev *dev, 634 __rte_unused uint16_t queue_pair_id) 635 { 636 return -ENOTSUP; 637 } 638 639 /** Return the number of allocated queue pairs */ 640 static uint32_t 641 openssl_pmd_qp_count(struct rte_cryptodev *dev) 642 { 643 return dev->data->nb_queue_pairs; 644 } 645 646 /** Returns the size of the session structure */ 647 static unsigned 648 openssl_pmd_session_get_size(struct rte_cryptodev *dev __rte_unused) 649 { 650 return sizeof(struct openssl_session); 651 } 652 653 /** Configure the session from a crypto xform chain */ 654 static void * 655 openssl_pmd_session_configure(struct rte_cryptodev *dev __rte_unused, 656 struct rte_crypto_sym_xform *xform, void *sess) 657 { 658 if (unlikely(sess == NULL)) { 659 OPENSSL_LOG_ERR("invalid session struct"); 660 return NULL; 661 } 662 663 if (openssl_set_session_parameters( 664 sess, xform) != 0) { 665 OPENSSL_LOG_ERR("failed configure session parameters"); 666 return NULL; 667 } 668 669 return sess; 670 } 671 672 673 /** Clear the memory of session so it doesn't leave key material behind */ 674 static void 675 openssl_pmd_session_clear(struct rte_cryptodev *dev __rte_unused, void *sess) 676 { 677 /* 678 * Current just resetting the whole data structure, need to investigate 679 * whether a more selective reset of key would be more performant 680 */ 681 if (sess) { 682 openssl_reset_session(sess); 683 memset(sess, 0, sizeof(struct openssl_session)); 684 } 685 } 686 687 struct rte_cryptodev_ops openssl_pmd_ops = { 688 .dev_configure = openssl_pmd_config, 689 .dev_start = openssl_pmd_start, 690 .dev_stop = openssl_pmd_stop, 691 .dev_close = openssl_pmd_close, 692 693 .stats_get = openssl_pmd_stats_get, 694 .stats_reset = openssl_pmd_stats_reset, 695 696 .dev_infos_get = openssl_pmd_info_get, 697 698 .queue_pair_setup = openssl_pmd_qp_setup, 699 .queue_pair_release = openssl_pmd_qp_release, 700 .queue_pair_start = openssl_pmd_qp_start, 701 .queue_pair_stop = openssl_pmd_qp_stop, 702 .queue_pair_count = openssl_pmd_qp_count, 703 704 .session_get_size = openssl_pmd_session_get_size, 705 .session_configure = openssl_pmd_session_configure, 706 .session_clear = openssl_pmd_session_clear 707 }; 708 709 struct rte_cryptodev_ops *rte_openssl_pmd_ops = &openssl_pmd_ops; 710