4 * Copyright(c) 2015 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>
40 #include <rte_mbuf_offload.h>
42 #include "rte_aesni_mb_pmd_private.h"
45 * Global static parameter used to create a unique name for each AES-NI multi
46 * buffer crypto device.
48 static unsigned unique_name_id;
51 create_unique_device_name(char *name, size_t size)
58 ret = snprintf(name, size, "%s_%u", CRYPTODEV_NAME_AESNI_MB_PMD,
65 typedef void (*hash_one_block_t)(void *data, void *digest);
66 typedef void (*aes_keyexp_t)(void *key, void *enc_exp_keys, void *dec_exp_keys);
69 * Calculate the authentication pre-computes
71 * @param one_block_hash Function pointer to calculate digest on ipad/opad
72 * @param ipad Inner pad output byte array
73 * @param opad Outer pad output byte array
74 * @param hkey Authentication key
75 * @param hkey_len Authentication key length
76 * @param blocksize Block size of selected hash algo
79 calculate_auth_precomputes(hash_one_block_t one_block_hash,
80 uint8_t *ipad, uint8_t *opad,
81 uint8_t *hkey, uint16_t hkey_len,
86 uint8_t ipad_buf[blocksize] __rte_aligned(16);
87 uint8_t opad_buf[blocksize] __rte_aligned(16);
89 /* Setup inner and outer pads */
90 memset(ipad_buf, HMAC_IPAD_VALUE, blocksize);
91 memset(opad_buf, HMAC_OPAD_VALUE, blocksize);
93 /* XOR hash key with inner and outer pads */
94 length = hkey_len > blocksize ? blocksize : hkey_len;
96 for (i = 0; i < length; i++) {
97 ipad_buf[i] ^= hkey[i];
98 opad_buf[i] ^= hkey[i];
101 /* Compute partial hashes */
102 (*one_block_hash)(ipad_buf, ipad);
103 (*one_block_hash)(opad_buf, opad);
106 memset(ipad_buf, 0, blocksize);
107 memset(opad_buf, 0, blocksize);
110 /** Get xform chain order */
112 aesni_mb_get_chain_order(const struct rte_crypto_xform *xform)
115 * Multi-buffer only supports HASH_CIPHER or CIPHER_HASH chained
116 * operations, all other options are invalid, so we must have exactly
117 * 2 xform structs chained together
119 if (xform->next == NULL || xform->next->next != NULL)
122 if (xform->type == RTE_CRYPTO_XFORM_AUTH &&
123 xform->next->type == RTE_CRYPTO_XFORM_CIPHER)
126 if (xform->type == RTE_CRYPTO_XFORM_CIPHER &&
127 xform->next->type == RTE_CRYPTO_XFORM_AUTH)
133 /** Set session authentication parameters */
135 aesni_mb_set_session_auth_parameters(const struct aesni_mb_ops *mb_ops,
136 struct aesni_mb_session *sess,
137 const struct rte_crypto_xform *xform)
139 hash_one_block_t hash_oneblock_fn;
141 if (xform->type != RTE_CRYPTO_XFORM_AUTH) {
142 MB_LOG_ERR("Crypto xform struct not of type auth");
146 /* Set Authentication Parameters */
147 if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_XCBC_MAC) {
148 sess->auth.algo = AES_XCBC;
149 (*mb_ops->aux.keyexp.aes_xcbc)(xform->auth.key.data,
150 sess->auth.xcbc.k1_expanded,
151 sess->auth.xcbc.k2, sess->auth.xcbc.k3);
155 switch (xform->auth.algo) {
156 case RTE_CRYPTO_AUTH_MD5_HMAC:
157 sess->auth.algo = MD5;
158 hash_oneblock_fn = mb_ops->aux.one_block.md5;
160 case RTE_CRYPTO_AUTH_SHA1_HMAC:
161 sess->auth.algo = SHA1;
162 hash_oneblock_fn = mb_ops->aux.one_block.sha1;
164 case RTE_CRYPTO_AUTH_SHA224_HMAC:
165 sess->auth.algo = SHA_224;
166 hash_oneblock_fn = mb_ops->aux.one_block.sha224;
168 case RTE_CRYPTO_AUTH_SHA256_HMAC:
169 sess->auth.algo = SHA_256;
170 hash_oneblock_fn = mb_ops->aux.one_block.sha256;
172 case RTE_CRYPTO_AUTH_SHA384_HMAC:
173 sess->auth.algo = SHA_384;
174 hash_oneblock_fn = mb_ops->aux.one_block.sha384;
176 case RTE_CRYPTO_AUTH_SHA512_HMAC:
177 sess->auth.algo = SHA_512;
178 hash_oneblock_fn = mb_ops->aux.one_block.sha512;
181 MB_LOG_ERR("Unsupported authentication algorithm selection");
185 /* Calculate Authentication precomputes */
186 calculate_auth_precomputes(hash_oneblock_fn,
187 sess->auth.pads.inner, sess->auth.pads.outer,
188 xform->auth.key.data,
189 xform->auth.key.length,
190 get_auth_algo_blocksize(sess->auth.algo));
195 /** Set session cipher parameters */
197 aesni_mb_set_session_cipher_parameters(const struct aesni_mb_ops *mb_ops,
198 struct aesni_mb_session *sess,
199 const struct rte_crypto_xform *xform)
201 aes_keyexp_t aes_keyexp_fn;
203 if (xform->type != RTE_CRYPTO_XFORM_CIPHER) {
204 MB_LOG_ERR("Crypto xform struct not of type cipher");
208 /* Select cipher direction */
209 switch (xform->cipher.op) {
210 case RTE_CRYPTO_CIPHER_OP_ENCRYPT:
211 sess->cipher.direction = ENCRYPT;
213 case RTE_CRYPTO_CIPHER_OP_DECRYPT:
214 sess->cipher.direction = DECRYPT;
217 MB_LOG_ERR("Unsupported cipher operation parameter");
221 /* Select cipher mode */
222 switch (xform->cipher.algo) {
223 case RTE_CRYPTO_CIPHER_AES_CBC:
224 sess->cipher.mode = CBC;
227 MB_LOG_ERR("Unsupported cipher mode parameter");
231 /* Check key length and choose key expansion function */
232 switch (xform->cipher.key.length) {
234 sess->cipher.key_length_in_bytes = AES_128_BYTES;
235 aes_keyexp_fn = mb_ops->aux.keyexp.aes128;
238 sess->cipher.key_length_in_bytes = AES_192_BYTES;
239 aes_keyexp_fn = mb_ops->aux.keyexp.aes192;
242 sess->cipher.key_length_in_bytes = AES_256_BYTES;
243 aes_keyexp_fn = mb_ops->aux.keyexp.aes256;
246 MB_LOG_ERR("Unsupported cipher key length");
250 /* Expanded cipher keys */
251 (*aes_keyexp_fn)(xform->cipher.key.data,
252 sess->cipher.expanded_aes_keys.encode,
253 sess->cipher.expanded_aes_keys.decode);
258 /** Parse crypto xform chain and set private session parameters */
260 aesni_mb_set_session_parameters(const struct aesni_mb_ops *mb_ops,
261 struct aesni_mb_session *sess,
262 const struct rte_crypto_xform *xform)
264 const struct rte_crypto_xform *auth_xform = NULL;
265 const struct rte_crypto_xform *cipher_xform = NULL;
267 /* Select Crypto operation - hash then cipher / cipher then hash */
268 switch (aesni_mb_get_chain_order(xform)) {
270 sess->chain_order = HASH_CIPHER;
272 cipher_xform = xform->next;
275 sess->chain_order = CIPHER_HASH;
276 auth_xform = xform->next;
277 cipher_xform = xform;
280 MB_LOG_ERR("Unsupported operation chain order parameter");
284 if (aesni_mb_set_session_auth_parameters(mb_ops, sess, auth_xform)) {
285 MB_LOG_ERR("Invalid/unsupported authentication parameters");
289 if (aesni_mb_set_session_cipher_parameters(mb_ops, sess,
291 MB_LOG_ERR("Invalid/unsupported cipher parameters");
297 /** Get multi buffer session */
298 static struct aesni_mb_session *
299 get_session(struct aesni_mb_qp *qp, struct rte_crypto_op *crypto_op)
301 struct aesni_mb_session *sess;
303 if (crypto_op->type == RTE_CRYPTO_OP_WITH_SESSION) {
304 if (unlikely(crypto_op->session->type !=
305 RTE_CRYPTODEV_AESNI_MB_PMD))
308 sess = (struct aesni_mb_session *)crypto_op->session->_private;
310 struct rte_cryptodev_session *c_sess = NULL;
312 if (rte_mempool_get(qp->sess_mp, (void **)&c_sess))
315 sess = (struct aesni_mb_session *)c_sess->_private;
317 if (unlikely(aesni_mb_set_session_parameters(qp->ops,
318 sess, crypto_op->xform) != 0))
326 * Process a crypto operation and complete a JOB_AES_HMAC job structure for
327 * submission to the multi buffer library for processing.
329 * @param qp queue pair
330 * @param job JOB_AES_HMAC structure to fill
331 * @param m mbuf to process
334 * - Completed JOB_AES_HMAC structure pointer on success
335 * - NULL pointer if completion of JOB_AES_HMAC structure isn't possible
337 static JOB_AES_HMAC *
338 process_crypto_op(struct aesni_mb_qp *qp, struct rte_mbuf *m,
339 struct rte_crypto_op *c_op, struct aesni_mb_session *session)
343 job = (*qp->ops->job.get_next)(&qp->mb_mgr);
344 if (unlikely(job == NULL))
347 /* Set crypto operation */
348 job->chain_order = session->chain_order;
350 /* Set cipher parameters */
351 job->cipher_direction = session->cipher.direction;
352 job->cipher_mode = session->cipher.mode;
354 job->aes_key_len_in_bytes = session->cipher.key_length_in_bytes;
355 job->aes_enc_key_expanded = session->cipher.expanded_aes_keys.encode;
356 job->aes_dec_key_expanded = session->cipher.expanded_aes_keys.decode;
359 /* Set authentication parameters */
360 job->hash_alg = session->auth.algo;
361 if (job->hash_alg == AES_XCBC) {
362 job->_k1_expanded = session->auth.xcbc.k1_expanded;
363 job->_k2 = session->auth.xcbc.k2;
364 job->_k3 = session->auth.xcbc.k3;
366 job->hashed_auth_key_xor_ipad = session->auth.pads.inner;
367 job->hashed_auth_key_xor_opad = session->auth.pads.outer;
370 /* Mutable crypto operation parameters */
372 /* Set digest output location */
373 if (job->cipher_direction == DECRYPT) {
374 job->auth_tag_output = (uint8_t *)rte_pktmbuf_append(m,
375 get_digest_byte_length(job->hash_alg));
377 if (job->auth_tag_output)
378 memset(job->auth_tag_output, 0,
379 sizeof(get_digest_byte_length(job->hash_alg)));
383 job->auth_tag_output = c_op->digest.data;
387 * Multiple buffer library current only support returning a truncated
388 * digest length as specified in the relevant IPsec RFCs
390 job->auth_tag_output_len_in_bytes =
391 get_truncated_digest_byte_length(job->hash_alg);
393 /* Set IV parameters */
394 job->iv = c_op->iv.data;
395 job->iv_len_in_bytes = c_op->iv.length;
398 job->src = rte_pktmbuf_mtod(m, uint8_t *);
399 job->dst = c_op->dst.m ?
400 rte_pktmbuf_mtod(c_op->dst.m, uint8_t *) +
402 rte_pktmbuf_mtod(m, uint8_t *) +
403 c_op->data.to_cipher.offset;
405 job->cipher_start_src_offset_in_bytes = c_op->data.to_cipher.offset;
406 job->msg_len_to_cipher_in_bytes = c_op->data.to_cipher.length;
408 job->hash_start_src_offset_in_bytes = c_op->data.to_hash.offset;
409 job->msg_len_to_hash_in_bytes = c_op->data.to_hash.length;
411 /* Set user data to be crypto operation data struct */
413 job->user_data2 = c_op;
419 * Process a completed job and return rte_mbuf which job processed
421 * @param job JOB_AES_HMAC job to process
424 * - Returns processed mbuf which is trimmed of output digest used in
425 * verification of supplied digest in the case of a HASH_CIPHER operation
426 * - Returns NULL on invalid job
428 static struct rte_mbuf *
429 post_process_mb_job(struct aesni_mb_qp *qp, JOB_AES_HMAC *job)
432 struct rte_crypto_op *c_op;
434 if (job->user_data == NULL)
437 /* handled retrieved job */
438 m = (struct rte_mbuf *)job->user_data;
439 c_op = (struct rte_crypto_op *)job->user_data2;
441 /* set status as successful by default */
442 c_op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
444 /* check if job has been processed */
445 if (unlikely(job->status != STS_COMPLETED)) {
446 c_op->status = RTE_CRYPTO_OP_STATUS_ERROR;
448 } else if (job->chain_order == HASH_CIPHER) {
449 /* Verify digest if required */
450 if (memcmp(job->auth_tag_output, c_op->digest.data,
451 job->auth_tag_output_len_in_bytes) != 0)
452 c_op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
454 /* trim area used for digest from mbuf */
455 rte_pktmbuf_trim(m, get_digest_byte_length(job->hash_alg));
458 /* Free session if a session-less crypto op */
459 if (c_op->type == RTE_CRYPTO_OP_SESSIONLESS) {
460 rte_mempool_put(qp->sess_mp, c_op->session);
461 c_op->session = NULL;
468 * Process a completed JOB_AES_HMAC job and keep processing jobs until
469 * get_completed_job return NULL
471 * @param qp Queue Pair to process
472 * @param job JOB_AES_HMAC job
475 * - Number of processed jobs
478 handle_completed_jobs(struct aesni_mb_qp *qp, JOB_AES_HMAC *job)
480 struct rte_mbuf *m = NULL;
481 unsigned processed_jobs = 0;
485 m = post_process_mb_job(qp, job);
487 rte_ring_enqueue(qp->processed_pkts, (void *)m);
489 qp->qp_stats.dequeue_err_count++;
491 job = (*qp->ops->job.get_completed_job)(&qp->mb_mgr);
494 return processed_jobs;
498 aesni_mb_pmd_enqueue_burst(void *queue_pair, struct rte_mbuf **bufs,
501 struct rte_mbuf_offload *ol;
503 struct aesni_mb_session *sess;
504 struct aesni_mb_qp *qp = queue_pair;
506 JOB_AES_HMAC *job = NULL;
508 int i, processed_jobs = 0;
510 for (i = 0; i < nb_bufs; i++) {
511 ol = rte_pktmbuf_offload_get(bufs[i], RTE_PKTMBUF_OL_CRYPTO);
512 if (unlikely(ol == NULL)) {
513 qp->qp_stats.enqueue_err_count++;
517 sess = get_session(qp, &ol->op.crypto);
518 if (unlikely(sess == NULL)) {
519 qp->qp_stats.enqueue_err_count++;
523 job = process_crypto_op(qp, bufs[i], &ol->op.crypto, sess);
524 if (unlikely(job == NULL)) {
525 qp->qp_stats.enqueue_err_count++;
530 job = (*qp->ops->job.submit)(&qp->mb_mgr);
533 * If submit returns a processed job then handle it,
534 * before submitting subsequent jobs
537 processed_jobs += handle_completed_jobs(qp, job);
540 if (processed_jobs == 0)
543 qp->qp_stats.enqueued_count += processed_jobs;
548 * If we haven't processed any jobs in submit loop, then flush jobs
549 * queue to stop the output stalling
551 job = (*qp->ops->job.flush_job)(&qp->mb_mgr);
553 qp->qp_stats.enqueued_count += handle_completed_jobs(qp, job);
559 aesni_mb_pmd_dequeue_burst(void *queue_pair,
560 struct rte_mbuf **bufs, uint16_t nb_bufs)
562 struct aesni_mb_qp *qp = queue_pair;
564 unsigned nb_dequeued;
566 nb_dequeued = rte_ring_dequeue_burst(qp->processed_pkts,
567 (void **)bufs, nb_bufs);
568 qp->qp_stats.dequeued_count += nb_dequeued;
574 static int cryptodev_aesni_mb_uninit(const char *name);
577 cryptodev_aesni_mb_create(const char *name, unsigned socket_id)
579 struct rte_cryptodev *dev;
580 char crypto_dev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
581 struct aesni_mb_private *internals;
582 enum aesni_mb_vector_mode vector_mode;
584 /* Check CPU for support for AES instruction set */
585 if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_AES)) {
586 MB_LOG_ERR("AES instructions not supported by CPU");
590 /* Check CPU for supported vector instruction set */
591 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
592 vector_mode = RTE_AESNI_MB_AVX2;
593 else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX))
594 vector_mode = RTE_AESNI_MB_AVX;
595 else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
596 vector_mode = RTE_AESNI_MB_SSE;
598 MB_LOG_ERR("Vector instructions are not supported by CPU");
602 /* create a unique device name */
603 if (create_unique_device_name(crypto_dev_name,
604 RTE_CRYPTODEV_NAME_MAX_LEN) != 0) {
605 MB_LOG_ERR("failed to create unique cryptodev name");
610 dev = rte_cryptodev_pmd_virtual_dev_init(crypto_dev_name,
611 sizeof(struct aesni_mb_private), socket_id);
613 MB_LOG_ERR("failed to create cryptodev vdev");
617 dev->dev_type = RTE_CRYPTODEV_AESNI_MB_PMD;
618 dev->dev_ops = rte_aesni_mb_pmd_ops;
620 /* register rx/tx burst functions for data path */
621 dev->dequeue_burst = aesni_mb_pmd_dequeue_burst;
622 dev->enqueue_burst = aesni_mb_pmd_enqueue_burst;
624 /* Set vector instructions mode supported */
625 internals = dev->data->dev_private;
627 internals->vector_mode = vector_mode;
628 internals->max_nb_queue_pairs = RTE_AESNI_MB_PMD_MAX_NB_QUEUE_PAIRS;
629 internals->max_nb_sessions = RTE_AESNI_MB_PMD_MAX_NB_SESSIONS;
633 MB_LOG_ERR("driver %s: cryptodev_aesni_create failed", name);
635 cryptodev_aesni_mb_uninit(crypto_dev_name);
641 cryptodev_aesni_mb_init(const char *name,
642 const char *params __rte_unused)
644 RTE_LOG(INFO, PMD, "Initialising %s\n", name);
646 return cryptodev_aesni_mb_create(name, rte_socket_id());
650 cryptodev_aesni_mb_uninit(const char *name)
655 RTE_LOG(INFO, PMD, "Closing AESNI crypto device %s on numa socket %u\n",
656 name, rte_socket_id());
661 static struct rte_driver cryptodev_aesni_mb_pmd_drv = {
662 .name = CRYPTODEV_NAME_AESNI_MB_PMD,
664 .init = cryptodev_aesni_mb_init,
665 .uninit = cryptodev_aesni_mb_uninit
668 PMD_REGISTER_DRIVER(cryptodev_aesni_mb_pmd_drv);