This set of interfaces puts resource initialization operations into the init2 interface, simplifying the initialization operations when users use the cipher algorithm.
Signed-off-by: Longfang Liu liulongfang@huawei.com --- include/wd_cipher.h | 29 ++++++++++ libwd_crypto.map | 3 + wd_cipher.c | 138 ++++++++++++++++++++++++++++++++++++-------- 3 files changed, 147 insertions(+), 23 deletions(-)
diff --git a/include/wd_cipher.h b/include/wd_cipher.h index 8e69852..301eded 100644 --- a/include/wd_cipher.h +++ b/include/wd_cipher.h @@ -105,6 +105,35 @@ struct wd_cipher_req { int wd_cipher_init(struct wd_ctx_config *config, struct wd_sched *sched); void wd_cipher_uninit(void);
+/** + * wd_cipher_init2_() - A simplify interface to initializate uadk + * encryption and decryption. This interface keeps most functions of + * wd_cipher_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 driver's default. + * Please do not use this interface with wd_cipher_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: Task types, including soft computing, hardware and hybrid computing. + * @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_cipher_init2(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params); + +#define wd_cipher_init2_(alg, sched_type, task_type) \ + wd_cipher_init2(alg, sched_type, task_type, NULL) + +/** + * wd_cipher_uninit2() - Uninitialise ctx configuration and scheduler. + */ +void wd_cipher_uninit2(void); + /** * wd_cipher_alloc_sess() Allocate a wd cipher session * @ setup Parameters to setup this session. diff --git a/libwd_crypto.map b/libwd_crypto.map index 5fadc53..f7d1aa0 100644 --- a/libwd_crypto.map +++ b/libwd_crypto.map @@ -2,6 +2,9 @@ UADK_CRYPTO_2.0 { global: wd_cipher_init; wd_cipher_uninit; + wd_cipher_init2; + wd_cipher_init2_; + wd_cipher_uninit2; wd_cipher_alloc_sess; wd_cipher_free_sess; wd_cipher_set_key; diff --git a/wd_cipher.c b/wd_cipher.c index b627c0d..95f075f 100644 --- a/wd_cipher.c +++ b/wd_cipher.c @@ -67,6 +67,17 @@ struct wd_cipher_sess { };
struct wd_env_config wd_cipher_env_config; +static struct wd_init_attrs wd_cipher_init_attrs; + +static struct wd_ctx_nums wd_cipher_ctx_num[] = { + {1, 1}, {} +}; + +static struct wd_ctx_params wd_cipher_ctx_params = { + .op_type_num = WD_CIPHER_DECRYPTION, + .ctx_set_num = wd_cipher_ctx_num, + .bmp = NULL, +};
#ifdef WD_STATIC_DRV static void wd_cipher_set_static_drv(void) @@ -228,38 +239,25 @@ void wd_cipher_free_sess(handle_t h_sess) free(sess); }
-int wd_cipher_init(struct wd_ctx_config *config, struct wd_sched *sched) +static int wd_cipher_common_init(struct wd_ctx_config *config, + struct wd_sched *sched) { void *priv; - bool flag; int ret;
- flag = wd_alg_try_init(&wd_cipher_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_CIPHER_EPOLL_EN", &wd_cipher_setting.config.epoll_en); if (ret < 0) - goto out_clear_init; + return ret;
ret = wd_init_ctx_config(&wd_cipher_setting.config, config); if (ret < 0) - goto out_clear_init; + return ret;
ret = wd_init_sched(&wd_cipher_setting.sched, sched); if (ret < 0) goto out_clear_ctx_config;
-#ifdef WD_STATIC_DRV - /* set driver */ - wd_cipher_set_static_drv(); -#endif - /* allocate async pool for every ctx */ ret = wd_init_async_request_pool(&wd_cipher_setting.pool, config->ctx_num, WD_POOL_MAX_ENTRIES, @@ -277,12 +275,10 @@ int wd_cipher_init(struct wd_ctx_config *config, struct wd_sched *sched)
ret = wd_cipher_setting.driver->init(&wd_cipher_setting.config, priv); if (ret < 0) { - WD_ERR("failed to do dirver init, ret = %d.\n", ret); + WD_ERR("failed to init cipher dirver!\n"); goto out_free_priv; }
- wd_alg_set_init(&wd_cipher_setting.status); - return 0;
out_free_priv: @@ -294,12 +290,10 @@ out_clear_sched: wd_clear_sched(&wd_cipher_setting.sched); out_clear_ctx_config: wd_clear_ctx_config(&wd_cipher_setting.config); -out_clear_init: - wd_alg_clear_init(&wd_cipher_setting.status); return ret; }
-void wd_cipher_uninit(void) +static void wd_cipher_common_uninit(void) { void *priv = wd_cipher_setting.priv;
@@ -310,9 +304,107 @@ void wd_cipher_uninit(void) wd_cipher_setting.priv = NULL; free(priv);
+ /* uninit async request pool */ wd_uninit_async_request_pool(&wd_cipher_setting.pool); + + /* unset config, sched, driver */ wd_clear_sched(&wd_cipher_setting.sched); wd_clear_ctx_config(&wd_cipher_setting.config); +} + +int wd_cipher_init(struct wd_ctx_config *config, struct wd_sched *sched) +{ + enum wd_status status; + bool flag; + int ret; + + wd_alg_get_init(&wd_cipher_setting.status, &status); + if (status == WD_INIT) { + WD_INFO("UADK cipher has been initialized with wd_cipher_init2()!\n"); + return 0; + } + + flag = wd_alg_try_init(&wd_cipher_setting.status); + if (!flag) + return 0; + + ret = wd_init_param_check(config, sched); + if (ret) + goto out_clear_init; + +#ifdef WD_STATIC_DRV + /* set driver */ + wd_cipher_set_static_drv(); +#endif + + ret = wd_cipher_common_init(config, sched); + if (ret) + goto out_clear_init; + + wd_alg_set_init(&wd_cipher_setting.status); + + return 0; + +out_clear_init: + wd_alg_clear_init(&wd_cipher_setting.status); + return ret; +} + +void wd_cipher_uninit(void) +{ + wd_cipher_common_uninit(); + wd_alg_clear_init(&wd_cipher_setting.status); +} + +int wd_cipher_init2(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params) +{ + enum wd_status status; + int ret = 0; + bool flag; + + wd_alg_get_init(&wd_cipher_setting.status, &status); + if (status == WD_INIT) { + WD_INFO("UADK cipher has been initialized with wd_cipher_init()!\n"); + return 0; + } + + flag = wd_alg_try_init(&wd_cipher_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_uninit; + } + + wd_cipher_init_attrs.alg = alg; + wd_cipher_init_attrs.sched_type = sched_type; + wd_cipher_init_attrs.ctx_params = ctx_params ? ctx_params : &wd_cipher_ctx_params; + wd_cipher_init_attrs.alg_init = wd_cipher_common_init; + wd_cipher_init_attrs.alg_poll_ctx = wd_cipher_poll_ctx; + ret = wd_alg_attrs_init(&wd_cipher_init_attrs); + if (ret) { + WD_ERR("fail to init alg attrs.\n"); + goto out_uninit; + } + + wd_alg_set_init(&wd_cipher_setting.status); + + return 0; + +out_uninit: + wd_alg_clear_init(&wd_cipher_setting.status); + return ret; +} + +void wd_cipher_uninit2(void) +{ + wd_cipher_common_uninit(); + + wd_alg_attrs_uninit(&wd_cipher_init_attrs); + wd_alg_clear_init(&wd_cipher_setting.status); }