From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 1197EA0C41; Thu, 15 Jul 2021 18:42:17 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 1D03A412A5; Thu, 15 Jul 2021 18:42:09 +0200 (CEST) Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam07on2087.outbound.protection.outlook.com [40.107.95.87]) by mails.dpdk.org (Postfix) with ESMTP id F28B14125B for ; Thu, 15 Jul 2021 18:42:05 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=N992fkHeJ91i+RovF6vg8qMCcAKLGXyFClp7dXc4XNny+j9B2eXK99SUxKHuvfUSvsbyI7Co8Y2U+w3xDr6KBXkFaClHRr+us+mt/oG3U0ZkxElBI76D+Z+/J6ITlxl3sDMksGUBSF2vQMPA7XRzv60dp4rswMBa1/tEdHbmARh9Lq4by1s9Sia5pbRKIwhwAdsCiFScebf1LNnca2912QIuY19Fq+4qj5PGETa3vS4ecRzXMHmshwMMnWtII5HW9afEZE+fYMWB1b9QzMjj31G3SlRgHYx55sfxvBQsyT9dE4Nc2fuT6ynwlDJcCkLEoAmM3Ayle8ADxxNxUw9GXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=OGj2qfgA9enBMp0FrtTabcR+gbQjQ+dGIKPMJHLwq9E=; b=masgFBFiWWVH8Iw474a7ii3R52o9gJVxhbOfU4Djq2eEA58EaQg3XmsIdtLFtgwCJS8bBDAv9LDIyj/P7DLbWUOogfWvIA4D/qaCN2xJZ9DT21QJ0/vKh0ogVcWxtSv9vfmSeEUtXscpfY6zVmCAzOrvC2ik+KSEN8AJGxDnrgUnazSwrqbeAtLylrvmczZ2XtaqsUEMYiSoMP5Ex4F58opargZI/FXzzxZ9Fgl0Qv6yJSZ7Yhp7uOIVVdr3FN2nSu4tr9X74XtAkR45SWrVsR+swhy8duO7+GKT/83h1joNXNPMrxC7Yl2d1yF5zwhoeoYg8LTjtMHnP5Fwogaz7w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.112.34) smtp.rcpttodomain=redhat.com smtp.mailfrom=nvidia.com; dmarc=pass (p=quarantine sp=none pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=OGj2qfgA9enBMp0FrtTabcR+gbQjQ+dGIKPMJHLwq9E=; b=ElWeMUIGSEpPjPtQ6SQDhzgcx2P9lZmasAeToCFzsiP+ybwjIGDTfAieqa/eg5X0Oab84hlbD+GHDm6+RPKjS+liF/JJMF25p90uwtNMG7pXfw9bs6q61T5WOPMw/6SpB4KmZ308V89aa2h38C/ovVsabU9mmDmQIkGlnNuxJwDFE441gW7Z2AFohoMSrkFC2mhkl3nYAyYDQxPg6naO3eOliW5muV9EXz1jPxh6FKPf6DlQmwF4pCnQpgQ4/w0QiHneXl7I+mTzd5bLcv2ugC0PePVAY1bU0sYs5VqhK6tA3rj6T/yIKoxijHGAOHVAKJaTa7/mvCNA8k9MYLkgbg== Received: from MW4PR04CA0025.namprd04.prod.outlook.com (2603:10b6:303:69::30) by CH2PR12MB4104.namprd12.prod.outlook.com (2603:10b6:610:a4::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4331.21; Thu, 15 Jul 2021 16:42:04 +0000 Received: from CO1NAM11FT066.eop-nam11.prod.protection.outlook.com (2603:10b6:303:69:cafe::57) by MW4PR04CA0025.outlook.office365.com (2603:10b6:303:69::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4331.23 via Frontend Transport; Thu, 15 Jul 2021 16:42:04 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.112.34) smtp.mailfrom=nvidia.com; redhat.com; dkim=none (message not signed) header.d=none;redhat.com; dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.112.34 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.112.34; helo=mail.nvidia.com; Received: from mail.nvidia.com (216.228.112.34) by CO1NAM11FT066.mail.protection.outlook.com (10.13.175.18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.4331.21 via Frontend Transport; Thu, 15 Jul 2021 16:42:04 +0000 Received: from nvidia.com (172.20.187.6) by HQMAIL107.nvidia.com (172.20.187.13) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Thu, 15 Jul 2021 16:42:02 +0000 From: Shiri Kuzin To: CC: , , , Date: Thu, 15 Jul 2021 19:41:12 +0300 Message-ID: <20210715164126.54073-3-shirik@nvidia.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210715164126.54073-1-shirik@nvidia.com> References: <20210715150817.51485-1-shirik@nvidia.com> <20210715164126.54073-1-shirik@nvidia.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [172.20.187.6] X-ClientProxiedBy: HQMAIL105.nvidia.com (172.20.187.12) To HQMAIL107.nvidia.com (172.20.187.13) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: b8c5764e-5d0e-4715-f5d7-08d947af7a23 X-MS-TrafficTypeDiagnostic: CH2PR12MB4104: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3968; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: /HqFgXFu9NmK8Wu9okAS5VkIX7C8zMgh3cxeTd/ZBhFyzbK1sNns6mcnOEW5mfFXgcAFeHJ4IABFTjQ8QEiP0K1tyBBtXMEYm5D22x9wjy8mAI7ySNOzWOpfDcLNltNypca+wT5nf1SX4edl6c6Bk7SXw6Ji3BDm0K66E5q8RSQzt1G8tJ0TjC63tAjS952bdHshJpLrtHBfibshhjcClfynbKmZebOkmrLoqDU/wukqfJ6jeif7b/7yAK3Jc/whrfmWP0Xf7nY2ezG6fXs/oJ/2+tPbpN7pFpZt9kDLBeKcFVYP+Y17rdSAIIqYeX3zUSnzwzu1Vwn9T27oIpcUGXcYSocEgb9TRH9vfnsZBZEKbjpUEaSTjjpudFaipU5Uf13OVU3O/QsJ6SlPRAyhZMPCj4kDsuRf/ZKI8RoCRfRl5O19pQ1k+ZHZGUsAOEt/PMxanuUsqVxKiqOoz2hfxnRy/mAcMZJQD5+VXniyZwo816ZTl65nfIXXshJo+XPkyPT80upQpPEsaU1/8sQXr9LjeiCD4oPa8J+LiyKu6Oc0XkdSFRLJgnK5EVknxN5aJixxcCLrQsD4xhP9BcGdppX9RhEeoLWPDVyHvetvRi3onuAtDsfTBnHl1HtxyzHnkD8wHprFhngBIvIMSZ7nixrpHs+hhIvxiMSXwsZiFGOPUABa6EL+vQZrMK4eDbC3PD/YhEeepKnPjgKMBjWI6zryEH/8l41BGzRtGiYkFww= X-Forefront-Antispam-Report: CIP:216.228.112.34; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:schybrid03.nvidia.com; CAT:NONE; SFS:(4636009)(346002)(136003)(39860400002)(376002)(396003)(36840700001)(46966006)(7636003)(6286002)(7696005)(82740400003)(316002)(55016002)(47076005)(70206006)(86362001)(36756003)(26005)(6916009)(1076003)(8676002)(356005)(70586007)(34020700004)(8936002)(6666004)(16526019)(5660300002)(2616005)(82310400003)(186003)(83380400001)(336012)(36906005)(2906002)(478600001)(54906003)(36860700001)(426003)(4326008); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Jul 2021 16:42:04.2302 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b8c5764e-5d0e-4715-f5d7-08d947af7a23 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.112.34]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT066.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4104 Subject: [dpdk-dev] [PATCH v8 02/16] crypto/mlx5: add DEK object management X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" A DEK(Data encryption Key) is an mlx5 HW object which represents the cipher algorithm key. The DEKs are used during data encryption/decryption operations. In symmetric algorithms like AES-STS, we use the same DEK for both encryption and decryption. Use the mlx5 hash-list tool to manage the DEK objects in the PMD. Provide the compare, create and destroy functions to manage DEKs in hash-list and introduce an internal API to setup and unset the DEK management and to prepare and destroy specific DEK object. The DEK hash-list will be created in dev_configure routine and destroyed in dev_close routine. Signed-off-by: Shiri Kuzin Acked-by: Matan Azrad --- drivers/crypto/mlx5/meson.build | 1 + drivers/crypto/mlx5/mlx5_crypto.c | 42 ++++--- drivers/crypto/mlx5/mlx5_crypto.h | 51 ++++++++ drivers/crypto/mlx5/mlx5_crypto_dek.c | 161 ++++++++++++++++++++++++++ 4 files changed, 239 insertions(+), 16 deletions(-) create mode 100644 drivers/crypto/mlx5/mlx5_crypto.h create mode 100644 drivers/crypto/mlx5/mlx5_crypto_dek.c diff --git a/drivers/crypto/mlx5/meson.build b/drivers/crypto/mlx5/meson.build index 6fd70bc477..d55cdbfe6f 100644 --- a/drivers/crypto/mlx5/meson.build +++ b/drivers/crypto/mlx5/meson.build @@ -11,6 +11,7 @@ fmt_name = 'mlx5_crypto' deps += ['common_mlx5', 'eal', 'cryptodev'] sources = files( 'mlx5_crypto.c', + 'mlx5_crypto_dek.c', ) cflags_options = [ '-std=c11', diff --git a/drivers/crypto/mlx5/mlx5_crypto.c b/drivers/crypto/mlx5/mlx5_crypto.c index fbe3c21aae..d2d82c7b15 100644 --- a/drivers/crypto/mlx5/mlx5_crypto.c +++ b/drivers/crypto/mlx5/mlx5_crypto.c @@ -3,12 +3,9 @@ */ #include -#include #include +#include #include -#include -#include -#include #include #include @@ -17,6 +14,7 @@ #include #include "mlx5_crypto_utils.h" +#include "mlx5_crypto.h" #define MLX5_CRYPTO_DRIVER_NAME mlx5_crypto #define MLX5_CRYPTO_LOG_NAME pmd.crypto.mlx5 @@ -24,16 +22,6 @@ #define MLX5_CRYPTO_FEATURE_FLAGS \ RTE_CRYPTODEV_FF_HW_ACCELERATED -struct mlx5_crypto_priv { - TAILQ_ENTRY(mlx5_crypto_priv) next; - struct ibv_context *ctx; /* Device context. */ - struct rte_pci_device *pci_dev; - struct rte_cryptodev *crypto_dev; - void *uar; /* User Access Region. */ - uint32_t pdn; /* Protection Domain number. */ - struct ibv_pd *pd; -}; - TAILQ_HEAD(mlx5_crypto_privs, mlx5_crypto_priv) mlx5_crypto_priv_list = TAILQ_HEAD_INITIALIZER(mlx5_crypto_priv_list); static pthread_mutex_t priv_list_lock = PTHREAD_MUTEX_INITIALIZER; @@ -51,11 +39,33 @@ static const struct rte_driver mlx5_drv = { static struct cryptodev_driver mlx5_cryptodev_driver; +static int +mlx5_crypto_dev_configure(struct rte_cryptodev *dev, + struct rte_cryptodev_config *config __rte_unused) +{ + struct mlx5_crypto_priv *priv = dev->data->dev_private; + + if (mlx5_crypto_dek_setup(priv) != 0) { + DRV_LOG(ERR, "Dek hash list creation has failed."); + return -ENOMEM; + } + return 0; +} + +static int +mlx5_crypto_dev_close(struct rte_cryptodev *dev) +{ + struct mlx5_crypto_priv *priv = dev->data->dev_private; + + mlx5_crypto_dek_unset(priv); + return 0; +} + static struct rte_cryptodev_ops mlx5_crypto_ops = { - .dev_configure = NULL, + .dev_configure = mlx5_crypto_dev_configure, .dev_start = NULL, .dev_stop = NULL, - .dev_close = NULL, + .dev_close = mlx5_crypto_dev_close, .dev_infos_get = NULL, .stats_get = NULL, .stats_reset = NULL, diff --git a/drivers/crypto/mlx5/mlx5_crypto.h b/drivers/crypto/mlx5/mlx5_crypto.h new file mode 100644 index 0000000000..167e9e57ad --- /dev/null +++ b/drivers/crypto/mlx5/mlx5_crypto.h @@ -0,0 +1,51 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright (c) 2021 NVIDIA Corporation & Affiliates + */ + +#ifndef MLX5_CRYPTO_H_ +#define MLX5_CRYPTO_H_ + +#include + +#include +#include + +#include + +#define MLX5_CRYPTO_DEK_HTABLE_SZ (1 << 11) +#define MLX5_CRYPTO_KEY_LENGTH 80 + +struct mlx5_crypto_priv { + TAILQ_ENTRY(mlx5_crypto_priv) next; + struct ibv_context *ctx; /* Device context. */ + struct rte_pci_device *pci_dev; + struct rte_cryptodev *crypto_dev; + void *uar; /* User Access Region. */ + uint32_t pdn; /* Protection Domain number. */ + struct ibv_pd *pd; + struct mlx5_hlist *dek_hlist; /* Dek hash list. */ +}; + +struct mlx5_crypto_dek { + struct mlx5_list_entry entry; /* Pointer to DEK hash list entry. */ + struct mlx5_devx_obj *obj; /* Pointer to DEK DevX object. */ + uint8_t data[MLX5_CRYPTO_KEY_LENGTH]; /* DEK key data. */ + bool size_is_48; /* Whether the key\data size is 48 bytes or not. */ +} __rte_cache_aligned; + +int +mlx5_crypto_dek_destroy(struct mlx5_crypto_priv *priv, + struct mlx5_crypto_dek *dek); + +struct mlx5_crypto_dek * +mlx5_crypto_dek_prepare(struct mlx5_crypto_priv *priv, + struct rte_crypto_cipher_xform *cipher); + +int +mlx5_crypto_dek_setup(struct mlx5_crypto_priv *priv); + +void +mlx5_crypto_dek_unset(struct mlx5_crypto_priv *priv); + +#endif /* MLX5_CRYPTO_H_ */ + diff --git a/drivers/crypto/mlx5/mlx5_crypto_dek.c b/drivers/crypto/mlx5/mlx5_crypto_dek.c new file mode 100644 index 0000000000..43d1bcc9f8 --- /dev/null +++ b/drivers/crypto/mlx5/mlx5_crypto_dek.c @@ -0,0 +1,161 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright (c) 2021 NVIDIA Corporation & Affiliates + */ + +#include +#include +#include +#include + +#include +#include + +#include "mlx5_crypto_utils.h" +#include "mlx5_crypto.h" + +struct mlx5_crypto_dek_ctx { + struct rte_crypto_cipher_xform *cipher; + struct mlx5_crypto_priv *priv; +}; + +int +mlx5_crypto_dek_destroy(struct mlx5_crypto_priv *priv, + struct mlx5_crypto_dek *dek) +{ + return mlx5_hlist_unregister(priv->dek_hlist, &dek->entry); +} + +struct mlx5_crypto_dek * +mlx5_crypto_dek_prepare(struct mlx5_crypto_priv *priv, + struct rte_crypto_cipher_xform *cipher) +{ + struct mlx5_hlist *dek_hlist = priv->dek_hlist; + struct mlx5_crypto_dek_ctx dek_ctx = { + .cipher = cipher, + .priv = priv, + }; + struct rte_crypto_cipher_xform *cipher_ctx = cipher; + uint64_t key64 = __rte_raw_cksum(cipher_ctx->key.data, + cipher_ctx->key.length, 0); + struct mlx5_list_entry *entry = mlx5_hlist_register(dek_hlist, + key64, &dek_ctx); + + return entry == NULL ? NULL : + container_of(entry, struct mlx5_crypto_dek, entry); +} + +static struct mlx5_list_entry * +mlx5_crypto_dek_clone_cb(void *tool_ctx __rte_unused, + struct mlx5_list_entry *oentry, + void *cb_ctx __rte_unused) +{ + struct mlx5_crypto_dek *entry = rte_zmalloc(__func__, sizeof(*entry), + RTE_CACHE_LINE_SIZE); + + if (!entry) { + DRV_LOG(ERR, "Cannot allocate dek resource memory."); + rte_errno = ENOMEM; + return NULL; + } + memcpy(entry, oentry, sizeof(*entry)); + return &entry->entry; +} + +static void +mlx5_crypto_dek_clone_free_cb(void *tool_ctx __rte_unused, + struct mlx5_list_entry *entry) +{ + struct mlx5_crypto_dek *dek = container_of(entry, + struct mlx5_crypto_dek, entry); + + rte_free(dek); +} + +static int +mlx5_crypto_dek_match_cb(void *tool_ctx __rte_unused, + struct mlx5_list_entry *entry, void *cb_ctx) +{ + struct mlx5_crypto_dek_ctx *ctx = cb_ctx; + struct rte_crypto_cipher_xform *cipher_ctx = ctx->cipher; + struct mlx5_crypto_dek *dek = + container_of(entry, typeof(*dek), entry); + uint32_t key_len = dek->size_is_48 ? 48 : 80; + + if (key_len != cipher_ctx->key.length) + return -1; + return memcmp(cipher_ctx->key.data, dek->data, key_len); +} + +static struct mlx5_list_entry * +mlx5_crypto_dek_create_cb(void *tool_ctx __rte_unused, void *cb_ctx) +{ + struct mlx5_crypto_dek_ctx *ctx = cb_ctx; + struct rte_crypto_cipher_xform *cipher_ctx = ctx->cipher; + struct mlx5_crypto_dek *dek = rte_zmalloc(__func__, sizeof(*dek), + RTE_CACHE_LINE_SIZE); + struct mlx5_devx_dek_attr dek_attr = { + .pd = ctx->priv->pdn, + .key_purpose = MLX5_CRYPTO_KEY_PURPOSE_AES_XTS, + .has_keytag = 1, + }; + + if (dek == NULL) { + DRV_LOG(ERR, "Failed to allocate dek memory."); + return NULL; + } + switch (cipher_ctx->key.length) { + case 48: + dek->size_is_48 = true; + dek_attr.key_size = MLX5_CRYPTO_KEY_SIZE_128b; + break; + case 80: + dek->size_is_48 = false; + dek_attr.key_size = MLX5_CRYPTO_KEY_SIZE_256b; + break; + default: + DRV_LOG(ERR, "Key size not supported."); + return NULL; + } + memcpy(&dek_attr.key, cipher_ctx->key.data, cipher_ctx->key.length); + dek->obj = mlx5_devx_cmd_create_dek_obj(ctx->priv->ctx, &dek_attr); + if (dek->obj == NULL) { + rte_free(dek); + return NULL; + } + memcpy(&dek->data, cipher_ctx->key.data, cipher_ctx->key.length); + return &dek->entry; +} + +static void +mlx5_crypto_dek_remove_cb(void *tool_ctx __rte_unused, + struct mlx5_list_entry *entry) +{ + struct mlx5_crypto_dek *dek = + container_of(entry, typeof(*dek), entry); + + claim_zero(mlx5_devx_cmd_destroy(dek->obj)); + rte_free(dek); +} + + +int +mlx5_crypto_dek_setup(struct mlx5_crypto_priv *priv) +{ + priv->dek_hlist = mlx5_hlist_create("dek_hlist", + MLX5_CRYPTO_DEK_HTABLE_SZ, + 0, 1, NULL, mlx5_crypto_dek_create_cb, + mlx5_crypto_dek_match_cb, + mlx5_crypto_dek_remove_cb, + mlx5_crypto_dek_clone_cb, + mlx5_crypto_dek_clone_free_cb); + if (priv->dek_hlist == NULL) + return -1; + return 0; +} + +void +mlx5_crypto_dek_unset(struct mlx5_crypto_priv *priv) +{ + mlx5_hlist_destroy(priv->dek_hlist); + priv->dek_hlist = NULL; +} -- 2.27.0