uadk_engine/0065-uadk_prov-Set-enable_sw_offload-from-uadk_provider.c.patch
2023-11-29 16:35:32 +08:00

266 lines
7.7 KiB
Diff

From bd2488f4f418e3a3cb4bee1aaedab25e7737fc99 Mon Sep 17 00:00:00 2001
From: Zhangfei Gao <zhangfei.gao@linaro.org>
Date: Tue, 14 Nov 2023 09:10:12 +0000
Subject: [PATCH 65/82] uadk_prov: Set enable_sw_offload from uadk_provider.cnf
Add para enable_sw_offload to offload small packets to sw.
This can be configured from uadk_provider.cnf with default 0.
Only offload when enable_sw_offload != 0
Other paras can be added accordingly later
For example:
vi uadk_provider.cnf
enable_sw_offload = 1
OPENSSL_CONF=uadk_provider.cnf openssl speed -evp md5
Signed-off-by: Zhangfei Gao <zhangfei.gao@linaro.org>
---
src/uadk_prov.h | 3 +++
src/uadk_prov_cipher.c | 41 ++++++++++++++++++++++-----------
src/uadk_prov_digest.c | 7 ++++--
src/uadk_prov_init.c | 52 ++++++++++++++++++++++++++++++++++++++++++
uadk_provider.cnf | 1 +
5 files changed, 89 insertions(+), 15 deletions(-)
diff --git a/src/uadk_prov.h b/src/uadk_prov.h
index 718e78c..b4871b3 100644
--- a/src/uadk_prov.h
+++ b/src/uadk_prov.h
@@ -124,4 +124,7 @@ void uadk_prov_destroy_digest(void);
void uadk_prov_destroy_cipher(void);
void uadk_prov_destroy_rsa(void);
void uadk_prov_destroy_dh(void);
+
+/* offload small packets to sw */
+extern int enable_sw_offload;
#endif
diff --git a/src/uadk_prov_cipher.c b/src/uadk_prov_cipher.c
index 9b0e9fe..5cb91f6 100644
--- a/src/uadk_prov_cipher.c
+++ b/src/uadk_prov_cipher.c
@@ -27,6 +27,7 @@
#include <uadk/wd_sched.h>
#include "uadk.h"
#include "uadk_async.h"
+#include "uadk_prov.h"
#define UADK_DO_SOFT (-0xE0)
#define UADK_DO_HW (-0xF0)
@@ -283,7 +284,8 @@ static int uadk_prov_cipher_init(struct cipher_priv_ctx *priv,
if (key)
memcpy(priv->key, key, keylen);
- uadk_prov_cipher_sw_init(priv, key, iv);
+ if (enable_sw_offload)
+ uadk_prov_cipher_sw_init(priv, key, iv);
return 1;
}
@@ -446,7 +448,8 @@ static void uadk_prov_cipher_ctx_init(struct cipher_priv_ctx *priv)
free(ctx_set_num);
if (unlikely(ret)) {
- priv->switch_flag = UADK_DO_SOFT;
+ if (priv->sw_cipher)
+ priv->switch_flag = UADK_DO_SOFT;
pthread_mutex_unlock(&cipher_mutex);
return;
}
@@ -634,10 +637,16 @@ static int uadk_prov_do_cipher(struct cipher_priv_ctx *priv, unsigned char *out,
int outlint = 0;
int ret;
- if (priv->switch_flag == UADK_DO_SOFT ||
- (priv->sw_cipher && priv->switch_flag != UADK_DO_HW &&
- inlen <= priv->switch_threshold)) {
- /* have issue if both using hw and soft partly */
+ if (priv->sw_cipher &&
+ (priv->switch_flag == UADK_DO_SOFT ||
+ (priv->switch_flag != UADK_DO_HW &&
+ inlen <= priv->switch_threshold))) {
+ /*
+ * Using soft only if enable_sw_offload, which is set in conf file,
+ * then sw_cipher is initialzied
+ * 1. small packets
+ * 2. already choose DO_SOFT, can be hw fail case or following sw case
+ */
ret = uadk_prov_cipher_soft_work(priv, out, &outlint, in, inlen);
if (ret) {
*outl = outlint;
@@ -746,7 +755,8 @@ static int uadk_prov_cipher_block_final(void *vctx, unsigned char *out,
int sw_final_len = 0;
int ret;
- if (priv->switch_flag == UADK_DO_SOFT) {
+ if (priv->sw_cipher &&
+ priv->switch_flag == UADK_DO_SOFT) {
if (!EVP_CipherFinal_ex(priv->sw_ctx, out, &sw_final_len)) {
fprintf(stderr, "EVP_CipherFinal_ex sw_ctx failed.\n");
return 0;
@@ -854,9 +864,10 @@ static int uadk_prov_cipher_stream_update(void *vctx, unsigned char *out,
return 0;
}
- if (priv->switch_flag == UADK_DO_SOFT ||
- (priv->sw_cipher && priv->switch_flag != UADK_DO_HW &&
- inl <= priv->switch_threshold)) {
+ if (priv->sw_cipher &&
+ (priv->switch_flag == UADK_DO_SOFT ||
+ (priv->switch_flag != UADK_DO_HW &&
+ inl <= priv->switch_threshold))) {
int len = 0;
/* have isseu if both using hw and soft partly */
@@ -883,7 +894,8 @@ static int uadk_prov_cipher_stream_final(void *vctx, unsigned char *out,
struct cipher_priv_ctx *priv = (struct cipher_priv_ctx *)vctx;
int sw_final_len = 0;
- if (priv->switch_flag == UADK_DO_SOFT) {
+ if (priv->sw_cipher &&
+ priv->switch_flag == UADK_DO_SOFT) {
if (!EVP_CipherFinal_ex(priv->sw_ctx, out, &sw_final_len)) {
fprintf(stderr, "EVP_CipherFinal_ex sw_ctx failed.\n");
return 0;
@@ -1083,8 +1095,11 @@ static void uadk_prov_cipher_freectx(void *ctx)
{
struct cipher_priv_ctx *priv = (struct cipher_priv_ctx *)ctx;
- EVP_CIPHER_free(priv->sw_cipher);
- EVP_CIPHER_CTX_free(priv->sw_ctx);
+ if (priv->sw_cipher)
+ EVP_CIPHER_free(priv->sw_cipher);
+
+ if (priv->sw_ctx)
+ EVP_CIPHER_CTX_free(priv->sw_ctx);
if (priv->sess) {
wd_cipher_free_sess(priv->sess);
diff --git a/src/uadk_prov_digest.c b/src/uadk_prov_digest.c
index 8d6bf06..378bcbc 100644
--- a/src/uadk_prov_digest.c
+++ b/src/uadk_prov_digest.c
@@ -29,6 +29,7 @@
#include <uadk/wd_sched.h>
#include "uadk.h"
#include "uadk_async.h"
+#include "uadk_prov.h"
#include "uadk_utils.h"
#define UADK_DO_SOFT (-0xE0)
@@ -288,7 +289,8 @@ static int uadk_digest_init(struct digest_priv_ctx *priv)
return 0;
}
- uadk_digests_soft_md(priv);
+ if (enable_sw_offload)
+ uadk_digests_soft_md(priv);
return 1;
@@ -403,7 +405,8 @@ static int uadk_do_digest_sync(struct digest_priv_ctx *priv)
{
int ret;
- if (priv->req.in_bytes <= priv->switch_threshold &&
+ if (priv->soft_md &&
+ priv->req.in_bytes <= priv->switch_threshold &&
priv->state == SEC_DIGEST_INIT)
return 0;
diff --git a/src/uadk_prov_init.c b/src/uadk_prov_init.c
index c3d4a63..9cea8b9 100644
--- a/src/uadk_prov_init.c
+++ b/src/uadk_prov_init.c
@@ -31,6 +31,17 @@
static const char UADK_DEFAULT_PROPERTIES[] = "provider=uadk_provider";
static OSSL_PROVIDER *prov;
+/* Functions provided by the core */
+static OSSL_FUNC_core_get_params_fn *c_get_params;
+static OSSL_FUNC_core_get_libctx_fn *c_get_libctx;
+
+struct uadk_provider_params {
+ char *enable_sw_offload;
+} uadk_params;
+
+/* offload small packets to sw */
+int enable_sw_offload;
+
const OSSL_ALGORITHM uadk_prov_digests[] = {
{ OSSL_DIGEST_NAME_MD5, UADK_DEFAULT_PROPERTIES,
uadk_md5_functions, "uadk_provider md5" },
@@ -153,6 +164,28 @@ static const OSSL_DISPATCH uadk_dispatch_table[] = {
{ 0, NULL }
};
+int uadk_get_params_from_core(const OSSL_CORE_HANDLE *handle)
+{
+ OSSL_PARAM core_params[2], *p = core_params;
+
+ *p++ = OSSL_PARAM_construct_utf8_ptr(
+ "enable_sw_offload",
+ (char **)&uadk_params.enable_sw_offload,
+ 0);
+
+ *p = OSSL_PARAM_construct_end();
+
+ if (!c_get_params(handle, core_params)) {
+ fprintf(stderr, "WARN: UADK get parameters from core is failed.\n");
+ return 0;
+ }
+
+ if (uadk_params.enable_sw_offload)
+ enable_sw_offload = atoi(uadk_params.enable_sw_offload);
+
+ return 1;
+}
+
static void provider_init_child_at_fork_handler(void)
{
int ret;
@@ -170,11 +203,30 @@ int OSSL_provider_init(const OSSL_CORE_HANDLE *handle,
struct uadk_prov_ctx *ctx;
int ret;
+ for (; oin->function_id != 0; oin++) {
+ switch (oin->function_id) {
+ case OSSL_FUNC_CORE_GET_PARAMS:
+ c_get_params = OSSL_FUNC_core_get_params(oin);
+ break;
+ case OSSL_FUNC_CORE_GET_LIBCTX:
+ c_get_libctx = OSSL_FUNC_core_get_libctx(oin);
+ break;
+ default:
+ /* Just ignore anything we don't understand */
+ break;
+ }
+ }
+
+ /* get parameters from uadk_provider.cnf */
+ if (!uadk_get_params_from_core(handle))
+ return 0;
+
ctx = OPENSSL_zalloc(sizeof(*ctx));
if (ctx == NULL)
return 0;
ctx->handle = handle;
+ ctx->libctx = (OSSL_LIB_CTX *)c_get_libctx(handle);
ret = async_module_init();
if (!ret)
fprintf(stderr, "async_module_init fail!\n");
diff --git a/uadk_provider.cnf b/uadk_provider.cnf
index c9d1596..7b277ac 100644
--- a/uadk_provider.cnf
+++ b/uadk_provider.cnf
@@ -13,3 +13,4 @@ uadk_provider = uadk_sect
[uadk_sect]
activate = 1
+enable_sw_offload = 0
--
2.25.1