1. Separate the advanced properties of the device from the driver class properties. 2. Fix some internal implementation bugs. 3. Update some comments.
Signed-off-by: Longfang Liu liulongfang@huawei.com --- include/wd.h | 6 +++++ include/wd_alg.h | 47 ++++++++++++++++++++++++++++++++++++- include/wd_util.h | 8 ++++--- wd.c | 19 ++++++++------- wd_alg.c | 59 +++++++++++++++++++++++++++++------------------ wd_sched.c | 2 ++ wd_util.c | 33 +++++++++++++++++--------- 7 files changed, 126 insertions(+), 48 deletions(-)
diff --git a/include/wd.h b/include/wd.h index 21af7ff..301bf1b 100644 --- a/include/wd.h +++ b/include/wd.h @@ -582,11 +582,17 @@ bool wd_need_info(void); struct wd_capability { char alg_name[CRYPTO_MAX_ALG_NAME]; char drv_name[CRYPTO_MAX_ALG_NAME]; + bool available; int priority; + int calc_type;
struct wd_capability *next; };
+/** + * wd_get_alg_cap() - Get the algorithm information supported + * in the current system. + */ struct wd_capability *wd_get_alg_cap(void); void wd_release_alg_cap(struct wd_capability *head);
diff --git a/include/wd_alg.h b/include/wd_alg.h index e25e191..a1b4c4f 100644 --- a/include/wd_alg.h +++ b/include/wd_alg.h @@ -31,6 +31,13 @@ enum alg_priority { * @drv_name: name of the current device driver * @alg_name: name of the algorithm supported by the driver * @priority: priority of the type of algorithm supported by the driver + * the larger the value of priority, the higher the priority of the driver, + * it will be used first when selecting a driver. + * soft calculation can be defined as 0. + * hard calculation can be defined as a value above 100. + * instruction acceleration can define a higher value according to + * the performance situation, such as 400. + * @calc_type: the calculation method of algorithm supported by the driver * @queue_num: number of device queues required by the device to * execute the algorithm task * @op_type_num: number of modes in which the device executes the @@ -52,6 +59,7 @@ struct wd_alg_driver { const char *drv_name; const char *alg_name; int priority; + int calc_type; int queue_num; int op_type_num; int priv_size; @@ -64,25 +72,62 @@ struct wd_alg_driver { int (*get_usage)(void *param); };
+/** + * wd_alg_driver_register() - Register a device driver. + * @wd_alg_driver: a device driver that supports an algorithm. + * + * Return the execution result, non-zero means error code. + */ int wd_alg_driver_register(struct wd_alg_driver *drv); void wd_alg_driver_unregister(struct wd_alg_driver *drv);
+/** + * @alg_name: name of the algorithm supported by the driver + * @drv_name: name of the current device driver + * @available: Indicates whether the current driver still has resources available + * @priority: priority of the type of algorithm supported by the driver + * @calc_type: the calculation method of algorithm supported by the driver + * @refcnt: the number of times the algorithm driver is being cited by the task + * + * @drv: device Drivers Supporting Algorithms + * @next: pointer to the next node of the algorithm linked list + */ struct wd_alg_list { const char *alg_name; const char *drv_name; - bool available; + bool available; int priority; + int calc_type; int refcnt;
struct wd_alg_driver *drv; struct wd_alg_list *next; };
+/** + * wd_request_drv() - Apply for an algorithm driver. + * @alg_name: task algorithm name. + * @hw_mask: the flag of shield hardware device drivers. + * + * Returns the applied algorithm driver, non means error. + */ struct wd_alg_driver *wd_request_drv(const char *alg_name, bool hw_mask); void wd_release_drv(struct wd_alg_driver *drv);
+/** + * wd_drv_alg_support() - Check the algorithms supported by the driver. + * @alg_name: task algorithm name. + * @drv: a device driver that supports an algorithm. + * + * Return check result. + */ bool wd_drv_alg_support(const char *alg_name, struct wd_alg_driver *drv); + +/** + * wd_enable_drv() - Re-enable use of the current device driver. + * @drv: a device driver that supports an algorithm. + */ void wd_enable_drv(struct wd_alg_driver *drv); void wd_disable_drv(struct wd_alg_driver *drv);
diff --git a/include/wd_util.h b/include/wd_util.h index a730f36..b20466f 100644 --- a/include/wd_util.h +++ b/include/wd_util.h @@ -420,9 +420,11 @@ static inline void wd_alg_clear_init(enum wd_status *status) /** * wd_ctx_param_init() - Initialize the current device driver according * to the obtained queue resource and the applied driver. - * @config: device resources requested by the current algorithm. - * @driver: device driver for the current algorithm application. - * @drv_priv: the parameter pointer of the current device driver. + * @ctx_params: queue configuration parameters required for device initialization. + * @user_ctx_params: user-configured queue context parameters. + * @ctx_set_num: the number of user-configured queue contexts. + * @driver: device drivers that support current business algorithms. + * @max_op_type: number of device queue types that support algorithmic services. * * Return 0 if succeed and other error number if fail. */ diff --git a/wd.c b/wd.c index b6286b5..a049035 100644 --- a/wd.c +++ b/wd.c @@ -886,7 +886,7 @@ char *wd_ctx_get_dev_name(handle_t h_ctx) void wd_release_alg_cap(struct wd_capability *head) { struct wd_capability *cap_pnext = head; - struct wd_capability *cap_node = NULL; + struct wd_capability *cap_node;
while (cap_pnext) { cap_node = cap_pnext; @@ -903,30 +903,30 @@ struct wd_capability *wd_get_alg_cap(void) struct wd_alg_list *head = wd_get_alg_head(); struct wd_alg_list *pnext = head->next; struct wd_capability *cap_head = NULL; - struct wd_capability *cap_node = NULL; struct wd_capability *cap_pnext = NULL; - int i = 0; + struct wd_capability *cap_node;
while (pnext) { cap_node = calloc(1, sizeof(struct wd_capability)); - if (!cap_head) { + if (!cap_node) { WD_ERR("fail to alloc wd capability head\n"); goto alloc_err; }
strcpy(cap_node->alg_name, pnext->alg_name); strcpy(cap_node->drv_name, pnext->drv_name); + cap_node->available = pnext->available; cap_node->priority = pnext->priority; + cap_node->calc_type = pnext->calc_type; cap_node->next = NULL;
- cap_pnext->next = cap_node; - cap_pnext = cap_node; pnext = pnext->next; - if (i == 0) { + if (!cap_pnext) { cap_head = cap_node; - cap_pnext = cap_head; + cap_pnext = cap_node; } - i++; + cap_pnext->next = cap_node; + cap_pnext = cap_node; }
return cap_head; @@ -935,4 +935,3 @@ alloc_err: wd_release_alg_cap(cap_head); return NULL; } - diff --git a/wd_alg.c b/wd_alg.c index 5e4edaf..a802f60 100644 --- a/wd_alg.c +++ b/wd_alg.c @@ -47,6 +47,28 @@ static bool wd_check_accel_dev(const char *dev_name) return false; }
+static bool wd_alg_check_available(int calc_type, const char *dev_name) +{ + bool ret = false; + + switch (calc_type) { + case UADK_ALG_SOFT: + break; + /* Should find the CPU if not support CE */ + case UADK_ALG_CE_INSTR: + break; + /* Should find the CPU if not support SVE */ + case UADK_ALG_SVE_INSTR: + break; + /* Check if the current driver has device support */ + case UADK_ALG_HW: + ret = wd_check_accel_dev(dev_name); + break; + } + + return ret; +} + int wd_alg_driver_register(struct wd_alg_driver *drv) { struct wd_alg_list *new_alg; @@ -65,21 +87,16 @@ int wd_alg_driver_register(struct wd_alg_driver *drv) new_alg->alg_name = drv->alg_name; new_alg->drv_name = drv->drv_name; new_alg->priority = drv->priority; + new_alg->calc_type = drv->calc_type; new_alg->drv = drv; new_alg->refcnt = 0; new_alg->next = NULL;
- if (drv->priority == UADK_ALG_HW) { - /* If not find dev, remove this driver node */ - new_alg->available = wd_check_accel_dev(drv->drv_name); - if (!new_alg->available) { - free(new_alg); - WD_ERR("failed to find alg driver's device!\n"); - return -WD_ENODEV; - } - } else { - /* Should find the CPU if not support SVE or CE */ - new_alg->available = true; + new_alg->available = wd_alg_check_available(drv->calc_type, drv->drv_name); + if (!new_alg->available) { + free(new_alg); + WD_ERR("failed to find alg driver's device!\n"); + return -WD_ENODEV; }
pthread_mutex_lock(&mutex); @@ -103,7 +120,8 @@ void wd_alg_driver_unregister(struct wd_alg_driver *drv) while (pnext) { if (!strcmp(drv->alg_name, pnext->alg_name) && !strcmp(drv->drv_name, pnext->drv_name) && - drv->priority == pnext->priority) { + drv->priority == pnext->priority && + drv->calc_type == pnext->calc_type) { break; } npre = pnext; @@ -162,19 +180,14 @@ void wd_enable_drv(struct wd_alg_driver *drv) while (pnext) { if (!strcmp(drv->alg_name, pnext->alg_name) && !strcmp(drv->drv_name, pnext->drv_name) && - drv->priority == pnext->priority) { + drv->priority == pnext->priority && + drv->calc_type == pnext->calc_type) { break; } pnext = pnext->next; }
- if (drv->priority == UADK_ALG_HW) { - /* If not find dev, remove this driver node */ - pnext->available = wd_check_accel_dev(drv->drv_name); - } else { - /* Should find the CPU if not support SVE or CE */ - pnext->available = true; - } + pnext->available = wd_alg_check_available(drv->calc_type, drv->drv_name); pthread_mutex_unlock(&mutex); }
@@ -190,7 +203,8 @@ void wd_disable_drv(struct wd_alg_driver *drv) while (pnext) { if (!strcmp(drv->alg_name, pnext->alg_name) && !strcmp(drv->drv_name, pnext->drv_name) && - drv->priority == pnext->priority) { + drv->priority == pnext->priority && + drv->calc_type == pnext->calc_type) { break; } pnext = pnext->next; @@ -217,7 +231,7 @@ struct wd_alg_driver *wd_request_drv(const char *alg_name, bool hw_mask) pthread_mutex_lock(&mutex); while (pnext) { /* hw_mask true mean not to used hardware dev */ - if (hw_mask && pnext->drv->priority == UADK_ALG_HW) { + if (hw_mask && pnext->drv->calc_type == UADK_ALG_HW) { pnext = pnext->next; continue; } @@ -262,4 +276,3 @@ void wd_release_drv(struct wd_alg_driver *drv) select_node->refcnt--; pthread_mutex_unlock(&mutex); } - diff --git a/wd_sched.c b/wd_sched.c index 9a52c4d..79f57fb 100644 --- a/wd_sched.c +++ b/wd_sched.c @@ -368,6 +368,7 @@ static int sched_none_poll_policy(handle_t h_sched_ctx,
while (loop_times > 0) { /* Default use ctx 0 */ + loop_times--; ret = sched_ctx->poll_func(0, 1, &poll_num); if ((ret < 0) && (ret != -EAGAIN)) return ret; @@ -409,6 +410,7 @@ static int sched_single_poll_policy(handle_t h_sched_ctx,
while (loop_times > 0) { /* Default async mode use ctx 0 */ + loop_times--; ret = sched_ctx->poll_func(0, 1, &poll_num); if ((ret < 0) && (ret != -EAGAIN)) return ret; diff --git a/wd_util.c b/wd_util.c index dab4fc8..f990eb5 100644 --- a/wd_util.c +++ b/wd_util.c @@ -79,7 +79,7 @@ struct acc_alg_item { };
static struct acc_alg_item alg_options[] = { - {"zlib", "zlib-deflate"}, + {"zlib", "zlib"}, {"gzip", "gzip"}, {"deflate", "deflate"}, {"lz77_zstd", "lz77_zstd"}, @@ -131,6 +131,10 @@ static struct acc_alg_item alg_options[] = { {"sha512", "digest"}, {"sha512-224", "digest"}, {"sha512-256", "digest"}, + {"cmac(aes)", "digest"}, + {"gmac(aes)", "digest"}, + {"xcbc-mac-96(aes)", "digest"}, + {"xcbc-prf-128(aes)", "digest"}, {"", ""} };
@@ -1851,9 +1855,10 @@ int wd_init_param_check(struct wd_ctx_config *config, struct wd_sched *sched)
static void wd_get_alg_type(const char *alg_name, char *alg_type) { + int alg_nums = ARRAY_SIZE(alg_options); int i;
- for (i = 0; i < ARRAY_SIZE(alg_options); i++) { + for (i = 0; i < alg_nums; i++) { if (strcmp(alg_name, alg_options[i].name) == 0) { (void)strcpy(alg_type, alg_options[i].algtype); break; @@ -1893,7 +1898,7 @@ static int file_check_valid(char *lib_file) static int wd_alg_init_fallback(struct wd_alg_driver *fb_driver) { if (!fb_driver->init) { - WD_ERR("soft sec driver have no init interface.\n"); + WD_ERR("soft acc driver have no init interface.\n"); return -WD_EINVAL; }
@@ -1905,7 +1910,7 @@ static int wd_alg_init_fallback(struct wd_alg_driver *fb_driver) static void wd_alg_uninit_fallback(struct wd_alg_driver *fb_driver) { if (!fb_driver->exit) { - WD_ERR("soft sec driver have no exit interface.\n"); + WD_ERR("soft acc driver have no exit interface.\n"); return; }
@@ -1959,7 +1964,7 @@ void wd_alg_uninit_driver(struct wd_ctx_config_internal *config,
driver->exit(priv); /* Ctx config just need clear once */ - if (driver->priority == UADK_ALG_HW) + if (driver->calc_type == UADK_ALG_HW) wd_clear_ctx_config(config);
if (driver->fallback) @@ -1967,7 +1972,7 @@ void wd_alg_uninit_driver(struct wd_ctx_config_internal *config,
if (priv) { free(priv); - priv = NULL; + *drv_priv = NULL; } }
@@ -2052,7 +2057,7 @@ int wd_get_lib_file_path(char *lib_file, char *lib_path, bool is_dir) len = strlen(file_path) - 1; for (i = len; i >= 0; i--) { if (file_path[i] == '/') { - memset(&file_path[i], 0, PATH_STR_SIZE - i + 1); + memset(&file_path[i], 0, PATH_STR_SIZE - i); break; } } @@ -2116,7 +2121,7 @@ void *wd_dlopen_drv(const char *cust_lib_dir) if (ret < 0) goto free_node;
- node->dlhandle = dlopen(lib_path, RTLD_NOW); + node->dlhandle = dlopen(lib_path, RTLD_GLOBAL | RTLD_NOW); if (!node->dlhandle) { free(node); /* there are many other files need to skip */ @@ -2183,6 +2188,9 @@ struct wd_alg_driver *wd_alg_drv_bind(int task_type, char *alg_name) } } break; + default: + WD_ERR("task type error.\n"); + return NULL; }
return set_driver; @@ -2400,7 +2408,7 @@ static int wd_alg_ctx_init(struct wd_init_attrs *attrs) op_type_num = ctx_params->op_type_num; ctx_set_num = wd_get_ctx_numbers(*ctx_params, op_type_num); if (!ctx_set_num || !op_type_num) { - WD_ERR("invalid: ctx_set_num is %d, op_type_num is %d!\n", + WD_ERR("invalid: ctx_set_num is %u, op_type_num is %u!\n", ctx_set_num, op_type_num); ret = -WD_EINVAL; goto out_freelist; @@ -2486,7 +2494,7 @@ int wd_alg_attrs_init(struct wd_init_attrs *attrs) return -WD_EINVAL;
if (attrs->driver) - driver_type = attrs->driver->priority; + driver_type = attrs->driver->calc_type;
switch (driver_type) { case UADK_ALG_SOFT: @@ -2555,6 +2563,10 @@ int wd_alg_attrs_init(struct wd_init_attrs *attrs) ret = alg_init_func(ctx_config, alg_sched); if (ret) goto out_pre_init; + break; + default: + WD_ERR("driver type error: %d\n", driver_type); + return -WD_EINVAL; }
return 0; @@ -2580,4 +2592,3 @@ void wd_alg_attrs_uninit(struct wd_init_attrs *attrs) } wd_sched_rr_release(alg_sched); } -
under the dynamic loading framework. There is no verification process for algorithms and interfaces. May cause wrong pairing of algorithms and drivers
Also, fix some bugs.
Signed-off-by: Longfang Liu liulongfang@huawei.com --- include/wd_cipher.h | 2 +- wd_cipher.c | 137 +++++++++++++++++++++++++++----------------- 2 files changed, 84 insertions(+), 55 deletions(-)
diff --git a/include/wd_cipher.h b/include/wd_cipher.h index 3c41b6b..3d6da22 100644 --- a/include/wd_cipher.h +++ b/include/wd_cipher.h @@ -127,7 +127,7 @@ void wd_cipher_uninit(void); 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_init2_(alg, sched_type, task_type, NULL)
/** * wd_cipher_uninit2() - Uninitialise ctx configuration and scheduler. diff --git a/wd_cipher.c b/wd_cipher.c index eca9711..2a7804d 100644 --- a/wd_cipher.c +++ b/wd_cipher.c @@ -48,14 +48,14 @@ static char *wd_cipher_alg_name[WD_CIPHER_ALG_TYPE_MAX][WD_CIPHER_MODE_TYPE_MAX] "cfb(sm4)", "cbc-cs1(sm4)", "cbc-cs2(sm4)", "cbc-cs3(sm4)"}, {"ecb(aes)", "cbc(aes)", "ctr(aes)", "xts(aes)", "ofb(aes)", "cfb(aes)", "cbc-cs1(aes)", "cbc-cs2(aes)", "cbc-cs3(aes)"}, - {"cbc(des)", "ecb(des)",}, - {"cbc(des3_ede)", "ecb(des3_ede)",} + {"ecb(des)", "cbc(des)",}, + {"ecb(des3_ede)", "cbc(des3_ede)",} };
struct wd_cipher_setting { enum wd_status status; struct wd_ctx_config_internal config; - struct wd_sched sched; + struct wd_sched sched; struct wd_async_msg_pool pool; struct wd_alg_driver *driver; void *priv; @@ -94,13 +94,6 @@ static int wd_cipher_open_driver(void) char lib_path[PATH_STR_SIZE]; int ret;
- /* - * Compatible with the normal acquisition of device - * drivers in the init interface - */ - if (wd_cipher_setting.dlh_list) - return 0; - ret = wd_get_lib_file_path("libhisi_sec.so", lib_path, false); if (ret) return ret; @@ -180,6 +173,24 @@ static int cipher_key_len_check(struct wd_cipher_sess *sess, __u32 length) return ret; }
+static bool wd_cipher_alg_check(const char *alg_name) +{ + int i, j; + + for (i = 0; i < WD_CIPHER_ALG_TYPE_MAX; i++) { + for (j = 0; j < WD_CIPHER_MODE_TYPE_MAX; j++) { + /* Some algorithms do not support all modes */ + if (!wd_cipher_alg_name[i][j] || + !strlen(wd_cipher_alg_name[i][j])) + continue; + if (!strcmp(alg_name, wd_cipher_alg_name[i][j])) + return true; + } + } + + return false; +} + int wd_cipher_set_key(handle_t h_sess, const __u8 *key, __u32 key_len) { struct wd_cipher_sess *sess = (struct wd_cipher_sess *)h_sess; @@ -230,8 +241,9 @@ handle_t wd_cipher_alloc_sess(struct wd_cipher_sess_setup *setup) if (setup->alg >= WD_CIPHER_ALG_TYPE_MAX || setup->mode >= WD_CIPHER_MODE_TYPE_MAX) { WD_ERR("failed to check algorithm!\n"); - return (handle_t)0; + goto err_sess; } + sess->alg_name = wd_cipher_alg_name[setup->alg][setup->mode]; sess->alg = setup->alg; sess->mode = setup->mode; @@ -321,12 +333,12 @@ out_clear_ctx_config: return ret; }
-static void wd_cipher_common_uninit(void) +static int wd_cipher_common_uninit(void) { void *priv = wd_cipher_setting.priv;
if (!priv) - return; + return -WD_EINVAL;
/* uninit async request pool */ wd_uninit_async_request_pool(&wd_cipher_setting.pool); @@ -335,7 +347,10 @@ static void wd_cipher_common_uninit(void) wd_clear_sched(&wd_cipher_setting.sched);
wd_alg_uninit_driver(&wd_cipher_setting.config, - wd_cipher_setting.driver, &priv); + wd_cipher_setting.driver, + &wd_cipher_setting.priv); + + return 0; }
int wd_cipher_init(struct wd_ctx_config *config, struct wd_sched *sched) @@ -374,7 +389,11 @@ out_clear_init:
void wd_cipher_uninit(void) { - wd_cipher_common_uninit(); + int ret; + + ret = wd_cipher_common_uninit(); + if (ret) + return;
wd_cipher_close_driver(); wd_alg_clear_init(&wd_cipher_setting.status); @@ -384,7 +403,7 @@ int wd_cipher_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_p { struct wd_ctx_nums cipher_ctx_num[WD_CIPHER_DECRYPTION + 1] = {0}; struct wd_ctx_params cipher_ctx_params = {0}; - int ret = 0; + int ret = -WD_EINVAL; bool flag;
pthread_atfork(NULL, NULL, wd_cipher_clear_status); @@ -393,16 +412,21 @@ int wd_cipher_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_p if (!flag) return 0;
- if (!alg || sched_type > SCHED_POLICY_BUTT || - task_type < 0 || task_type > TASK_MAX_TYPE) { + 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; + } + + flag = wd_cipher_alg_check(alg); + if (!flag) { + WD_ERR("invalid: cipher:%s unsupported!\n", alg); goto out_uninit; }
/* * Driver lib file path could set by env param. - * than open tham by wd_dlopen_drv() + * then open tham by wd_dlopen_drv() * use NULL means dynamic query path */ wd_cipher_setting.dlh_list = wd_dlopen_drv(NULL); @@ -411,44 +435,44 @@ int wd_cipher_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_p goto out_uninit; }
-res_retry: - memset(&wd_cipher_setting.config, 0, sizeof(struct wd_ctx_config_internal)); + while (ret != 0) { + memset(&wd_cipher_setting.config, 0, sizeof(struct wd_ctx_config_internal));
- /* Get alg driver and dev name */ - wd_cipher_setting.driver = wd_alg_drv_bind(task_type, alg); - if (!wd_cipher_setting.driver) { - WD_ERR("fail to bind a valid driver.\n"); - ret = -WD_EINVAL; - goto out_dlopen; - } + /* Get alg driver and dev name */ + wd_cipher_setting.driver = wd_alg_drv_bind(task_type, alg); + if (!wd_cipher_setting.driver) { + WD_ERR("failed to bind %s driver.\n", alg); + goto out_dlopen; + }
- ret = wd_ctx_param_init(&cipher_ctx_params, ctx_params, - cipher_ctx_num, wd_cipher_setting.driver, - WD_CIPHER_DECRYPTION + 1); - if (ret) { - if (ret == -WD_EAGAIN) { - wd_disable_drv(wd_cipher_setting.driver); - wd_alg_drv_unbind(wd_cipher_setting.driver); - goto res_retry; + ret = wd_ctx_param_init(&cipher_ctx_params, ctx_params, + cipher_ctx_num, wd_cipher_setting.driver, + WD_CIPHER_DECRYPTION + 1); + if (ret) { + if (ret == -WD_EAGAIN) { + wd_disable_drv(wd_cipher_setting.driver); + wd_alg_drv_unbind(wd_cipher_setting.driver); + continue; + } + goto out_driver; } - goto out_driver; - }
- wd_cipher_init_attrs.alg = alg; - wd_cipher_init_attrs.sched_type = sched_type; - wd_cipher_init_attrs.driver = wd_cipher_setting.driver; - wd_cipher_init_attrs.ctx_params = &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) { - if (ret == -WD_ENODEV) { - wd_disable_drv(wd_cipher_setting.driver); - wd_alg_drv_unbind(wd_cipher_setting.driver); - goto res_retry; + wd_cipher_init_attrs.alg = alg; + wd_cipher_init_attrs.sched_type = sched_type; + wd_cipher_init_attrs.driver = wd_cipher_setting.driver; + wd_cipher_init_attrs.ctx_params = &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) { + if (ret == -WD_ENODEV) { + wd_disable_drv(wd_cipher_setting.driver); + wd_alg_drv_unbind(wd_cipher_setting.driver); + continue; + } + WD_ERR("fail to init alg attrs.\n"); + goto out_driver; } - WD_ERR("fail to init alg attrs.\n"); - goto out_driver; }
wd_alg_set_init(&wd_cipher_setting.status); @@ -466,7 +490,11 @@ out_uninit:
void wd_cipher_uninit2(void) { - wd_cipher_common_uninit(); + int ret; + + ret = wd_cipher_common_uninit(); + if (ret) + return;
wd_alg_attrs_uninit(&wd_cipher_init_attrs); wd_alg_drv_unbind(wd_cipher_setting.driver); @@ -585,6 +613,7 @@ static int send_recv_sync(struct wd_ctx_internal *ctx, ret = wd_handle_msg_sync(&msg_handle, ctx->ctx, msg, NULL, wd_cipher_setting.config.epoll_en); pthread_spin_unlock(&ctx->lock); + return ret; }
under the dynamic loading framework. There is no verification processing of the compression algorithm and the compression service interface. May cause mispairing of compression algorithms and compression drivers. Also, fix some other bugs.
Signed-off-by: Longfang Liu liulongfang@huawei.com --- wd_comp.c | 137 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 83 insertions(+), 54 deletions(-)
diff --git a/wd_comp.c b/wd_comp.c index b7e0eb7..27792d9 100644 --- a/wd_comp.c +++ b/wd_comp.c @@ -27,6 +27,10 @@
#define cpu_to_be32(x) swap_byte(x)
+static char *wd_comp_alg_name[WD_COMP_ALG_MAX] = { + "zlib", "gzip", "deflate", "lz77_zstd" +}; + struct wd_comp_sess { enum wd_comp_alg_type alg_type; enum wd_comp_level comp_lv; @@ -68,13 +72,6 @@ static int wd_comp_open_driver(void) const char *alg_name = "zlib"; int ret;
- /* - * Compatible with the normal acquisition of device - * drivers in the init interface - */ - if (wd_comp_setting.dlh_list) - return 0; - ret = wd_get_lib_file_path("libhisi_zip.so", lib_path, false); if (ret) return ret; @@ -102,6 +99,21 @@ static void wd_comp_clear_status(void) wd_alg_clear_init(&wd_comp_setting.status); }
+static bool wd_comp_alg_check(const char *alg_name) +{ + int i; + + for (i = 0; i < WD_COMP_ALG_MAX; i++) { + /* Some algorithms do not support all modes */ + if (!wd_comp_alg_name[i] || !strlen(wd_comp_alg_name[i])) + continue; + if (!strcmp(alg_name, wd_comp_alg_name[i])) + return true; + } + + return false; +} + static int wd_comp_init_nolock(struct wd_ctx_config *config, struct wd_sched *sched) { int ret; @@ -143,21 +155,24 @@ out_clear_ctx_config: return ret; }
-static void wd_comp_uninit_nolock(void) +static int wd_comp_uninit_nolock(void) { void *priv = wd_comp_setting.priv;
if (!priv) - return; + return -WD_EINVAL;
- /* uninit async request pool */ + /* Uninit async request pool */ wd_uninit_async_request_pool(&wd_comp_setting.pool);
- /* unset config, sched, driver */ + /* Unset config, sched, driver */ wd_clear_sched(&wd_comp_setting.sched);
wd_alg_uninit_driver(&wd_comp_setting.config, - wd_comp_setting.driver, &priv); + wd_comp_setting.driver, + &wd_comp_setting.priv); + + return 0; }
int wd_comp_init(struct wd_ctx_config *config, struct wd_sched *sched) @@ -196,7 +211,11 @@ out_clear_init:
void wd_comp_uninit(void) { - wd_comp_uninit_nolock(); + int ret; + + ret = wd_comp_uninit_nolock(); + if (ret) + return;
wd_comp_close_driver(); wd_alg_clear_init(&wd_comp_setting.status); @@ -206,7 +225,7 @@ int wd_comp_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_par { struct wd_ctx_nums comp_ctx_num[WD_DIR_MAX] = {0}; struct wd_ctx_params comp_ctx_params; - int ret = 0; + int ret = -WD_EINVAL; bool flag;
pthread_atfork(NULL, NULL, wd_comp_clear_status); @@ -215,16 +234,21 @@ int wd_comp_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_par if (!flag) return 0;
- if (!alg || sched_type > SCHED_POLICY_BUTT || - task_type < 0 || task_type > TASK_MAX_TYPE) { + 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; + } + + flag = wd_comp_alg_check(alg); + if (!flag) { + WD_ERR("invalid: comp:%s unsupported!\n", alg); goto out_uninit; }
/* * Driver lib file path could set by env param. - * than open tham by wd_dlopen_drv() + * then open tham by wd_dlopen_drv() * use NULL means dynamic query path */ wd_comp_setting.dlh_list = wd_dlopen_drv(NULL); @@ -233,52 +257,53 @@ int wd_comp_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_par goto out_uninit; }
-res_retry: - memset(&wd_comp_setting.config, 0, sizeof(struct wd_ctx_config_internal)); + while (ret != 0) { + memset(&wd_comp_setting.config, 0, sizeof(struct wd_ctx_config_internal));
- /* Get alg driver and dev name */ - wd_comp_setting.driver = wd_alg_drv_bind(task_type, alg); - if (!wd_comp_setting.driver) { - WD_ERR("fail to bind a valid driver.\n"); - goto out_dlopen; - } + /* Get alg driver and dev name */ + wd_comp_setting.driver = wd_alg_drv_bind(task_type, alg); + if (!wd_comp_setting.driver) { + WD_ERR("failed to bind %s driver.\n", alg); + goto out_dlclose; + }
- ret = wd_ctx_param_init(&comp_ctx_params, ctx_params, - comp_ctx_num, wd_comp_setting.driver, - WD_DIR_MAX); - if (ret) { - if (ret == -WD_EAGAIN) { - wd_disable_drv(wd_comp_setting.driver); - wd_alg_drv_unbind(wd_comp_setting.driver); - goto res_retry; + ret = wd_ctx_param_init(&comp_ctx_params, ctx_params, + comp_ctx_num, wd_comp_setting.driver, + WD_DIR_MAX); + if (ret) { + if (ret == -WD_EAGAIN) { + wd_disable_drv(wd_comp_setting.driver); + wd_alg_drv_unbind(wd_comp_setting.driver); + continue; + } + goto out_unbind_drv; } - goto out_driver; - } - - wd_comp_init_attrs.alg = alg; - wd_comp_init_attrs.sched_type = sched_type; - wd_comp_init_attrs.driver = wd_comp_setting.driver; - wd_comp_init_attrs.ctx_params = &comp_ctx_params; - wd_comp_init_attrs.alg_init = wd_comp_init_nolock; - wd_comp_init_attrs.alg_poll_ctx = wd_comp_poll_ctx; - ret = wd_alg_attrs_init(&wd_comp_init_attrs); - if (ret) { - if (ret == -WD_ENODEV) { - wd_disable_drv(wd_comp_setting.driver); - wd_alg_drv_unbind(wd_comp_setting.driver); - goto res_retry; + + wd_comp_init_attrs.alg = alg; + wd_comp_init_attrs.sched_type = sched_type; + wd_comp_init_attrs.driver = wd_comp_setting.driver; + wd_comp_init_attrs.ctx_params = &comp_ctx_params; + wd_comp_init_attrs.alg_init = wd_comp_init_nolock; + wd_comp_init_attrs.alg_poll_ctx = wd_comp_poll_ctx; + ret = wd_alg_attrs_init(&wd_comp_init_attrs); + if (ret) { + if (ret == -WD_ENODEV) { + wd_disable_drv(wd_comp_setting.driver); + wd_alg_drv_unbind(wd_comp_setting.driver); + continue; + } + WD_ERR("fail to init alg attrs.\n"); + goto out_unbind_drv; } - WD_ERR("fail to init alg attrs.\n"); - goto out_driver; }
wd_alg_set_init(&wd_comp_setting.status);
return 0;
-out_driver: +out_unbind_drv: wd_alg_drv_unbind(wd_comp_setting.driver); -out_dlopen: +out_dlclose: wd_dlclose_drv(wd_comp_setting.dlh_list); out_uninit: wd_alg_clear_init(&wd_comp_setting.status); @@ -287,7 +312,11 @@ out_uninit:
void wd_comp_uninit2(void) { - wd_comp_uninit_nolock(); + int ret; + + ret = wd_comp_uninit_nolock(); + if (ret) + return;
wd_alg_attrs_uninit(&wd_comp_init_attrs); wd_alg_drv_unbind(wd_comp_setting.driver);
After the dynamic loading framework separates the priority attribute and the driver type attribute. An update is required in the device driver.
Signed-off-by: Longfang Liu liulongfang@huawei.com --- drv/hisi_comp.c | 5 +++-- drv/hisi_hpre.c | 21 ++++++++++++++++----- drv/hisi_sec.c | 10 +++++----- 3 files changed, 24 insertions(+), 12 deletions(-)
diff --git a/drv/hisi_comp.c b/drv/hisi_comp.c index 01e2ad8..4def014 100644 --- a/drv/hisi_comp.c +++ b/drv/hisi_comp.c @@ -1059,8 +1059,9 @@ static int hisi_zip_comp_recv(handle_t ctx, void *comp_msg) #define GEN_ZIP_ALG_DRIVER(zip_alg_name) \ {\ .drv_name = "hisi_zip",\ - .alg_name = zip_alg_name,\ - .priority = UADK_ALG_HW,\ + .alg_name = (zip_alg_name),\ + .calc_type = UADK_ALG_HW,\ + .priority = 100,\ .priv_size = sizeof(struct hisi_zip_ctx),\ .queue_num = ZIP_CTX_Q_NUM_DEF,\ .op_type_num = 2,\ diff --git a/drv/hisi_hpre.c b/drv/hisi_hpre.c index 4d21788..0dad26b 100644 --- a/drv/hisi_hpre.c +++ b/drv/hisi_hpre.c @@ -2437,11 +2437,17 @@ static int ecc_recv(handle_t ctx, void *ecc_msg) return ecc_sqe_parse((struct hisi_qp *)h_qp, msg, &hw_msg); }
+static int hpre_get_usage(void *param) +{ + return 0; +} + #define GEN_HPRE_ALG_DRIVER(hpre_alg_name) \ {\ .drv_name = "hisi_hpre",\ - .alg_name = hpre_alg_name,\ - .priority = UADK_ALG_HW,\ + .alg_name = (hpre_alg_name),\ + .calc_type = UADK_ALG_HW,\ + .priority = 100,\ .priv_size = sizeof(struct hisi_hpre_ctx),\ .queue_num = HPRE_CTX_Q_NUM_DEF,\ .op_type_num = 1,\ @@ -2450,9 +2456,10 @@ static int ecc_recv(handle_t ctx, void *ecc_msg) .exit = hpre_exit,\ .send = ecc_send,\ .recv = ecc_recv,\ + .get_usage = hpre_get_usage,\ }
-static struct wd_alg_driver hpre_alg_driver[] = { +static struct wd_alg_driver hpre_ecc_driver[] = { GEN_HPRE_ALG_DRIVER("sm2"), GEN_HPRE_ALG_DRIVER("ecdh"), GEN_HPRE_ALG_DRIVER("ecdsa"), @@ -2463,7 +2470,8 @@ static struct wd_alg_driver hpre_alg_driver[] = { static struct wd_alg_driver hpre_rsa_driver = { .drv_name = "hisi_hpre", .alg_name = "rsa", - .priority = UADK_ALG_HW, + .calc_type = UADK_ALG_HW, + .priority = 100, .priv_size = sizeof(struct hisi_hpre_ctx), .queue_num = HPRE_CTX_Q_NUM_DEF, .op_type_num = 1, @@ -2472,12 +2480,14 @@ static struct wd_alg_driver hpre_rsa_driver = { .exit = hpre_exit, .send = rsa_send, .recv = rsa_recv, + .get_usage = hpre_get_usage, };
static struct wd_alg_driver hpre_dh_driver = { .drv_name = "hisi_hpre", .alg_name = "dh", - .priority = UADK_ALG_HW, + .calc_type = UADK_ALG_HW, + .priority = 100, .priv_size = sizeof(struct hisi_hpre_ctx), .queue_num = HPRE_CTX_Q_NUM_DEF, .op_type_num = 1, @@ -2486,6 +2496,7 @@ static struct wd_alg_driver hpre_dh_driver = { .exit = hpre_exit, .send = dh_send, .recv = dh_recv, + .get_usage = hpre_get_usage, }; static void __attribute__((constructor)) hisi_hpre_probe(void) { diff --git a/drv/hisi_sec.c b/drv/hisi_sec.c index bf05e05..1f5682c 100644 --- a/drv/hisi_sec.c +++ b/drv/hisi_sec.c @@ -517,8 +517,8 @@ static __u32 g_sec_hmac_full_len[WD_DIGEST_TYPE_MAX] = { SEC_HMAC_SHA512_MAC_LEN, SEC_HMAC_SHA512_224_MAC_LEN, SEC_HMAC_SHA512_256_MAC_LEN };
-int hisi_sec_init(void *conf, void *priv); -void hisi_sec_exit(void *priv); +static int hisi_sec_init(void *conf, void *priv); +static void hisi_sec_exit(void *priv);
static int hisi_sec_get_usage(void *param) { @@ -528,8 +528,9 @@ static int hisi_sec_get_usage(void *param) #define GEN_SEC_ALG_DRIVER(sec_alg_name) \ {\ .drv_name = "hisi_sec2",\ - .alg_name = sec_alg_name,\ - .priority = UADK_ALG_HW,\ + .alg_name = (sec_alg_name),\ + .calc_type = UADK_ALG_HW,\ + .priority = 100,\ .priv_size = sizeof(struct hisi_sec_ctx),\ .queue_num = SEC_CTX_Q_NUM_DEF,\ .op_type_num = 1,\ @@ -587,7 +588,6 @@ static struct wd_alg_driver aead_alg_driver[] = { GEN_SEC_ALG_DRIVER("authenc(hmac(sha256),cbc(aes))"), GEN_SEC_ALG_DRIVER("ccm(sm4)"), GEN_SEC_ALG_DRIVER("gcm(sm4)"), - GEN_SEC_ALG_DRIVER("authenc(hmac(sha256),cbc(sm4))"), };
static void dump_sec_msg(void *msg, const char *alg)