libwd/0012-uadk-rsa-add-the-init2-interface-for-rsa.patch
2023-05-26 16:41:41 +08:00

234 lines
6.1 KiB
Diff

From 89f5b9d27d44ff6b7c8b14f3bedb1f6a3471690e Mon Sep 17 00:00:00 2001
From: Weili Qian <qianweili@huawei.com>
Date: Sat, 11 Feb 2023 15:19:30 +0800
Subject: [PATCH 12/28] uadk/rsa: add the init2 interface for rsa
This set of interfaces puts resource initialization
operations into the init2 interface, simplifying the
initialization operations when users use the rsa algorithm.
Signed-off-by: Weili Qian <qianweili@huawei.com>
---
include/wd_rsa.h | 28 ++++++++++++
libwd_crypto.map | 3 ++
wd_rsa.c | 109 ++++++++++++++++++++++++++++++++++++++---------
3 files changed, 121 insertions(+), 19 deletions(-)
diff --git a/include/wd_rsa.h b/include/wd_rsa.h
index e16171f..733d0b7 100644
--- a/include/wd_rsa.h
+++ b/include/wd_rsa.h
@@ -120,6 +120,34 @@ int wd_rsa_init(struct wd_ctx_config *config, struct wd_sched *sched);
*/
void wd_rsa_uninit(void);
+/**
+ * wd_rsa_init2_() - A simplify interface to initializate rsa.
+ * This interface keeps most functions of
+ * wd_rsa_init(). Users just need to descripe the deployment of
+ * business scenarios. Then the initialization will request appropriate
+ * resources to support the business scenarios.
+ * To make the initializate simpler, ctx_params support set NULL.
+ * And then the function will set them as default.
+ * Please do not use this interface with wd_rsa_init() together, or
+ * some resources may be leak.
+ *
+ * @alg: The algorithm users want to use.
+ * @sched_type: The scheduling type users want to use.
+ * @task_type: Reserved.
+ * @ctx_params: The ctxs resources users want to use. Include per operation
+ * type ctx numbers and business process run numa.
+ *
+ * Return 0 if succeed and others if fail.
+ */
+int wd_rsa_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params);
+
+#define wd_rsa_init2(alg, sched_type, task_type) \
+ wd_rsa_init2_(alg, sched_type, task_type, NULL)
+
+/**
+ * wd_rsa_uninit2() - Uninitialise ctx configuration and scheduler.
+ */
+void wd_rsa_uninit2(void);
/**
* wd_rsa_alloc_sess() - Allocate a wd rsa session.
diff --git a/libwd_crypto.map b/libwd_crypto.map
index 2983f20..871ef36 100644
--- a/libwd_crypto.map
+++ b/libwd_crypto.map
@@ -81,6 +81,9 @@ global:
wd_rsa_set_kg_out_psz;
wd_rsa_init;
wd_rsa_uninit;
+ wd_rsa_init2;
+ wd_rsa_init2_;
+ wd_rsa_uninit2;
wd_rsa_alloc_sess;
wd_rsa_free_sess;
wd_do_rsa_async;
diff --git a/wd_rsa.c b/wd_rsa.c
index 0b76c48..99bfe48 100644
--- a/wd_rsa.c
+++ b/wd_rsa.c
@@ -82,6 +82,17 @@ static struct wd_rsa_setting {
} wd_rsa_setting;
struct wd_env_config wd_rsa_env_config;
+static struct wd_init_attrs wd_rsa_init_attrs;
+
+static struct wd_ctx_nums wd_rsa_ctx_num[] = {
+ {1, 1}, {}
+};
+
+static struct wd_ctx_params wd_rsa_ctx_params = {
+ .op_type_num = 1,
+ .ctx_set_num = wd_rsa_ctx_num,
+ .bmp = NULL,
+};
#ifdef WD_STATIC_DRV
static void wd_rsa_set_static_drv(void)
@@ -120,30 +131,19 @@ static void wd_rsa_clear_status(void)
wd_alg_clear_init(&wd_rsa_setting.status);
}
-int wd_rsa_init(struct wd_ctx_config *config, struct wd_sched *sched)
+static int wd_rsa_common_init(struct wd_ctx_config *config, struct wd_sched *sched)
{
void *priv;
- bool flag;
int ret;
- pthread_atfork(NULL, NULL, wd_rsa_clear_status);
-
- flag = wd_alg_try_init(&wd_rsa_setting.status);
- if (!flag)
- return 0;
-
- ret = wd_init_param_check(config, sched);
- if (ret)
- goto out_clear_init;
-
ret = wd_set_epoll_en("WD_RSA_EPOLL_EN",
&wd_rsa_setting.config.epoll_en);
if (ret < 0)
- goto out_clear_init;
+ return ret;
ret = wd_init_ctx_config(&wd_rsa_setting.config, config);
if (ret < 0)
- goto out_clear_init;
+ return ret;
ret = wd_init_sched(&wd_rsa_setting.sched, sched);
if (ret < 0)
@@ -175,8 +175,6 @@ int wd_rsa_init(struct wd_ctx_config *config, struct wd_sched *sched)
goto out_free_priv;
}
- wd_alg_set_init(&wd_rsa_setting.status);
-
return 0;
out_free_priv:
@@ -188,12 +186,10 @@ out_clear_sched:
wd_clear_sched(&wd_rsa_setting.sched);
out_clear_ctx_config:
wd_clear_ctx_config(&wd_rsa_setting.config);
-out_clear_init:
- wd_alg_clear_init(&wd_rsa_setting.status);
return ret;
}
-void wd_rsa_uninit(void)
+static void wd_rsa_common_uninit(void)
{
if (!wd_rsa_setting.priv) {
WD_ERR("invalid: repeat uninit rsa!\n");
@@ -211,6 +207,81 @@ void wd_rsa_uninit(void)
/* unset config, sched, driver */
wd_clear_sched(&wd_rsa_setting.sched);
wd_clear_ctx_config(&wd_rsa_setting.config);
+}
+
+int wd_rsa_init(struct wd_ctx_config *config, struct wd_sched *sched)
+{
+ bool flag;
+ int ret;
+
+ pthread_atfork(NULL, NULL, wd_rsa_clear_status);
+
+ flag = wd_alg_try_init(&wd_rsa_setting.status);
+ if (!flag)
+ return 0;
+
+ ret = wd_init_param_check(config, sched);
+ if (ret)
+ goto out_clear_init;
+
+ ret = wd_rsa_common_init(config, sched);
+ if (ret)
+ goto out_clear_init;
+
+ wd_alg_set_init(&wd_rsa_setting.status);
+
+ return 0;
+
+out_clear_init:
+ wd_alg_clear_init(&wd_rsa_setting.status);
+ return ret;
+}
+
+void wd_rsa_uninit(void)
+{
+ wd_rsa_common_uninit();
+ wd_alg_clear_init(&wd_rsa_setting.status);
+}
+
+int wd_rsa_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params)
+{
+ bool flag;
+ int ret;
+
+ pthread_atfork(NULL, NULL, wd_rsa_clear_status);
+
+ flag = wd_alg_try_init(&wd_rsa_setting.status);
+ if (!flag)
+ return 0;
+
+ if (!alg || sched_type > SCHED_POLICY_BUTT || task_type < 0 || task_type > TASK_MAX_TYPE) {
+ WD_ERR("invalid: input param is wrong!\n");
+ ret = -WD_EINVAL;
+ goto out_clear_init;
+ }
+
+ wd_rsa_init_attrs.alg = alg;
+ wd_rsa_init_attrs.sched_type = sched_type;
+ wd_rsa_init_attrs.ctx_params = ctx_params ? ctx_params : &wd_rsa_ctx_params;
+ wd_rsa_init_attrs.alg_init = wd_rsa_common_init;
+ wd_rsa_init_attrs.alg_poll_ctx = wd_rsa_poll_ctx;
+ ret = wd_alg_attrs_init(&wd_rsa_init_attrs);
+ if (ret)
+ goto out_clear_init;
+
+ wd_alg_set_init(&wd_rsa_setting.status);
+
+ return 0;
+
+out_clear_init:
+ wd_alg_clear_init(&wd_rsa_setting.status);
+ return ret;
+}
+
+void wd_rsa_uninit2(void)
+{
+ wd_rsa_common_uninit();
+ wd_alg_attrs_uninit(&wd_rsa_init_attrs);
wd_alg_clear_init(&wd_rsa_setting.status);
}
--
2.25.1