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_config.h>
35 #include <rte_hexdump.h>
36 #include <rte_cryptodev.h>
37 #include <rte_cryptodev_pmd.h>
39 #include <rte_malloc.h>
40 #include <rte_cpuflags.h>
41 #include <rte_mbuf_offload.h>
43 #include "rte_aesni_mb_pmd_private.h"
46 * Global static parameter used to create a unique name for each AES-NI multi
47 * buffer crypto device.
49 static unsigned unique_name_id;
52 create_unique_device_name(char *name, size_t size)
59 ret = snprintf(name, size, "%s_%u", CRYPTODEV_NAME_AESNI_MB_PMD,
66 typedef void (*hash_one_block_t)(void *data, void *digest);
67 typedef void (*aes_keyexp_t)(void *key, void *enc_exp_keys, void *dec_exp_keys);
70 * Calculate the authentication pre-computes
72 * @param one_block_hash Function pointer to calculate digest on ipad/opad
73 * @param ipad Inner pad output byte array
74 * @param opad Outer pad output byte array
75 * @param hkey Authentication key
76 * @param hkey_len Authentication key length
77 * @param blocksize Block size of selected hash algo
80 calculate_auth_precomputes(hash_one_block_t one_block_hash,
81 uint8_t *ipad, uint8_t *opad,
82 uint8_t *hkey, uint16_t hkey_len,
87 uint8_t ipad_buf[blocksize] __rte_aligned(16);
88 uint8_t opad_buf[blocksize] __rte_aligned(16);
90 /* Setup inner and outer pads */
91 memset(ipad_buf, HMAC_IPAD_VALUE, blocksize);
92 memset(opad_buf, HMAC_OPAD_VALUE, blocksize);
94 /* XOR hash key with inner and outer pads */
95 length = hkey_len > blocksize ? blocksize : hkey_len;
97 for (i = 0; i < length; i++) {
98 ipad_buf[i] ^= hkey[i];
99 opad_buf[i] ^= hkey[i];
102 /* Compute partial hashes */
103 (*one_block_hash)(ipad_buf, ipad);
104 (*one_block_hash)(opad_buf, opad);
107 memset(ipad_buf, 0, blocksize);
108 memset(opad_buf, 0, blocksize);
111 /** Get xform chain order */
113 aesni_mb_get_chain_order(const struct rte_crypto_xform *xform)
116 * Multi-buffer only supports HASH_CIPHER or CIPHER_HASH chained
117 * operations, all other options are invalid, so we must have exactly
118 * 2 xform structs chained together
120 if (xform->next == NULL || xform->next->next != NULL)
123 if (xform->type == RTE_CRYPTO_XFORM_AUTH &&
124 xform->next->type == RTE_CRYPTO_XFORM_CIPHER)
127 if (xform->type == RTE_CRYPTO_XFORM_CIPHER &&
128 xform->next->type == RTE_CRYPTO_XFORM_AUTH)
134 /** Set session authentication parameters */
136 aesni_mb_set_session_auth_parameters(const struct aesni_mb_ops *mb_ops,
137 struct aesni_mb_session *sess,
138 const struct rte_crypto_xform *xform)
140 hash_one_block_t hash_oneblock_fn;
142 if (xform->type != RTE_CRYPTO_XFORM_AUTH) {
143 MB_LOG_ERR("Crypto xform struct not of type auth");
147 /* Set Authentication Parameters */
148 if (xform->auth.algo == RTE_CRYPTO_AUTH_AES_XCBC_MAC) {
149 sess->auth.algo = AES_XCBC;
150 (*mb_ops->aux.keyexp.aes_xcbc)(xform->auth.key.data,
151 sess->auth.xcbc.k1_expanded,
152 sess->auth.xcbc.k2, sess->auth.xcbc.k3);
156 switch (xform->auth.algo) {
157 case RTE_CRYPTO_AUTH_MD5_HMAC:
158 sess->auth.algo = MD5;
159 hash_oneblock_fn = mb_ops->aux.one_block.md5;
161 case RTE_CRYPTO_AUTH_SHA1_HMAC:
162 sess->auth.algo = SHA1;
163 hash_oneblock_fn = mb_ops->aux.one_block.sha1;
165 case RTE_CRYPTO_AUTH_SHA224_HMAC:
166 sess->auth.algo = SHA_224;
167 hash_oneblock_fn = mb_ops->aux.one_block.sha224;
169 case RTE_CRYPTO_AUTH_SHA256_HMAC:
170 sess->auth.algo = SHA_256;
171 hash_oneblock_fn = mb_ops->aux.one_block.sha256;
173 case RTE_CRYPTO_AUTH_SHA384_HMAC:
174 sess->auth.algo = SHA_384;
175 hash_oneblock_fn = mb_ops->aux.one_block.sha384;
177 case RTE_CRYPTO_AUTH_SHA512_HMAC:
178 sess->auth.algo = SHA_512;
179 hash_oneblock_fn = mb_ops->aux.one_block.sha512;
182 MB_LOG_ERR("Unsupported authentication algorithm selection");
186 /* Calculate Authentication precomputes */
187 calculate_auth_precomputes(hash_oneblock_fn,
188 sess->auth.pads.inner, sess->auth.pads.outer,
189 xform->auth.key.data,
190 xform->auth.key.length,
191 get_auth_algo_blocksize(sess->auth.algo));
196 /** Set session cipher parameters */
198 aesni_mb_set_session_cipher_parameters(const struct aesni_mb_ops *mb_ops,
199 struct aesni_mb_session *sess,
200 const struct rte_crypto_xform *xform)
202 aes_keyexp_t aes_keyexp_fn;
204 if (xform->type != RTE_CRYPTO_XFORM_CIPHER) {
205 MB_LOG_ERR("Crypto xform struct not of type cipher");
209 /* Select cipher direction */
210 switch (xform->cipher.op) {
211 case RTE_CRYPTO_CIPHER_OP_ENCRYPT:
212 sess->cipher.direction = ENCRYPT;
214 case RTE_CRYPTO_CIPHER_OP_DECRYPT:
215 sess->cipher.direction = DECRYPT;
218 MB_LOG_ERR("Unsupported cipher operation parameter");
222 /* Select cipher mode */
223 switch (xform->cipher.algo) {
224 case RTE_CRYPTO_CIPHER_AES_CBC:
225 sess->cipher.mode = CBC;
228 MB_LOG_ERR("Unsupported cipher mode parameter");
232 /* Check key length and choose key expansion function */
233 switch (xform->cipher.key.length) {
235 sess->cipher.key_length_in_bytes = AES_128_BYTES;
236 aes_keyexp_fn = mb_ops->aux.keyexp.aes128;
239 sess->cipher.key_length_in_bytes = AES_192_BYTES;
240 aes_keyexp_fn = mb_ops->aux.keyexp.aes192;
243 sess->cipher.key_length_in_bytes = AES_256_BYTES;
244 aes_keyexp_fn = mb_ops->aux.keyexp.aes256;
247 MB_LOG_ERR("Unsupported cipher key length");
251 /* Expanded cipher keys */
252 (*aes_keyexp_fn)(xform->cipher.key.data,
253 sess->cipher.expanded_aes_keys.encode,
254 sess->cipher.expanded_aes_keys.decode);
259 /** Parse crypto xform chain and set private session parameters */
261 aesni_mb_set_session_parameters(const struct aesni_mb_ops *mb_ops,
262 struct aesni_mb_session *sess,
263 const struct rte_crypto_xform *xform)
265 const struct rte_crypto_xform *auth_xform = NULL;
266 const struct rte_crypto_xform *cipher_xform = NULL;
268 /* Select Crypto operation - hash then cipher / cipher then hash */
269 switch (aesni_mb_get_chain_order(xform)) {
271 sess->chain_order = HASH_CIPHER;
273 cipher_xform = xform->next;
276 sess->chain_order = CIPHER_HASH;
277 auth_xform = xform->next;
278 cipher_xform = xform;
281 MB_LOG_ERR("Unsupported operation chain order parameter");
285 if (aesni_mb_set_session_auth_parameters(mb_ops, sess, auth_xform)) {
286 MB_LOG_ERR("Invalid/unsupported authentication parameters");
290 if (aesni_mb_set_session_cipher_parameters(mb_ops, sess,
292 MB_LOG_ERR("Invalid/unsupported cipher parameters");
298 /** Get multi buffer session */
299 static struct aesni_mb_session *
300 get_session(struct aesni_mb_qp *qp, struct rte_crypto_op *crypto_op)
302 struct aesni_mb_session *sess;
304 if (crypto_op->type == RTE_CRYPTO_OP_WITH_SESSION) {
305 if (unlikely(crypto_op->session->type !=
306 RTE_CRYPTODEV_AESNI_MB_PMD))
309 sess = (struct aesni_mb_session *)crypto_op->session->_private;
311 struct rte_cryptodev_session *c_sess = NULL;
313 if (rte_mempool_get(qp->sess_mp, (void **)&c_sess))
316 sess = (struct aesni_mb_session *)c_sess->_private;
318 if (unlikely(aesni_mb_set_session_parameters(qp->ops,
319 sess, crypto_op->xform) != 0))
327 * Process a crypto operation and complete a JOB_AES_HMAC job structure for
328 * submission to the multi buffer library for processing.
330 * @param qp queue pair
331 * @param job JOB_AES_HMAC structure to fill
332 * @param m mbuf to process
335 * - Completed JOB_AES_HMAC structure pointer on success
336 * - NULL pointer if completion of JOB_AES_HMAC structure isn't possible
338 static JOB_AES_HMAC *
339 process_crypto_op(struct aesni_mb_qp *qp, struct rte_mbuf *m,
340 struct rte_crypto_op *c_op, struct aesni_mb_session *session)
344 job = (*qp->ops->job.get_next)(&qp->mb_mgr);
345 if (unlikely(job == NULL))
348 /* Set crypto operation */
349 job->chain_order = session->chain_order;
351 /* Set cipher parameters */
352 job->cipher_direction = session->cipher.direction;
353 job->cipher_mode = session->cipher.mode;
355 job->aes_key_len_in_bytes = session->cipher.key_length_in_bytes;
356 job->aes_enc_key_expanded = session->cipher.expanded_aes_keys.encode;
357 job->aes_dec_key_expanded = session->cipher.expanded_aes_keys.decode;
360 /* Set authentication parameters */
361 job->hash_alg = session->auth.algo;
362 if (job->hash_alg == AES_XCBC) {
363 job->_k1_expanded = session->auth.xcbc.k1_expanded;
364 job->_k2 = session->auth.xcbc.k2;
365 job->_k3 = session->auth.xcbc.k3;
367 job->hashed_auth_key_xor_ipad = session->auth.pads.inner;
368 job->hashed_auth_key_xor_opad = session->auth.pads.outer;
371 /* Mutable crypto operation parameters */
373 /* Set digest output location */
374 if (job->cipher_direction == DECRYPT) {
375 job->auth_tag_output = (uint8_t *)rte_pktmbuf_append(m,
376 get_digest_byte_length(job->hash_alg));
378 if (job->auth_tag_output)
379 memset(job->auth_tag_output, 0,
380 sizeof(get_digest_byte_length(job->hash_alg)));
384 job->auth_tag_output = c_op->digest.data;
388 * Multiple buffer library current only support returning a truncated
389 * digest length as specified in the relevant IPsec RFCs
391 job->auth_tag_output_len_in_bytes =
392 get_truncated_digest_byte_length(job->hash_alg);
394 /* Set IV parameters */
395 job->iv = c_op->iv.data;
396 job->iv_len_in_bytes = c_op->iv.length;
399 job->src = rte_pktmbuf_mtod(m, uint8_t *);
400 job->dst = c_op->dst.m ?
401 rte_pktmbuf_mtod(c_op->dst.m, uint8_t *) +
403 rte_pktmbuf_mtod(m, uint8_t *) +
404 c_op->data.to_cipher.offset;
406 job->cipher_start_src_offset_in_bytes = c_op->data.to_cipher.offset;
407 job->msg_len_to_cipher_in_bytes = c_op->data.to_cipher.length;
409 job->hash_start_src_offset_in_bytes = c_op->data.to_hash.offset;
410 job->msg_len_to_hash_in_bytes = c_op->data.to_hash.length;
412 /* Set user data to be crypto operation data struct */
414 job->user_data2 = c_op;
420 * Process a completed job and return rte_mbuf which job processed
422 * @param job JOB_AES_HMAC job to process
425 * - Returns processed mbuf which is trimmed of output digest used in
426 * verification of supplied digest in the case of a HASH_CIPHER operation
427 * - Returns NULL on invalid job
429 static struct rte_mbuf *
430 post_process_mb_job(struct aesni_mb_qp *qp, JOB_AES_HMAC *job)
433 struct rte_crypto_op *c_op;
435 if (job->user_data == NULL)
438 /* handled retrieved job */
439 m = (struct rte_mbuf *)job->user_data;
440 c_op = (struct rte_crypto_op *)job->user_data2;
442 /* set status as successful by default */
443 c_op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
445 /* check if job has been processed */
446 if (unlikely(job->status != STS_COMPLETED)) {
447 c_op->status = RTE_CRYPTO_OP_STATUS_ERROR;
449 } else if (job->chain_order == HASH_CIPHER) {
450 /* Verify digest if required */
451 if (memcmp(job->auth_tag_output, c_op->digest.data,
452 job->auth_tag_output_len_in_bytes) != 0)
453 c_op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
455 /* trim area used for digest from mbuf */
456 rte_pktmbuf_trim(m, get_digest_byte_length(job->hash_alg));
459 /* Free session if a session-less crypto op */
460 if (c_op->type == RTE_CRYPTO_OP_SESSIONLESS) {
461 rte_mempool_put(qp->sess_mp, c_op->session);
462 c_op->session = NULL;
469 * Process a completed JOB_AES_HMAC job and keep processing jobs until
470 * get_completed_job return NULL
472 * @param qp Queue Pair to process
473 * @param job JOB_AES_HMAC job
476 * - Number of processed jobs
479 handle_completed_jobs(struct aesni_mb_qp *qp, JOB_AES_HMAC *job)
481 struct rte_mbuf *m = NULL;
482 unsigned processed_jobs = 0;
486 m = post_process_mb_job(qp, job);
488 rte_ring_enqueue(qp->processed_pkts, (void *)m);
490 qp->qp_stats.dequeue_err_count++;
492 job = (*qp->ops->job.get_completed_job)(&qp->mb_mgr);
495 return processed_jobs;
499 aesni_mb_pmd_enqueue_burst(void *queue_pair, struct rte_mbuf **bufs,
502 struct rte_mbuf_offload *ol;
504 struct aesni_mb_session *sess;
505 struct aesni_mb_qp *qp = queue_pair;
507 JOB_AES_HMAC *job = NULL;
509 int i, processed_jobs = 0;
511 for (i = 0; i < nb_bufs; i++) {
512 ol = rte_pktmbuf_offload_get(bufs[i], RTE_PKTMBUF_OL_CRYPTO);
513 if (unlikely(ol == NULL)) {
514 qp->qp_stats.enqueue_err_count++;
518 sess = get_session(qp, &ol->op.crypto);
519 if (unlikely(sess == NULL)) {
520 qp->qp_stats.enqueue_err_count++;
524 job = process_crypto_op(qp, bufs[i], &ol->op.crypto, sess);
525 if (unlikely(job == NULL)) {
526 qp->qp_stats.enqueue_err_count++;
531 job = (*qp->ops->job.submit)(&qp->mb_mgr);
534 * If submit returns a processed job then handle it,
535 * before submitting subsequent jobs
538 processed_jobs += handle_completed_jobs(qp, job);
541 if (processed_jobs == 0)
544 qp->qp_stats.enqueued_count += processed_jobs;
549 * If we haven't processed any jobs in submit loop, then flush jobs
550 * queue to stop the output stalling
552 job = (*qp->ops->job.flush_job)(&qp->mb_mgr);
554 qp->qp_stats.enqueued_count += handle_completed_jobs(qp, job);
560 aesni_mb_pmd_dequeue_burst(void *queue_pair,
561 struct rte_mbuf **bufs, uint16_t nb_bufs)
563 struct aesni_mb_qp *qp = queue_pair;
565 unsigned nb_dequeued;
567 nb_dequeued = rte_ring_dequeue_burst(qp->processed_pkts,
568 (void **)bufs, nb_bufs);
569 qp->qp_stats.dequeued_count += nb_dequeued;
575 static int cryptodev_aesni_mb_uninit(const char *name);
578 cryptodev_aesni_mb_create(const char *name, unsigned socket_id)
580 struct rte_cryptodev *dev;
581 char crypto_dev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
582 struct aesni_mb_private *internals;
583 enum aesni_mb_vector_mode vector_mode;
585 /* Check CPU for support for AES instruction set */
586 if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_AES)) {
587 MB_LOG_ERR("AES instructions not supported by CPU");
591 /* Check CPU for supported vector instruction set */
592 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
593 vector_mode = RTE_AESNI_MB_AVX2;
594 else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX))
595 vector_mode = RTE_AESNI_MB_AVX;
596 else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_1))
597 vector_mode = RTE_AESNI_MB_SSE;
599 MB_LOG_ERR("Vector instructions are not supported by CPU");
603 /* create a unique device name */
604 if (create_unique_device_name(crypto_dev_name,
605 RTE_CRYPTODEV_NAME_MAX_LEN) != 0) {
606 MB_LOG_ERR("failed to create unique cryptodev name");
611 dev = rte_cryptodev_pmd_virtual_dev_init(crypto_dev_name,
612 sizeof(struct aesni_mb_private), socket_id);
614 MB_LOG_ERR("failed to create cryptodev vdev");
618 dev->dev_type = RTE_CRYPTODEV_AESNI_MB_PMD;
619 dev->dev_ops = rte_aesni_mb_pmd_ops;
621 /* register rx/tx burst functions for data path */
622 dev->dequeue_burst = aesni_mb_pmd_dequeue_burst;
623 dev->enqueue_burst = aesni_mb_pmd_enqueue_burst;
625 /* Set vector instructions mode supported */
626 internals = dev->data->dev_private;
628 internals->vector_mode = vector_mode;
629 internals->max_nb_queue_pairs = RTE_AESNI_MB_PMD_MAX_NB_QUEUE_PAIRS;
630 internals->max_nb_sessions = RTE_AESNI_MB_PMD_MAX_NB_SESSIONS;
632 return dev->data->dev_id;
634 MB_LOG_ERR("driver %s: cryptodev_aesni_create failed", name);
636 cryptodev_aesni_mb_uninit(crypto_dev_name);
642 cryptodev_aesni_mb_init(const char *name,
643 const char *params __rte_unused)
645 RTE_LOG(INFO, PMD, "Initialising %s\n", name);
647 return cryptodev_aesni_mb_create(name, rte_socket_id());
651 cryptodev_aesni_mb_uninit(const char *name)
656 RTE_LOG(INFO, PMD, "Closing AESNI crypto device %s on numa socket %u\n",
657 name, rte_socket_id());
662 static struct rte_driver cryptodev_aesni_mb_pmd_drv = {
663 .name = CRYPTODEV_NAME_AESNI_MB_PMD,
665 .init = cryptodev_aesni_mb_init,
666 .uninit = cryptodev_aesni_mb_uninit
669 PMD_REGISTER_DRIVER(cryptodev_aesni_mb_pmd_drv);