1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015-2017 Intel Corporation
5 #include <intel-ipsec-mb.h>
7 #include <rte_common.h>
8 #include <rte_hexdump.h>
9 #include <rte_cryptodev.h>
10 #include <rte_cryptodev_pmd.h>
11 #include <rte_bus_vdev.h>
12 #include <rte_malloc.h>
13 #include <rte_cpuflags.h>
14 #include <rte_per_lcore.h>
16 #include "aesni_mb_pmd_private.h"
18 int aesni_mb_logtype_driver;
20 #define AES_CCM_DIGEST_MIN_LEN 4
21 #define AES_CCM_DIGEST_MAX_LEN 16
22 #define HMAC_MAX_BLOCK_SIZE 128
23 static uint8_t cryptodev_driver_id;
26 * Needed to support CPU-CRYPTO API (rte_cryptodev_sym_cpu_crypto_process),
27 * as we still use JOB based API even for synchronous processing.
29 static RTE_DEFINE_PER_LCORE(MB_MGR *, sync_mb_mgr);
31 typedef void (*hash_one_block_t)(const void *data, void *digest);
32 typedef void (*aes_keyexp_t)(const void *key, void *enc_exp_keys, void *dec_exp_keys);
35 * Calculate the authentication pre-computes
37 * @param one_block_hash Function pointer to calculate digest on ipad/opad
38 * @param ipad Inner pad output byte array
39 * @param opad Outer pad output byte array
40 * @param hkey Authentication key
41 * @param hkey_len Authentication key length
42 * @param blocksize Block size of selected hash algo
45 calculate_auth_precomputes(hash_one_block_t one_block_hash,
46 uint8_t *ipad, uint8_t *opad,
47 const uint8_t *hkey, uint16_t hkey_len,
52 uint8_t ipad_buf[blocksize] __rte_aligned(16);
53 uint8_t opad_buf[blocksize] __rte_aligned(16);
55 /* Setup inner and outer pads */
56 memset(ipad_buf, HMAC_IPAD_VALUE, blocksize);
57 memset(opad_buf, HMAC_OPAD_VALUE, blocksize);
59 /* XOR hash key with inner and outer pads */
60 length = hkey_len > blocksize ? blocksize : hkey_len;
62 for (i = 0; i < length; i++) {
63 ipad_buf[i] ^= hkey[i];
64 opad_buf[i] ^= hkey[i];
67 /* Compute partial hashes */
68 (*one_block_hash)(ipad_buf, ipad);
69 (*one_block_hash)(opad_buf, opad);
72 memset(ipad_buf, 0, blocksize);
73 memset(opad_buf, 0, blocksize);
76 /** Get xform chain order */
77 static enum aesni_mb_operation
78 aesni_mb_get_chain_order(const struct rte_crypto_sym_xform *xform)
81 return AESNI_MB_OP_NOT_SUPPORTED;
83 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
84 if (xform->next == NULL)
85 return AESNI_MB_OP_CIPHER_ONLY;
86 if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
87 return AESNI_MB_OP_CIPHER_HASH;
90 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
91 if (xform->next == NULL)
92 return AESNI_MB_OP_HASH_ONLY;
93 if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
94 return AESNI_MB_OP_HASH_CIPHER;
96 #if IMB_VERSION_NUM > IMB_VERSION(0, 52, 0)
97 if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
98 if (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) {
100 * CCM requires to hash first and cipher later
103 if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_CCM)
104 return AESNI_MB_OP_AEAD_HASH_CIPHER;
106 return AESNI_MB_OP_AEAD_CIPHER_HASH;
108 if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_CCM)
109 return AESNI_MB_OP_AEAD_CIPHER_HASH;
111 return AESNI_MB_OP_AEAD_HASH_CIPHER;
115 if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
116 if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_CCM ||
117 xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM) {
118 if (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
119 return AESNI_MB_OP_AEAD_CIPHER_HASH;
121 return AESNI_MB_OP_AEAD_HASH_CIPHER;
126 return AESNI_MB_OP_NOT_SUPPORTED;
129 /** Set session authentication parameters */
131 aesni_mb_set_session_auth_parameters(const MB_MGR *mb_mgr,
132 struct aesni_mb_session *sess,
133 const struct rte_crypto_sym_xform *xform)
135 hash_one_block_t hash_oneblock_fn = NULL;
136 unsigned int key_larger_block_size = 0;
137 uint8_t hashed_key[HMAC_MAX_BLOCK_SIZE] = { 0 };
138 uint32_t auth_precompute = 1;
141 sess->auth.algo = NULL_HASH;
145 if (xform->type != RTE_CRYPTO_SYM_XFORM_AUTH) {
146 AESNI_MB_LOG(ERR, "Crypto xform struct not of type auth");
150 /* Set the request digest size */
151 sess->auth.req_digest_len = xform->auth.digest_length;
153 /* Select auth generate/verify */
154 sess->auth.operation = xform->auth.op;
156 /* Set Authentication Parameters */
157 if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_XCBC_MAC) {
158 sess->auth.algo = AES_XCBC;
160 uint16_t xcbc_mac_digest_len =
161 get_truncated_digest_byte_length(AES_XCBC);
162 if (sess->auth.req_digest_len != xcbc_mac_digest_len) {
163 AESNI_MB_LOG(ERR, "Invalid digest size\n");
166 sess->auth.gen_digest_len = sess->auth.req_digest_len;
168 IMB_AES_XCBC_KEYEXP(mb_mgr, xform->auth.key.data,
169 sess->auth.xcbc.k1_expanded,
170 sess->auth.xcbc.k2, sess->auth.xcbc.k3);
174 if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_CMAC) {
177 sess->auth.algo = AES_CMAC;
179 uint16_t cmac_digest_len = get_digest_byte_length(AES_CMAC);
181 if (sess->auth.req_digest_len > cmac_digest_len) {
182 AESNI_MB_LOG(ERR, "Invalid digest size\n");
186 * Multi-buffer lib supports digest sizes from 4 to 16 bytes
187 * in version 0.50 and sizes of 12 and 16 bytes,
189 * If size requested is different, generate the full digest
190 * (16 bytes) in a temporary location and then memcpy
191 * the requested number of bytes.
193 if (sess->auth.req_digest_len < 4)
194 sess->auth.gen_digest_len = cmac_digest_len;
196 sess->auth.gen_digest_len = sess->auth.req_digest_len;
198 IMB_AES_KEYEXP_128(mb_mgr, xform->auth.key.data,
199 sess->auth.cmac.expkey, dust);
200 IMB_AES_CMAC_SUBKEY_GEN_128(mb_mgr, sess->auth.cmac.expkey,
201 sess->auth.cmac.skey1, sess->auth.cmac.skey2);
205 if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC) {
206 if (xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) {
207 sess->cipher.direction = ENCRYPT;
208 sess->chain_order = CIPHER_HASH;
210 sess->cipher.direction = DECRYPT;
212 sess->auth.algo = AES_GMAC;
214 * Multi-buffer lib supports 8, 12 and 16 bytes of digest.
215 * If size requested is different, generate the full digest
216 * (16 bytes) in a temporary location and then memcpy
217 * the requested number of bytes.
219 if (sess->auth.req_digest_len != 16 &&
220 sess->auth.req_digest_len != 12 &&
221 sess->auth.req_digest_len != 8) {
222 sess->auth.gen_digest_len = 16;
224 sess->auth.gen_digest_len = sess->auth.req_digest_len;
226 sess->iv.length = xform->auth.iv.length;
227 sess->iv.offset = xform->auth.iv.offset;
229 switch (xform->auth.key.length) {
231 IMB_AES128_GCM_PRE(mb_mgr, xform->auth.key.data,
232 &sess->cipher.gcm_key);
233 sess->cipher.key_length_in_bytes = AES_128_BYTES;
236 IMB_AES192_GCM_PRE(mb_mgr, xform->auth.key.data,
237 &sess->cipher.gcm_key);
238 sess->cipher.key_length_in_bytes = AES_192_BYTES;
241 IMB_AES256_GCM_PRE(mb_mgr, xform->auth.key.data,
242 &sess->cipher.gcm_key);
243 sess->cipher.key_length_in_bytes = AES_256_BYTES;
246 RTE_LOG(ERR, PMD, "failed to parse test type\n");
253 switch (xform->auth.algo) {
254 case RTE_CRYPTO_AUTH_MD5_HMAC:
255 sess->auth.algo = MD5;
256 hash_oneblock_fn = mb_mgr->md5_one_block;
258 case RTE_CRYPTO_AUTH_SHA1_HMAC:
259 sess->auth.algo = SHA1;
260 hash_oneblock_fn = mb_mgr->sha1_one_block;
261 if (xform->auth.key.length > get_auth_algo_blocksize(SHA1)) {
263 xform->auth.key.data,
264 xform->auth.key.length,
266 key_larger_block_size = 1;
269 case RTE_CRYPTO_AUTH_SHA1:
270 sess->auth.algo = PLAIN_SHA1;
273 case RTE_CRYPTO_AUTH_SHA224_HMAC:
274 sess->auth.algo = SHA_224;
275 hash_oneblock_fn = mb_mgr->sha224_one_block;
276 if (xform->auth.key.length > get_auth_algo_blocksize(SHA_224)) {
278 xform->auth.key.data,
279 xform->auth.key.length,
281 key_larger_block_size = 1;
284 case RTE_CRYPTO_AUTH_SHA224:
285 sess->auth.algo = PLAIN_SHA_224;
288 case RTE_CRYPTO_AUTH_SHA256_HMAC:
289 sess->auth.algo = SHA_256;
290 hash_oneblock_fn = mb_mgr->sha256_one_block;
291 if (xform->auth.key.length > get_auth_algo_blocksize(SHA_256)) {
293 xform->auth.key.data,
294 xform->auth.key.length,
296 key_larger_block_size = 1;
299 case RTE_CRYPTO_AUTH_SHA256:
300 sess->auth.algo = PLAIN_SHA_256;
303 case RTE_CRYPTO_AUTH_SHA384_HMAC:
304 sess->auth.algo = SHA_384;
305 hash_oneblock_fn = mb_mgr->sha384_one_block;
306 if (xform->auth.key.length > get_auth_algo_blocksize(SHA_384)) {
308 xform->auth.key.data,
309 xform->auth.key.length,
311 key_larger_block_size = 1;
314 case RTE_CRYPTO_AUTH_SHA384:
315 sess->auth.algo = PLAIN_SHA_384;
318 case RTE_CRYPTO_AUTH_SHA512_HMAC:
319 sess->auth.algo = SHA_512;
320 hash_oneblock_fn = mb_mgr->sha512_one_block;
321 if (xform->auth.key.length > get_auth_algo_blocksize(SHA_512)) {
323 xform->auth.key.data,
324 xform->auth.key.length,
326 key_larger_block_size = 1;
329 case RTE_CRYPTO_AUTH_SHA512:
330 sess->auth.algo = PLAIN_SHA_512;
334 AESNI_MB_LOG(ERR, "Unsupported authentication algorithm selection");
337 uint16_t trunc_digest_size =
338 get_truncated_digest_byte_length(sess->auth.algo);
339 uint16_t full_digest_size =
340 get_digest_byte_length(sess->auth.algo);
342 if (sess->auth.req_digest_len > full_digest_size ||
343 sess->auth.req_digest_len == 0) {
344 AESNI_MB_LOG(ERR, "Invalid digest size\n");
348 if (sess->auth.req_digest_len != trunc_digest_size &&
349 sess->auth.req_digest_len != full_digest_size)
350 sess->auth.gen_digest_len = full_digest_size;
352 sess->auth.gen_digest_len = sess->auth.req_digest_len;
354 /* Plain SHA does not require precompute key */
355 if (auth_precompute == 0)
358 /* Calculate Authentication precomputes */
359 if (key_larger_block_size) {
360 calculate_auth_precomputes(hash_oneblock_fn,
361 sess->auth.pads.inner, sess->auth.pads.outer,
363 xform->auth.key.length,
364 get_auth_algo_blocksize(sess->auth.algo));
366 calculate_auth_precomputes(hash_oneblock_fn,
367 sess->auth.pads.inner, sess->auth.pads.outer,
368 xform->auth.key.data,
369 xform->auth.key.length,
370 get_auth_algo_blocksize(sess->auth.algo));
376 /** Set session cipher parameters */
378 aesni_mb_set_session_cipher_parameters(const MB_MGR *mb_mgr,
379 struct aesni_mb_session *sess,
380 const struct rte_crypto_sym_xform *xform)
386 sess->cipher.mode = NULL_CIPHER;
390 if (xform->type != RTE_CRYPTO_SYM_XFORM_CIPHER) {
391 AESNI_MB_LOG(ERR, "Crypto xform struct not of type cipher");
395 /* Select cipher direction */
396 switch (xform->cipher.op) {
397 case RTE_CRYPTO_CIPHER_OP_ENCRYPT:
398 sess->cipher.direction = ENCRYPT;
400 case RTE_CRYPTO_CIPHER_OP_DECRYPT:
401 sess->cipher.direction = DECRYPT;
404 AESNI_MB_LOG(ERR, "Invalid cipher operation parameter");
408 /* Select cipher mode */
409 switch (xform->cipher.algo) {
410 case RTE_CRYPTO_CIPHER_AES_CBC:
411 sess->cipher.mode = CBC;
414 case RTE_CRYPTO_CIPHER_AES_CTR:
415 sess->cipher.mode = CNTR;
418 case RTE_CRYPTO_CIPHER_AES_DOCSISBPI:
419 sess->cipher.mode = DOCSIS_SEC_BPI;
422 case RTE_CRYPTO_CIPHER_DES_CBC:
423 sess->cipher.mode = DES;
425 case RTE_CRYPTO_CIPHER_DES_DOCSISBPI:
426 sess->cipher.mode = DOCSIS_DES;
428 case RTE_CRYPTO_CIPHER_3DES_CBC:
429 sess->cipher.mode = DES3;
433 AESNI_MB_LOG(ERR, "Unsupported cipher mode parameter");
437 /* Set IV parameters */
438 sess->iv.offset = xform->cipher.iv.offset;
439 sess->iv.length = xform->cipher.iv.length;
441 /* Check key length and choose key expansion function for AES */
443 switch (xform->cipher.key.length) {
445 sess->cipher.key_length_in_bytes = AES_128_BYTES;
446 IMB_AES_KEYEXP_128(mb_mgr, xform->cipher.key.data,
447 sess->cipher.expanded_aes_keys.encode,
448 sess->cipher.expanded_aes_keys.decode);
451 sess->cipher.key_length_in_bytes = AES_192_BYTES;
452 IMB_AES_KEYEXP_192(mb_mgr, xform->cipher.key.data,
453 sess->cipher.expanded_aes_keys.encode,
454 sess->cipher.expanded_aes_keys.decode);
457 sess->cipher.key_length_in_bytes = AES_256_BYTES;
458 IMB_AES_KEYEXP_256(mb_mgr, xform->cipher.key.data,
459 sess->cipher.expanded_aes_keys.encode,
460 sess->cipher.expanded_aes_keys.decode);
463 AESNI_MB_LOG(ERR, "Invalid cipher key length");
466 } else if (is_3DES) {
467 uint64_t *keys[3] = {sess->cipher.exp_3des_keys.key[0],
468 sess->cipher.exp_3des_keys.key[1],
469 sess->cipher.exp_3des_keys.key[2]};
471 switch (xform->cipher.key.length) {
473 IMB_DES_KEYSCHED(mb_mgr, keys[0],
474 xform->cipher.key.data);
475 IMB_DES_KEYSCHED(mb_mgr, keys[1],
476 xform->cipher.key.data + 8);
477 IMB_DES_KEYSCHED(mb_mgr, keys[2],
478 xform->cipher.key.data + 16);
480 /* Initialize keys - 24 bytes: [K1-K2-K3] */
481 sess->cipher.exp_3des_keys.ks_ptr[0] = keys[0];
482 sess->cipher.exp_3des_keys.ks_ptr[1] = keys[1];
483 sess->cipher.exp_3des_keys.ks_ptr[2] = keys[2];
486 IMB_DES_KEYSCHED(mb_mgr, keys[0],
487 xform->cipher.key.data);
488 IMB_DES_KEYSCHED(mb_mgr, keys[1],
489 xform->cipher.key.data + 8);
490 /* Initialize keys - 16 bytes: [K1=K1,K2=K2,K3=K1] */
491 sess->cipher.exp_3des_keys.ks_ptr[0] = keys[0];
492 sess->cipher.exp_3des_keys.ks_ptr[1] = keys[1];
493 sess->cipher.exp_3des_keys.ks_ptr[2] = keys[0];
496 IMB_DES_KEYSCHED(mb_mgr, keys[0],
497 xform->cipher.key.data);
499 /* Initialize keys - 8 bytes: [K1 = K2 = K3] */
500 sess->cipher.exp_3des_keys.ks_ptr[0] = keys[0];
501 sess->cipher.exp_3des_keys.ks_ptr[1] = keys[0];
502 sess->cipher.exp_3des_keys.ks_ptr[2] = keys[0];
505 AESNI_MB_LOG(ERR, "Invalid cipher key length");
509 sess->cipher.key_length_in_bytes = 24;
511 if (xform->cipher.key.length != 8) {
512 AESNI_MB_LOG(ERR, "Invalid cipher key length");
515 sess->cipher.key_length_in_bytes = 8;
517 IMB_DES_KEYSCHED(mb_mgr,
518 (uint64_t *)sess->cipher.expanded_aes_keys.encode,
519 xform->cipher.key.data);
520 IMB_DES_KEYSCHED(mb_mgr,
521 (uint64_t *)sess->cipher.expanded_aes_keys.decode,
522 xform->cipher.key.data);
529 aesni_mb_set_session_aead_parameters(const MB_MGR *mb_mgr,
530 struct aesni_mb_session *sess,
531 const struct rte_crypto_sym_xform *xform)
533 switch (xform->aead.op) {
534 case RTE_CRYPTO_AEAD_OP_ENCRYPT:
535 sess->cipher.direction = ENCRYPT;
536 sess->auth.operation = RTE_CRYPTO_AUTH_OP_GENERATE;
538 case RTE_CRYPTO_AEAD_OP_DECRYPT:
539 sess->cipher.direction = DECRYPT;
540 sess->auth.operation = RTE_CRYPTO_AUTH_OP_VERIFY;
543 AESNI_MB_LOG(ERR, "Invalid aead operation parameter");
547 switch (xform->aead.algo) {
548 case RTE_CRYPTO_AEAD_AES_CCM:
549 sess->cipher.mode = CCM;
550 sess->auth.algo = AES_CCM;
552 /* Check key length and choose key expansion function for AES */
553 switch (xform->aead.key.length) {
555 sess->cipher.key_length_in_bytes = AES_128_BYTES;
556 IMB_AES_KEYEXP_128(mb_mgr, xform->aead.key.data,
557 sess->cipher.expanded_aes_keys.encode,
558 sess->cipher.expanded_aes_keys.decode);
561 AESNI_MB_LOG(ERR, "Invalid cipher key length");
567 case RTE_CRYPTO_AEAD_AES_GCM:
568 sess->cipher.mode = GCM;
569 sess->auth.algo = AES_GMAC;
571 switch (xform->aead.key.length) {
573 sess->cipher.key_length_in_bytes = AES_128_BYTES;
574 IMB_AES128_GCM_PRE(mb_mgr, xform->aead.key.data,
575 &sess->cipher.gcm_key);
578 sess->cipher.key_length_in_bytes = AES_192_BYTES;
579 IMB_AES192_GCM_PRE(mb_mgr, xform->aead.key.data,
580 &sess->cipher.gcm_key);
583 sess->cipher.key_length_in_bytes = AES_256_BYTES;
584 IMB_AES256_GCM_PRE(mb_mgr, xform->aead.key.data,
585 &sess->cipher.gcm_key);
588 AESNI_MB_LOG(ERR, "Invalid cipher key length");
595 AESNI_MB_LOG(ERR, "Unsupported aead mode parameter");
599 /* Set IV parameters */
600 sess->iv.offset = xform->aead.iv.offset;
601 sess->iv.length = xform->aead.iv.length;
603 sess->auth.req_digest_len = xform->aead.digest_length;
604 /* CCM digests must be between 4 and 16 and an even number */
605 if (sess->auth.req_digest_len < AES_CCM_DIGEST_MIN_LEN ||
606 sess->auth.req_digest_len > AES_CCM_DIGEST_MAX_LEN ||
607 (sess->auth.req_digest_len & 1) == 1) {
608 AESNI_MB_LOG(ERR, "Invalid digest size\n");
611 sess->auth.gen_digest_len = sess->auth.req_digest_len;
616 /** Parse crypto xform chain and set private session parameters */
618 aesni_mb_set_session_parameters(const MB_MGR *mb_mgr,
619 struct aesni_mb_session *sess,
620 const struct rte_crypto_sym_xform *xform)
622 const struct rte_crypto_sym_xform *auth_xform = NULL;
623 const struct rte_crypto_sym_xform *cipher_xform = NULL;
624 const struct rte_crypto_sym_xform *aead_xform = NULL;
627 /* Select Crypto operation - hash then cipher / cipher then hash */
628 switch (aesni_mb_get_chain_order(xform)) {
629 case AESNI_MB_OP_HASH_CIPHER:
630 sess->chain_order = HASH_CIPHER;
632 cipher_xform = xform->next;
634 case AESNI_MB_OP_CIPHER_HASH:
635 sess->chain_order = CIPHER_HASH;
636 auth_xform = xform->next;
637 cipher_xform = xform;
639 case AESNI_MB_OP_HASH_ONLY:
640 sess->chain_order = HASH_CIPHER;
644 case AESNI_MB_OP_CIPHER_ONLY:
646 * Multi buffer library operates only at two modes,
647 * CIPHER_HASH and HASH_CIPHER. When doing ciphering only,
648 * chain order depends on cipher operation: encryption is always
649 * the first operation and decryption the last one.
651 if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
652 sess->chain_order = CIPHER_HASH;
654 sess->chain_order = HASH_CIPHER;
656 cipher_xform = xform;
658 case AESNI_MB_OP_AEAD_CIPHER_HASH:
659 sess->chain_order = CIPHER_HASH;
660 sess->aead.aad_len = xform->aead.aad_length;
663 case AESNI_MB_OP_AEAD_HASH_CIPHER:
664 sess->chain_order = HASH_CIPHER;
665 sess->aead.aad_len = xform->aead.aad_length;
668 case AESNI_MB_OP_NOT_SUPPORTED:
670 AESNI_MB_LOG(ERR, "Unsupported operation chain order parameter");
674 /* Default IV length = 0 */
677 ret = aesni_mb_set_session_auth_parameters(mb_mgr, sess, auth_xform);
679 AESNI_MB_LOG(ERR, "Invalid/unsupported authentication parameters");
683 ret = aesni_mb_set_session_cipher_parameters(mb_mgr, sess,
686 AESNI_MB_LOG(ERR, "Invalid/unsupported cipher parameters");
691 ret = aesni_mb_set_session_aead_parameters(mb_mgr, sess,
694 AESNI_MB_LOG(ERR, "Invalid/unsupported aead parameters");
703 * burst enqueue, place crypto operations on ingress queue for processing.
705 * @param __qp Queue Pair to process
706 * @param ops Crypto operations for processing
707 * @param nb_ops Number of crypto operations for processing
710 * - Number of crypto operations enqueued
713 aesni_mb_pmd_enqueue_burst(void *__qp, struct rte_crypto_op **ops,
716 struct aesni_mb_qp *qp = __qp;
718 unsigned int nb_enqueued;
720 nb_enqueued = rte_ring_enqueue_burst(qp->ingress_queue,
721 (void **)ops, nb_ops, NULL);
723 qp->stats.enqueued_count += nb_enqueued;
728 /** Get multi buffer session */
729 static inline struct aesni_mb_session *
730 get_session(struct aesni_mb_qp *qp, struct rte_crypto_op *op)
732 struct aesni_mb_session *sess = NULL;
734 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
735 if (likely(op->sym->session != NULL))
736 sess = (struct aesni_mb_session *)
737 get_sym_session_private_data(
739 cryptodev_driver_id);
741 void *_sess = rte_cryptodev_sym_session_create(qp->sess_mp);
742 void *_sess_private_data = NULL;
747 if (rte_mempool_get(qp->sess_mp_priv,
748 (void **)&_sess_private_data))
751 sess = (struct aesni_mb_session *)_sess_private_data;
753 if (unlikely(aesni_mb_set_session_parameters(qp->mb_mgr,
754 sess, op->sym->xform) != 0)) {
755 rte_mempool_put(qp->sess_mp, _sess);
756 rte_mempool_put(qp->sess_mp_priv, _sess_private_data);
759 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
760 set_sym_session_private_data(op->sym->session,
761 cryptodev_driver_id, _sess_private_data);
764 if (unlikely(sess == NULL))
765 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
770 static inline uint64_t
771 auth_start_offset(struct rte_crypto_op *op, struct aesni_mb_session *session,
774 struct rte_mbuf *m_src, *m_dst;
775 uint8_t *p_src, *p_dst;
776 uintptr_t u_src, u_dst;
777 uint32_t cipher_end, auth_end;
779 /* Only cipher then hash needs special calculation. */
780 if (!oop || session->chain_order != CIPHER_HASH)
781 return op->sym->auth.data.offset;
783 m_src = op->sym->m_src;
784 m_dst = op->sym->m_dst;
786 p_src = rte_pktmbuf_mtod(m_src, uint8_t *);
787 p_dst = rte_pktmbuf_mtod(m_dst, uint8_t *);
788 u_src = (uintptr_t)p_src;
789 u_dst = (uintptr_t)p_dst + op->sym->auth.data.offset;
792 * Copy the content between cipher offset and auth offset for generating
795 if (op->sym->cipher.data.offset > op->sym->auth.data.offset)
796 memcpy(p_dst + op->sym->auth.data.offset,
797 p_src + op->sym->auth.data.offset,
798 op->sym->cipher.data.offset -
799 op->sym->auth.data.offset);
802 * Copy the content between (cipher offset + length) and (auth offset +
803 * length) for generating correct digest
805 cipher_end = op->sym->cipher.data.offset + op->sym->cipher.data.length;
806 auth_end = op->sym->auth.data.offset + op->sym->auth.data.length;
807 if (cipher_end < auth_end)
808 memcpy(p_dst + cipher_end, p_src + cipher_end,
809 auth_end - cipher_end);
812 * Since intel-ipsec-mb only supports positive values,
813 * we need to deduct the correct offset between src and dst.
816 return u_src < u_dst ? (u_dst - u_src) :
817 (UINT64_MAX - u_src + u_dst + 1);
821 set_cpu_mb_job_params(JOB_AES_HMAC *job, struct aesni_mb_session *session,
822 union rte_crypto_sym_ofs sofs, void *buf, uint32_t len,
823 void *iv, void *aad, void *digest, void *udata)
825 /* Set crypto operation */
826 job->chain_order = session->chain_order;
828 /* Set cipher parameters */
829 job->cipher_direction = session->cipher.direction;
830 job->cipher_mode = session->cipher.mode;
832 job->aes_key_len_in_bytes = session->cipher.key_length_in_bytes;
834 /* Set authentication parameters */
835 job->hash_alg = session->auth.algo;
838 switch (job->hash_alg) {
840 job->u.XCBC._k1_expanded = session->auth.xcbc.k1_expanded;
841 job->u.XCBC._k2 = session->auth.xcbc.k2;
842 job->u.XCBC._k3 = session->auth.xcbc.k3;
844 job->aes_enc_key_expanded =
845 session->cipher.expanded_aes_keys.encode;
846 job->aes_dec_key_expanded =
847 session->cipher.expanded_aes_keys.decode;
851 job->u.CCM.aad = (uint8_t *)aad + 18;
852 job->u.CCM.aad_len_in_bytes = session->aead.aad_len;
853 job->aes_enc_key_expanded =
854 session->cipher.expanded_aes_keys.encode;
855 job->aes_dec_key_expanded =
856 session->cipher.expanded_aes_keys.decode;
861 job->u.CMAC._key_expanded = session->auth.cmac.expkey;
862 job->u.CMAC._skey1 = session->auth.cmac.skey1;
863 job->u.CMAC._skey2 = session->auth.cmac.skey2;
864 job->aes_enc_key_expanded =
865 session->cipher.expanded_aes_keys.encode;
866 job->aes_dec_key_expanded =
867 session->cipher.expanded_aes_keys.decode;
871 if (session->cipher.mode == GCM) {
872 job->u.GCM.aad = aad;
873 job->u.GCM.aad_len_in_bytes = session->aead.aad_len;
876 job->u.GCM.aad = buf;
877 job->u.GCM.aad_len_in_bytes = len;
878 job->cipher_mode = GCM;
880 job->aes_enc_key_expanded = &session->cipher.gcm_key;
881 job->aes_dec_key_expanded = &session->cipher.gcm_key;
885 job->u.HMAC._hashed_auth_key_xor_ipad =
886 session->auth.pads.inner;
887 job->u.HMAC._hashed_auth_key_xor_opad =
888 session->auth.pads.outer;
890 if (job->cipher_mode == DES3) {
891 job->aes_enc_key_expanded =
892 session->cipher.exp_3des_keys.ks_ptr;
893 job->aes_dec_key_expanded =
894 session->cipher.exp_3des_keys.ks_ptr;
896 job->aes_enc_key_expanded =
897 session->cipher.expanded_aes_keys.encode;
898 job->aes_dec_key_expanded =
899 session->cipher.expanded_aes_keys.decode;
904 * Multi-buffer library current only support returning a truncated
905 * digest length as specified in the relevant IPsec RFCs
908 /* Set digest location and length */
909 job->auth_tag_output = digest;
910 job->auth_tag_output_len_in_bytes = session->auth.gen_digest_len;
912 /* Set IV parameters */
913 job->iv_len_in_bytes = session->iv.length;
915 /* Data Parameters */
917 job->dst = (uint8_t *)buf + sofs.ofs.cipher.head;
918 job->cipher_start_src_offset_in_bytes = sofs.ofs.cipher.head;
919 job->hash_start_src_offset_in_bytes = sofs.ofs.auth.head;
920 if (job->hash_alg == AES_GMAC && session->cipher.mode != GCM) {
921 job->msg_len_to_hash_in_bytes = 0;
922 job->msg_len_to_cipher_in_bytes = 0;
924 job->msg_len_to_hash_in_bytes = len - sofs.ofs.auth.head -
926 job->msg_len_to_cipher_in_bytes = len - sofs.ofs.cipher.head -
927 sofs.ofs.cipher.tail;
930 job->user_data = udata;
934 * Process a crypto operation and complete a JOB_AES_HMAC job structure for
935 * submission to the multi buffer library for processing.
937 * @param qp queue pair
938 * @param job JOB_AES_HMAC structure to fill
939 * @param m mbuf to process
942 * - Completed JOB_AES_HMAC structure pointer on success
943 * - NULL pointer if completion of JOB_AES_HMAC structure isn't possible
946 set_mb_job_params(JOB_AES_HMAC *job, struct aesni_mb_qp *qp,
947 struct rte_crypto_op *op, uint8_t *digest_idx)
949 struct rte_mbuf *m_src = op->sym->m_src, *m_dst;
950 struct aesni_mb_session *session;
951 uint32_t m_offset, oop;
953 session = get_session(qp, op);
954 if (session == NULL) {
955 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
959 /* Set crypto operation */
960 job->chain_order = session->chain_order;
962 /* Set cipher parameters */
963 job->cipher_direction = session->cipher.direction;
964 job->cipher_mode = session->cipher.mode;
966 job->aes_key_len_in_bytes = session->cipher.key_length_in_bytes;
968 /* Set authentication parameters */
969 job->hash_alg = session->auth.algo;
971 switch (job->hash_alg) {
973 job->u.XCBC._k1_expanded = session->auth.xcbc.k1_expanded;
974 job->u.XCBC._k2 = session->auth.xcbc.k2;
975 job->u.XCBC._k3 = session->auth.xcbc.k3;
977 job->aes_enc_key_expanded =
978 session->cipher.expanded_aes_keys.encode;
979 job->aes_dec_key_expanded =
980 session->cipher.expanded_aes_keys.decode;
984 job->u.CCM.aad = op->sym->aead.aad.data + 18;
985 job->u.CCM.aad_len_in_bytes = session->aead.aad_len;
986 job->aes_enc_key_expanded =
987 session->cipher.expanded_aes_keys.encode;
988 job->aes_dec_key_expanded =
989 session->cipher.expanded_aes_keys.decode;
993 job->u.CMAC._key_expanded = session->auth.cmac.expkey;
994 job->u.CMAC._skey1 = session->auth.cmac.skey1;
995 job->u.CMAC._skey2 = session->auth.cmac.skey2;
996 job->aes_enc_key_expanded =
997 session->cipher.expanded_aes_keys.encode;
998 job->aes_dec_key_expanded =
999 session->cipher.expanded_aes_keys.decode;
1003 if (session->cipher.mode == GCM) {
1004 job->u.GCM.aad = op->sym->aead.aad.data;
1005 job->u.GCM.aad_len_in_bytes = session->aead.aad_len;
1008 job->u.GCM.aad = rte_pktmbuf_mtod_offset(m_src,
1009 uint8_t *, op->sym->auth.data.offset);
1010 job->u.GCM.aad_len_in_bytes = op->sym->auth.data.length;
1011 job->cipher_mode = GCM;
1013 job->aes_enc_key_expanded = &session->cipher.gcm_key;
1014 job->aes_dec_key_expanded = &session->cipher.gcm_key;
1018 job->u.HMAC._hashed_auth_key_xor_ipad = session->auth.pads.inner;
1019 job->u.HMAC._hashed_auth_key_xor_opad = session->auth.pads.outer;
1021 if (job->cipher_mode == DES3) {
1022 job->aes_enc_key_expanded =
1023 session->cipher.exp_3des_keys.ks_ptr;
1024 job->aes_dec_key_expanded =
1025 session->cipher.exp_3des_keys.ks_ptr;
1027 job->aes_enc_key_expanded =
1028 session->cipher.expanded_aes_keys.encode;
1029 job->aes_dec_key_expanded =
1030 session->cipher.expanded_aes_keys.decode;
1034 if (!op->sym->m_dst) {
1035 /* in-place operation */
1038 } else if (op->sym->m_dst == op->sym->m_src) {
1039 /* in-place operation */
1043 /* out-of-place operation */
1044 m_dst = op->sym->m_dst;
1048 if (job->hash_alg == AES_CCM || (job->hash_alg == AES_GMAC &&
1049 session->cipher.mode == GCM))
1050 m_offset = op->sym->aead.data.offset;
1052 m_offset = op->sym->cipher.data.offset;
1054 /* Set digest output location */
1055 if (job->hash_alg != NULL_HASH &&
1056 session->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
1057 job->auth_tag_output = qp->temp_digests[*digest_idx];
1058 *digest_idx = (*digest_idx + 1) % MAX_JOBS;
1060 if (job->hash_alg == AES_CCM || (job->hash_alg == AES_GMAC &&
1061 session->cipher.mode == GCM))
1062 job->auth_tag_output = op->sym->aead.digest.data;
1064 job->auth_tag_output = op->sym->auth.digest.data;
1066 if (session->auth.req_digest_len != session->auth.gen_digest_len) {
1067 job->auth_tag_output = qp->temp_digests[*digest_idx];
1068 *digest_idx = (*digest_idx + 1) % MAX_JOBS;
1072 * Multi-buffer library current only support returning a truncated
1073 * digest length as specified in the relevant IPsec RFCs
1076 /* Set digest length */
1077 job->auth_tag_output_len_in_bytes = session->auth.gen_digest_len;
1079 /* Set IV parameters */
1080 job->iv_len_in_bytes = session->iv.length;
1082 /* Data Parameters */
1083 job->src = rte_pktmbuf_mtod(m_src, uint8_t *);
1084 job->dst = rte_pktmbuf_mtod_offset(m_dst, uint8_t *, m_offset);
1086 switch (job->hash_alg) {
1088 job->cipher_start_src_offset_in_bytes =
1089 op->sym->aead.data.offset;
1090 job->msg_len_to_cipher_in_bytes = op->sym->aead.data.length;
1091 job->hash_start_src_offset_in_bytes = op->sym->aead.data.offset;
1092 job->msg_len_to_hash_in_bytes = op->sym->aead.data.length;
1094 job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1095 session->iv.offset + 1);
1099 if (session->cipher.mode == GCM) {
1100 job->cipher_start_src_offset_in_bytes =
1101 op->sym->aead.data.offset;
1102 job->hash_start_src_offset_in_bytes =
1103 op->sym->aead.data.offset;
1104 job->msg_len_to_cipher_in_bytes =
1105 op->sym->aead.data.length;
1106 job->msg_len_to_hash_in_bytes =
1107 op->sym->aead.data.length;
1109 job->cipher_start_src_offset_in_bytes =
1110 op->sym->auth.data.offset;
1111 job->hash_start_src_offset_in_bytes =
1112 op->sym->auth.data.offset;
1113 job->msg_len_to_cipher_in_bytes = 0;
1114 job->msg_len_to_hash_in_bytes = 0;
1117 job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1118 session->iv.offset);
1122 job->cipher_start_src_offset_in_bytes =
1123 op->sym->cipher.data.offset;
1124 job->msg_len_to_cipher_in_bytes = op->sym->cipher.data.length;
1126 job->hash_start_src_offset_in_bytes = auth_start_offset(op,
1128 job->msg_len_to_hash_in_bytes = op->sym->auth.data.length;
1130 job->iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1131 session->iv.offset);
1134 /* Set user data to be crypto operation data struct */
1135 job->user_data = op;
1141 verify_digest(JOB_AES_HMAC *job, void *digest, uint16_t len, uint8_t *status)
1143 /* Verify digest if required */
1144 if (memcmp(job->auth_tag_output, digest, len) != 0)
1145 *status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1149 generate_digest(JOB_AES_HMAC *job, struct rte_crypto_op *op,
1150 struct aesni_mb_session *sess)
1152 /* No extra copy needed */
1153 if (likely(sess->auth.req_digest_len == sess->auth.gen_digest_len))
1157 * This can only happen for HMAC, so only digest
1158 * for authentication algos is required
1160 memcpy(op->sym->auth.digest.data, job->auth_tag_output,
1161 sess->auth.req_digest_len);
1165 * Process a completed job and return rte_mbuf which job processed
1167 * @param qp Queue Pair to process
1168 * @param job JOB_AES_HMAC job to process
1171 * - Returns processed crypto operation.
1172 * - Returns NULL on invalid job
1174 static inline struct rte_crypto_op *
1175 post_process_mb_job(struct aesni_mb_qp *qp, JOB_AES_HMAC *job)
1177 struct rte_crypto_op *op = (struct rte_crypto_op *)job->user_data;
1178 struct aesni_mb_session *sess = get_sym_session_private_data(
1180 cryptodev_driver_id);
1182 if (likely(op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)) {
1183 switch (job->status) {
1185 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1187 if (job->hash_alg == NULL_HASH)
1190 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
1191 if (job->hash_alg == AES_CCM ||
1192 (job->hash_alg == AES_GMAC &&
1193 sess->cipher.mode == GCM))
1195 op->sym->aead.digest.data,
1196 sess->auth.req_digest_len,
1200 op->sym->auth.digest.data,
1201 sess->auth.req_digest_len,
1204 generate_digest(job, op, sess);
1207 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1211 /* Free session if a session-less crypto op */
1212 if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
1213 memset(sess, 0, sizeof(struct aesni_mb_session));
1214 memset(op->sym->session, 0,
1215 rte_cryptodev_sym_get_existing_header_session_size(
1217 rte_mempool_put(qp->sess_mp_priv, sess);
1218 rte_mempool_put(qp->sess_mp, op->sym->session);
1219 op->sym->session = NULL;
1226 post_process_mb_sync_job(JOB_AES_HMAC *job)
1230 st = job->user_data;
1231 st[0] = (job->status == STS_COMPLETED) ? 0 : EBADMSG;
1235 * Process a completed JOB_AES_HMAC job and keep processing jobs until
1236 * get_completed_job return NULL
1238 * @param qp Queue Pair to process
1239 * @param job JOB_AES_HMAC job
1242 * - Number of processed jobs
1245 handle_completed_jobs(struct aesni_mb_qp *qp, JOB_AES_HMAC *job,
1246 struct rte_crypto_op **ops, uint16_t nb_ops)
1248 struct rte_crypto_op *op = NULL;
1249 unsigned processed_jobs = 0;
1251 while (job != NULL) {
1252 op = post_process_mb_job(qp, job);
1255 ops[processed_jobs++] = op;
1256 qp->stats.dequeued_count++;
1258 qp->stats.dequeue_err_count++;
1261 if (processed_jobs == nb_ops)
1264 job = IMB_GET_COMPLETED_JOB(qp->mb_mgr);
1267 return processed_jobs;
1270 static inline uint32_t
1271 handle_completed_sync_jobs(JOB_AES_HMAC *job, MB_MGR *mb_mgr)
1275 for (i = 0; job != NULL; i++, job = IMB_GET_COMPLETED_JOB(mb_mgr))
1276 post_process_mb_sync_job(job);
1281 static inline uint32_t
1282 flush_mb_sync_mgr(MB_MGR *mb_mgr)
1286 job = IMB_FLUSH_JOB(mb_mgr);
1287 return handle_completed_sync_jobs(job, mb_mgr);
1290 static inline uint16_t
1291 flush_mb_mgr(struct aesni_mb_qp *qp, struct rte_crypto_op **ops,
1294 int processed_ops = 0;
1296 /* Flush the remaining jobs */
1297 JOB_AES_HMAC *job = IMB_FLUSH_JOB(qp->mb_mgr);
1300 processed_ops += handle_completed_jobs(qp, job,
1301 &ops[processed_ops], nb_ops - processed_ops);
1303 return processed_ops;
1306 static inline JOB_AES_HMAC *
1307 set_job_null_op(JOB_AES_HMAC *job, struct rte_crypto_op *op)
1309 job->chain_order = HASH_CIPHER;
1310 job->cipher_mode = NULL_CIPHER;
1311 job->hash_alg = NULL_HASH;
1312 job->cipher_direction = DECRYPT;
1314 /* Set user data to be crypto operation data struct */
1315 job->user_data = op;
1321 aesni_mb_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
1324 struct aesni_mb_qp *qp = queue_pair;
1326 struct rte_crypto_op *op;
1329 int retval, processed_jobs = 0;
1331 if (unlikely(nb_ops == 0))
1334 uint8_t digest_idx = qp->digest_idx;
1336 /* Get next free mb job struct from mb manager */
1337 job = IMB_GET_NEXT_JOB(qp->mb_mgr);
1338 if (unlikely(job == NULL)) {
1339 /* if no free mb job structs we need to flush mb_mgr */
1340 processed_jobs += flush_mb_mgr(qp,
1341 &ops[processed_jobs],
1342 nb_ops - processed_jobs);
1344 if (nb_ops == processed_jobs)
1347 job = IMB_GET_NEXT_JOB(qp->mb_mgr);
1351 * Get next operation to process from ingress queue.
1352 * There is no need to return the job to the MB_MGR
1353 * if there are no more operations to process, since the MB_MGR
1354 * can use that pointer again in next get_next calls.
1356 retval = rte_ring_dequeue(qp->ingress_queue, (void **)&op);
1360 retval = set_mb_job_params(job, qp, op, &digest_idx);
1361 if (unlikely(retval != 0)) {
1362 qp->stats.dequeue_err_count++;
1363 set_job_null_op(job, op);
1366 /* Submit job to multi-buffer for processing */
1367 #ifdef RTE_LIBRTE_PMD_AESNI_MB_DEBUG
1368 job = IMB_SUBMIT_JOB(qp->mb_mgr);
1370 job = IMB_SUBMIT_JOB_NOCHECK(qp->mb_mgr);
1373 * If submit returns a processed job then handle it,
1374 * before submitting subsequent jobs
1377 processed_jobs += handle_completed_jobs(qp, job,
1378 &ops[processed_jobs],
1379 nb_ops - processed_jobs);
1381 } while (processed_jobs < nb_ops);
1383 qp->digest_idx = digest_idx;
1385 if (processed_jobs < 1)
1386 processed_jobs += flush_mb_mgr(qp,
1387 &ops[processed_jobs],
1388 nb_ops - processed_jobs);
1390 return processed_jobs;
1394 alloc_init_mb_mgr(enum aesni_mb_vector_mode vector_mode)
1396 MB_MGR *mb_mgr = alloc_mb_mgr(0);
1400 switch (vector_mode) {
1401 case RTE_AESNI_MB_SSE:
1402 init_mb_mgr_sse(mb_mgr);
1404 case RTE_AESNI_MB_AVX:
1405 init_mb_mgr_avx(mb_mgr);
1407 case RTE_AESNI_MB_AVX2:
1408 init_mb_mgr_avx2(mb_mgr);
1410 case RTE_AESNI_MB_AVX512:
1411 init_mb_mgr_avx512(mb_mgr);
1414 AESNI_MB_LOG(ERR, "Unsupported vector mode %u\n", vector_mode);
1415 free_mb_mgr(mb_mgr);
1423 aesni_mb_fill_error_code(struct rte_crypto_sym_vec *vec, int32_t err)
1427 for (i = 0; i != vec->num; ++i)
1428 vec->status[i] = err;
1432 check_crypto_sgl(union rte_crypto_sym_ofs so, const struct rte_crypto_sgl *sgl)
1434 /* no multi-seg support with current AESNI-MB PMD */
1437 else if (so.ofs.cipher.head + so.ofs.cipher.tail > sgl->vec[0].len)
1442 static inline JOB_AES_HMAC *
1443 submit_sync_job(MB_MGR *mb_mgr)
1445 #ifdef RTE_LIBRTE_PMD_AESNI_MB_DEBUG
1446 return IMB_SUBMIT_JOB(mb_mgr);
1448 return IMB_SUBMIT_JOB_NOCHECK(mb_mgr);
1452 static inline uint32_t
1453 generate_sync_dgst(struct rte_crypto_sym_vec *vec,
1454 const uint8_t dgst[][DIGEST_LENGTH_MAX], uint32_t len)
1458 for (i = 0, k = 0; i != vec->num; i++) {
1459 if (vec->status[i] == 0) {
1460 memcpy(vec->digest[i], dgst[i], len);
1468 static inline uint32_t
1469 verify_sync_dgst(struct rte_crypto_sym_vec *vec,
1470 const uint8_t dgst[][DIGEST_LENGTH_MAX], uint32_t len)
1474 for (i = 0, k = 0; i != vec->num; i++) {
1475 if (vec->status[i] == 0) {
1476 if (memcmp(vec->digest[i], dgst[i], len) != 0)
1477 vec->status[i] = EBADMSG;
1487 aesni_mb_cpu_crypto_process_bulk(struct rte_cryptodev *dev,
1488 struct rte_cryptodev_sym_session *sess, union rte_crypto_sym_ofs sofs,
1489 struct rte_crypto_sym_vec *vec)
1492 uint32_t i, j, k, len;
1496 struct aesni_mb_private *priv;
1497 struct aesni_mb_session *s;
1498 uint8_t tmp_dgst[vec->num][DIGEST_LENGTH_MAX];
1500 s = get_sym_session_private_data(sess, dev->driver_id);
1502 aesni_mb_fill_error_code(vec, EINVAL);
1506 /* get per-thread MB MGR, create one if needed */
1507 mb_mgr = RTE_PER_LCORE(sync_mb_mgr);
1508 if (mb_mgr == NULL) {
1510 priv = dev->data->dev_private;
1511 mb_mgr = alloc_init_mb_mgr(priv->vector_mode);
1512 if (mb_mgr == NULL) {
1513 aesni_mb_fill_error_code(vec, ENOMEM);
1516 RTE_PER_LCORE(sync_mb_mgr) = mb_mgr;
1519 for (i = 0, j = 0, k = 0; i != vec->num; i++) {
1522 ret = check_crypto_sgl(sofs, vec->sgl + i);
1524 vec->status[i] = ret;
1528 buf = vec->sgl[i].vec[0].base;
1529 len = vec->sgl[i].vec[0].len;
1531 job = IMB_GET_NEXT_JOB(mb_mgr);
1533 k += flush_mb_sync_mgr(mb_mgr);
1534 job = IMB_GET_NEXT_JOB(mb_mgr);
1535 RTE_ASSERT(job != NULL);
1538 /* Submit job for processing */
1539 set_cpu_mb_job_params(job, s, sofs, buf, len,
1540 vec->iv[i], vec->aad[i], tmp_dgst[i],
1542 job = submit_sync_job(mb_mgr);
1545 /* handle completed jobs */
1546 k += handle_completed_sync_jobs(job, mb_mgr);
1549 /* flush remaining jobs */
1551 k += flush_mb_sync_mgr(mb_mgr);
1553 /* finish processing for successful jobs: check/update digest */
1555 if (s->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY)
1556 k = verify_sync_dgst(vec,
1557 (const uint8_t (*)[DIGEST_LENGTH_MAX])tmp_dgst,
1558 s->auth.req_digest_len);
1560 k = generate_sync_dgst(vec,
1561 (const uint8_t (*)[DIGEST_LENGTH_MAX])tmp_dgst,
1562 s->auth.req_digest_len);
1568 static int cryptodev_aesni_mb_remove(struct rte_vdev_device *vdev);
1571 vec_mode_to_flags(enum aesni_mb_vector_mode mode)
1574 case RTE_AESNI_MB_SSE:
1575 return RTE_CRYPTODEV_FF_CPU_SSE;
1576 case RTE_AESNI_MB_AVX:
1577 return RTE_CRYPTODEV_FF_CPU_AVX;
1578 case RTE_AESNI_MB_AVX2:
1579 return RTE_CRYPTODEV_FF_CPU_AVX2;
1580 case RTE_AESNI_MB_AVX512:
1581 return RTE_CRYPTODEV_FF_CPU_AVX512;
1583 AESNI_MB_LOG(ERR, "Unsupported vector mode %u\n", mode);
1589 cryptodev_aesni_mb_create(const char *name,
1590 struct rte_vdev_device *vdev,
1591 struct rte_cryptodev_pmd_init_params *init_params)
1593 struct rte_cryptodev *dev;
1594 struct aesni_mb_private *internals;
1595 enum aesni_mb_vector_mode vector_mode;
1598 dev = rte_cryptodev_pmd_create(name, &vdev->device, init_params);
1600 AESNI_MB_LOG(ERR, "failed to create cryptodev vdev");
1604 /* Check CPU for supported vector instruction set */
1605 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512F))
1606 vector_mode = RTE_AESNI_MB_AVX512;
1607 else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
1608 vector_mode = RTE_AESNI_MB_AVX2;
1609 else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX))
1610 vector_mode = RTE_AESNI_MB_AVX;
1612 vector_mode = RTE_AESNI_MB_SSE;
1614 dev->driver_id = cryptodev_driver_id;
1615 dev->dev_ops = rte_aesni_mb_pmd_ops;
1617 /* register rx/tx burst functions for data path */
1618 dev->dequeue_burst = aesni_mb_pmd_dequeue_burst;
1619 dev->enqueue_burst = aesni_mb_pmd_enqueue_burst;
1621 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
1622 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
1623 RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT |
1624 RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO;
1626 /* Check CPU for support for AES instruction set */
1627 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AES))
1628 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AESNI;
1630 AESNI_MB_LOG(WARNING, "AES instructions not supported by CPU");
1632 dev->feature_flags |= vec_mode_to_flags(vector_mode);
1634 mb_mgr = alloc_init_mb_mgr(vector_mode);
1635 if (mb_mgr == NULL) {
1636 rte_cryptodev_pmd_destroy(dev);
1640 /* Set vector instructions mode supported */
1641 internals = dev->data->dev_private;
1643 internals->vector_mode = vector_mode;
1644 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
1645 internals->mb_mgr = mb_mgr;
1647 AESNI_MB_LOG(INFO, "IPSec Multi-buffer library version used: %s\n",
1648 imb_get_version_str());
1653 cryptodev_aesni_mb_probe(struct rte_vdev_device *vdev)
1655 struct rte_cryptodev_pmd_init_params init_params = {
1657 sizeof(struct aesni_mb_private),
1659 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS
1661 const char *name, *args;
1664 name = rte_vdev_device_name(vdev);
1668 args = rte_vdev_device_args(vdev);
1670 retval = rte_cryptodev_pmd_parse_input_args(&init_params, args);
1672 AESNI_MB_LOG(ERR, "Failed to parse initialisation arguments[%s]",
1677 return cryptodev_aesni_mb_create(name, vdev, &init_params);
1681 cryptodev_aesni_mb_remove(struct rte_vdev_device *vdev)
1683 struct rte_cryptodev *cryptodev;
1684 struct aesni_mb_private *internals;
1687 name = rte_vdev_device_name(vdev);
1691 cryptodev = rte_cryptodev_pmd_get_named_dev(name);
1692 if (cryptodev == NULL)
1695 internals = cryptodev->data->dev_private;
1697 free_mb_mgr(internals->mb_mgr);
1698 if (RTE_PER_LCORE(sync_mb_mgr)) {
1699 free_mb_mgr(RTE_PER_LCORE(sync_mb_mgr));
1700 RTE_PER_LCORE(sync_mb_mgr) = NULL;
1703 return rte_cryptodev_pmd_destroy(cryptodev);
1706 static struct rte_vdev_driver cryptodev_aesni_mb_pmd_drv = {
1707 .probe = cryptodev_aesni_mb_probe,
1708 .remove = cryptodev_aesni_mb_remove
1711 static struct cryptodev_driver aesni_mb_crypto_drv;
1713 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd_drv);
1714 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd);
1715 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_AESNI_MB_PMD,
1716 "max_nb_queue_pairs=<int> "
1718 RTE_PMD_REGISTER_CRYPTO_DRIVER(aesni_mb_crypto_drv,
1719 cryptodev_aesni_mb_pmd_drv.driver,
1720 cryptodev_driver_id);
1722 RTE_INIT(aesni_mb_init_log)
1724 aesni_mb_logtype_driver = rte_log_register("pmd.crypto.aesni_mb");