4 * Copyright(c) 2015-2016 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
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.
33 #include <rte_common.h>
34 #include <rte_hexdump.h>
35 #include <rte_cryptodev.h>
36 #include <rte_cryptodev_pmd.h>
38 #include <rte_malloc.h>
39 #include <rte_cpuflags.h>
41 #include "rte_aesni_mb_pmd_private.h"
44 * Global static parameter used to create a unique name for each AES-NI multi
45 * buffer crypto device.
47 static unsigned unique_name_id;
50 create_unique_device_name(char *name, size_t size)
57 ret = snprintf(name, size, "%s_%u", RTE_STR(CRYPTODEV_NAME_AESNI_MB_PMD),
64 typedef void (*hash_one_block_t)(void *data, void *digest);
65 typedef void (*aes_keyexp_t)(void *key, void *enc_exp_keys, void *dec_exp_keys);
68 * Calculate the authentication pre-computes
70 * @param one_block_hash Function pointer to calculate digest on ipad/opad
71 * @param ipad Inner pad output byte array
72 * @param opad Outer pad output byte array
73 * @param hkey Authentication key
74 * @param hkey_len Authentication key length
75 * @param blocksize Block size of selected hash algo
78 calculate_auth_precomputes(hash_one_block_t one_block_hash,
79 uint8_t *ipad, uint8_t *opad,
80 uint8_t *hkey, uint16_t hkey_len,
85 uint8_t ipad_buf[blocksize] __rte_aligned(16);
86 uint8_t opad_buf[blocksize] __rte_aligned(16);
88 /* Setup inner and outer pads */
89 memset(ipad_buf, HMAC_IPAD_VALUE, blocksize);
90 memset(opad_buf, HMAC_OPAD_VALUE, blocksize);
92 /* XOR hash key with inner and outer pads */
93 length = hkey_len > blocksize ? blocksize : hkey_len;
95 for (i = 0; i < length; i++) {
96 ipad_buf[i] ^= hkey[i];
97 opad_buf[i] ^= hkey[i];
100 /* Compute partial hashes */
101 (*one_block_hash)(ipad_buf, ipad);
102 (*one_block_hash)(opad_buf, opad);
105 memset(ipad_buf, 0, blocksize);
106 memset(opad_buf, 0, blocksize);
109 /** Get xform chain order */
111 aesni_mb_get_chain_order(const struct rte_crypto_sym_xform *xform)
114 * Multi-buffer only supports HASH_CIPHER or CIPHER_HASH chained
115 * operations, all other options are invalid, so we must have exactly
116 * 2 xform structs chained together
118 if (xform->next == NULL || xform->next->next != NULL)
121 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
122 xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
125 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
126 xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
132 /** Set session authentication parameters */
134 aesni_mb_set_session_auth_parameters(const struct aesni_mb_ops *mb_ops,
135 struct aesni_mb_session *sess,
136 const struct rte_crypto_sym_xform *xform)
138 hash_one_block_t hash_oneblock_fn;
140 if (xform->type != RTE_CRYPTO_SYM_XFORM_AUTH) {
141 MB_LOG_ERR("Crypto xform struct not of type auth");
145 /* Set Authentication Parameters */
146 if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_XCBC_MAC) {
147 sess->auth.algo = AES_XCBC;
148 (*mb_ops->aux.keyexp.aes_xcbc)(xform->auth.key.data,
149 sess->auth.xcbc.k1_expanded,
150 sess->auth.xcbc.k2, sess->auth.xcbc.k3);
154 switch (xform->auth.algo) {
155 case RTE_CRYPTO_AUTH_MD5_HMAC:
156 sess->auth.algo = MD5;
157 hash_oneblock_fn = mb_ops->aux.one_block.md5;
159 case RTE_CRYPTO_AUTH_SHA1_HMAC:
160 sess->auth.algo = SHA1;
161 hash_oneblock_fn = mb_ops->aux.one_block.sha1;
163 case RTE_CRYPTO_AUTH_SHA224_HMAC:
164 sess->auth.algo = SHA_224;
165 hash_oneblock_fn = mb_ops->aux.one_block.sha224;
167 case RTE_CRYPTO_AUTH_SHA256_HMAC:
168 sess->auth.algo = SHA_256;
169 hash_oneblock_fn = mb_ops->aux.one_block.sha256;
171 case RTE_CRYPTO_AUTH_SHA384_HMAC:
172 sess->auth.algo = SHA_384;
173 hash_oneblock_fn = mb_ops->aux.one_block.sha384;
175 case RTE_CRYPTO_AUTH_SHA512_HMAC:
176 sess->auth.algo = SHA_512;
177 hash_oneblock_fn = mb_ops->aux.one_block.sha512;
180 MB_LOG_ERR("Unsupported authentication algorithm selection");
184 /* Calculate Authentication precomputes */
185 calculate_auth_precomputes(hash_oneblock_fn,
186 sess->auth.pads.inner, sess->auth.pads.outer,
187 xform->auth.key.data,
188 xform->auth.key.length,
189 get_auth_algo_blocksize(sess->auth.algo));
194 /** Set session cipher parameters */
196 aesni_mb_set_session_cipher_parameters(const struct aesni_mb_ops *mb_ops,
197 struct aesni_mb_session *sess,
198 const struct rte_crypto_sym_xform *xform)
200 aes_keyexp_t aes_keyexp_fn;
202 if (xform->type != RTE_CRYPTO_SYM_XFORM_CIPHER) {
203 MB_LOG_ERR("Crypto xform struct not of type cipher");
207 /* Select cipher direction */
208 switch (xform->cipher.op) {
209 case RTE_CRYPTO_CIPHER_OP_ENCRYPT:
210 sess->cipher.direction = ENCRYPT;
212 case RTE_CRYPTO_CIPHER_OP_DECRYPT:
213 sess->cipher.direction = DECRYPT;
216 MB_LOG_ERR("Unsupported cipher operation parameter");
220 /* Select cipher mode */
221 switch (xform->cipher.algo) {
222 case RTE_CRYPTO_CIPHER_AES_CBC:
223 sess->cipher.mode = CBC;
225 case RTE_CRYPTO_CIPHER_AES_CTR:
226 sess->cipher.mode = CNTR;
229 MB_LOG_ERR("Unsupported cipher mode parameter");
233 /* Check key length and choose key expansion function */
234 switch (xform->cipher.key.length) {
236 sess->cipher.key_length_in_bytes = AES_128_BYTES;
237 aes_keyexp_fn = mb_ops->aux.keyexp.aes128;
240 sess->cipher.key_length_in_bytes = AES_192_BYTES;
241 aes_keyexp_fn = mb_ops->aux.keyexp.aes192;
244 sess->cipher.key_length_in_bytes = AES_256_BYTES;
245 aes_keyexp_fn = mb_ops->aux.keyexp.aes256;
248 MB_LOG_ERR("Unsupported cipher key length");
252 /* Expanded cipher keys */
253 (*aes_keyexp_fn)(xform->cipher.key.data,
254 sess->cipher.expanded_aes_keys.encode,
255 sess->cipher.expanded_aes_keys.decode);
260 /** Parse crypto xform chain and set private session parameters */
262 aesni_mb_set_session_parameters(const struct aesni_mb_ops *mb_ops,
263 struct aesni_mb_session *sess,
264 const struct rte_crypto_sym_xform *xform)
266 const struct rte_crypto_sym_xform *auth_xform = NULL;
267 const struct rte_crypto_sym_xform *cipher_xform = NULL;
269 /* Select Crypto operation - hash then cipher / cipher then hash */
270 switch (aesni_mb_get_chain_order(xform)) {
272 sess->chain_order = HASH_CIPHER;
274 cipher_xform = xform->next;
277 sess->chain_order = CIPHER_HASH;
278 auth_xform = xform->next;
279 cipher_xform = xform;
282 MB_LOG_ERR("Unsupported operation chain order parameter");
286 if (aesni_mb_set_session_auth_parameters(mb_ops, sess, auth_xform)) {
287 MB_LOG_ERR("Invalid/unsupported authentication parameters");
291 if (aesni_mb_set_session_cipher_parameters(mb_ops, sess,
293 MB_LOG_ERR("Invalid/unsupported cipher parameters");
299 /** Get multi buffer session */
300 static struct aesni_mb_session *
301 get_session(struct aesni_mb_qp *qp, struct rte_crypto_op *op)
303 struct aesni_mb_session *sess = NULL;
305 if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_WITH_SESSION) {
306 if (unlikely(op->sym->session->dev_type !=
307 RTE_CRYPTODEV_AESNI_MB_PMD))
310 sess = (struct aesni_mb_session *)op->sym->session->_private;
314 if (rte_mempool_get(qp->sess_mp, (void **)&_sess))
317 sess = (struct aesni_mb_session *)
318 ((struct rte_cryptodev_sym_session *)_sess)->_private;
320 if (unlikely(aesni_mb_set_session_parameters(qp->ops,
321 sess, op->sym->xform) != 0)) {
322 rte_mempool_put(qp->sess_mp, _sess);
325 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
332 * Process a crypto operation and complete a JOB_AES_HMAC job structure for
333 * submission to the multi buffer library for processing.
335 * @param qp queue pair
336 * @param job JOB_AES_HMAC structure to fill
337 * @param m mbuf to process
340 * - Completed JOB_AES_HMAC structure pointer on success
341 * - NULL pointer if completion of JOB_AES_HMAC structure isn't possible
343 static JOB_AES_HMAC *
344 process_crypto_op(struct aesni_mb_qp *qp, struct rte_crypto_op *op,
345 struct aesni_mb_session *session)
349 struct rte_mbuf *m_src = op->sym->m_src, *m_dst;
350 uint16_t m_offset = 0;
352 job = (*qp->ops->job.get_next)(&qp->mb_mgr);
353 if (unlikely(job == NULL))
356 /* Set crypto operation */
357 job->chain_order = session->chain_order;
359 /* Set cipher parameters */
360 job->cipher_direction = session->cipher.direction;
361 job->cipher_mode = session->cipher.mode;
363 job->aes_key_len_in_bytes = session->cipher.key_length_in_bytes;
364 job->aes_enc_key_expanded = session->cipher.expanded_aes_keys.encode;
365 job->aes_dec_key_expanded = session->cipher.expanded_aes_keys.decode;
368 /* Set authentication parameters */
369 job->hash_alg = session->auth.algo;
370 if (job->hash_alg == AES_XCBC) {
371 job->_k1_expanded = session->auth.xcbc.k1_expanded;
372 job->_k2 = session->auth.xcbc.k2;
373 job->_k3 = session->auth.xcbc.k3;
375 job->hashed_auth_key_xor_ipad = session->auth.pads.inner;
376 job->hashed_auth_key_xor_opad = session->auth.pads.outer;
379 /* Mutable crypto operation parameters */
380 if (op->sym->m_dst) {
381 m_src = m_dst = op->sym->m_dst;
383 /* append space for output data to mbuf */
384 char *odata = rte_pktmbuf_append(m_dst,
385 rte_pktmbuf_data_len(op->sym->m_src));
387 MB_LOG_ERR("failed to allocate space in destination "
388 "mbuf for source data");
392 memcpy(odata, rte_pktmbuf_mtod(op->sym->m_src, void*),
393 rte_pktmbuf_data_len(op->sym->m_src));
396 m_offset = op->sym->cipher.data.offset;
399 /* Set digest output location */
400 if (job->cipher_direction == DECRYPT) {
401 job->auth_tag_output = (uint8_t *)rte_pktmbuf_append(m_dst,
402 get_digest_byte_length(job->hash_alg));
404 if (job->auth_tag_output == NULL) {
405 MB_LOG_ERR("failed to allocate space in output mbuf "
410 memset(job->auth_tag_output, 0,
411 sizeof(get_digest_byte_length(job->hash_alg)));
414 job->auth_tag_output = op->sym->auth.digest.data;
418 * Multi-buffer library current only support returning a truncated
419 * digest length as specified in the relevant IPsec RFCs
421 job->auth_tag_output_len_in_bytes =
422 get_truncated_digest_byte_length(job->hash_alg);
424 /* Set IV parameters */
425 job->iv = op->sym->cipher.iv.data;
426 job->iv_len_in_bytes = op->sym->cipher.iv.length;
429 job->src = rte_pktmbuf_mtod(m_src, uint8_t *);
430 job->dst = rte_pktmbuf_mtod_offset(m_dst, uint8_t *, m_offset);
432 job->cipher_start_src_offset_in_bytes = op->sym->cipher.data.offset;
433 job->msg_len_to_cipher_in_bytes = op->sym->cipher.data.length;
435 job->hash_start_src_offset_in_bytes = op->sym->auth.data.offset;
436 job->msg_len_to_hash_in_bytes = op->sym->auth.data.length;
438 /* Set user data to be crypto operation data struct */
440 job->user_data2 = m_dst;
446 * Process a completed job and return rte_mbuf which job processed
448 * @param job JOB_AES_HMAC job to process
451 * - Returns processed mbuf which is trimmed of output digest used in
452 * verification of supplied digest in the case of a HASH_CIPHER operation
453 * - Returns NULL on invalid job
455 static struct rte_crypto_op *
456 post_process_mb_job(struct aesni_mb_qp *qp, JOB_AES_HMAC *job)
458 struct rte_crypto_op *op =
459 (struct rte_crypto_op *)job->user_data;
460 struct rte_mbuf *m_dst =
461 (struct rte_mbuf *)job->user_data2;
463 if (op == NULL || m_dst == NULL)
466 /* set status as successful by default */
467 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
469 /* check if job has been processed */
470 if (unlikely(job->status != STS_COMPLETED)) {
471 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
473 } else if (job->chain_order == HASH_CIPHER) {
474 /* Verify digest if required */
475 if (memcmp(job->auth_tag_output, op->sym->auth.digest.data,
476 job->auth_tag_output_len_in_bytes) != 0)
477 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
479 /* trim area used for digest from mbuf */
480 rte_pktmbuf_trim(m_dst, get_digest_byte_length(job->hash_alg));
483 /* Free session if a session-less crypto op */
484 if (op->sym->sess_type == RTE_CRYPTO_SYM_OP_SESSIONLESS) {
485 rte_mempool_put(qp->sess_mp, op->sym->session);
486 op->sym->session = NULL;
493 * Process a completed JOB_AES_HMAC job and keep processing jobs until
494 * get_completed_job return NULL
496 * @param qp Queue Pair to process
497 * @param job JOB_AES_HMAC job
500 * - Number of processed jobs
503 handle_completed_jobs(struct aesni_mb_qp *qp, JOB_AES_HMAC *job)
505 struct rte_crypto_op *op = NULL;
506 unsigned processed_jobs = 0;
510 op = post_process_mb_job(qp, job);
512 rte_ring_enqueue(qp->processed_ops, (void *)op);
514 qp->stats.dequeue_err_count++;
515 job = (*qp->ops->job.get_completed_job)(&qp->mb_mgr);
518 return processed_jobs;
522 aesni_mb_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
525 struct aesni_mb_session *sess;
526 struct aesni_mb_qp *qp = queue_pair;
528 JOB_AES_HMAC *job = NULL;
530 int i, processed_jobs = 0;
532 for (i = 0; i < nb_ops; i++) {
533 #ifdef RTE_LIBRTE_AESNI_MB_DEBUG
534 if (unlikely(op->type != RTE_CRYPTO_OP_TYPE_SYMMETRIC)) {
535 MB_LOG_ERR("PMD only supports symmetric crypto "
536 "operation requests, op (%p) is not a "
537 "symmetric operation.", op);
538 qp->stats.enqueue_err_count++;
542 sess = get_session(qp, ops[i]);
543 if (unlikely(sess == NULL)) {
544 qp->stats.enqueue_err_count++;
548 job = process_crypto_op(qp, ops[i], sess);
549 if (unlikely(job == NULL)) {
550 qp->stats.enqueue_err_count++;
555 job = (*qp->ops->job.submit)(&qp->mb_mgr);
558 * If submit returns a processed job then handle it,
559 * before submitting subsequent jobs
562 processed_jobs += handle_completed_jobs(qp, job);
565 if (processed_jobs == 0)
568 qp->stats.enqueued_count += processed_jobs;
573 * If we haven't processed any jobs in submit loop, then flush jobs
574 * queue to stop the output stalling
576 job = (*qp->ops->job.flush_job)(&qp->mb_mgr);
578 qp->stats.enqueued_count += handle_completed_jobs(qp, job);
584 aesni_mb_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
587 struct aesni_mb_qp *qp = queue_pair;
589 unsigned nb_dequeued;
591 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
592 (void **)ops, nb_ops);
593 qp->stats.dequeued_count += nb_dequeued;
599 static int cryptodev_aesni_mb_remove(const char *name);
602 cryptodev_aesni_mb_create(const char *name,
603 struct rte_crypto_vdev_init_params *init_params)
605 struct rte_cryptodev *dev;
606 char crypto_dev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
607 struct aesni_mb_private *internals;
608 enum aesni_mb_vector_mode vector_mode;
610 /* Check CPU for support for AES instruction set */
611 if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_AES)) {
612 MB_LOG_ERR("AES instructions not supported by CPU");
616 /* Check CPU for supported vector instruction set */
617 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
618 vector_mode = RTE_AESNI_MB_AVX2;
619 else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX))
620 vector_mode = RTE_AESNI_MB_AVX;
621 else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
622 vector_mode = RTE_AESNI_MB_SSE;
624 MB_LOG_ERR("Vector instructions are not supported by CPU");
628 /* create a unique device name */
629 if (create_unique_device_name(crypto_dev_name,
630 RTE_CRYPTODEV_NAME_MAX_LEN) != 0) {
631 MB_LOG_ERR("failed to create unique cryptodev name");
636 dev = rte_cryptodev_pmd_virtual_dev_init(crypto_dev_name,
637 sizeof(struct aesni_mb_private), init_params->socket_id);
639 MB_LOG_ERR("failed to create cryptodev vdev");
643 dev->dev_type = RTE_CRYPTODEV_AESNI_MB_PMD;
644 dev->dev_ops = rte_aesni_mb_pmd_ops;
646 /* register rx/tx burst functions for data path */
647 dev->dequeue_burst = aesni_mb_pmd_dequeue_burst;
648 dev->enqueue_burst = aesni_mb_pmd_enqueue_burst;
650 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
651 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
652 RTE_CRYPTODEV_FF_CPU_AESNI;
654 switch (vector_mode) {
655 case RTE_AESNI_MB_SSE:
656 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_SSE;
658 case RTE_AESNI_MB_AVX:
659 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX;
661 case RTE_AESNI_MB_AVX2:
662 dev->feature_flags |= RTE_CRYPTODEV_FF_CPU_AVX2;
668 /* Set vector instructions mode supported */
669 internals = dev->data->dev_private;
671 internals->vector_mode = vector_mode;
672 internals->max_nb_queue_pairs = init_params->max_nb_queue_pairs;
673 internals->max_nb_sessions = init_params->max_nb_sessions;
677 MB_LOG_ERR("driver %s: cryptodev_aesni_create failed", name);
679 cryptodev_aesni_mb_remove(crypto_dev_name);
685 cryptodev_aesni_mb_probe(const char *name,
686 const char *input_args)
688 struct rte_crypto_vdev_init_params init_params = {
689 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_QUEUE_PAIRS,
690 RTE_CRYPTODEV_VDEV_DEFAULT_MAX_NB_SESSIONS,
694 rte_cryptodev_parse_vdev_init_params(&init_params, input_args);
696 RTE_LOG(INFO, PMD, "Initialising %s on NUMA node %d\n", name,
697 init_params.socket_id);
698 RTE_LOG(INFO, PMD, " Max number of queue pairs = %d\n",
699 init_params.max_nb_queue_pairs);
700 RTE_LOG(INFO, PMD, " Max number of sessions = %d\n",
701 init_params.max_nb_sessions);
703 return cryptodev_aesni_mb_create(name, &init_params);
707 cryptodev_aesni_mb_remove(const char *name)
712 RTE_LOG(INFO, PMD, "Closing AESNI crypto device %s on numa socket %u\n",
713 name, rte_socket_id());
718 static struct rte_vdev_driver cryptodev_aesni_mb_pmd_drv = {
719 .probe = cryptodev_aesni_mb_probe,
720 .remove = cryptodev_aesni_mb_remove
723 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd_drv);
724 RTE_PMD_REGISTER_ALIAS(CRYPTODEV_NAME_AESNI_MB_PMD, cryptodev_aesni_mb_pmd);
725 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_AESNI_MB_PMD,
726 "max_nb_queue_pairs=<int> "
727 "max_nb_sessions=<int> "