From patchwork Mon Dec 18 23:00:25 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Github ODP bot X-Patchwork-Id: 122331 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp3430006qgn; Mon, 18 Dec 2017 15:07:24 -0800 (PST) X-Google-Smtp-Source: ACJfBosTWA7i7tnkNKUy6qqb9VGXdNvl715pwk8jlugBQbxu45Z4GipfZ3t4PpIJCl2HR9PbZyGZ X-Received: by 10.233.223.198 with SMTP id t189mr2129909qkf.228.1513638443926; Mon, 18 Dec 2017 15:07:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1513638443; cv=none; d=google.com; s=arc-20160816; b=WEuus3xE+U18GIyDcF8HTJBGtzgKnykttH9DCbZpDnZgYvb4v/3t+3A5re8hvIYpas BQR/drqv2fiIqa5MHT44kDoeClCl/mdoLDoJnAdd3N3XowCyol5/DRXTZtzvEy9uKrlr apuxaW5pIaRIv6K1agDX41fsSUf41vEBKb0k5BmwufwZd+6IPJLdRQqjq350PUL8e8B/ xqsMQkqwLwweNojjqzyyvIUb18PyBGtmfqlSJh8mFObaVosz0HFsUrBdNoJlO9QK5n9x sS5TJ8rYcJBbEvjA2tbWUSrvY/1GuVq/RVvMdzzygbiPtJ1yqAC8vnva1/oDedGujZbG xctQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:github-pr-num :references:in-reply-to:message-id:date:to:from:delivered-to :arc-authentication-results; bh=ZDiOzLI6AjEWyTAdLEAQn84mMy5eRXxH3ac0THsepcM=; b=WaRmNu0gnJgXGMBkLnMMryog+y1VYlNA8P3841VuUmcMA7OJSQ/Cnm7QX4VTWGW5W6 NxFcsyPchaQYw3bbOBkUP9tefRd8SSY1TOivqI13H7wR8aSyeyXh3juTu4Epzf7SWeoU Y09dwjK0ZWkH8PX+Pg5wL6ms8QVQl7Of4aGN2bSn934WxqBEgyK5aP9P7+awomBBfUs0 LZvnVm1xc8IofHwzQCttKMJTYbvo7owJL+Cvgw6hfjosFFMG/X3yDiBcV7zfeQ9DwdUu N66c/rnfTXT6EaB4ssS10dv8nI+FL8uI589VsVEkXlSEvcghv+9YiJrynBrflYohpi6l h+7Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.197.127.237 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=yandex.ru Return-Path: Received: from lists.linaro.org (ec2-54-197-127-237.compute-1.amazonaws.com. [54.197.127.237]) by mx.google.com with ESMTP id y207si666577qka.165.2017.12.18.15.07.23; Mon, 18 Dec 2017 15:07:23 -0800 (PST) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.197.127.237 as permitted sender) client-ip=54.197.127.237; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.197.127.237 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=yandex.ru Received: by lists.linaro.org (Postfix, from userid 109) id 98F45614E3; Mon, 18 Dec 2017 23:07:23 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2 autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id BA1AC608CC; Mon, 18 Dec 2017 23:02:09 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id B63AA6099A; Mon, 18 Dec 2017 23:01:57 +0000 (UTC) Received: from forward103p.mail.yandex.net (forward103p.mail.yandex.net [77.88.28.106]) by lists.linaro.org (Postfix) with ESMTPS id 9D320608DC for ; Mon, 18 Dec 2017 23:00:35 +0000 (UTC) Received: from mxback20j.mail.yandex.net (mxback20j.mail.yandex.net [IPv6:2a02:6b8:0:1619::114]) by forward103p.mail.yandex.net (Yandex) with ESMTP id 6B79E218154C for ; Tue, 19 Dec 2017 02:00:28 +0300 (MSK) Received: from smtp4o.mail.yandex.net (smtp4o.mail.yandex.net [2a02:6b8:0:1a2d::28]) by mxback20j.mail.yandex.net (nwsmtp/Yandex) with ESMTP id lCzlQpU8nx-0SNSYKQH; Tue, 19 Dec 2017 02:00:28 +0300 Received: by smtp4o.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id 0pSVwpoBcF-0Rxe3n0B; Tue, 19 Dec 2017 02:00:27 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client certificate not present) From: Github ODP bot To: lng-odp@lists.linaro.org Date: Tue, 19 Dec 2017 02:00:25 +0300 Message-Id: <1513638025-25901-5-git-send-email-odpbot@yandex.ru> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1513638025-25901-1-git-send-email-odpbot@yandex.ru> References: <1513638025-25901-1-git-send-email-odpbot@yandex.ru> Github-pr-num: 342 Subject: [lng-odp] [PATCH v8 4/4] linux-gen: crypto: significant speedup of all operations X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" From: Dmitry Eremin-Solenikov Per idea of Janne Peltonen, do not allocate/free crypto contexts for each operation, providing significant speed increase. Each thread on startup allocates hmac+cipher contexts pair for each crypto session. Then they are initialized on demand, when session is first executed using this thread. Signed-off-by: Dmitry Eremin-Solenikov --- /** Email created from pull request 342 (lumag:openssl-ctx) ** https://github.com/Linaro/odp/pull/342 ** Patch: https://github.com/Linaro/odp/pull/342.patch ** Base sha: 6b5cdc77eb9759a2349b10372a964648559bc92c ** Merge commit sha: 947faaa98e74853237fe84fcdf7e9e07a64af10d **/ platform/linux-generic/include/odp_internal.h | 2 + platform/linux-generic/odp_crypto.c | 208 +++++++++++++++++--------- platform/linux-generic/odp_init.c | 13 ++ 3 files changed, 154 insertions(+), 69 deletions(-) diff --git a/platform/linux-generic/include/odp_internal.h b/platform/linux-generic/include/odp_internal.h index fc69cd0c5..94aa16ccd 100644 --- a/platform/linux-generic/include/odp_internal.h +++ b/platform/linux-generic/include/odp_internal.h @@ -108,6 +108,8 @@ int odp_queue_term_global(void); int odp_crypto_init_global(void); int odp_crypto_term_global(void); +int _odp_crypto_init_local(void); +int _odp_crypto_term_local(void); int odp_timer_init_global(void); int odp_timer_term_global(void); diff --git a/platform/linux-generic/odp_crypto.c b/platform/linux-generic/odp_crypto.c index 9b5d94034..84c2c00bf 100644 --- a/platform/linux-generic/odp_crypto.c +++ b/platform/linux-generic/odp_crypto.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -85,6 +86,9 @@ typedef odp_crypto_alg_err_t (*crypto_func_t)(odp_crypto_op_param_t *param, odp_crypto_generic_session_t *session); +typedef odp_atomic_u32_t crypto_valid; +#define CV_BITS 32 + /** * Per crypto session data structure */ @@ -112,8 +116,32 @@ struct odp_crypto_generic_session_t { const EVP_MD *evp_md; crypto_func_t func; } auth; + + /* These bitfields are cleared at odp_crypto_session_destroy() + * together with the rest of data */ + crypto_valid cipher_valid[(ODP_THREAD_COUNT_MAX + CV_BITS - 1) / + CV_BITS]; + crypto_valid hmac_valid[(ODP_THREAD_COUNT_MAX + CV_BITS - 1) / + CV_BITS]; + unsigned idx; }; +/* Use _mm versions, because they are always inlined */ +static int crypto_should_init(crypto_valid *ptr, unsigned int id) +{ + unsigned int mask = 1 << (id % CV_BITS); + crypto_valid *cv = ptr + (id / CV_BITS); + uint32_t cur = _odp_atomic_u32_load_mm(ptr, _ODP_MEMMODEL_ACQ); + + + while (!_odp_atomic_u32_cmp_xchg_strong_mm(cv, &cur, cur | mask, + _ODP_MEMMODEL_RLS, + _ODP_MEMMODEL_ACQ)) + ; + + return !(cur & mask); +} + typedef struct odp_crypto_global_s odp_crypto_global_t; struct odp_crypto_global_s { @@ -125,6 +153,16 @@ struct odp_crypto_global_s { static odp_crypto_global_t *global; +typedef struct crypto_local_t { + struct { + HMAC_CTX *hmac; + EVP_CIPHER_CTX *cipher; + } ctx[MAX_SESSIONS]; + int id; +} crypto_local_t; + +static __thread crypto_local_t local; + static odp_crypto_generic_op_result_t *get_op_result_from_event(odp_event_t ev) { @@ -146,6 +184,8 @@ odp_crypto_generic_session_t *alloc_session(void) } odp_spinlock_unlock(&global->lock); + session->idx = (session - global->sessions) / sizeof(*session); + return session; } @@ -165,24 +205,51 @@ null_crypto_routine(odp_crypto_op_param_t *param ODP_UNUSED, return ODP_CRYPTO_ALG_ERR_NONE; } +/* Mimic new OpenSSL 1.1.y API */ +#if OPENSSL_VERSION_NUMBER < 0x10100000L +static HMAC_CTX *HMAC_CTX_new(void) +{ + HMAC_CTX *ctx = malloc(sizeof(*ctx)); + + HMAC_CTX_init(ctx); + return ctx; +} + +static void HMAC_CTX_free(HMAC_CTX *ctx) +{ + HMAC_CTX_cleanup(ctx); + free(ctx); +} +#endif + static -void packet_hmac_calculate(HMAC_CTX *ctx, - odp_crypto_op_param_t *param, - odp_crypto_generic_session_t *session, - uint8_t *hash) +void packet_hmac(odp_crypto_op_param_t *param, + odp_crypto_generic_session_t *session, + uint8_t *hash) { + HMAC_CTX *ctx = local.ctx[session->idx].hmac; odp_packet_t pkt = param->out_pkt; uint32_t offset = param->auth_range.offset; uint32_t len = param->auth_range.length; ODP_ASSERT(offset + len <= odp_packet_len(pkt)); - HMAC_Init_ex(ctx, - session->auth.key, - session->auth.key_length, - session->auth.evp_md, - NULL); + if (crypto_should_init(session->hmac_valid, local.id)) { + HMAC_Init_ex(ctx, + session->auth.key, + session->auth.key_length, + session->auth.evp_md, + NULL); + } else { + /* Reinitialize HMAC calculation without resetting the key */ + HMAC_Init_ex(ctx, + NULL, + 0, + NULL, + NULL); + } + /* Hash it */ while (len > 0) { uint32_t seglen = 0; /* GCC */ void *mapaddr = odp_packet_offset(pkt, offset, &seglen, NULL); @@ -196,34 +263,6 @@ void packet_hmac_calculate(HMAC_CTX *ctx, HMAC_Final(ctx, hash, NULL); } -#if OPENSSL_VERSION_NUMBER < 0x10100000L -static -void packet_hmac(odp_crypto_op_param_t *param, - odp_crypto_generic_session_t *session, - uint8_t *hash) -{ - HMAC_CTX ctx; - - /* Hash it */ - HMAC_CTX_init(&ctx); - packet_hmac_calculate(&ctx, param, session, hash); - HMAC_CTX_cleanup(&ctx); -} -#else -static -void packet_hmac(odp_crypto_op_param_t *param, - odp_crypto_generic_session_t *session, - uint8_t *hash) -{ - HMAC_CTX *ctx; - - /* Hash it */ - ctx = HMAC_CTX_new(); - packet_hmac_calculate(ctx, param, session, hash); - HMAC_CTX_free(ctx); -} -#endif - static odp_crypto_alg_err_t auth_gen(odp_crypto_op_param_t *param, odp_crypto_generic_session_t *session) @@ -386,7 +425,7 @@ static odp_crypto_alg_err_t cipher_encrypt(odp_crypto_op_param_t *param, odp_crypto_generic_session_t *session) { - EVP_CIPHER_CTX *ctx; + EVP_CIPHER_CTX *ctx = local.ctx[session->idx].cipher; void *iv_ptr; int ret; @@ -398,16 +437,15 @@ odp_crypto_alg_err_t cipher_encrypt(odp_crypto_op_param_t *param, return ODP_CRYPTO_ALG_ERR_IV_INVALID; /* Encrypt it */ - ctx = EVP_CIPHER_CTX_new(); - EVP_EncryptInit_ex(ctx, session->cipher.evp_cipher, NULL, - session->cipher.key_data, NULL); + if (crypto_should_init(session->cipher_valid, local.id)) { + EVP_EncryptInit_ex(ctx, session->cipher.evp_cipher, NULL, + session->cipher.key_data, NULL); + EVP_CIPHER_CTX_set_padding(ctx, 0); + } EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv_ptr); - EVP_CIPHER_CTX_set_padding(ctx, 0); ret = internal_encrypt(ctx, param); - EVP_CIPHER_CTX_free(ctx); - return ret <= 0 ? ODP_CRYPTO_ALG_ERR_DATA_SIZE : ODP_CRYPTO_ALG_ERR_NONE; } @@ -416,7 +454,7 @@ static odp_crypto_alg_err_t cipher_decrypt(odp_crypto_op_param_t *param, odp_crypto_generic_session_t *session) { - EVP_CIPHER_CTX *ctx; + EVP_CIPHER_CTX *ctx = local.ctx[session->idx].cipher; void *iv_ptr; int ret; @@ -428,16 +466,15 @@ odp_crypto_alg_err_t cipher_decrypt(odp_crypto_op_param_t *param, return ODP_CRYPTO_ALG_ERR_IV_INVALID; /* Decrypt it */ - ctx = EVP_CIPHER_CTX_new(); - EVP_DecryptInit_ex(ctx, session->cipher.evp_cipher, NULL, - session->cipher.key_data, NULL); + if (crypto_should_init(session->cipher_valid, local.id)) { + EVP_DecryptInit_ex(ctx, session->cipher.evp_cipher, NULL, + session->cipher.key_data, NULL); + EVP_CIPHER_CTX_set_padding(ctx, 0); + } EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv_ptr); - EVP_CIPHER_CTX_set_padding(ctx, 0); ret = internal_decrypt(ctx, param); - EVP_CIPHER_CTX_free(ctx); - return ret <= 0 ? ODP_CRYPTO_ALG_ERR_DATA_SIZE : ODP_CRYPTO_ALG_ERR_NONE; } @@ -473,7 +510,7 @@ static odp_crypto_alg_err_t aes_gcm_encrypt(odp_crypto_op_param_t *param, odp_crypto_generic_session_t *session) { - EVP_CIPHER_CTX *ctx; + EVP_CIPHER_CTX *ctx = local.ctx[session->idx].cipher; const uint8_t *aad_head = param->aad.ptr; uint32_t aad_len = param->aad.length; void *iv_ptr; @@ -489,13 +526,14 @@ odp_crypto_alg_err_t aes_gcm_encrypt(odp_crypto_op_param_t *param, return ODP_CRYPTO_ALG_ERR_IV_INVALID; /* Encrypt it */ - ctx = EVP_CIPHER_CTX_new(); - EVP_EncryptInit_ex(ctx, session->cipher.evp_cipher, NULL, - session->cipher.key_data, NULL); - EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, - session->p.iv.length, NULL); + if (crypto_should_init(session->cipher_valid, local.id)) { + EVP_EncryptInit_ex(ctx, session->cipher.evp_cipher, NULL, + session->cipher.key_data, NULL); + EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, + session->p.iv.length, NULL); + EVP_CIPHER_CTX_set_padding(ctx, 0); + } EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv_ptr); - EVP_CIPHER_CTX_set_padding(ctx, 0); /* Authenticate header data (if any) without encrypting them */ if (aad_len > 0) @@ -509,8 +547,6 @@ odp_crypto_alg_err_t aes_gcm_encrypt(odp_crypto_op_param_t *param, odp_packet_copy_from_mem(param->out_pkt, param->hash_result_offset, session->p.auth_digest_len, block); - EVP_CIPHER_CTX_free(ctx); - return ret <= 0 ? ODP_CRYPTO_ALG_ERR_DATA_SIZE : ODP_CRYPTO_ALG_ERR_NONE; } @@ -519,7 +555,7 @@ static odp_crypto_alg_err_t aes_gcm_decrypt(odp_crypto_op_param_t *param, odp_crypto_generic_session_t *session) { - EVP_CIPHER_CTX *ctx; + EVP_CIPHER_CTX *ctx = local.ctx[session->idx].cipher; const uint8_t *aad_head = param->aad.ptr; uint32_t aad_len = param->aad.length; int dummy_len = 0; @@ -535,13 +571,14 @@ odp_crypto_alg_err_t aes_gcm_decrypt(odp_crypto_op_param_t *param, return ODP_CRYPTO_ALG_ERR_IV_INVALID; /* Decrypt it */ - ctx = EVP_CIPHER_CTX_new(); - EVP_DecryptInit_ex(ctx, session->cipher.evp_cipher, NULL, - session->cipher.key_data, NULL); - EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, - session->p.iv.length, NULL); + if (crypto_should_init(session->cipher_valid, local.id)) { + EVP_DecryptInit_ex(ctx, session->cipher.evp_cipher, NULL, + session->cipher.key_data, NULL); + EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, + session->p.iv.length, NULL); + EVP_CIPHER_CTX_set_padding(ctx, 0); + } EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv_ptr); - EVP_CIPHER_CTX_set_padding(ctx, 0); odp_packet_copy_to_mem(param->out_pkt, param->hash_result_offset, session->p.auth_digest_len, block); @@ -555,8 +592,6 @@ odp_crypto_alg_err_t aes_gcm_decrypt(odp_crypto_op_param_t *param, ret = internal_decrypt(ctx, param); - EVP_CIPHER_CTX_free(ctx); - return ret <= 0 ? ODP_CRYPTO_ALG_ERR_ICV_CHECK : ODP_CRYPTO_ALG_ERR_NONE; } @@ -1060,6 +1095,41 @@ int odp_crypto_term_global(void) return rc; } +int _odp_crypto_init_local(void) +{ + unsigned i; + + memset(&local, 0, sizeof(local)); + + for (i = 0; i < MAX_SESSIONS; i++) { + local.ctx[i].hmac = HMAC_CTX_new(); + local.ctx[i].cipher = EVP_CIPHER_CTX_new(); + + if (local.ctx[i].hmac == NULL || local.ctx[i].cipher == NULL) { + _odp_crypto_term_local(); + return -1; + } + } + + local.id = odp_thread_id(); + + return 0; +} + +int _odp_crypto_term_local(void) +{ + unsigned i; + + for (i = 0; i < MAX_SESSIONS; i++) { + if (local.ctx[i].hmac != NULL) + HMAC_CTX_free(local.ctx[i].hmac); + if (local.ctx[i].cipher != NULL) + EVP_CIPHER_CTX_free(local.ctx[i].cipher); + } + + return 0; +} + odp_random_kind_t odp_random_max_kind(void) { return ODP_RANDOM_CRYPTO; diff --git a/platform/linux-generic/odp_init.c b/platform/linux-generic/odp_init.c index fe03709b7..e5de95025 100644 --- a/platform/linux-generic/odp_init.c +++ b/platform/linux-generic/odp_init.c @@ -300,6 +300,12 @@ int odp_init_local(odp_instance_t instance, odp_thread_type_t thr_type) } stage = PKTIO_INIT; + if (_odp_crypto_init_local()) { + ODP_ERR("ODP crypto local init failed.\n"); + goto init_fail; + } + stage = CRYPTO_INIT; + if (odp_pool_init_local()) { ODP_ERR("ODP pool local init failed.\n"); goto init_fail; @@ -352,6 +358,13 @@ int _odp_term_local(enum init_stage stage) } /* Fall through */ + case CRYPTO_INIT: + if (_odp_crypto_term_local()) { + ODP_ERR("ODP crypto local term failed.\n"); + rc = -1; + } + /* Fall through */ + case POOL_INIT: if (odp_pool_term_local()) { ODP_ERR("ODP buffer pool local term failed.\n");