cryptodev: move IV parameters to session
[dpdk.git] / test / test / test_cryptodev_perf.c
index 7a90667..8bd2e78 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  *   BSD LICENSE
  *
- *   Copyright(c) 2015-2016 Intel Corporation. All rights reserved.
+ *   Copyright(c) 2015-2017 Intel Corporation. All rights reserved.
  *
  *   Redistribution and use in source and binary forms, with or without
  *   modification, are permitted provided that the following conditions
@@ -43,6 +43,8 @@
 #include "test_cryptodev.h"
 #include "test_cryptodev_gcm_test_vectors.h"
 
+#define AES_CIPHER_IV_LENGTH 16
+#define TRIPLE_DES_CIPHER_IV_LENGTH 8
 
 #define PERF_NUM_OPS_INFLIGHT          (128)
 #define DEFAULT_NUM_REQS_TO_SUBMIT     (10000000)
@@ -67,9 +69,6 @@ enum chain_mode {
 
 
 struct symmetric_op {
-       const uint8_t *iv_data;
-       uint32_t iv_len;
-
        const uint8_t *aad_data;
        uint32_t aad_len;
 
@@ -96,6 +95,8 @@ struct symmetric_session_attrs {
        const uint8_t *key_auth_data;
        uint32_t key_auth_len;
 
+       const uint8_t *iv_data;
+       uint16_t iv_len;
        uint32_t digest_len;
 };
 
@@ -110,7 +111,6 @@ struct symmetric_session_attrs {
 
 struct crypto_params {
        uint8_t *aad;
-       uint8_t *iv;
        uint8_t *digest;
 };
 
@@ -265,7 +265,8 @@ testsuite_setup(void)
                        RTE_CRYPTO_OP_TYPE_SYMMETRIC,
                        NUM_MBUFS, MBUF_CACHE_SIZE,
                        DEFAULT_NUM_XFORMS *
-                       sizeof(struct rte_crypto_sym_xform),
+                       sizeof(struct rte_crypto_sym_xform) +
+                       MAXIMUM_IV_LENGTH,
                        rte_socket_id());
                if (ts_params->op_mpool == NULL) {
                        RTE_LOG(ERR, USER1, "Can't create CRYPTO_OP_POOL\n");
@@ -1933,7 +1934,8 @@ test_perf_crypto_qp_vary_burst_size(uint16_t dev_num)
        ut_params->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
        ut_params->cipher_xform.cipher.key.data = aes_cbc_128_key;
        ut_params->cipher_xform.cipher.key.length = CIPHER_IV_LENGTH_AES_CBC;
-
+       ut_params->cipher_xform.cipher.iv.offset = IV_OFFSET;
+       ut_params->cipher_xform.cipher.iv.length = CIPHER_IV_LENGTH_AES_CBC;
 
        /* Setup HMAC Parameters */
        ut_params->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
@@ -1978,19 +1980,13 @@ test_perf_crypto_qp_vary_burst_size(uint16_t dev_num)
                                data_params[0].length);
                op->sym->auth.digest.length = DIGEST_BYTE_LENGTH_SHA256;
 
-               op->sym->auth.data.offset = CIPHER_IV_LENGTH_AES_CBC;
+               op->sym->auth.data.offset = 0;
                op->sym->auth.data.length = data_params[0].length;
 
+               rte_memcpy(rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET),
+                               aes_cbc_128_iv, CIPHER_IV_LENGTH_AES_CBC);
 
-               op->sym->cipher.iv.data = (uint8_t *)rte_pktmbuf_prepend(m,
-                               CIPHER_IV_LENGTH_AES_CBC);
-               op->sym->cipher.iv.phys_addr = rte_pktmbuf_mtophys(m);
-               op->sym->cipher.iv.length = CIPHER_IV_LENGTH_AES_CBC;
-
-               rte_memcpy(op->sym->cipher.iv.data, aes_cbc_128_iv,
-                               CIPHER_IV_LENGTH_AES_CBC);
-
-               op->sym->cipher.data.offset = CIPHER_IV_LENGTH_AES_CBC;
+               op->sym->cipher.data.offset = 0;
                op->sym->cipher.data.length = data_params[0].length;
 
                op->sym->m_src = m;
@@ -2649,6 +2645,8 @@ test_perf_create_aes_sha_session(uint8_t dev_id, enum chain_mode chain,
 
        cipher_xform.cipher.key.data = aes_key;
        cipher_xform.cipher.key.length = cipher_key_len;
+       cipher_xform.cipher.iv.offset = IV_OFFSET;
+       cipher_xform.cipher.iv.length = AES_CIPHER_IV_LENGTH;
        if (chain != CIPHER_ONLY) {
                /* Setup HMAC Parameters */
                auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
@@ -2697,6 +2695,9 @@ test_perf_create_snow3g_session(uint8_t dev_id, enum chain_mode chain,
 
        cipher_xform.cipher.key.data = snow3g_cipher_key;
        cipher_xform.cipher.key.length = cipher_key_len;
+       cipher_xform.cipher.iv.offset = IV_OFFSET;
+       cipher_xform.cipher.iv.length = SNOW3G_CIPHER_IV_LENGTH;
+
 
        /* Setup HMAC Parameters */
        auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
@@ -2744,17 +2745,20 @@ test_perf_create_openssl_session(uint8_t dev_id, enum chain_mode chain,
        /* Setup Cipher Parameters */
        cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
        cipher_xform.cipher.algo = cipher_algo;
+       cipher_xform.cipher.iv.offset = IV_OFFSET;
        cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
 
        switch (cipher_algo) {
        case RTE_CRYPTO_CIPHER_3DES_CBC:
        case RTE_CRYPTO_CIPHER_3DES_CTR:
                cipher_xform.cipher.key.data = triple_des_key;
+               cipher_xform.cipher.iv.length = TRIPLE_DES_CIPHER_IV_LENGTH;
                break;
        case RTE_CRYPTO_CIPHER_AES_CBC:
        case RTE_CRYPTO_CIPHER_AES_CTR:
        case RTE_CRYPTO_CIPHER_AES_GCM:
                cipher_xform.cipher.key.data = aes_key;
+               cipher_xform.cipher.iv.length = AES_CIPHER_IV_LENGTH;
                break;
        default:
                return NULL;
@@ -2819,6 +2823,8 @@ test_perf_create_armv8_session(uint8_t dev_id, enum chain_mode chain,
        }
 
        cipher_xform.cipher.key.length = cipher_key_len;
+       cipher_xform.cipher.iv.offset = IV_OFFSET;
+       cipher_xform.cipher.iv.length = AES_CIPHER_IV_LENGTH;
 
        /* Setup Auth Parameters */
        auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
@@ -2847,9 +2853,7 @@ test_perf_create_armv8_session(uint8_t dev_id, enum chain_mode chain,
        }
 }
 
-#define AES_CIPHER_IV_LENGTH 16
 #define AES_GCM_AAD_LENGTH 16
-#define TRIPLE_DES_CIPHER_IV_LENGTH 8
 
 static struct rte_mbuf *
 test_perf_create_pktmbuf(struct rte_mempool *mpool, unsigned buf_sz)
@@ -2887,23 +2891,21 @@ test_perf_set_crypto_op_aes(struct rte_crypto_op *op, struct rte_mbuf *m,
                op->sym->auth.data.length = 0;
        } else {
                op->sym->auth.digest.data = rte_pktmbuf_mtod_offset(m,
-                                uint8_t *, AES_CIPHER_IV_LENGTH + data_len);
+                                uint8_t *, data_len);
                op->sym->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
-                               AES_CIPHER_IV_LENGTH + data_len);
+                               data_len);
                op->sym->auth.digest.length = digest_len;
-               op->sym->auth.data.offset = AES_CIPHER_IV_LENGTH;
+               op->sym->auth.data.offset = 0;
                op->sym->auth.data.length = data_len;
        }
 
 
-       /* Cipher Parameters */
-       op->sym->cipher.iv.data = rte_pktmbuf_mtod(m, uint8_t *);
-       op->sym->cipher.iv.phys_addr = rte_pktmbuf_mtophys(m);
-       op->sym->cipher.iv.length = AES_CIPHER_IV_LENGTH;
-
-       rte_memcpy(op->sym->cipher.iv.data, aes_iv, AES_CIPHER_IV_LENGTH);
+       /* Copy the IV at the end of the crypto operation */
+       rte_memcpy(rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET),
+                       aes_iv, AES_CIPHER_IV_LENGTH);
 
-       op->sym->cipher.data.offset = AES_CIPHER_IV_LENGTH;
+       /* Cipher Parameters */
+       op->sym->cipher.data.offset = 0;
        op->sym->cipher.data.length = data_len;
 
        op->sym->m_src = m;
@@ -2930,9 +2932,9 @@ test_perf_set_crypto_op_aes_gcm(struct rte_crypto_op *op, struct rte_mbuf *m,
        op->sym->auth.aad.data = aes_gcm_aad;
        op->sym->auth.aad.length = AES_GCM_AAD_LENGTH;
 
-       /* Cipher Parameters */
-       op->sym->cipher.iv.data = aes_iv;
-       op->sym->cipher.iv.length = AES_CIPHER_IV_LENGTH;
+       /* Copy IV at the end of the crypto operation */
+       rte_memcpy(rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET),
+                       aes_iv, AES_CIPHER_IV_LENGTH);
 
        /* Data lengths/offsets Parameters */
        op->sym->auth.data.offset = 0;
@@ -2951,24 +2953,27 @@ test_perf_set_crypto_op_snow3g(struct rte_crypto_op *op, struct rte_mbuf *m,
                struct rte_cryptodev_sym_session *sess, unsigned data_len,
                unsigned digest_len)
 {
+       uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op,
+                       uint8_t *, IV_OFFSET);
+
        if (rte_crypto_op_attach_sym_session(op, sess) != 0) {
                rte_crypto_op_free(op);
                return NULL;
        }
 
+       rte_memcpy(iv_ptr, snow3g_iv, SNOW3G_CIPHER_IV_LENGTH);
+
        /* Authentication Parameters */
        op->sym->auth.digest.data = (uint8_t *)m->buf_addr +
                                                (m->data_off + data_len);
        op->sym->auth.digest.phys_addr =
                                rte_pktmbuf_mtophys_offset(m, data_len);
        op->sym->auth.digest.length = digest_len;
-       op->sym->auth.aad.data = snow3g_iv;
+       op->sym->auth.aad.data = iv_ptr;
+       op->sym->auth.aad.phys_addr = rte_crypto_op_ctophys_offset(op,
+                       IV_OFFSET);
        op->sym->auth.aad.length = SNOW3G_CIPHER_IV_LENGTH;
 
-       /* Cipher Parameters */
-       op->sym->cipher.iv.data = snow3g_iv;
-       op->sym->cipher.iv.length = SNOW3G_CIPHER_IV_LENGTH;
-
        /* Data lengths/offsets Parameters */
        op->sym->auth.data.offset = 0;
        op->sym->auth.data.length = data_len << 3;
@@ -2992,13 +2997,11 @@ test_perf_set_crypto_op_snow3g_cipher(struct rte_crypto_op *op,
                return NULL;
        }
 
-       /* Cipher Parameters */
-       op->sym->cipher.iv.data = rte_pktmbuf_mtod(m, uint8_t *);
-       op->sym->cipher.iv.length = SNOW3G_CIPHER_IV_LENGTH;
-       rte_memcpy(op->sym->cipher.iv.data, snow3g_iv, SNOW3G_CIPHER_IV_LENGTH);
-       op->sym->cipher.iv.phys_addr = rte_pktmbuf_mtophys(m);
+       /* Copy IV at the end of the crypto operation */
+       rte_memcpy(rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET),
+                       snow3g_iv, SNOW3G_CIPHER_IV_LENGTH);
 
-       op->sym->cipher.data.offset = SNOW3G_CIPHER_IV_LENGTH;
+       op->sym->cipher.data.offset = 0;
        op->sym->cipher.data.length = data_len << 3;
 
        op->sym->m_src = m;
@@ -3028,14 +3031,16 @@ test_perf_set_crypto_op_snow3g_hash(struct rte_crypto_op *op,
                                rte_pktmbuf_mtophys_offset(m, data_len +
                                        SNOW3G_CIPHER_IV_LENGTH);
        op->sym->auth.digest.length = digest_len;
-       op->sym->auth.aad.data = rte_pktmbuf_mtod(m, uint8_t *);
+       op->sym->auth.aad.data = rte_crypto_op_ctod_offset(op,
+                       uint8_t *, IV_OFFSET);
+       op->sym->auth.aad.phys_addr = rte_crypto_op_ctophys_offset(op,
+                       IV_OFFSET);
        op->sym->auth.aad.length = SNOW3G_CIPHER_IV_LENGTH;
        rte_memcpy(op->sym->auth.aad.data, snow3g_iv,
                        SNOW3G_CIPHER_IV_LENGTH);
-       op->sym->auth.aad.phys_addr = rte_pktmbuf_mtophys(m);
 
        /* Data lengths/offsets Parameters */
-       op->sym->auth.data.offset = SNOW3G_CIPHER_IV_LENGTH;
+       op->sym->auth.data.offset = 0;
        op->sym->auth.data.length = data_len << 3;
 
        op->sym->m_src = m;
@@ -3061,9 +3066,9 @@ test_perf_set_crypto_op_3des(struct rte_crypto_op *op, struct rte_mbuf *m,
                                rte_pktmbuf_mtophys_offset(m, data_len);
        op->sym->auth.digest.length = digest_len;
 
-       /* Cipher Parameters */
-       op->sym->cipher.iv.data = triple_des_iv;
-       op->sym->cipher.iv.length = TRIPLE_DES_CIPHER_IV_LENGTH;
+       /* Copy IV at the end of the crypto operation */
+       rte_memcpy(rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET),
+                       triple_des_iv, TRIPLE_DES_CIPHER_IV_LENGTH);
 
        /* Data lengths/offsets Parameters */
        op->sym->auth.data.offset = 0;
@@ -3129,10 +3134,9 @@ test_perf_aes_sha(uint8_t dev_id, uint16_t queue_id,
                        return -1;
                }
 
-               /* Make room for Digest and IV in mbuf */
+               /* Make room for Digest in mbuf */
                if (pparams->chain != CIPHER_ONLY)
                        rte_pktmbuf_append(mbufs[i], digest_length);
-               rte_pktmbuf_prepend(mbufs[i], AES_CIPHER_IV_LENGTH);
        }
 
 
@@ -3253,12 +3257,12 @@ test_perf_snow3g(uint8_t dev_id, uint16_t queue_id,
        /* Generate a burst of crypto operations */
        for (i = 0; i < (pparams->burst_size * NUM_MBUF_SETS); i++) {
                /*
-                * Buffer size + iv/aad len is allocated, for perf tests they
+                * Buffer size is allocated, for perf tests they
                 * are equal + digest len.
                 */
                mbufs[i] = test_perf_create_pktmbuf(
                                ts_params->mbuf_mp,
-                               pparams->buf_size + SNOW3G_CIPHER_IV_LENGTH +
+                               pparams->buf_size  +
                                digest_length);
 
                if (mbufs[i] == NULL) {
@@ -4128,6 +4132,8 @@ test_perf_create_session(uint8_t dev_id, struct perf_test_params *pparams)
        cipher_xform.cipher.op = pparams->session_attrs->cipher;
        cipher_xform.cipher.key.data = cipher_key;
        cipher_xform.cipher.key.length = pparams->session_attrs->key_cipher_len;
+       cipher_xform.cipher.iv.length = pparams->session_attrs->iv_len;
+       cipher_xform.cipher.iv.offset = IV_OFFSET;
 
        auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
        auth_xform.next = NULL;
@@ -4159,46 +4165,40 @@ perf_gcm_set_crypto_op(struct rte_crypto_op *op, struct rte_mbuf *m,
                struct crypto_params *m_hlp,
                struct perf_test_params *params)
 {
+       uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op,
+                       uint8_t *, IV_OFFSET);
+
        if (rte_crypto_op_attach_sym_session(op, sess) != 0) {
                rte_crypto_op_free(op);
                return NULL;
        }
 
-       uint16_t iv_pad_len = ALIGN_POW2_ROUNDUP(params->symmetric_op->iv_len,
-                                                16);
-
        op->sym->auth.digest.data = m_hlp->digest;
        op->sym->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(
                                          m,
                                          params->symmetric_op->aad_len +
-                                         iv_pad_len +
                                          params->symmetric_op->p_len);
 
        op->sym->auth.digest.length = params->symmetric_op->t_len;
 
        op->sym->auth.aad.data = m_hlp->aad;
        op->sym->auth.aad.length = params->symmetric_op->aad_len;
-       op->sym->auth.aad.phys_addr = rte_pktmbuf_mtophys_offset(
-                                         m,
-                                         iv_pad_len);
+       op->sym->auth.aad.phys_addr = rte_pktmbuf_mtophys(m);
 
        rte_memcpy(op->sym->auth.aad.data, params->symmetric_op->aad_data,
                       params->symmetric_op->aad_len);
 
-       op->sym->cipher.iv.data = m_hlp->iv;
-       rte_memcpy(op->sym->cipher.iv.data, params->symmetric_op->iv_data,
-                      params->symmetric_op->iv_len);
-       if (params->symmetric_op->iv_len == 12)
-               op->sym->cipher.iv.data[15] = 1;
-
-       op->sym->cipher.iv.length = params->symmetric_op->iv_len;
+       rte_memcpy(iv_ptr, params->session_attrs->iv_data,
+                      params->session_attrs->iv_len);
+       if (params->session_attrs->iv_len == 12)
+               iv_ptr[15] = 1;
 
        op->sym->auth.data.offset =
-                       iv_pad_len + params->symmetric_op->aad_len;
+                       params->symmetric_op->aad_len;
        op->sym->auth.data.length = params->symmetric_op->p_len;
 
        op->sym->cipher.data.offset =
-                       iv_pad_len + params->symmetric_op->aad_len;
+                       params->symmetric_op->aad_len;
        op->sym->cipher.data.length = params->symmetric_op->p_len;
 
        op->sym->m_src = m;
@@ -4212,8 +4212,6 @@ test_perf_create_pktmbuf_fill(struct rte_mempool *mpool,
                unsigned buf_sz, struct crypto_params *m_hlp)
 {
        struct rte_mbuf *m = rte_pktmbuf_alloc(mpool);
-       uint16_t iv_pad_len =
-                       ALIGN_POW2_ROUNDUP(params->symmetric_op->iv_len, 16);
        uint16_t aad_len = params->symmetric_op->aad_len;
        uint16_t digest_size = params->symmetric_op->t_len;
        char *p;
@@ -4225,13 +4223,6 @@ test_perf_create_pktmbuf_fill(struct rte_mempool *mpool,
        }
        m_hlp->aad = (uint8_t *)p;
 
-       p = rte_pktmbuf_append(m, iv_pad_len);
-       if (p == NULL) {
-               rte_pktmbuf_free(m);
-               return NULL;
-       }
-       m_hlp->iv = (uint8_t *)p;
-
        p = rte_pktmbuf_append(m, buf_sz);
        if (p == NULL) {
                rte_pktmbuf_free(m);
@@ -4350,22 +4341,20 @@ perf_AES_GCM(uint8_t dev_id, uint16_t queue_id,
 
                for (m = 0; m < burst_dequeued; m++) {
                        if (test_ops) {
-                               uint16_t iv_pad_len = ALIGN_POW2_ROUNDUP
-                                       (pparams->symmetric_op->iv_len, 16);
                                uint8_t *pkt = rte_pktmbuf_mtod(
                                        proc_ops[m]->sym->m_src,
                                        uint8_t *);
 
                                TEST_ASSERT_BUFFERS_ARE_EQUAL(
                                        pparams->symmetric_op->c_data,
-                                       pkt + iv_pad_len +
+                                       pkt +
                                        pparams->symmetric_op->aad_len,
                                        pparams->symmetric_op->c_len,
                                        "GCM Ciphertext data not as expected");
 
                                TEST_ASSERT_BUFFERS_ARE_EQUAL(
                                        pparams->symmetric_op->t_data,
-                                       pkt + iv_pad_len +
+                                       pkt +
                                        pparams->symmetric_op->aad_len +
                                        pparams->symmetric_op->c_len,
                                        pparams->symmetric_op->t_len,
@@ -4440,11 +4429,11 @@ test_perf_AES_GCM(int continual_buf_len, int continual_size)
                session_attrs[i].key_auth_len = 0;
                session_attrs[i].digest_len =
                                gcm_test->auth_tag.len;
+               session_attrs[i].iv_len = gcm_test->iv.len;
+               session_attrs[i].iv_data = gcm_test->iv.data;
 
                ops_set[i].aad_data = gcm_test->aad.data;
                ops_set[i].aad_len = gcm_test->aad.len;
-               ops_set[i].iv_data = gcm_test->iv.data;
-               ops_set[i].iv_len = gcm_test->iv.len;
                ops_set[i].p_data = gcm_test->plaintext.data;
                ops_set[i].p_len = buf_lengths[i];
                ops_set[i].c_data = gcm_test->ciphertext.data;