Update the current driver adaptation and usage method, from fixed use of Hisilicon device driver to automatic registration of the driver according to the algorithm.
When the algorithm API layer uses the driver, it is no longer bound to the fixed device driver, but dynamically obtained and stored according to the algorithm query to use.
Update the driver and API layer of the zip module, keep the function of the init interface unchanged, update the implementation of the init2 interface and match the dynamic loading function.
liulongfang (4): uadk: Add driver dynamic loading function uadk: added ability to query supported algorithms uadk: improve the dynamic loading public framework uadk/zip: Adapt the zip module to the dynamic loading framework
Makefile.am | 4 +- drv/hisi_comp.c | 101 ++++++++- include/drv/wd_comp_drv.h | 27 --- include/wd.h | 12 + include/wd_alg.h | 85 ++++++++ include/wd_alg_common.h | 10 + include/wd_sched.h | 6 +- include/wd_util.h | 18 +- libwd.map | 8 + wd.c | 56 ++++- wd_alg.c | 265 ++++++++++++++++++++++ wd_comp.c | 160 +++++++------- wd_util.c | 447 +++++++++++++++++++++++++++++++++++++- 13 files changed, 1068 insertions(+), 131 deletions(-) create mode 100644 include/wd_alg.h create mode 100644 wd_alg.c
According to the logical layering of UADK, the device driver has been updated from the previous fixed binding HiSilicon accelerator to the dynamic registration method through the algorithm linked list method.
After the update, it can support the use of instruction acceleration and third-party device drivers.
Signed-off-by: liulongfang liulongfang@huawei.com --- Makefile.am | 4 +- include/wd_alg.h | 85 +++++++++++++ include/wd_alg_common.h | 1 + libwd.map | 8 ++ wd_alg.c | 265 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 361 insertions(+), 2 deletions(-) create mode 100644 include/wd_alg.h create mode 100644 wd_alg.c
diff --git a/Makefile.am b/Makefile.am index 4ef5c2c..5b45626 100644 --- a/Makefile.am +++ b/Makefile.am @@ -33,7 +33,7 @@ AM_CFLAGS+= -DUADK_RELEASED_TIME=""Released ${MONTH} ${DAY}, ${YEAR}"" pkginclude_HEADERS = include/wd.h include/wd_cipher.h include/wd_aead.h \ include/wd_comp.h include/wd_dh.h include/wd_digest.h \ include/wd_rsa.h include/uacce.h include/wd_alg_common.h \ - include/wd_ecc.h include/wd_sched.h + include/wd_ecc.h include/wd_sched.h include/wd_alg.h
nobase_pkginclude_HEADERS = v1/wd.h v1/wd_cipher.h v1/wd_aead.h v1/uacce.h v1/wd_dh.h \ v1/wd_digest.h v1/wd_rsa.h v1/wd_bmm.h @@ -41,7 +41,7 @@ nobase_pkginclude_HEADERS = v1/wd.h v1/wd_cipher.h v1/wd_aead.h v1/uacce.h v1/wd lib_LTLIBRARIES=libwd.la libwd_comp.la libwd_crypto.la libhisi_zip.la \ libhisi_hpre.la libhisi_sec.la
-libwd_la_SOURCES=wd.c wd_mempool.c wd.h \ +libwd_la_SOURCES=wd.c wd_mempool.c wd.h wd_alg.c wd_alg.h \ v1/wd.c v1/wd.h v1/wd_adapter.c v1/wd_adapter.h \ v1/wd_rng.c v1/wd_rng.h \ v1/wd_rsa.c v1/wd_rsa.h \ diff --git a/include/wd_alg.h b/include/wd_alg.h new file mode 100644 index 0000000..965c0aa --- /dev/null +++ b/include/wd_alg.h @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: Apache-2.0 +/* + * Copyright 2020-2021 Huawei Technologies Co.,Ltd. All rights reserved. + * Copyright 2020-2021 Linaro ltd. + */ + +#ifndef __WD_ALG_H +#define __WD_ALG_H +#include <errno.h> +#include <fcntl.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <syslog.h> +#include <unistd.h> +#include <asm/types.h> + +#ifdef __cplusplus +extern "C" { +#endif + +#define handle_t uintptr_t +enum alg_priority { + UADK_ALG_SOFT = 0x0, + UADK_ALG_CE_INSTR = 0x1, + UADK_ALG_SVE_INSTR = 0x2, + UADK_ALG_HW = 0x3 +}; + +/* + * @init: callback interface for initializing device drivers + * @exit: callback interface for destroying device drivers + * @send: callback interface used to send task packets to + * hardware devices. + * @recv: callback interface used to retrieve the calculation + * result of the task packets from the hardware device. + * @get_usage: callback interface used to obtain the + * utilization rate of hardware devices. + */ +struct wd_alg_driver { + const char *drv_name; + const char *alg_name; + int priority; + int queue_num; + int op_type_num; + int priv_size; + handle_t fallback; + + int (*init)(void *conf, void *priv); + void (*exit)(void *priv); + int (*send)(handle_t ctx, void *drv_msg); + int (*recv)(handle_t ctx, void *drv_msg); + int (*get_usage)(void *param); +}; + +int wd_alg_driver_register(struct wd_alg_driver *drv); +void wd_alg_driver_unregister(struct wd_alg_driver *drv); + +struct wd_alg_list { + const char *alg_name; + const char *drv_name; + int priority; + bool available; + int refcnt; + + struct wd_alg_driver *drv; + struct wd_alg_list *next; +}; + +struct wd_alg_driver *wd_request_drv(const char *alg_name, bool hw_mask); +void wd_release_drv(struct wd_alg_driver *drv); + +int wd_drv_alg_support(const char *alg_name, + struct wd_alg_driver *drv); +void wd_enable_drv(struct wd_alg_driver *drv); +void wd_disable_drv(struct wd_alg_driver *drv); + +struct wd_alg_list *wd_get_alg_head(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/wd_alg_common.h b/include/wd_alg_common.h index 7a9b739..6938e50 100644 --- a/include/wd_alg_common.h +++ b/include/wd_alg_common.h @@ -10,6 +10,7 @@ #include <pthread.h> #include <stdbool.h> #include "wd.h" +#include "wd_alg.h"
#ifdef __cplusplus extern "C" { diff --git a/libwd.map b/libwd.map index 459f9ba..5522ec0 100644 --- a/libwd.map +++ b/libwd.map @@ -41,5 +41,13 @@ global: wd_add_dev_to_list; wd_find_dev_by_numa;
+ wd_alg_driver_register; + wd_alg_driver_unregister; + wd_request_drv; + wd_release_drv; + wd_drv_alg_support; + wd_enable_drv; + wd_disable_drv; + wd_get_alg_head; local: *; }; diff --git a/wd_alg.c b/wd_alg.c new file mode 100644 index 0000000..2cc9d1e --- /dev/null +++ b/wd_alg.c @@ -0,0 +1,265 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/* + * Copyright 2020-2021 Huawei Technologies Co.,Ltd. All rights reserved. + * Copyright 2020-2021 Linaro ltd. + */ + +#define _GNU_SOURCE +#include <ctype.h> +#include <dirent.h> +#include <errno.h> +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> +#include <pthread.h> + +#include "wd.h" +#include "wd_alg.h" + +#define SYS_CLASS_DIR "/sys/class/uacce" +static struct wd_alg_list alg_list_head; +static struct wd_alg_list *alg_list_tail = &alg_list_head; + +static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + +bool wd_check_accel_dev(const char *drv_name) +{ + struct dirent *dev_dir; + DIR *wd_class; + + wd_class = opendir(SYS_CLASS_DIR); + if (!wd_class) { + WD_ERR("UADK framework isn't enabled in system!\n"); + return NULL; + } + + while ((dev_dir = readdir(wd_class)) != NULL) { + if (!strncmp(dev_dir->d_name, ".", LINUX_CRTDIR_SIZE) || + !strncmp(dev_dir->d_name, "..", LINUX_PRTDIR_SIZE)) + continue; + + if (!strncmp(dev_dir->d_name, drv_name, strlen(drv_name))) { + closedir(wd_class); + return true; + } + } + closedir(wd_class); + + return false; +} + +int wd_alg_driver_register(struct wd_alg_driver *drv) +{ + struct wd_alg_list *new_alg; + + if (!drv) { + WD_ERR("invalid: register drv is NULL!\n"); + return -WD_ENOMEM; + } + + new_alg = calloc(1, sizeof(struct wd_alg_list)); + if (!new_alg) { + WD_ERR("faild to alloc alg driver memory!\n"); + return -WD_ENOMEM; + } + + new_alg->alg_name = drv->alg_name; + new_alg->drv_name = drv->drv_name; + new_alg->priority = drv->priority; + 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("faild 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; + } + + pthread_mutex_lock(&mutex); + alg_list_tail->next = new_alg; + alg_list_tail = new_alg; + pthread_mutex_unlock(&mutex); + + WD_ERR("Successful to register %s alg!\n", drv->alg_name); + + return 0; +} + +void wd_alg_driver_unregister(struct wd_alg_driver *drv) +{ + struct wd_alg_list *npre = &alg_list_head; + struct wd_alg_list *pnext = npre->next; + + /* Alg driver list has no drivers */ + if (!pnext || !drv) + return; + + pthread_mutex_lock(&mutex); + while (pnext) { + if (!strcmp(drv->alg_name, pnext->alg_name) && + !strcmp(drv->drv_name, pnext->drv_name) && + drv->priority == pnext->priority) { + break; + } + npre = pnext; + pnext = pnext->next; + } + + if (pnext->refcnt > 0) { + WD_ERR("driver<%s> still in used: %d\n", pnext->drv_name, pnext->refcnt); + pthread_mutex_unlock(&mutex); + return; + } + + if (pnext == alg_list_tail) + alg_list_tail = npre; + + npre->next = pnext->next; + free(pnext); + pthread_mutex_unlock(&mutex); + + WD_ERR("Successful to unregister %s alg!\n", drv->alg_name); +} + +struct wd_alg_list *wd_get_alg_head(void) +{ + return &alg_list_head; +} + +int wd_drv_alg_support(const char *alg_name, + struct wd_alg_driver *drv) +{ + struct wd_alg_list *head = &alg_list_head; + struct wd_alg_list *pnext = head->next; + + while (pnext) { + if (!strcmp(alg_name, pnext->alg_name) && + pnext->drv == drv) { + return 0; + } + pnext = pnext->next; + } + + return -WD_EINVAL; +} + +void wd_enable_drv(struct wd_alg_driver *drv) +{ + struct wd_alg_list *head = &alg_list_head; + struct wd_alg_list *pnext = head->next; + + if (!pnext || !drv) + return; + + pthread_mutex_lock(&mutex); + while (pnext) { + if (!strcmp(drv->alg_name, pnext->alg_name) && + !strcmp(drv->drv_name, pnext->drv_name) && + drv->priority == pnext->priority) { + 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; + } + pthread_mutex_unlock(&mutex); +} + +void wd_disable_drv(struct wd_alg_driver *drv) +{ + struct wd_alg_list *head = &alg_list_head; + struct wd_alg_list *pnext = head->next; + + if (!pnext || !drv) + return; + + pthread_mutex_lock(&mutex); + while (pnext) { + if (!strcmp(drv->alg_name, pnext->alg_name) && + !strcmp(drv->drv_name, pnext->drv_name) && + drv->priority == pnext->priority) { + break; + } + pnext = pnext->next; + } + + pnext->available = false; + pthread_mutex_unlock(&mutex); +} + +struct wd_alg_driver *wd_request_drv(const char *alg_name, bool hw_mask) +{ + struct wd_alg_list *head = &alg_list_head; + struct wd_alg_list *pnext = head->next; + struct wd_alg_list *select_node = NULL; + struct wd_alg_driver *drv = NULL; + int tmp_priority = -1; + + if (!pnext || !alg_name) { + WD_ERR("invalid: request alg param is error!\n"); + return NULL; + } + + /* Check the list to get an best driver */ + pthread_mutex_lock(&mutex); + while (pnext) { + /* hw_mask true mean not to used hardware dev */ + if (hw_mask && pnext->drv->priority == UADK_ALG_HW) { + pnext = pnext->next; + continue; + } + + if (!strcmp(alg_name, pnext->alg_name) && pnext->available && + pnext->drv->priority > tmp_priority) { + tmp_priority = pnext->drv->priority; + select_node = pnext; + drv = pnext->drv; + } + pnext = pnext->next; + } + + if (select_node) + select_node->refcnt++; + pthread_mutex_unlock(&mutex); + + return drv; +} + +void wd_release_drv(struct wd_alg_driver *drv) +{ + struct wd_alg_list *head = &alg_list_head; + struct wd_alg_list *pnext = head->next; + + if (!pnext || !drv) + return; + + pthread_mutex_lock(&mutex); + while (pnext) { + if (!strcmp(drv->alg_name, pnext->alg_name) && + !strcmp(drv->drv_name, pnext->drv_name) && + drv->priority == pnext->priority) { + break; + } + pnext = pnext->next; + } + + if (pnext->refcnt > 0) { + pnext->refcnt--; + } + pthread_mutex_unlock(&mutex); +} +
在 2022/11/24 15:43, liulongfang 写道:
According to the logical layering of UADK, the device driver has been updated from the previous fixed binding HiSilicon accelerator to the dynamic registration method through the algorithm linked list method.
After the update, it can support the use of instruction acceleration and third-party device drivers.
Signed-off-by: liulongfang liulongfang@huawei.com
Makefile.am | 4 +- include/wd_alg.h | 85 +++++++++++++ include/wd_alg_common.h | 1 + libwd.map | 8 ++ wd_alg.c | 265 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 361 insertions(+), 2 deletions(-) create mode 100644 include/wd_alg.h create mode 100644 wd_alg.c
diff --git a/Makefile.am b/Makefile.am index 4ef5c2c..5b45626 100644 --- a/Makefile.am +++ b/Makefile.am @@ -33,7 +33,7 @@ AM_CFLAGS+= -DUADK_RELEASED_TIME=""Released ${MONTH} ${DAY}, ${YEAR}"" pkginclude_HEADERS = include/wd.h include/wd_cipher.h include/wd_aead.h \ include/wd_comp.h include/wd_dh.h include/wd_digest.h \ include/wd_rsa.h include/uacce.h include/wd_alg_common.h \
include/wd_ecc.h include/wd_sched.h
include/wd_ecc.h include/wd_sched.h include/wd_alg.h
nobase_pkginclude_HEADERS = v1/wd.h v1/wd_cipher.h v1/wd_aead.h v1/uacce.h v1/wd_dh.h \ v1/wd_digest.h v1/wd_rsa.h v1/wd_bmm.h
@@ -41,7 +41,7 @@ nobase_pkginclude_HEADERS = v1/wd.h v1/wd_cipher.h v1/wd_aead.h v1/uacce.h v1/wd lib_LTLIBRARIES=libwd.la libwd_comp.la libwd_crypto.la libhisi_zip.la \ libhisi_hpre.la libhisi_sec.la
-libwd_la_SOURCES=wd.c wd_mempool.c wd.h \ +libwd_la_SOURCES=wd.c wd_mempool.c wd.h wd_alg.c wd_alg.h \ v1/wd.c v1/wd.h v1/wd_adapter.c v1/wd_adapter.h \ v1/wd_rng.c v1/wd_rng.h \ v1/wd_rsa.c v1/wd_rsa.h \ diff --git a/include/wd_alg.h b/include/wd_alg.h new file mode 100644 index 0000000..965c0aa --- /dev/null +++ b/include/wd_alg.h @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: Apache-2.0 +/*
- Copyright 2020-2021 Huawei Technologies Co.,Ltd. All rights reserved.
- Copyright 2020-2021 Linaro ltd.
2022, why start 2020?
- */
+#ifndef __WD_ALG_H +#define __WD_ALG_H +#include <errno.h> +#include <fcntl.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <syslog.h> +#include <unistd.h> +#include <asm/types.h>
check if header not used
+#ifdef __cplusplus +extern "C" { +#endif
+#define handle_t uintptr_t +enum alg_priority {
- UADK_ALG_SOFT = 0x0,
- UADK_ALG_CE_INSTR = 0x1,
- UADK_ALG_SVE_INSTR = 0x2,
- UADK_ALG_HW = 0x3
+};
+/*
- @init: callback interface for initializing device drivers
- @exit: callback interface for destroying device drivers
- @send: callback interface used to send task packets to
hardware devices.
- @recv: callback interface used to retrieve the calculation
result of the task packets from the hardware device.
- @get_usage: callback interface used to obtain the
utilization rate of hardware devices.
The comment is incomplete.
- */
+struct wd_alg_driver {
- const char *drv_name;
- const char *alg_name;
- int priority;
- int queue_num;
- int op_type_num;
- int priv_size;
- handle_t fallback;
- int (*init)(void *conf, void *priv);
- void (*exit)(void *priv);
- int (*send)(handle_t ctx, void *drv_msg);
- int (*recv)(handle_t ctx, void *drv_msg);
- int (*get_usage)(void *param);
+};
+int wd_alg_driver_register(struct wd_alg_driver *drv); +void wd_alg_driver_unregister(struct wd_alg_driver *drv);
+struct wd_alg_list {
- const char *alg_name;
- const char *drv_name;
- int priority;
these have defined in wd_alg_driver *drv.
- bool available;
- int refcnt;
- struct wd_alg_driver *drv;
- struct wd_alg_list *next;
+};
+struct wd_alg_driver *wd_request_drv(const char *alg_name, bool hw_mask); +void wd_release_drv(struct wd_alg_driver *drv);
+int wd_drv_alg_support(const char *alg_name,
- struct wd_alg_driver *drv);
+void wd_enable_drv(struct wd_alg_driver *drv); +void wd_disable_drv(struct wd_alg_driver *drv);
+struct wd_alg_list *wd_get_alg_head(void);
+#ifdef __cplusplus +} +#endif
+#endif diff --git a/include/wd_alg_common.h b/include/wd_alg_common.h index 7a9b739..6938e50 100644 --- a/include/wd_alg_common.h +++ b/include/wd_alg_common.h @@ -10,6 +10,7 @@ #include <pthread.h> #include <stdbool.h> #include "wd.h" +#include "wd_alg.h"
#ifdef __cplusplus extern "C" { diff --git a/libwd.map b/libwd.map index 459f9ba..5522ec0 100644 --- a/libwd.map +++ b/libwd.map @@ -41,5 +41,13 @@ global: wd_add_dev_to_list; wd_find_dev_by_numa;
- wd_alg_driver_register;
- wd_alg_driver_unregister;
- wd_request_drv;
- wd_release_drv;
- wd_drv_alg_support;
- wd_enable_drv;
- wd_disable_drv;
- wd_get_alg_head; local: *; };
diff --git a/wd_alg.c b/wd_alg.c new file mode 100644 index 0000000..2cc9d1e --- /dev/null +++ b/wd_alg.c @@ -0,0 +1,265 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/*
- Copyright 2020-2021 Huawei Technologies Co.,Ltd. All rights reserved.
- Copyright 2020-2021 Linaro ltd.
Same as above
- */
+#define _GNU_SOURCE +#include <ctype.h> +#include <dirent.h> +#include <errno.h> +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> +#include <pthread.h>
Same as above
+#include "wd.h" +#include "wd_alg.h"
+#define SYS_CLASS_DIR "/sys/class/uacce" +static struct wd_alg_list alg_list_head; +static struct wd_alg_list *alg_list_tail = &alg_list_head;
+static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+bool wd_check_accel_dev(const char *drv_name)
static bool wd_check_accel_dev() ?
+{
- struct dirent *dev_dir;
- DIR *wd_class;
- wd_class = opendir(SYS_CLASS_DIR);
- if (!wd_class) {
WD_ERR("UADK framework isn't enabled in system!\n");
return NULL;
- }
- while ((dev_dir = readdir(wd_class)) != NULL) {
if (!strncmp(dev_dir->d_name, ".", LINUX_CRTDIR_SIZE) ||
!strncmp(dev_dir->d_name, "..", LINUX_PRTDIR_SIZE))
continue;
if (!strncmp(dev_dir->d_name, drv_name, strlen(drv_name))) {
closedir(wd_class);
return true;
}
- }
- closedir(wd_class);
- return false;
+}
+int wd_alg_driver_register(struct wd_alg_driver *drv) +{
- struct wd_alg_list *new_alg;
- if (!drv) {
WD_ERR("invalid: register drv is NULL!\n");
return -WD_ENOMEM;
WD_INVAL maybe better?
- }
- new_alg = calloc(1, sizeof(struct wd_alg_list));
- if (!new_alg) {
WD_ERR("faild to alloc alg driver memory!\n");
return -WD_ENOMEM;
- }
- new_alg->alg_name = drv->alg_name;
- new_alg->drv_name = drv->drv_name;
- new_alg->priority = drv->priority;
- new_alg->drv = drv;
- new_alg->refcnt = 0;
- new_alg->next = NULL;
- if (drv->priority == UADK_ALG_HW) {
Normally use the digit indicate priority. but here use ALG_HW/ ALG_SOFT. So: drv->type == 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("faild 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;
- }
- pthread_mutex_lock(&mutex);
- alg_list_tail->next = new_alg;
- alg_list_tail = new_alg;
- pthread_mutex_unlock(&mutex);
- WD_ERR("Successful to register %s alg!\n", drv->alg_name)
remove or not use WD_ERR
- return 0;
+}
+void wd_alg_driver_unregister(struct wd_alg_driver *drv) +{
- struct wd_alg_list *npre = &alg_list_head;
- struct wd_alg_list *pnext = npre->next;
- /* Alg driver list has no drivers */
- if (!pnext || !drv)
return;
- pthread_mutex_lock(&mutex);
- while (pnext) {
if (!strcmp(drv->alg_name, pnext->alg_name) &&
!strcmp(drv->drv_name, pnext->drv_name) &&
drv->priority == pnext->priority) {
break;
}
npre = pnext;
pnext = pnext->next;
- }
- if (pnext->refcnt > 0) {
WD_ERR("driver<%s> still in used: %d\n", pnext->drv_name, pnext->refcnt);
pthread_mutex_unlock(&mutex);
return;
- }
- if (pnext == alg_list_tail)
alg_list_tail = npre;
- npre->next = pnext->next;
- free(pnext);
- pthread_mutex_unlock(&mutex);
- WD_ERR("Successful to unregister %s alg!\n", drv->alg_name);
+}
+struct wd_alg_list *wd_get_alg_head(void) +{
- return &alg_list_head;
+}
+int wd_drv_alg_support(const char *alg_name,
- struct wd_alg_driver *drv)
+{
- struct wd_alg_list *head = &alg_list_head;
- struct wd_alg_list *pnext = head->next;
- while (pnext) {
if (!strcmp(alg_name, pnext->alg_name) &&
pnext->drv == drv) {
return 0;
true
}
pnext = pnext->next;
- }
- return -WD_EINVAL;
false
+}
+void wd_enable_drv(struct wd_alg_driver *drv) +{
- struct wd_alg_list *head = &alg_list_head;
- struct wd_alg_list *pnext = head->next;
- if (!pnext || !drv)
return;
- pthread_mutex_lock(&mutex);
- while (pnext) {
if (!strcmp(drv->alg_name, pnext->alg_name) &&
!strcmp(drv->drv_name, pnext->drv_name) &&
drv->priority == pnext->priority) {
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;
- }
- pthread_mutex_unlock(&mutex);
+}
+void wd_disable_drv(struct wd_alg_driver *drv) +{
- struct wd_alg_list *head = &alg_list_head;
- struct wd_alg_list *pnext = head->next;
- if (!pnext || !drv)
return;
- pthread_mutex_lock(&mutex);
- while (pnext) {
if (!strcmp(drv->alg_name, pnext->alg_name) &&
!strcmp(drv->drv_name, pnext->drv_name) &&
drv->priority == pnext->priority) {
break;
}
pnext = pnext->next;
- }
- pnext->available = false;
- pthread_mutex_unlock(&mutex);
+}
wd_enable_drv() and wd_disable_drv()
how to use them? if no use need to remove.
+struct wd_alg_driver *wd_request_drv(const char *alg_name, bool hw_mask) +{
- struct wd_alg_list *head = &alg_list_head;
- struct wd_alg_list *pnext = head->next;
- struct wd_alg_list *select_node = NULL;
- struct wd_alg_driver *drv = NULL;
- int tmp_priority = -1;
- if (!pnext || !alg_name) {
WD_ERR("invalid: request alg param is error!\n");
return NULL;
- }
- /* Check the list to get an best driver */
- pthread_mutex_lock(&mutex);
- while (pnext) {
/* hw_mask true mean not to used hardware dev */
if (hw_mask && pnext->drv->priority == UADK_ALG_HW) {
pnext = pnext->next;
continue;
}
if (!strcmp(alg_name, pnext->alg_name) && pnext->available &&
pnext->drv->priority > tmp_priority) {
tmp_priority = pnext->drv->priority;
select_node = pnext;
drv = pnext->drv;
}
pnext = pnext->next;
- }
- if (select_node)
select_node->refcnt++;
- pthread_mutex_unlock(&mutex);
- return drv;
+}
+void wd_release_drv(struct wd_alg_driver *drv) +{
- struct wd_alg_list *head = &alg_list_head;
- struct wd_alg_list *pnext = head->next;
- if (!pnext || !drv)
return;
- pthread_mutex_lock(&mutex);
- while (pnext) {
if (!strcmp(drv->alg_name, pnext->alg_name) &&
!strcmp(drv->drv_name, pnext->drv_name) &&
drv->priority == pnext->priority) {
break;
}
pnext = pnext->next;
- }
- if (pnext->refcnt > 0) {
pnext->refcnt--;
- }
- pthread_mutex_unlock(&mutex);
+}
在 2022/11/25 14:46, fanghao (A) 写道:
在 2022/11/24 15:43, liulongfang 写道:
According to the logical layering of UADK, the device driver has been updated from the previous fixed binding HiSilicon accelerator to the dynamic registration method through the algorithm linked list method.
After the update, it can support the use of instruction acceleration and third-party device drivers.
Signed-off-by: liulongfang liulongfang@huawei.com
Makefile.am | 4 +- include/wd_alg.h | 85 +++++++++++++ include/wd_alg_common.h | 1 + libwd.map | 8 ++ wd_alg.c | 265 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 361 insertions(+), 2 deletions(-) create mode 100644 include/wd_alg.h create mode 100644 wd_alg.c
diff --git a/Makefile.am b/Makefile.am index 4ef5c2c..5b45626 100644 --- a/Makefile.am +++ b/Makefile.am @@ -33,7 +33,7 @@ AM_CFLAGS+= -DUADK_RELEASED_TIME=""Released ${MONTH} ${DAY}, ${YEAR}"" pkginclude_HEADERS = include/wd.h include/wd_cipher.h include/wd_aead.h \ include/wd_comp.h include/wd_dh.h include/wd_digest.h \ include/wd_rsa.h include/uacce.h include/wd_alg_common.h \ - include/wd_ecc.h include/wd_sched.h + include/wd_ecc.h include/wd_sched.h include/wd_alg.h nobase_pkginclude_HEADERS = v1/wd.h v1/wd_cipher.h v1/wd_aead.h v1/uacce.h v1/wd_dh.h \ v1/wd_digest.h v1/wd_rsa.h v1/wd_bmm.h @@ -41,7 +41,7 @@ nobase_pkginclude_HEADERS = v1/wd.h v1/wd_cipher.h v1/wd_aead.h v1/uacce.h v1/wd lib_LTLIBRARIES=libwd.la libwd_comp.la libwd_crypto.la libhisi_zip.la \ libhisi_hpre.la libhisi_sec.la -libwd_la_SOURCES=wd.c wd_mempool.c wd.h \ +libwd_la_SOURCES=wd.c wd_mempool.c wd.h wd_alg.c wd_alg.h \ v1/wd.c v1/wd.h v1/wd_adapter.c v1/wd_adapter.h \ v1/wd_rng.c v1/wd_rng.h \ v1/wd_rsa.c v1/wd_rsa.h \ diff --git a/include/wd_alg.h b/include/wd_alg.h new file mode 100644 index 0000000..965c0aa --- /dev/null +++ b/include/wd_alg.h @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: Apache-2.0 +/*
- Copyright 2020-2021 Huawei Technologies Co.,Ltd. All rights reserved.
- Copyright 2020-2021 Linaro ltd.
2022, why start 2020?
- */
+#ifndef __WD_ALG_H +#define __WD_ALG_H +#include <errno.h> +#include <fcntl.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <syslog.h> +#include <unistd.h> +#include <asm/types.h>
check if header not used
+#ifdef __cplusplus +extern "C" { +#endif
+#define handle_t uintptr_t +enum alg_priority { + UADK_ALG_SOFT = 0x0, + UADK_ALG_CE_INSTR = 0x1, + UADK_ALG_SVE_INSTR = 0x2, + UADK_ALG_HW = 0x3 +};
+/*
- @init: callback interface for initializing device drivers
- @exit: callback interface for destroying device drivers
- @send: callback interface used to send task packets to
- * hardware devices.
- @recv: callback interface used to retrieve the calculation
- * result of the task packets from the hardware device.
- @get_usage: callback interface used to obtain the
- * utilization rate of hardware devices.
The comment is incomplete.
- */
+struct wd_alg_driver { + const char *drv_name; + const char *alg_name; + int priority; + int queue_num; + int op_type_num; + int priv_size; + handle_t fallback;
+ int (*init)(void *conf, void *priv); + void (*exit)(void *priv); + int (*send)(handle_t ctx, void *drv_msg); + int (*recv)(handle_t ctx, void *drv_msg); + int (*get_usage)(void *param); +};
+int wd_alg_driver_register(struct wd_alg_driver *drv); +void wd_alg_driver_unregister(struct wd_alg_driver *drv);
+struct wd_alg_list { + const char *alg_name; + const char *drv_name; + int priority;
these have defined in wd_alg_driver *drv.
You can see this by trying to change it after you read it yourself. The list operation without this part of ALG will be particularly complicated.
I currently keep it to ensure the simplicity and efficiency of list queries.
Thanks, Longfang.
+ bool available; + int refcnt;
+ struct wd_alg_driver *drv; + struct wd_alg_list *next; +};
+struct wd_alg_driver *wd_request_drv(const char *alg_name, bool hw_mask); +void wd_release_drv(struct wd_alg_driver *drv);
+int wd_drv_alg_support(const char *alg_name, + struct wd_alg_driver *drv); +void wd_enable_drv(struct wd_alg_driver *drv); +void wd_disable_drv(struct wd_alg_driver *drv);
+struct wd_alg_list *wd_get_alg_head(void);
+#ifdef __cplusplus +} +#endif
+#endif diff --git a/include/wd_alg_common.h b/include/wd_alg_common.h index 7a9b739..6938e50 100644 --- a/include/wd_alg_common.h +++ b/include/wd_alg_common.h @@ -10,6 +10,7 @@ #include <pthread.h> #include <stdbool.h> #include "wd.h" +#include "wd_alg.h" #ifdef __cplusplus extern "C" { diff --git a/libwd.map b/libwd.map index 459f9ba..5522ec0 100644 --- a/libwd.map +++ b/libwd.map @@ -41,5 +41,13 @@ global: wd_add_dev_to_list; wd_find_dev_by_numa; + wd_alg_driver_register; + wd_alg_driver_unregister; + wd_request_drv; + wd_release_drv; + wd_drv_alg_support; + wd_enable_drv; + wd_disable_drv; + wd_get_alg_head; local: *; }; diff --git a/wd_alg.c b/wd_alg.c new file mode 100644 index 0000000..2cc9d1e --- /dev/null +++ b/wd_alg.c @@ -0,0 +1,265 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/*
- Copyright 2020-2021 Huawei Technologies Co.,Ltd. All rights reserved.
- Copyright 2020-2021 Linaro ltd.
Same as above
- */
+#define _GNU_SOURCE +#include <ctype.h> +#include <dirent.h> +#include <errno.h> +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> +#include <pthread.h>
Same as above
+#include "wd.h" +#include "wd_alg.h"
+#define SYS_CLASS_DIR "/sys/class/uacce" +static struct wd_alg_list alg_list_head; +static struct wd_alg_list *alg_list_tail = &alg_list_head;
+static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+bool wd_check_accel_dev(const char *drv_name)
static bool wd_check_accel_dev() ?
+{ + struct dirent *dev_dir; + DIR *wd_class;
+ wd_class = opendir(SYS_CLASS_DIR); + if (!wd_class) { + WD_ERR("UADK framework isn't enabled in system!\n"); + return NULL; + }
+ while ((dev_dir = readdir(wd_class)) != NULL) { + if (!strncmp(dev_dir->d_name, ".", LINUX_CRTDIR_SIZE) || + !strncmp(dev_dir->d_name, "..", LINUX_PRTDIR_SIZE)) + continue;
+ if (!strncmp(dev_dir->d_name, drv_name, strlen(drv_name))) { + closedir(wd_class); + return true; + } + } + closedir(wd_class);
+ return false; +}
+int wd_alg_driver_register(struct wd_alg_driver *drv) +{ + struct wd_alg_list *new_alg;
+ if (!drv) { + WD_ERR("invalid: register drv is NULL!\n"); + return -WD_ENOMEM;
WD_INVAL maybe better?
+ }
+ new_alg = calloc(1, sizeof(struct wd_alg_list)); + if (!new_alg) { + WD_ERR("faild to alloc alg driver memory!\n"); + return -WD_ENOMEM; + }
+ new_alg->alg_name = drv->alg_name; + new_alg->drv_name = drv->drv_name; + new_alg->priority = drv->priority; + new_alg->drv = drv; + new_alg->refcnt = 0; + new_alg->next = NULL;
+ if (drv->priority == UADK_ALG_HW) {
Normally use the digit indicate priority. but here use ALG_HW/ ALG_SOFT. So: drv->type == 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("faild 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; + }
+ pthread_mutex_lock(&mutex); + alg_list_tail->next = new_alg; + alg_list_tail = new_alg; + pthread_mutex_unlock(&mutex);
+ WD_ERR("Successful to register %s alg!\n", drv->alg_name)
remove or not use WD_ERR
+ return 0; +}
+void wd_alg_driver_unregister(struct wd_alg_driver *drv) +{ + struct wd_alg_list *npre = &alg_list_head; + struct wd_alg_list *pnext = npre->next;
+ /* Alg driver list has no drivers */ + if (!pnext || !drv) + return;
+ pthread_mutex_lock(&mutex); + while (pnext) { + if (!strcmp(drv->alg_name, pnext->alg_name) && + !strcmp(drv->drv_name, pnext->drv_name) && + drv->priority == pnext->priority) { + break; + } + npre = pnext; + pnext = pnext->next; + }
+ if (pnext->refcnt > 0) { + WD_ERR("driver<%s> still in used: %d\n", pnext->drv_name, pnext->refcnt); + pthread_mutex_unlock(&mutex); + return; + }
+ if (pnext == alg_list_tail) + alg_list_tail = npre;
+ npre->next = pnext->next; + free(pnext); + pthread_mutex_unlock(&mutex);
+ WD_ERR("Successful to unregister %s alg!\n", drv->alg_name); +}
+struct wd_alg_list *wd_get_alg_head(void) +{ + return &alg_list_head; +}
+int wd_drv_alg_support(const char *alg_name, + struct wd_alg_driver *drv) +{ + struct wd_alg_list *head = &alg_list_head; + struct wd_alg_list *pnext = head->next;
+ while (pnext) { + if (!strcmp(alg_name, pnext->alg_name) && + pnext->drv == drv) { + return 0;
true
+ } + pnext = pnext->next; + }
+ return -WD_EINVAL;
false
+}
+void wd_enable_drv(struct wd_alg_driver *drv) +{ + struct wd_alg_list *head = &alg_list_head; + struct wd_alg_list *pnext = head->next;
+ if (!pnext || !drv) + return;
+ pthread_mutex_lock(&mutex); + while (pnext) { + if (!strcmp(drv->alg_name, pnext->alg_name) && + !strcmp(drv->drv_name, pnext->drv_name) && + drv->priority == pnext->priority) { + 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; + } + pthread_mutex_unlock(&mutex); +}
+void wd_disable_drv(struct wd_alg_driver *drv) +{ + struct wd_alg_list *head = &alg_list_head; + struct wd_alg_list *pnext = head->next;
+ if (!pnext || !drv) + return;
+ pthread_mutex_lock(&mutex); + while (pnext) { + if (!strcmp(drv->alg_name, pnext->alg_name) && + !strcmp(drv->drv_name, pnext->drv_name) && + drv->priority == pnext->priority) { + break; + } + pnext = pnext->next; + }
+ pnext->available = false; + pthread_mutex_unlock(&mutex); +}
wd_enable_drv() and wd_disable_drv()
how to use them? if no use need to remove.
+struct wd_alg_driver *wd_request_drv(const char *alg_name, bool hw_mask) +{ + struct wd_alg_list *head = &alg_list_head; + struct wd_alg_list *pnext = head->next; + struct wd_alg_list *select_node = NULL; + struct wd_alg_driver *drv = NULL; + int tmp_priority = -1;
+ if (!pnext || !alg_name) { + WD_ERR("invalid: request alg param is error!\n"); + return NULL; + }
+ /* Check the list to get an best driver */ + pthread_mutex_lock(&mutex); + while (pnext) { + /* hw_mask true mean not to used hardware dev */ + if (hw_mask && pnext->drv->priority == UADK_ALG_HW) { + pnext = pnext->next; + continue; + }
+ if (!strcmp(alg_name, pnext->alg_name) && pnext->available && + pnext->drv->priority > tmp_priority) { + tmp_priority = pnext->drv->priority; + select_node = pnext; + drv = pnext->drv; + } + pnext = pnext->next; + }
+ if (select_node) + select_node->refcnt++; + pthread_mutex_unlock(&mutex);
+ return drv; +}
+void wd_release_drv(struct wd_alg_driver *drv) +{ + struct wd_alg_list *head = &alg_list_head; + struct wd_alg_list *pnext = head->next;
+ if (!pnext || !drv) + return;
+ pthread_mutex_lock(&mutex); + while (pnext) { + if (!strcmp(drv->alg_name, pnext->alg_name) && + !strcmp(drv->drv_name, pnext->drv_name) && + drv->priority == pnext->priority) { + break; + } + pnext = pnext->next; + }
+ if (pnext->refcnt > 0) { + pnext->refcnt--; + } + pthread_mutex_unlock(&mutex); +}
.
After the driver dynamic loading function is added, the corresponding function of querying all algorithms supported on the current UADK is added.
Signed-off-by: liulongfang liulongfang@huawei.com --- include/wd.h | 12 +++++++++++ wd.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-)
diff --git a/include/wd.h b/include/wd.h index 136d661..f2b7c06 100644 --- a/include/wd.h +++ b/include/wd.h @@ -29,6 +29,7 @@ extern "C" { #define LINUX_PRTDIR_SIZE 2 #define WD_CTX_CNT_NUM 1024 #define WD_IPC_KEY 0x500011 +#define CRYPTO_MAX_ALG_NAME 128
/* Required compiler attributes */ #define likely(x) __builtin_expect(!!(x), 1) @@ -618,6 +619,17 @@ bool wd_need_debug(void); */ bool wd_need_info(void);
+struct wd_capability { + char alg_name[CRYPTO_MAX_ALG_NAME]; + char drv_name[CRYPTO_MAX_ALG_NAME]; + int priority; + + struct wd_capability *next; +}; + +struct wd_capability *wd_get_alg_cap(void); +void wd_release_alg_cap(struct wd_capability *head); + #ifdef __cplusplus } #endif diff --git a/wd.c b/wd.c index 629c0df..c882c1f 100644 --- a/wd.c +++ b/wd.c @@ -19,7 +19,7 @@ #include <sched.h>
#include "wd.h" - +#include "wd_alg.h" #define SYS_CLASS_DIR "/sys/class/uacce"
enum UADK_LOG_LEVEL { @@ -882,3 +882,57 @@ char *wd_ctx_get_dev_name(handle_t h_ctx)
return ctx->dev_name; } + +void wd_release_alg_cap(struct wd_capability *head) +{ + struct wd_capability *cap_pnext = head; + struct wd_capability *cap_node = NULL; + + while (cap_pnext) { + cap_node = cap_pnext; + cap_pnext = cap_pnext->next; + free(cap_node); + } + + if (head) + free(head); +} + +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; + + while (pnext) { + cap_node = calloc(1, sizeof(struct wd_capability)); + if (!cap_head) { + 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->priority = pnext->priority; + cap_node->next = NULL; + + cap_pnext->next = cap_node; + cap_pnext = cap_node; + pnext = pnext->next; + if (i == 0) { + cap_head = cap_node; + cap_pnext = cap_head; + } + i++; + } + + return cap_head; + +alloc_err: + wd_release_alg_cap(cap_head); + return NULL; +} +
在 2022/11/24 15:43, liulongfang 写道:
After the driver dynamic loading function is added, the corresponding function of querying all algorithms supported on the current UADK is added.
Signed-off-by: liulongfang liulongfang@huawei.com
include/wd.h | 12 +++++++++++ wd.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-)
diff --git a/include/wd.h b/include/wd.h index 136d661..f2b7c06 100644 --- a/include/wd.h +++ b/include/wd.h @@ -29,6 +29,7 @@ extern "C" { #define LINUX_PRTDIR_SIZE 2 #define WD_CTX_CNT_NUM 1024 #define WD_IPC_KEY 0x500011 +#define CRYPTO_MAX_ALG_NAME 128
/* Required compiler attributes */ #define likely(x) __builtin_expect(!!(x), 1) @@ -618,6 +619,17 @@ bool wd_need_debug(void); */ bool wd_need_info(void);
+struct wd_capability {
- char alg_name[CRYPTO_MAX_ALG_NAME];
- char drv_name[CRYPTO_MAX_ALG_NAME];
- int priority;
- struct wd_capability *next;
+};
+struct wd_capability *wd_get_alg_cap(void); +void wd_release_alg_cap(struct wd_capability *head);
- #ifdef __cplusplus } #endif
diff --git a/wd.c b/wd.c index 629c0df..c882c1f 100644 --- a/wd.c +++ b/wd.c @@ -19,7 +19,7 @@ #include <sched.h>
#include "wd.h"
+#include "wd_alg.h" #define SYS_CLASS_DIR "/sys/class/uacce"
enum UADK_LOG_LEVEL { @@ -882,3 +882,57 @@ char *wd_ctx_get_dev_name(handle_t h_ctx)
return ctx->dev_name; }
+void wd_release_alg_cap(struct wd_capability *head) +{
- struct wd_capability *cap_pnext = head;
- struct wd_capability *cap_node = NULL;
- while (cap_pnext) {
cap_node = cap_pnext;
cap_pnext = cap_pnext->next;
free(cap_node);
- }
- if (head)
free(head);
+}
+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;
- while (pnext) {
cap_node = calloc(1, sizeof(struct wd_capability));
if (!cap_head) {
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->priority = pnext->priority;
cap_node->next = NULL;
cap_pnext->next = cap_node;
cap_pnext = cap_node;
pnext = pnext->next;
if (i == 0) {
cap_head = cap_node;
cap_pnext = cap_head;
}
i++;
- }
- return cap_head;
+alloc_err:
- wd_release_alg_cap(cap_head);
- return NULL;
+}
I just guess these functions just for dfx, and called by uadk_tool. Suggest add complete dfx series later.
These functions need move to wd alg.
After the dynamic loading function is added, device resource initialization, driver acquisition, and scheduler initialization functions need to be extracted into the public framework.
so that the algorithm can quickly adapt to the dynamic loading function internally.
Signed-off-by: liulongfang liulongfang@huawei.com --- include/wd_alg_common.h | 9 + include/wd_sched.h | 6 +- include/wd_util.h | 18 +- wd_comp.c | 2 +- wd_util.c | 447 +++++++++++++++++++++++++++++++++++++++- 5 files changed, 477 insertions(+), 5 deletions(-)
diff --git a/include/wd_alg_common.h b/include/wd_alg_common.h index 6938e50..b96e6d8 100644 --- a/include/wd_alg_common.h +++ b/include/wd_alg_common.h @@ -28,6 +28,12 @@ extern "C" { #define CTX_TYPE_INVALID 9999 #define POLL_TIME 1000
+enum alg_task_type { + TASK_MIX = 0x0, + TASK_HW, + TASK_INSTR, +}; + /** * struct wd_ctx_nums - Define the ctx sets numbers. * @sync_ctx_num: The ctx numbers which are used for sync mode for each @@ -80,6 +86,9 @@ struct wd_sched { handle_t h_sched_ctx; };
+typedef int (*wd_alg_init)(struct wd_ctx_config *config, struct wd_sched *sched); +typedef int (*wd_alg_poll_ctx)(__u32 idx, __u32 expt, __u32 *count); + #ifdef __cplusplus } #endif diff --git a/include/wd_sched.h b/include/wd_sched.h index 2ae6103..a492d70 100644 --- a/include/wd_sched.h +++ b/include/wd_sched.h @@ -18,7 +18,11 @@ extern "C" { enum sched_policy_type { /* requests will be sent to ctxs one by one */ SCHED_POLICY_RR = 0, - SCHED_POLICY_BUTT + /* requests will no need ctxs */ + SCHED_POLICY_NONE, + /* requests will need a fixed ctx */ + SCHED_POLICY_SINGLE, + SCHED_POLICY_BUTT, };
struct sched_params { diff --git a/include/wd_util.h b/include/wd_util.h index 9b581d4..67d8b62 100644 --- a/include/wd_util.h +++ b/include/wd_util.h @@ -133,9 +133,12 @@ struct wd_msg_handle { struct wd_init_attrs { __u32 sched_type; char *alg; + struct wd_alg_driver *driver; struct wd_sched *sched; struct wd_ctx_params *ctx_params; struct wd_ctx_config *ctx_config; + wd_alg_init alg_init; + wd_alg_poll_ctx alg_poll_ctx; };
/* @@ -431,13 +434,24 @@ static inline void wd_alg_clear_init(enum wd_status *status) }
/** - * wd_alg_pre_init() - Request the ctxs and initialize the sched_domain + * wd_alg_attrs_init() - Request the ctxs and initialize the sched_domain * with the given devices list, ctxs number and numa mask. * @attrs: the algorithm initialization parameters. * * Return device if succeed and other error number if fail. */ -int wd_alg_pre_init(struct wd_init_attrs *attrs); +int wd_alg_attrs_init(struct wd_init_attrs *alg_init_attrs); +void wd_alg_attrs_uninit(struct wd_init_attrs *attrs); + +struct wd_alg_driver *wd_alg_drv_request(int task_type, char *alg_name); +int wd_alg_init_driver(struct wd_ctx_config_internal *config, + struct wd_alg_driver *driver, void **drv_priv); +void wd_alg_uninit_driver(struct wd_ctx_config_internal *config, + struct wd_alg_driver *driver, void *drv_priv); + +void *wd_dlopen_drv(const char *cust_lib_dir); +int wd_dlclose_drv(void *dlh_list); +void wd_get_alg_type(const char *alg_name, char *alg_type);
/** * wd_dfx_msg_cnt() - Message counter interface for ctx diff --git a/wd_comp.c b/wd_comp.c index cd938d9..bb142a7 100644 --- a/wd_comp.c +++ b/wd_comp.c @@ -230,7 +230,7 @@ int wd_comp_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_par wd_comp_sched->name = SCHED_RR_NAME; wd_comp_init_attrs.sched = wd_comp_sched;
- ret = wd_alg_pre_init(&wd_comp_init_attrs); + ret = wd_alg_attrs_init(&wd_comp_init_attrs); if (ret) goto out_freesched;
diff --git a/wd_util.c b/wd_util.c index 433dd56..bc97f6e 100644 --- a/wd_util.c +++ b/wd_util.c @@ -5,6 +5,8 @@ */
#define _GNU_SOURCE +#include <dirent.h> +#include <dlfcn.h> #include <pthread.h> #include <semaphore.h> #include <string.h> @@ -23,6 +25,8 @@ #define WD_INIT_SLEEP_UTIME 1000 #define WD_INIT_RETRY_TIMES 10000
+#define DRV_LIB_DIR "/root/lib" + struct msg_pool { /* message array allocated dynamically */ void *msgs; @@ -64,6 +68,72 @@ struct async_task_queue { int (*alg_poll_ctx)(__u32, __u32, __u32 *); };
+struct drv_lib_list { + void *dlhandle; + struct drv_lib_list *next; +}; + +struct acc_alg_item { + char *name; + char *algtype; +}; + +static struct acc_alg_item alg_options[] = { + {"zlib", "zlib-deflate"}, + {"gzip", "gzip"}, + {"deflate", "deflate"}, + {"lz77_zstd", "lz77_zstd"}, + + {"rsa", "rsa"}, + {"dh", "dh"}, + {"ecdh", "ecdh"}, + {"x25519", "x25519"}, + {"x448", "x448"}, + {"ecdsa", "ecdsa"}, + {"sm2", "sm2"}, + + {"ecb(aes)", "cipher"}, + {"cbc(aes)", "cipher"}, + {"xts(aes)", "cipher"}, + {"ofb(aes)", "cipher"}, + {"cfb(aes)", "cipher"}, + {"ctr(aes)", "cipher"}, + {"cbc-cs1(aes)", "cipher"}, + {"cbc-cs2(aes)", "cipher"}, + {"cbc-cs3(aes)", "cipher"}, + {"ecb(sm4)", "cipher"}, + {"xts(sm4)", "cipher"}, + {"cbc(sm4)", "cipher"}, + {"ofb(sm4)", "cipher"}, + {"cfb(sm4)", "cipher"}, + {"ctr(sm4)", "cipher"}, + {"cbc-cs1(sm4)", "cipher"}, + {"cbc-cs2(sm4)", "cipher"}, + {"cbc-cs3(sm4)", "cipher"}, + {"ecb(des)", "cipher"}, + {"cbc(des)", "cipher"}, + {"ecb(des3_ede)", "cipher"}, + {"cbc(des3_ede)", "cipher"}, + + {"ccm(aes)", "aead"}, + {"gcm(aes)", "aead"}, + {"ccm(sm4)", "aead"}, + {"gcm(sm4)", "aead"}, + {"authenc(hmac(sha256),cbc(aes))", "aead"}, + {"authenc(hmac(sha256),cbc(sm4))", "aead"}, + + {"sm3", "digest"}, + {"md5", "digest"}, + {"sha1", "digest"}, + {"sha256", "digest"}, + {"sha224", "digest"}, + {"sha384", "digest"}, + {"sha512", "digest"}, + {"sha512-224", "digest"}, + {"sha512-256", "digest"}, + {"", ""} +}; + static void clone_ctx_to_internal(struct wd_ctx *ctx, struct wd_ctx_internal *ctx_in) { @@ -1779,6 +1849,257 @@ int wd_init_param_check(struct wd_ctx_config *config, struct wd_sched *sched) return 0; }
+void wd_get_alg_type(const char *alg_name, char *alg_type) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(alg_options); i++) { + if (strcmp(alg_name, alg_options[i].name) == 0) { + (void)strcpy(alg_type, alg_options[i].algtype); + break; + } + } +} + +/** + * There are many other .so files in this file directory (/root/lib/), + * and it is necessary to screen out valid uadk driver files + * through this function. + */ +static int file_check_valid(char *lib_file) +{ +#define FILE_HEAD_SZ 4 +#define FILE_TAIL_SZ 4 + int file_len = strlen(lib_file); + char file_head[FILE_HEAD_SZ] = "lib"; + char file_tail[FILE_TAIL_SZ] = ".so"; + int i, j; + + /* Lib file name is libxx_xxx.so */ + for (i = 0; i < FILE_HEAD_SZ - 1; i++) { + if (lib_file[i] != file_head[i]) + return -EINVAL; + } + + for (i = file_len - (FILE_TAIL_SZ - 1), j = 0; + i < file_len && j < FILE_TAIL_SZ; i++, j++) { + if (lib_file[i] != file_tail[j]) + return -EINVAL; + } + + return 0; +} + +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"); + return -WD_EINVAL; + } + + fb_driver->init(NULL, NULL); + + return 0; +} + +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"); + return; + } + + fb_driver->exit(NULL); +} + +int wd_alg_init_driver(struct wd_ctx_config_internal *config, + struct wd_alg_driver *driver, void **drv_priv) +{ + void *priv; + int ret; + + /* Init ctx related resources in specific driver */ + priv = calloc(1, driver->priv_size); + if (!priv) + return -WD_ENOMEM; + + if (!driver->init) { + driver->fallback = 0; + WD_ERR("driver have no init interface.\n"); + ret = -WD_EINVAL; + goto err_alloc; + } + + ret = driver->init(config, priv); + if (ret < 0) { + WD_ERR("driver init failed.\n"); + goto err_alloc; + } + + if (driver->fallback) { + ret = wd_alg_init_fallback((struct wd_alg_driver *)driver->fallback); + if (ret) { + driver->fallback = 0; + WD_ERR("soft alg driver init failed.\n"); + } + } + *drv_priv = priv; + + return 0; + +err_alloc: + free(priv); + return ret; +} + +void wd_alg_uninit_driver(struct wd_ctx_config_internal *config, + struct wd_alg_driver *driver, void *drv_priv) +{ + driver->exit(drv_priv); + /* Ctx config just need clear once */ + if (driver->priority == UADK_ALG_HW) + wd_clear_ctx_config(config); + + if (driver->fallback) + wd_alg_uninit_fallback((struct wd_alg_driver *)driver->fallback); +} + +int wd_dlclose_drv(void *dlh_list) +{ + struct drv_lib_list *dlhead = (struct drv_lib_list *)dlh_list; + struct drv_lib_list *dlnode; + + if (!dlhead) + return -WD_EINVAL; + + while (dlhead) { + dlnode = dlhead; + dlhead = dlhead->next; + dlclose(dlnode); + } + + return 0; +} + +static void add_lib_to_list(struct drv_lib_list *head, + struct drv_lib_list *node) +{ + struct drv_lib_list *tmp = head; + + while (tmp->next) + tmp = tmp->next; + + tmp->next = node; +} + +void *wd_dlopen_drv(const char *cust_lib_dir) +{ + typedef int (*alg_ops)(struct wd_alg_driver *drv); + struct drv_lib_list *node, *head = NULL; + char lib_dir_path[PATH_STR_SIZE]; + char lib_path[PATH_STR_SIZE]; + struct dirent *lib_dir; + alg_ops dl_func = NULL; + DIR *wd_dir; + int ret; + + if (!cust_lib_dir) + strncpy(lib_dir_path, DRV_LIB_DIR, PATH_STR_SIZE - 1); + else + strncpy(lib_dir_path, cust_lib_dir, PATH_STR_SIZE - 1); + + wd_dir = opendir(lib_dir_path); + if (!wd_dir) { + WD_ERR("UADK driver lib file: %s not exist!\n", lib_dir_path); + return NULL; + } + + while ((lib_dir = readdir(wd_dir)) != NULL) { + if (!strncmp(lib_dir->d_name, ".", LINUX_CRTDIR_SIZE) || + !strncmp(lib_dir->d_name, "..", LINUX_PRTDIR_SIZE)) + continue; + + ret = file_check_valid(lib_dir->d_name); + if (ret) + continue; + + node = calloc(1, sizeof(*node)); + if (!node) + goto free_list; + + ret = snprintf(lib_path, PATH_STR_SIZE, "%s/%s", lib_dir_path, lib_dir->d_name); + if (ret < 0) + goto free_list; + + node->dlhandle = dlopen(lib_path, RTLD_NOW); + if (!node->dlhandle) { + WD_ERR("fail to open lib file: %s\n", dlerror()); + /* there are many other files need to skip */ + continue; + } + + dl_func = dlsym(node->dlhandle, "wd_alg_driver_register"); + if (dl_func == NULL) { + dlclose(node->dlhandle); + continue; + } + + if (!head) + head = node; + else + add_lib_to_list(head, node); + } + closedir(wd_dir); + + return (void *)head; + +free_list: + closedir(wd_dir); + wd_dlclose_drv(head); + return NULL; +} + +struct wd_alg_driver *wd_alg_drv_request(int task_type, char *alg_name) +{ + struct wd_alg_driver *set_driver = NULL; + struct wd_alg_driver *drv; + + /* Get alg driver and dev name */ + switch (task_type) { + case TASK_INSTR: + drv = wd_request_drv(alg_name, true); + if (!drv) { + WD_ERR("no soft %s driver support\n", alg_name); + return NULL; + } + set_driver = drv; + set_driver->fallback = 0; + break; + case TASK_HW: + case TASK_MIX: + drv = wd_request_drv(alg_name, false); + if (!drv) { + WD_ERR("no HW %s driver support\n", alg_name); + return NULL; + } + set_driver = drv; + set_driver->fallback = 0; + if (task_type == TASK_MIX) { + drv = wd_request_drv(alg_name, true); + if (!drv) { + set_driver->fallback = 0; + WD_ERR("no soft %s driver support\n", alg_name); + } else { + set_driver->fallback = (handle_t)drv; + WD_ERR("successful to get soft driver\n"); + } + } + break; + } + + return set_driver; +} + bool wd_alg_try_init(enum wd_status *status) { enum wd_status expected; @@ -1960,7 +2281,7 @@ free_ctxs: return ret; }
-int wd_alg_pre_init(struct wd_init_attrs *attrs) +static int wd_alg_ctx_init(struct wd_init_attrs *attrs) { struct wd_ctx_config *ctx_config = attrs->ctx_config; struct wd_ctx_params *ctx_params = attrs->ctx_params; @@ -2034,3 +2355,127 @@ out_freelist:
return ret; } + +static void wd_alg_ctx_uninit(struct wd_ctx_config *ctx_config) +{ + int i; + + for (i = 0; i < ctx_config->ctx_num; i++) + if (ctx_config->ctxs[i].ctx) { + wd_release_ctx(ctx_config->ctxs[i].ctx); + ctx_config->ctxs[i].ctx = 0; + } + + free(ctx_config->ctxs); +} + +int wd_alg_attrs_init(struct wd_init_attrs *alg_init_attrs) +{ + wd_alg_poll_ctx alg_poll_func = alg_init_attrs->alg_poll_ctx; + wd_alg_init alg_init_func = alg_init_attrs->alg_init; + struct wd_alg_driver *driver = alg_init_attrs->driver; + __u32 sched_type = alg_init_attrs->sched_type; + char *alg = alg_init_attrs->alg; + struct wd_ctx_config *ctx_config = NULL; + struct wd_sched *alg_sched = NULL; + char alg_type[WD_NAME_SIZE]; + __u32 op_type_num; + int ret; + + if (alg_init_attrs->ctx_params) + return -WD_EINVAL; + + op_type_num = alg_init_attrs->ctx_params->op_type_num; + + switch (driver->priority) { + case UADK_ALG_SOFT: + case UADK_ALG_CE_INSTR: + /* No need to alloc resource */ + if (sched_type != SCHED_POLICY_NONE) + return -WD_EINVAL; + + alg_sched = wd_sched_rr_alloc(SCHED_POLICY_NONE, 1, 1, alg_poll_func); + if (!alg_sched) { + WD_ERR("fail to alloc scheduler\n"); + return -WD_EINVAL; + } + + ret = wd_sched_rr_instance(alg_sched, NULL); + if (ret) { + WD_ERR("fail to instance scheduler\n"); + goto out_freesched; + } + break; + case UADK_ALG_SVE_INSTR: + /* Todo lock cpu core */ + if (sched_type != SCHED_POLICY_SINGLE) + return -WD_EINVAL; + + alg_sched = wd_sched_rr_alloc(SCHED_POLICY_SINGLE, 1, 1, alg_poll_func); + if (!alg_sched) { + WD_ERR("fail to alloc scheduler\n"); + return -WD_EINVAL; + } + + ret = wd_sched_rr_instance(alg_sched, NULL); + if (ret) { + WD_ERR("fail to instance scheduler\n"); + goto out_freesched; + } + break; + case UADK_ALG_HW: + wd_get_alg_type(alg, alg_type); + alg_init_attrs->alg = alg_type; + + ctx_config = calloc(1, sizeof(*ctx_config)); + if (!ctx_config) { + WD_ERR("fail to alloc ctx config\n"); + return -WD_ENOMEM; + } + + alg_sched = wd_sched_rr_alloc(sched_type, op_type_num, + numa_max_node() + 1, alg_poll_func); + if (!alg_sched) { + WD_ERR("fail to instance scheduler\n"); + ret = -WD_EINVAL; + goto out_ctx_config; + } + + ret = wd_alg_ctx_init(alg_init_attrs); + if (ret) { + WD_ERR("fail to init ctx\n"); + goto out_freesched; + } + + ret = alg_init_func(ctx_config, alg_sched); + if (ret) + goto out_pre_init; + } + + alg_init_attrs->ctx_config = ctx_config; + alg_init_attrs->sched = alg_sched; + + return 0; + +out_pre_init: + wd_alg_ctx_uninit(ctx_config); +out_freesched: + wd_sched_rr_release(alg_sched); +out_ctx_config: + if (ctx_config) + free(ctx_config); + return ret; +} + +void wd_alg_attrs_uninit(struct wd_init_attrs *attrs) +{ + struct wd_ctx_config *ctx_config = attrs->ctx_config; + struct wd_sched *alg_sched = attrs->sched; + + if (ctx_config) { + wd_alg_ctx_uninit(ctx_config); + free(ctx_config); + } + wd_sched_rr_release(alg_sched); +} +
在 2022/11/24 15:43, liulongfang 写道:
After the dynamic loading function is added, device resource initialization, driver acquisition, and scheduler initialization functions need to be extracted into the public framework.
so that the algorithm can quickly adapt to the dynamic loading function internally.
Signed-off-by: liulongfang liulongfang@huawei.com
include/wd_alg_common.h | 9 + include/wd_sched.h | 6 +- include/wd_util.h | 18 +- wd_comp.c | 2 +- wd_util.c | 447 +++++++++++++++++++++++++++++++++++++++- 5 files changed, 477 insertions(+), 5 deletions(-)
diff --git a/include/wd_alg_common.h b/include/wd_alg_common.h index 6938e50..b96e6d8 100644 --- a/include/wd_alg_common.h +++ b/include/wd_alg_common.h @@ -28,6 +28,12 @@ extern "C" { #define CTX_TYPE_INVALID 9999 #define POLL_TIME 1000
+enum alg_task_type {
- TASK_MIX = 0x0,
- TASK_HW,
- TASK_INSTR,
+};
- /**
- struct wd_ctx_nums - Define the ctx sets numbers.
- @sync_ctx_num: The ctx numbers which are used for sync mode for each
@@ -80,6 +86,9 @@ struct wd_sched { handle_t h_sched_ctx; };
+typedef int (*wd_alg_init)(struct wd_ctx_config *config, struct wd_sched *sched); +typedef int (*wd_alg_poll_ctx)(__u32 idx, __u32 expt, __u32 *count);
- #ifdef __cplusplus } #endif
diff --git a/include/wd_sched.h b/include/wd_sched.h index 2ae6103..a492d70 100644 --- a/include/wd_sched.h +++ b/include/wd_sched.h @@ -18,7 +18,11 @@ extern "C" { enum sched_policy_type { /* requests will be sent to ctxs one by one */ SCHED_POLICY_RR = 0,
- SCHED_POLICY_BUTT
/* requests will no need ctxs */
SCHED_POLICY_NONE,
/* requests will need a fixed ctx */
SCHED_POLICY_SINGLE,
SCHED_POLICY_BUTT, };
struct sched_params {
diff --git a/include/wd_util.h b/include/wd_util.h index 9b581d4..67d8b62 100644 --- a/include/wd_util.h +++ b/include/wd_util.h @@ -133,9 +133,12 @@ struct wd_msg_handle { struct wd_init_attrs { __u32 sched_type; char *alg;
struct wd_alg_driver *driver; struct wd_sched *sched; struct wd_ctx_params *ctx_params; struct wd_ctx_config *ctx_config;
wd_alg_init alg_init;
wd_alg_poll_ctx alg_poll_ctx; };
/*
@@ -431,13 +434,24 @@ static inline void wd_alg_clear_init(enum wd_status *status) }
/**
- wd_alg_pre_init() - Request the ctxs and initialize the sched_domain
*/
- wd_alg_attrs_init() - Request the ctxs and initialize the sched_domain
with the given devices list, ctxs number and numa mask.
- @attrs: the algorithm initialization parameters.
- Return device if succeed and other error number if fail.
-int wd_alg_pre_init(struct wd_init_attrs *attrs); +int wd_alg_attrs_init(struct wd_init_attrs *alg_init_attrs); +void wd_alg_attrs_uninit(struct wd_init_attrs *attrs);
+struct wd_alg_driver *wd_alg_drv_request(int task_type, char *alg_name); +int wd_alg_init_driver(struct wd_ctx_config_internal *config,
- struct wd_alg_driver *driver, void **drv_priv);
+void wd_alg_uninit_driver(struct wd_ctx_config_internal *config,
- struct wd_alg_driver *driver, void *drv_priv);
+void *wd_dlopen_drv(const char *cust_lib_dir); +int wd_dlclose_drv(void *dlh_list); +void wd_get_alg_type(const char *alg_name, char *alg_type);
/**
- wd_dfx_msg_cnt() - Message counter interface for ctx
diff --git a/wd_comp.c b/wd_comp.c index cd938d9..bb142a7 100644 --- a/wd_comp.c +++ b/wd_comp.c @@ -230,7 +230,7 @@ int wd_comp_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_par wd_comp_sched->name = SCHED_RR_NAME; wd_comp_init_attrs.sched = wd_comp_sched;
- ret = wd_alg_pre_init(&wd_comp_init_attrs);
- ret = wd_alg_attrs_init(&wd_comp_init_attrs); if (ret) goto out_freesched;
diff --git a/wd_util.c b/wd_util.c index 433dd56..bc97f6e 100644 --- a/wd_util.c +++ b/wd_util.c @@ -5,6 +5,8 @@ */
#define _GNU_SOURCE +#include <dirent.h> +#include <dlfcn.h> #include <pthread.h> #include <semaphore.h> #include <string.h> @@ -23,6 +25,8 @@ #define WD_INIT_SLEEP_UTIME 1000 #define WD_INIT_RETRY_TIMES 10000
+#define DRV_LIB_DIR "/root/lib"
- struct msg_pool { /* message array allocated dynamically */ void *msgs;
@@ -64,6 +68,72 @@ struct async_task_queue { int (*alg_poll_ctx)(__u32, __u32, __u32 *); };
+struct drv_lib_list {
- void *dlhandle;
- struct drv_lib_list *next;
+};
+struct acc_alg_item {
- char *name;
- char *algtype;
+};
+static struct acc_alg_item alg_options[] = {
- {"zlib", "zlib-deflate"},
- {"gzip", "gzip"},
- {"deflate", "deflate"},
- {"lz77_zstd", "lz77_zstd"},
- {"rsa", "rsa"},
- {"dh", "dh"},
- {"ecdh", "ecdh"},
- {"x25519", "x25519"},
- {"x448", "x448"},
- {"ecdsa", "ecdsa"},
- {"sm2", "sm2"},
- {"ecb(aes)", "cipher"},
- {"cbc(aes)", "cipher"},
- {"xts(aes)", "cipher"},
- {"ofb(aes)", "cipher"},
- {"cfb(aes)", "cipher"},
- {"ctr(aes)", "cipher"},
- {"cbc-cs1(aes)", "cipher"},
- {"cbc-cs2(aes)", "cipher"},
- {"cbc-cs3(aes)", "cipher"},
- {"ecb(sm4)", "cipher"},
- {"xts(sm4)", "cipher"},
- {"cbc(sm4)", "cipher"},
- {"ofb(sm4)", "cipher"},
- {"cfb(sm4)", "cipher"},
- {"ctr(sm4)", "cipher"},
- {"cbc-cs1(sm4)", "cipher"},
- {"cbc-cs2(sm4)", "cipher"},
- {"cbc-cs3(sm4)", "cipher"},
- {"ecb(des)", "cipher"},
- {"cbc(des)", "cipher"},
- {"ecb(des3_ede)", "cipher"},
- {"cbc(des3_ede)", "cipher"},
- {"ccm(aes)", "aead"},
- {"gcm(aes)", "aead"},
- {"ccm(sm4)", "aead"},
- {"gcm(sm4)", "aead"},
- {"authenc(hmac(sha256),cbc(aes))", "aead"},
- {"authenc(hmac(sha256),cbc(sm4))", "aead"},
- {"sm3", "digest"},
- {"md5", "digest"},
- {"sha1", "digest"},
- {"sha256", "digest"},
- {"sha224", "digest"},
- {"sha384", "digest"},
- {"sha512", "digest"},
- {"sha512-224", "digest"},
- {"sha512-256", "digest"},
- {"", ""}
+};
- static void clone_ctx_to_internal(struct wd_ctx *ctx, struct wd_ctx_internal *ctx_in) {
@@ -1779,6 +1849,257 @@ int wd_init_param_check(struct wd_ctx_config *config, struct wd_sched *sched) return 0; }
+void wd_get_alg_type(const char *alg_name, char *alg_type)
static ?
+{
- int i;
- for (i = 0; i < ARRAY_SIZE(alg_options); i++) {
if (strcmp(alg_name, alg_options[i].name) == 0) {
(void)strcpy(alg_type, alg_options[i].algtype);
break;
}
- }
+}
if unmatched, how to deal with?
+/**
- There are many other .so files in this file directory (/root/lib/),
- and it is necessary to screen out valid uadk driver files
- through this function.
- */
+static int file_check_valid(char *lib_file) +{ +#define FILE_HEAD_SZ 4 +#define FILE_TAIL_SZ 4
- int file_len = strlen(lib_file);
- char file_head[FILE_HEAD_SZ] = "lib";
- char file_tail[FILE_TAIL_SZ] = ".so";
- int i, j;
- /* Lib file name is libxx_xxx.so */
- for (i = 0; i < FILE_HEAD_SZ - 1; i++) {
if (lib_file[i] != file_head[i])
return -EINVAL;
- }
- for (i = file_len - (FILE_TAIL_SZ - 1), j = 0;
i < file_len && j < FILE_TAIL_SZ; i++, j++) {
if (lib_file[i] != file_tail[j])
return -EINVAL;
- }
- return 0;
+}
+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");
return -WD_EINVAL;
- }
- fb_driver->init(NULL, NULL);
- return 0;
+}
+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");
return;
- }
- fb_driver->exit(NULL);
+}
+int wd_alg_init_driver(struct wd_ctx_config_internal *config,
- struct wd_alg_driver *driver, void **drv_priv)
+{
- void *priv;
- int ret;
- /* Init ctx related resources in specific driver */
- priv = calloc(1, driver->priv_size);
- if (!priv)
return -WD_ENOMEM;
- if (!driver->init) {
driver->fallback = 0;
WD_ERR("driver have no init interface.\n");
ret = -WD_EINVAL;
goto err_alloc;
- }
- ret = driver->init(config, priv);
- if (ret < 0) {
WD_ERR("driver init failed.\n");
goto err_alloc;
- }
- if (driver->fallback) {
ret = wd_alg_init_fallback((struct wd_alg_driver *)driver->fallback);
if (ret) {
driver->fallback = 0;
WD_ERR("soft alg driver init failed.\n");
}
- }
- *drv_priv = priv;
- return 0;
+err_alloc:
- free(priv);
- return ret;
+}
+void wd_alg_uninit_driver(struct wd_ctx_config_internal *config,
- struct wd_alg_driver *driver, void *drv_priv)
+{
- driver->exit(drv_priv);
- /* Ctx config just need clear once */
- if (driver->priority == UADK_ALG_HW)
wd_clear_ctx_config(config);
- if (driver->fallback)
wd_alg_uninit_fallback((struct wd_alg_driver *)driver->fallback);
+}
+int wd_dlclose_drv(void *dlh_list) +{
- struct drv_lib_list *dlhead = (struct drv_lib_list *)dlh_list;
- struct drv_lib_list *dlnode;
- if (!dlhead)
return -WD_EINVAL;
- while (dlhead) {
dlnode = dlhead;
dlhead = dlhead->next;
dlclose(dlnode);
no free(node) memory?
- }
- return 0;
+}
+static void add_lib_to_list(struct drv_lib_list *head,
struct drv_lib_list *node)
+{
- struct drv_lib_list *tmp = head;
- while (tmp->next)
tmp = tmp->next;
- tmp->next = node;
+}
+void *wd_dlopen_drv(const char *cust_lib_dir) +{
- typedef int (*alg_ops)(struct wd_alg_driver *drv);
- struct drv_lib_list *node, *head = NULL;
- char lib_dir_path[PATH_STR_SIZE];
- char lib_path[PATH_STR_SIZE];
- struct dirent *lib_dir;
- alg_ops dl_func = NULL;
- DIR *wd_dir;
- int ret;
- if (!cust_lib_dir)
strncpy(lib_dir_path, DRV_LIB_DIR, PATH_STR_SIZE - 1);
- else
strncpy(lib_dir_path, cust_lib_dir, PATH_STR_SIZE - 1);
- wd_dir = opendir(lib_dir_path);
- if (!wd_dir) {
WD_ERR("UADK driver lib file: %s not exist!\n", lib_dir_path);
return NULL;
- }
- while ((lib_dir = readdir(wd_dir)) != NULL) {
if (!strncmp(lib_dir->d_name, ".", LINUX_CRTDIR_SIZE) ||
!strncmp(lib_dir->d_name, "..", LINUX_PRTDIR_SIZE))
continue;
ret = file_check_valid(lib_dir->d_name);
if (ret)
continue;
node = calloc(1, sizeof(*node));
if (!node)
goto free_list;
ret = snprintf(lib_path, PATH_STR_SIZE, "%s/%s", lib_dir_path, lib_dir->d_name);
if (ret < 0)
goto free_list;
node->dlhandle = dlopen(lib_path, RTLD_NOW);
if (!node->dlhandle) {
WD_ERR("fail to open lib file: %s\n", dlerror());
/* there are many other files need to skip */
need free(node) ?
continue;
}
dl_func = dlsym(node->dlhandle, "wd_alg_driver_register");
if (dl_func == NULL) {
dlclose(node->dlhandle);
need free(node) ?
or you can put "node = calloc(1, sizeof(*node));" after this step.
continue;
}
if (!head)
head = node;
else
add_lib_to_list(head, node);
- }
- closedir(wd_dir);
- return (void *)head;
+free_list:
- closedir(wd_dir);
- wd_dlclose_drv(head);
- return NULL;
+}
+struct wd_alg_driver *wd_alg_drv_request(int task_type, char *alg_name) +{
- struct wd_alg_driver *set_driver = NULL;
- struct wd_alg_driver *drv;
- /* Get alg driver and dev name */
- switch (task_type) {
- case TASK_INSTR:
drv = wd_request_drv(alg_name, true);
if (!drv) {
WD_ERR("no soft %s driver support\n", alg_name);
return NULL;
}
set_driver = drv;
set_driver->fallback = 0;
break;
- case TASK_HW:
- case TASK_MIX:
drv = wd_request_drv(alg_name, false);
if (!drv) {
WD_ERR("no HW %s driver support\n", alg_name);
return NULL;
}
set_driver = drv;
set_driver->fallback = 0;
if (task_type == TASK_MIX) {
drv = wd_request_drv(alg_name, true);
if (!drv) {
set_driver->fallback = 0;
WD_ERR("no soft %s driver support\n", alg_name);
} else {
set_driver->fallback = (handle_t)drv;
WD_ERR("successful to get soft driver\n");
}
}
break;
- }
- return set_driver;
+}
- bool wd_alg_try_init(enum wd_status *status) { enum wd_status expected;
@@ -1960,7 +2281,7 @@ free_ctxs: return ret; }
-int wd_alg_pre_init(struct wd_init_attrs *attrs) +static int wd_alg_ctx_init(struct wd_init_attrs *attrs) { struct wd_ctx_config *ctx_config = attrs->ctx_config; struct wd_ctx_params *ctx_params = attrs->ctx_params; @@ -2034,3 +2355,127 @@ out_freelist:
return ret; }
+static void wd_alg_ctx_uninit(struct wd_ctx_config *ctx_config) +{
- int i;
- for (i = 0; i < ctx_config->ctx_num; i++)
if (ctx_config->ctxs[i].ctx) {
wd_release_ctx(ctx_config->ctxs[i].ctx);
ctx_config->ctxs[i].ctx = 0;
- }
- free(ctx_config->ctxs);
+}
+int wd_alg_attrs_init(struct wd_init_attrs *alg_init_attrs) +{
- wd_alg_poll_ctx alg_poll_func = alg_init_attrs->alg_poll_ctx;
- wd_alg_init alg_init_func = alg_init_attrs->alg_init;
- struct wd_alg_driver *driver = alg_init_attrs->driver;
- __u32 sched_type = alg_init_attrs->sched_type;
- char *alg = alg_init_attrs->alg;
- struct wd_ctx_config *ctx_config = NULL;
- struct wd_sched *alg_sched = NULL;
- char alg_type[WD_NAME_SIZE];
- __u32 op_type_num;
- int ret;
- if (alg_init_attrs->ctx_params)
return -WD_EINVAL;
- op_type_num = alg_init_attrs->ctx_params->op_type_num;
- switch (driver->priority) {
- case UADK_ALG_SOFT:
- case UADK_ALG_CE_INSTR:
/* No need to alloc resource */
if (sched_type != SCHED_POLICY_NONE)
return -WD_EINVAL;
alg_sched = wd_sched_rr_alloc(SCHED_POLICY_NONE, 1, 1, alg_poll_func);
if (!alg_sched) {
WD_ERR("fail to alloc scheduler\n");
return -WD_EINVAL;
}
ret = wd_sched_rr_instance(alg_sched, NULL);
if (ret) {
WD_ERR("fail to instance scheduler\n");
goto out_freesched;
}
break;
- case UADK_ALG_SVE_INSTR:
/* Todo lock cpu core */
if (sched_type != SCHED_POLICY_SINGLE)
return -WD_EINVAL;
alg_sched = wd_sched_rr_alloc(SCHED_POLICY_SINGLE, 1, 1, alg_poll_func);
if (!alg_sched) {
WD_ERR("fail to alloc scheduler\n");
return -WD_EINVAL;
}
ret = wd_sched_rr_instance(alg_sched, NULL);
if (ret) {
WD_ERR("fail to instance scheduler\n");
goto out_freesched;
}
break;
- case UADK_ALG_HW:
wd_get_alg_type(alg, alg_type);
alg_init_attrs->alg = alg_type;
ctx_config = calloc(1, sizeof(*ctx_config));
if (!ctx_config) {
WD_ERR("fail to alloc ctx config\n");
return -WD_ENOMEM;
}
alg_sched = wd_sched_rr_alloc(sched_type, op_type_num,
numa_max_node() + 1, alg_poll_func);
if (!alg_sched) {
WD_ERR("fail to instance scheduler\n");
ret = -WD_EINVAL;
goto out_ctx_config;
}
ret = wd_alg_ctx_init(alg_init_attrs);
if (ret) {
WD_ERR("fail to init ctx\n");
goto out_freesched;
}
ret = alg_init_func(ctx_config, alg_sched);
if (ret)
goto out_pre_init;
- }
- alg_init_attrs->ctx_config = ctx_config;
- alg_init_attrs->sched = alg_sched;
- return 0;
+out_pre_init:
- wd_alg_ctx_uninit(ctx_config);
+out_freesched:
- wd_sched_rr_release(alg_sched);
+out_ctx_config:
- if (ctx_config)
free(ctx_config);
- return ret;
+}
+void wd_alg_attrs_uninit(struct wd_init_attrs *attrs) +{
- struct wd_ctx_config *ctx_config = attrs->ctx_config;
- struct wd_sched *alg_sched = attrs->sched;
- if (ctx_config) {
wd_alg_ctx_uninit(ctx_config);
free(ctx_config);
- }
- wd_sched_rr_release(alg_sched);
+}
在 2022/11/25 17:38, fanghao (A) 写道:
在 2022/11/24 15:43, liulongfang 写道:
After the dynamic loading function is added, device resource initialization, driver acquisition, and scheduler initialization functions need to be extracted into the public framework.
so that the algorithm can quickly adapt to the dynamic loading function internally.
Signed-off-by: liulongfang liulongfang@huawei.com
include/wd_alg_common.h | 9 + include/wd_sched.h | 6 +- include/wd_util.h | 18 +- wd_comp.c | 2 +- wd_util.c | 447 +++++++++++++++++++++++++++++++++++++++- 5 files changed, 477 insertions(+), 5 deletions(-)
diff --git a/include/wd_alg_common.h b/include/wd_alg_common.h index 6938e50..b96e6d8 100644 --- a/include/wd_alg_common.h +++ b/include/wd_alg_common.h @@ -28,6 +28,12 @@ extern "C" { #define CTX_TYPE_INVALID 9999 #define POLL_TIME 1000 +enum alg_task_type { + TASK_MIX = 0x0, + TASK_HW, + TASK_INSTR, +};
/** * struct wd_ctx_nums - Define the ctx sets numbers. * @sync_ctx_num: The ctx numbers which are used for sync mode for each @@ -80,6 +86,9 @@ struct wd_sched { handle_t h_sched_ctx; }; +typedef int (*wd_alg_init)(struct wd_ctx_config *config, struct wd_sched *sched); +typedef int (*wd_alg_poll_ctx)(__u32 idx, __u32 expt, __u32 *count);
#ifdef __cplusplus } #endif diff --git a/include/wd_sched.h b/include/wd_sched.h index 2ae6103..a492d70 100644 --- a/include/wd_sched.h +++ b/include/wd_sched.h @@ -18,7 +18,11 @@ extern "C" { enum sched_policy_type { /* requests will be sent to ctxs one by one */ SCHED_POLICY_RR = 0, - SCHED_POLICY_BUTT + /* requests will no need ctxs */ + SCHED_POLICY_NONE, + /* requests will need a fixed ctx */ + SCHED_POLICY_SINGLE, + SCHED_POLICY_BUTT, }; struct sched_params { diff --git a/include/wd_util.h b/include/wd_util.h index 9b581d4..67d8b62 100644 --- a/include/wd_util.h +++ b/include/wd_util.h @@ -133,9 +133,12 @@ struct wd_msg_handle { struct wd_init_attrs { __u32 sched_type; char *alg; + struct wd_alg_driver *driver; struct wd_sched *sched; struct wd_ctx_params *ctx_params; struct wd_ctx_config *ctx_config; + wd_alg_init alg_init; + wd_alg_poll_ctx alg_poll_ctx; }; /* @@ -431,13 +434,24 @@ static inline void wd_alg_clear_init(enum wd_status *status) } /**
- wd_alg_pre_init() - Request the ctxs and initialize the sched_domain
- wd_alg_attrs_init() - Request the ctxs and initialize the sched_domain
* with the given devices list, ctxs number and numa mask. * @attrs: the algorithm initialization parameters. * * Return device if succeed and other error number if fail. */ -int wd_alg_pre_init(struct wd_init_attrs *attrs); +int wd_alg_attrs_init(struct wd_init_attrs *alg_init_attrs); +void wd_alg_attrs_uninit(struct wd_init_attrs *attrs);
+struct wd_alg_driver *wd_alg_drv_request(int task_type, char *alg_name); +int wd_alg_init_driver(struct wd_ctx_config_internal *config, + struct wd_alg_driver *driver, void **drv_priv); +void wd_alg_uninit_driver(struct wd_ctx_config_internal *config, + struct wd_alg_driver *driver, void *drv_priv);
+void *wd_dlopen_drv(const char *cust_lib_dir); +int wd_dlclose_drv(void *dlh_list); +void wd_get_alg_type(const char *alg_name, char *alg_type); /** * wd_dfx_msg_cnt() - Message counter interface for ctx diff --git a/wd_comp.c b/wd_comp.c index cd938d9..bb142a7 100644 --- a/wd_comp.c +++ b/wd_comp.c @@ -230,7 +230,7 @@ int wd_comp_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_par wd_comp_sched->name = SCHED_RR_NAME; wd_comp_init_attrs.sched = wd_comp_sched; - ret = wd_alg_pre_init(&wd_comp_init_attrs); + ret = wd_alg_attrs_init(&wd_comp_init_attrs); if (ret) goto out_freesched; diff --git a/wd_util.c b/wd_util.c index 433dd56..bc97f6e 100644 --- a/wd_util.c +++ b/wd_util.c @@ -5,6 +5,8 @@ */ #define _GNU_SOURCE +#include <dirent.h> +#include <dlfcn.h> #include <pthread.h> #include <semaphore.h> #include <string.h> @@ -23,6 +25,8 @@ #define WD_INIT_SLEEP_UTIME 1000 #define WD_INIT_RETRY_TIMES 10000 +#define DRV_LIB_DIR "/root/lib"
struct msg_pool { /* message array allocated dynamically */ void *msgs; @@ -64,6 +68,72 @@ struct async_task_queue { int (*alg_poll_ctx)(__u32, __u32, __u32 *); }; +struct drv_lib_list { + void *dlhandle; + struct drv_lib_list *next; +};
+struct acc_alg_item { + char *name; + char *algtype; +};
+static struct acc_alg_item alg_options[] = { + {"zlib", "zlib-deflate"}, + {"gzip", "gzip"}, + {"deflate", "deflate"}, + {"lz77_zstd", "lz77_zstd"},
+ {"rsa", "rsa"}, + {"dh", "dh"}, + {"ecdh", "ecdh"}, + {"x25519", "x25519"}, + {"x448", "x448"}, + {"ecdsa", "ecdsa"}, + {"sm2", "sm2"},
+ {"ecb(aes)", "cipher"}, + {"cbc(aes)", "cipher"}, + {"xts(aes)", "cipher"}, + {"ofb(aes)", "cipher"}, + {"cfb(aes)", "cipher"}, + {"ctr(aes)", "cipher"}, + {"cbc-cs1(aes)", "cipher"}, + {"cbc-cs2(aes)", "cipher"}, + {"cbc-cs3(aes)", "cipher"}, + {"ecb(sm4)", "cipher"}, + {"xts(sm4)", "cipher"}, + {"cbc(sm4)", "cipher"}, + {"ofb(sm4)", "cipher"}, + {"cfb(sm4)", "cipher"}, + {"ctr(sm4)", "cipher"}, + {"cbc-cs1(sm4)", "cipher"}, + {"cbc-cs2(sm4)", "cipher"}, + {"cbc-cs3(sm4)", "cipher"}, + {"ecb(des)", "cipher"}, + {"cbc(des)", "cipher"}, + {"ecb(des3_ede)", "cipher"}, + {"cbc(des3_ede)", "cipher"},
+ {"ccm(aes)", "aead"}, + {"gcm(aes)", "aead"}, + {"ccm(sm4)", "aead"}, + {"gcm(sm4)", "aead"}, + {"authenc(hmac(sha256),cbc(aes))", "aead"}, + {"authenc(hmac(sha256),cbc(sm4))", "aead"},
+ {"sm3", "digest"}, + {"md5", "digest"}, + {"sha1", "digest"}, + {"sha256", "digest"}, + {"sha224", "digest"}, + {"sha384", "digest"}, + {"sha512", "digest"}, + {"sha512-224", "digest"}, + {"sha512-256", "digest"}, + {"", ""} +};
static void clone_ctx_to_internal(struct wd_ctx *ctx, struct wd_ctx_internal *ctx_in) { @@ -1779,6 +1849,257 @@ int wd_init_param_check(struct wd_ctx_config *config, struct wd_sched *sched) return 0; } +void wd_get_alg_type(const char *alg_name, char *alg_type)
static ?
Yes, Here is the need to add static modification.
+{ + int i;
+ for (i = 0; i < ARRAY_SIZE(alg_options); i++) { + if (strcmp(alg_name, alg_options[i].name) == 0) { + (void)strcpy(alg_type, alg_options[i].algtype); + break; + } + } +}
if unmatched, how to deal with?
If it does not match, alg_type has no operation, and still keeps its original NULL.
+/**
- There are many other .so files in this file directory (/root/lib/),
- and it is necessary to screen out valid uadk driver files
- through this function.
- */
+static int file_check_valid(char *lib_file) +{ +#define FILE_HEAD_SZ 4 +#define FILE_TAIL_SZ 4 + int file_len = strlen(lib_file); + char file_head[FILE_HEAD_SZ] = "lib"; + char file_tail[FILE_TAIL_SZ] = ".so"; + int i, j;
+ /* Lib file name is libxx_xxx.so */ + for (i = 0; i < FILE_HEAD_SZ - 1; i++) { + if (lib_file[i] != file_head[i]) + return -EINVAL; + }
+ for (i = file_len - (FILE_TAIL_SZ - 1), j = 0; + i < file_len && j < FILE_TAIL_SZ; i++, j++) { + if (lib_file[i] != file_tail[j]) + return -EINVAL; + }
+ return 0; +}
+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"); + return -WD_EINVAL; + }
+ fb_driver->init(NULL, NULL);
+ return 0; +}
+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"); + return; + }
+ fb_driver->exit(NULL); +}
+int wd_alg_init_driver(struct wd_ctx_config_internal *config, + struct wd_alg_driver *driver, void **drv_priv) +{ + void *priv; + int ret;
+ /* Init ctx related resources in specific driver */ + priv = calloc(1, driver->priv_size); + if (!priv) + return -WD_ENOMEM;
+ if (!driver->init) { + driver->fallback = 0; + WD_ERR("driver have no init interface.\n"); + ret = -WD_EINVAL; + goto err_alloc; + }
+ ret = driver->init(config, priv); + if (ret < 0) { + WD_ERR("driver init failed.\n"); + goto err_alloc; + }
+ if (driver->fallback) { + ret = wd_alg_init_fallback((struct wd_alg_driver *)driver->fallback); + if (ret) { + driver->fallback = 0; + WD_ERR("soft alg driver init failed.\n"); + } + } + *drv_priv = priv;
+ return 0;
+err_alloc: + free(priv); + return ret; +}
+void wd_alg_uninit_driver(struct wd_ctx_config_internal *config, + struct wd_alg_driver *driver, void *drv_priv) +{ + driver->exit(drv_priv); + /* Ctx config just need clear once */ + if (driver->priority == UADK_ALG_HW) + wd_clear_ctx_config(config);
+ if (driver->fallback) + wd_alg_uninit_fallback((struct wd_alg_driver *)driver->fallback); +}
+int wd_dlclose_drv(void *dlh_list) +{ + struct drv_lib_list *dlhead = (struct drv_lib_list *)dlh_list; + struct drv_lib_list *dlnode;
+ if (!dlhead) + return -WD_EINVAL;
+ while (dlhead) { + dlnode = dlhead; + dlhead = dlhead->next; + dlclose(dlnode);
no free(node) memory?
Yes, It's need to free()
+ }
+ return 0; +}
+static void add_lib_to_list(struct drv_lib_list *head, + struct drv_lib_list *node) +{ + struct drv_lib_list *tmp = head;
+ while (tmp->next) + tmp = tmp->next;
+ tmp->next = node; +}
+void *wd_dlopen_drv(const char *cust_lib_dir) +{ + typedef int (*alg_ops)(struct wd_alg_driver *drv); + struct drv_lib_list *node, *head = NULL; + char lib_dir_path[PATH_STR_SIZE]; + char lib_path[PATH_STR_SIZE]; + struct dirent *lib_dir; + alg_ops dl_func = NULL; + DIR *wd_dir; + int ret;
+ if (!cust_lib_dir) + strncpy(lib_dir_path, DRV_LIB_DIR, PATH_STR_SIZE - 1); + else + strncpy(lib_dir_path, cust_lib_dir, PATH_STR_SIZE - 1);
+ wd_dir = opendir(lib_dir_path); + if (!wd_dir) { + WD_ERR("UADK driver lib file: %s not exist!\n", lib_dir_path); + return NULL; + }
+ while ((lib_dir = readdir(wd_dir)) != NULL) { + if (!strncmp(lib_dir->d_name, ".", LINUX_CRTDIR_SIZE) || + !strncmp(lib_dir->d_name, "..", LINUX_PRTDIR_SIZE)) + continue;
+ ret = file_check_valid(lib_dir->d_name); + if (ret) + continue;
+ node = calloc(1, sizeof(*node)); + if (!node) + goto free_list;
+ ret = snprintf(lib_path, PATH_STR_SIZE, "%s/%s", lib_dir_path, lib_dir->d_name); + if (ret < 0) + goto free_list;
+ node->dlhandle = dlopen(lib_path, RTLD_NOW); + if (!node->dlhandle) { + WD_ERR("fail to open lib file: %s\n", dlerror()); + /* there are many other files need to skip */
need free(node) ?
+ continue; + }
+ dl_func = dlsym(node->dlhandle, "wd_alg_driver_register"); + if (dl_func == NULL) { + dlclose(node->dlhandle);
need free(node) ?
or you can put "node = calloc(1, sizeof(*node));" after this step.
Yes, there is no exception handling inside this function
Thanks, Longfang.
+ continue; + }
+ if (!head) + head = node; + else + add_lib_to_list(head, node); + } + closedir(wd_dir);
+ return (void *)head;
+free_list: + closedir(wd_dir); + wd_dlclose_drv(head); + return NULL; +}
+struct wd_alg_driver *wd_alg_drv_request(int task_type, char *alg_name) +{ + struct wd_alg_driver *set_driver = NULL; + struct wd_alg_driver *drv;
+ /* Get alg driver and dev name */ + switch (task_type) { + case TASK_INSTR: + drv = wd_request_drv(alg_name, true); + if (!drv) { + WD_ERR("no soft %s driver support\n", alg_name); + return NULL; + } + set_driver = drv; + set_driver->fallback = 0; + break; + case TASK_HW: + case TASK_MIX: + drv = wd_request_drv(alg_name, false); + if (!drv) { + WD_ERR("no HW %s driver support\n", alg_name); + return NULL; + } + set_driver = drv; + set_driver->fallback = 0; + if (task_type == TASK_MIX) { + drv = wd_request_drv(alg_name, true); + if (!drv) { + set_driver->fallback = 0; + WD_ERR("no soft %s driver support\n", alg_name); + } else { + set_driver->fallback = (handle_t)drv; + WD_ERR("successful to get soft driver\n"); + } + } + break; + }
+ return set_driver; +}
bool wd_alg_try_init(enum wd_status *status) { enum wd_status expected; @@ -1960,7 +2281,7 @@ free_ctxs: return ret; } -int wd_alg_pre_init(struct wd_init_attrs *attrs) +static int wd_alg_ctx_init(struct wd_init_attrs *attrs) { struct wd_ctx_config *ctx_config = attrs->ctx_config; struct wd_ctx_params *ctx_params = attrs->ctx_params; @@ -2034,3 +2355,127 @@ out_freelist: return ret; }
+static void wd_alg_ctx_uninit(struct wd_ctx_config *ctx_config) +{ + int i;
+ for (i = 0; i < ctx_config->ctx_num; i++) + if (ctx_config->ctxs[i].ctx) { + wd_release_ctx(ctx_config->ctxs[i].ctx); + ctx_config->ctxs[i].ctx = 0; + }
+ free(ctx_config->ctxs); +}
+int wd_alg_attrs_init(struct wd_init_attrs *alg_init_attrs) +{ + wd_alg_poll_ctx alg_poll_func = alg_init_attrs->alg_poll_ctx; + wd_alg_init alg_init_func = alg_init_attrs->alg_init; + struct wd_alg_driver *driver = alg_init_attrs->driver; + __u32 sched_type = alg_init_attrs->sched_type; + char *alg = alg_init_attrs->alg; + struct wd_ctx_config *ctx_config = NULL; + struct wd_sched *alg_sched = NULL; + char alg_type[WD_NAME_SIZE]; + __u32 op_type_num; + int ret;
+ if (alg_init_attrs->ctx_params) + return -WD_EINVAL;
+ op_type_num = alg_init_attrs->ctx_params->op_type_num;
+ switch (driver->priority) { + case UADK_ALG_SOFT: + case UADK_ALG_CE_INSTR: + /* No need to alloc resource */ + if (sched_type != SCHED_POLICY_NONE) + return -WD_EINVAL;
+ alg_sched = wd_sched_rr_alloc(SCHED_POLICY_NONE, 1, 1, alg_poll_func); + if (!alg_sched) { + WD_ERR("fail to alloc scheduler\n"); + return -WD_EINVAL; + }
+ ret = wd_sched_rr_instance(alg_sched, NULL); + if (ret) { + WD_ERR("fail to instance scheduler\n"); + goto out_freesched; + } + break; + case UADK_ALG_SVE_INSTR: + /* Todo lock cpu core */ + if (sched_type != SCHED_POLICY_SINGLE) + return -WD_EINVAL;
+ alg_sched = wd_sched_rr_alloc(SCHED_POLICY_SINGLE, 1, 1, alg_poll_func); + if (!alg_sched) { + WD_ERR("fail to alloc scheduler\n"); + return -WD_EINVAL; + }
+ ret = wd_sched_rr_instance(alg_sched, NULL); + if (ret) { + WD_ERR("fail to instance scheduler\n"); + goto out_freesched; + } + break; + case UADK_ALG_HW: + wd_get_alg_type(alg, alg_type); + alg_init_attrs->alg = alg_type;
+ ctx_config = calloc(1, sizeof(*ctx_config)); + if (!ctx_config) { + WD_ERR("fail to alloc ctx config\n"); + return -WD_ENOMEM; + }
+ alg_sched = wd_sched_rr_alloc(sched_type, op_type_num, + numa_max_node() + 1, alg_poll_func); + if (!alg_sched) { + WD_ERR("fail to instance scheduler\n"); + ret = -WD_EINVAL; + goto out_ctx_config; + }
+ ret = wd_alg_ctx_init(alg_init_attrs); + if (ret) { + WD_ERR("fail to init ctx\n"); + goto out_freesched; + }
+ ret = alg_init_func(ctx_config, alg_sched); + if (ret) + goto out_pre_init; + }
+ alg_init_attrs->ctx_config = ctx_config; + alg_init_attrs->sched = alg_sched;
+ return 0;
+out_pre_init: + wd_alg_ctx_uninit(ctx_config); +out_freesched: + wd_sched_rr_release(alg_sched); +out_ctx_config: + if (ctx_config) + free(ctx_config); + return ret; +}
+void wd_alg_attrs_uninit(struct wd_init_attrs *attrs) +{ + struct wd_ctx_config *ctx_config = attrs->ctx_config; + struct wd_sched *alg_sched = attrs->sched;
+ if (ctx_config) { + wd_alg_ctx_uninit(ctx_config); + free(ctx_config); + } + wd_sched_rr_release(alg_sched); +}
.
在 2022/11/24 15:43, liulongfang 写道:
After the dynamic loading function is added, device resource initialization, driver acquisition, and scheduler initialization functions need to be extracted into the public framework.
so that the algorithm can quickly adapt to the dynamic loading function internally.
Signed-off-by: liulongfang liulongfang@huawei.com
include/wd_alg_common.h | 9 + include/wd_sched.h | 6 +- include/wd_util.h | 18 +- wd_comp.c | 2 +- wd_util.c | 447 +++++++++++++++++++++++++++++++++++++++- 5 files changed, 477 insertions(+), 5 deletions(-)
diff --git a/include/wd_alg_common.h b/include/wd_alg_common.h index 6938e50..b96e6d8 100644 --- a/include/wd_alg_common.h +++ b/include/wd_alg_common.h @@ -28,6 +28,12 @@ extern "C" { #define CTX_TYPE_INVALID 9999 #define POLL_TIME 1000
+enum alg_task_type {
- TASK_MIX = 0x0,
- TASK_HW,
- TASK_INSTR,
+};
- /**
- struct wd_ctx_nums - Define the ctx sets numbers.
- @sync_ctx_num: The ctx numbers which are used for sync mode for each
@@ -80,6 +86,9 @@ struct wd_sched { handle_t h_sched_ctx; };
+typedef int (*wd_alg_init)(struct wd_ctx_config *config, struct wd_sched *sched); +typedef int (*wd_alg_poll_ctx)(__u32 idx, __u32 expt, __u32 *count);
- #ifdef __cplusplus } #endif
diff --git a/include/wd_sched.h b/include/wd_sched.h index 2ae6103..a492d70 100644 --- a/include/wd_sched.h +++ b/include/wd_sched.h @@ -18,7 +18,11 @@ extern "C" { enum sched_policy_type { /* requests will be sent to ctxs one by one */ SCHED_POLICY_RR = 0,
- SCHED_POLICY_BUTT
/* requests will no need ctxs */
SCHED_POLICY_NONE,
/* requests will need a fixed ctx */
SCHED_POLICY_SINGLE,
SCHED_POLICY_BUTT, };
struct sched_params {
diff --git a/include/wd_util.h b/include/wd_util.h index 9b581d4..67d8b62 100644 --- a/include/wd_util.h +++ b/include/wd_util.h @@ -133,9 +133,12 @@ struct wd_msg_handle { struct wd_init_attrs { __u32 sched_type; char *alg;
struct wd_alg_driver *driver; struct wd_sched *sched; struct wd_ctx_params *ctx_params; struct wd_ctx_config *ctx_config;
wd_alg_init alg_init;
wd_alg_poll_ctx alg_poll_ctx; };
/*
@@ -431,13 +434,24 @@ static inline void wd_alg_clear_init(enum wd_status *status) }
/**
- wd_alg_pre_init() - Request the ctxs and initialize the sched_domain
*/
- wd_alg_attrs_init() - Request the ctxs and initialize the sched_domain
with the given devices list, ctxs number and numa mask.
- @attrs: the algorithm initialization parameters.
- Return device if succeed and other error number if fail.
-int wd_alg_pre_init(struct wd_init_attrs *attrs); +int wd_alg_attrs_init(struct wd_init_attrs *alg_init_attrs); +void wd_alg_attrs_uninit(struct wd_init_attrs *attrs);
+struct wd_alg_driver *wd_alg_drv_request(int task_type, char *alg_name); +int wd_alg_init_driver(struct wd_ctx_config_internal *config,
- struct wd_alg_driver *driver, void **drv_priv);
+void wd_alg_uninit_driver(struct wd_ctx_config_internal *config,
- struct wd_alg_driver *driver, void *drv_priv);
+void *wd_dlopen_drv(const char *cust_lib_dir); +int wd_dlclose_drv(void *dlh_list); +void wd_get_alg_type(const char *alg_name, char *alg_type);
/**
- wd_dfx_msg_cnt() - Message counter interface for ctx
diff --git a/wd_comp.c b/wd_comp.c index cd938d9..bb142a7 100644 --- a/wd_comp.c +++ b/wd_comp.c @@ -230,7 +230,7 @@ int wd_comp_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_par wd_comp_sched->name = SCHED_RR_NAME; wd_comp_init_attrs.sched = wd_comp_sched;
- ret = wd_alg_pre_init(&wd_comp_init_attrs);
- ret = wd_alg_attrs_init(&wd_comp_init_attrs); if (ret) goto out_freesched;
diff --git a/wd_util.c b/wd_util.c index 433dd56..bc97f6e 100644 --- a/wd_util.c +++ b/wd_util.c @@ -5,6 +5,8 @@ */
#define _GNU_SOURCE +#include <dirent.h> +#include <dlfcn.h> #include <pthread.h> #include <semaphore.h> #include <string.h> @@ -23,6 +25,8 @@ #define WD_INIT_SLEEP_UTIME 1000 #define WD_INIT_RETRY_TIMES 10000
+#define DRV_LIB_DIR "/root/lib"
- struct msg_pool { /* message array allocated dynamically */ void *msgs;
@@ -64,6 +68,72 @@ struct async_task_queue { int (*alg_poll_ctx)(__u32, __u32, __u32 *); };
+struct drv_lib_list {
- void *dlhandle;
- struct drv_lib_list *next;
+};
+struct acc_alg_item {
- char *name;
- char *algtype;
+};
+static struct acc_alg_item alg_options[] = {
- {"zlib", "zlib-deflate"},
- {"gzip", "gzip"},
- {"deflate", "deflate"},
- {"lz77_zstd", "lz77_zstd"},
- {"rsa", "rsa"},
- {"dh", "dh"},
- {"ecdh", "ecdh"},
- {"x25519", "x25519"},
- {"x448", "x448"},
- {"ecdsa", "ecdsa"},
- {"sm2", "sm2"},
- {"ecb(aes)", "cipher"},
- {"cbc(aes)", "cipher"},
- {"xts(aes)", "cipher"},
- {"ofb(aes)", "cipher"},
- {"cfb(aes)", "cipher"},
- {"ctr(aes)", "cipher"},
- {"cbc-cs1(aes)", "cipher"},
- {"cbc-cs2(aes)", "cipher"},
- {"cbc-cs3(aes)", "cipher"},
- {"ecb(sm4)", "cipher"},
- {"xts(sm4)", "cipher"},
- {"cbc(sm4)", "cipher"},
- {"ofb(sm4)", "cipher"},
- {"cfb(sm4)", "cipher"},
- {"ctr(sm4)", "cipher"},
- {"cbc-cs1(sm4)", "cipher"},
- {"cbc-cs2(sm4)", "cipher"},
- {"cbc-cs3(sm4)", "cipher"},
- {"ecb(des)", "cipher"},
- {"cbc(des)", "cipher"},
- {"ecb(des3_ede)", "cipher"},
- {"cbc(des3_ede)", "cipher"},
- {"ccm(aes)", "aead"},
- {"gcm(aes)", "aead"},
- {"ccm(sm4)", "aead"},
- {"gcm(sm4)", "aead"},
- {"authenc(hmac(sha256),cbc(aes))", "aead"},
- {"authenc(hmac(sha256),cbc(sm4))", "aead"},
- {"sm3", "digest"},
- {"md5", "digest"},
- {"sha1", "digest"},
- {"sha256", "digest"},
- {"sha224", "digest"},
- {"sha384", "digest"},
- {"sha512", "digest"},
- {"sha512-224", "digest"},
- {"sha512-256", "digest"},
- {"", ""}
+};
- static void clone_ctx_to_internal(struct wd_ctx *ctx, struct wd_ctx_internal *ctx_in) {
@@ -1779,6 +1849,257 @@ int wd_init_param_check(struct wd_ctx_config *config, struct wd_sched *sched) return 0; }
+void wd_get_alg_type(const char *alg_name, char *alg_type) +{
- int i;
- for (i = 0; i < ARRAY_SIZE(alg_options); i++) {
if (strcmp(alg_name, alg_options[i].name) == 0) {
(void)strcpy(alg_type, alg_options[i].algtype);
break;
}
- }
+}
+/**
- There are many other .so files in this file directory (/root/lib/),
- and it is necessary to screen out valid uadk driver files
- through this function.
- */
+static int file_check_valid(char *lib_file) +{ +#define FILE_HEAD_SZ 4 +#define FILE_TAIL_SZ 4
- int file_len = strlen(lib_file);
- char file_head[FILE_HEAD_SZ] = "lib";
- char file_tail[FILE_TAIL_SZ] = ".so";
- int i, j;
- /* Lib file name is libxx_xxx.so */
- for (i = 0; i < FILE_HEAD_SZ - 1; i++) {
if (lib_file[i] != file_head[i])
return -EINVAL;
- }
- for (i = file_len - (FILE_TAIL_SZ - 1), j = 0;
i < file_len && j < FILE_TAIL_SZ; i++, j++) {
if (lib_file[i] != file_tail[j])
return -EINVAL;
- }
- return 0;
+}
+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");
return -WD_EINVAL;
- }
- fb_driver->init(NULL, NULL);
- return 0;
+}
+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");
return;
- }
- fb_driver->exit(NULL);
+}
+int wd_alg_init_driver(struct wd_ctx_config_internal *config,
- struct wd_alg_driver *driver, void **drv_priv)
+{
- void *priv;
- int ret;
- /* Init ctx related resources in specific driver */
- priv = calloc(1, driver->priv_size);
- if (!priv)
return -WD_ENOMEM;
- if (!driver->init) {
driver->fallback = 0;
WD_ERR("driver have no init interface.\n");
ret = -WD_EINVAL;
goto err_alloc;
- }
- ret = driver->init(config, priv);
- if (ret < 0) {
WD_ERR("driver init failed.\n");
goto err_alloc;
- }
- if (driver->fallback) {
ret = wd_alg_init_fallback((struct wd_alg_driver *)driver->fallback);
if (ret) {
driver->fallback = 0;
WD_ERR("soft alg driver init failed.\n");
}
- }
- *drv_priv = priv;
- return 0;
+err_alloc:
- free(priv);
- return ret;
+}
+void wd_alg_uninit_driver(struct wd_ctx_config_internal *config,
- struct wd_alg_driver *driver, void *drv_priv)
+{
- driver->exit(drv_priv);
- /* Ctx config just need clear once */
- if (driver->priority == UADK_ALG_HW)
wd_clear_ctx_config(config);
- if (driver->fallback)
wd_alg_uninit_fallback((struct wd_alg_driver *)driver->fallback);
+}
+int wd_dlclose_drv(void *dlh_list) +{
- struct drv_lib_list *dlhead = (struct drv_lib_list *)dlh_list;
- struct drv_lib_list *dlnode;
- if (!dlhead)
return -WD_EINVAL;
- while (dlhead) {
dlnode = dlhead;
dlhead = dlhead->next;
dlclose(dlnode);
- }
- return 0;
+}
+static void add_lib_to_list(struct drv_lib_list *head,
struct drv_lib_list *node)
+{
- struct drv_lib_list *tmp = head;
- while (tmp->next)
tmp = tmp->next;
- tmp->next = node;
+}
+void *wd_dlopen_drv(const char *cust_lib_dir) +{
- typedef int (*alg_ops)(struct wd_alg_driver *drv);
- struct drv_lib_list *node, *head = NULL;
- char lib_dir_path[PATH_STR_SIZE];
- char lib_path[PATH_STR_SIZE];
- struct dirent *lib_dir;
- alg_ops dl_func = NULL;
- DIR *wd_dir;
- int ret;
- if (!cust_lib_dir)
strncpy(lib_dir_path, DRV_LIB_DIR, PATH_STR_SIZE - 1);
- else
strncpy(lib_dir_path, cust_lib_dir, PATH_STR_SIZE - 1);
- wd_dir = opendir(lib_dir_path);
- if (!wd_dir) {
WD_ERR("UADK driver lib file: %s not exist!\n", lib_dir_path);
return NULL;
- }
- while ((lib_dir = readdir(wd_dir)) != NULL) {
if (!strncmp(lib_dir->d_name, ".", LINUX_CRTDIR_SIZE) ||
!strncmp(lib_dir->d_name, "..", LINUX_PRTDIR_SIZE))
continue;
ret = file_check_valid(lib_dir->d_name);
if (ret)
continue;
node = calloc(1, sizeof(*node));
if (!node)
goto free_list;
ret = snprintf(lib_path, PATH_STR_SIZE, "%s/%s", lib_dir_path, lib_dir->d_name);
if (ret < 0)
goto free_list;
node->dlhandle = dlopen(lib_path, RTLD_NOW);
if (!node->dlhandle) {
WD_ERR("fail to open lib file: %s\n", dlerror());
/* there are many other files need to skip */
continue;
}
dl_func = dlsym(node->dlhandle, "wd_alg_driver_register");
if (dl_func == NULL) {
dlclose(node->dlhandle);
continue;
}
if (!head)
head = node;
else
add_lib_to_list(head, node);
- }
- closedir(wd_dir);
- return (void *)head;
+free_list:
- closedir(wd_dir);
- wd_dlclose_drv(head);
- return NULL;
+}
+struct wd_alg_driver *wd_alg_drv_request(int task_type, char *alg_name)
find the right driver and bind, "wd_alg_drv_bind" maybe more appropriate.
+{
- struct wd_alg_driver *set_driver = NULL;
- struct wd_alg_driver *drv;
- /* Get alg driver and dev name */
- switch (task_type) {
- case TASK_INSTR:
drv = wd_request_drv(alg_name, true);
if (!drv) {
WD_ERR("no soft %s driver support\n", alg_name);
return NULL;
}
set_driver = drv;
set_driver->fallback = 0;
break;
- case TASK_HW:
- case TASK_MIX:
drv = wd_request_drv(alg_name, false);
if (!drv) {
WD_ERR("no HW %s driver support\n", alg_name);
return NULL;
}
set_driver = drv;
set_driver->fallback = 0;
if (task_type == TASK_MIX) {
drv = wd_request_drv(alg_name, true);
if (!drv) {
set_driver->fallback = 0;
WD_ERR("no soft %s driver support\n", alg_name);
} else {
set_driver->fallback = (handle_t)drv;
WD_ERR("successful to get soft driver\n");
}
}
break;
- }
- return set_driver;
+}
- bool wd_alg_try_init(enum wd_status *status) { enum wd_status expected;
@@ -1960,7 +2281,7 @@ free_ctxs: return ret; }
-int wd_alg_pre_init(struct wd_init_attrs *attrs) +static int wd_alg_ctx_init(struct wd_init_attrs *attrs) { struct wd_ctx_config *ctx_config = attrs->ctx_config; struct wd_ctx_params *ctx_params = attrs->ctx_params; @@ -2034,3 +2355,127 @@ out_freelist:
return ret; }
+static void wd_alg_ctx_uninit(struct wd_ctx_config *ctx_config) +{
- int i;
- for (i = 0; i < ctx_config->ctx_num; i++)
if (ctx_config->ctxs[i].ctx) {
wd_release_ctx(ctx_config->ctxs[i].ctx);
ctx_config->ctxs[i].ctx = 0;
- }
- free(ctx_config->ctxs);
+}
+int wd_alg_attrs_init(struct wd_init_attrs *alg_init_attrs) +{
- wd_alg_poll_ctx alg_poll_func = alg_init_attrs->alg_poll_ctx;
- wd_alg_init alg_init_func = alg_init_attrs->alg_init;
- struct wd_alg_driver *driver = alg_init_attrs->driver;
- __u32 sched_type = alg_init_attrs->sched_type;
- char *alg = alg_init_attrs->alg;
- struct wd_ctx_config *ctx_config = NULL;
- struct wd_sched *alg_sched = NULL;
- char alg_type[WD_NAME_SIZE];
- __u32 op_type_num;
- int ret;
- if (alg_init_attrs->ctx_params)
return -WD_EINVAL;
- op_type_num = alg_init_attrs->ctx_params->op_type_num;
- switch (driver->priority) {
This can named "driver->type", and driver->priority can defined a digit number.
static struct wd_alg_driver hisi_zlib_driver = { .drv_name = "hisi_zip", .alg_name = "zlib", + .priority = 900, /* The larger the number, the higher the priority.*/ + .alg_type = UADK_ALG_HW, .priv_size = sizeof(struct hisi_zip_ctx), .queue_num = ZIP_CTX_Q_NUM_DEF, .op_type_num = 2, .fallback = 0, .init = hisi_zip_init, .exit = hisi_zip_exit, .send = hisi_zip_comp_send, .recv = hisi_zip_comp_recv, };
- case UADK_ALG_SOFT:
- case UADK_ALG_CE_INSTR:
/* No need to alloc resource */
if (sched_type != SCHED_POLICY_NONE)
return -WD_EINVAL;
alg_sched = wd_sched_rr_alloc(SCHED_POLICY_NONE, 1, 1, alg_poll_func);
if (!alg_sched) {
WD_ERR("fail to alloc scheduler\n");
return -WD_EINVAL;
}
ret = wd_sched_rr_instance(alg_sched, NULL);
if (ret) {
WD_ERR("fail to instance scheduler\n");
goto out_freesched;
}
break;
- case UADK_ALG_SVE_INSTR:
/* Todo lock cpu core */
if (sched_type != SCHED_POLICY_SINGLE)
return -WD_EINVAL;
alg_sched = wd_sched_rr_alloc(SCHED_POLICY_SINGLE, 1, 1, alg_poll_func);
if (!alg_sched) {
WD_ERR("fail to alloc scheduler\n");
return -WD_EINVAL;
}
ret = wd_sched_rr_instance(alg_sched, NULL);
if (ret) {
WD_ERR("fail to instance scheduler\n");
goto out_freesched;
}
break;
- case UADK_ALG_HW:
wd_get_alg_type(alg, alg_type);
alg_init_attrs->alg = alg_type;
ctx_config = calloc(1, sizeof(*ctx_config));
if (!ctx_config) {
WD_ERR("fail to alloc ctx config\n");
return -WD_ENOMEM;
}
alg_sched = wd_sched_rr_alloc(sched_type, op_type_num,
numa_max_node() + 1, alg_poll_func);
if (!alg_sched) {
WD_ERR("fail to instance scheduler\n");
ret = -WD_EINVAL;
goto out_ctx_config;
}
ret = wd_alg_ctx_init(alg_init_attrs);
if (ret) {
WD_ERR("fail to init ctx\n");
goto out_freesched;
}
ret = alg_init_func(ctx_config, alg_sched);
if (ret)
goto out_pre_init;
- }
- alg_init_attrs->ctx_config = ctx_config;
- alg_init_attrs->sched = alg_sched;
- return 0;
+out_pre_init:
- wd_alg_ctx_uninit(ctx_config);
+out_freesched:
- wd_sched_rr_release(alg_sched);
+out_ctx_config:
- if (ctx_config)
free(ctx_config);
- return ret;
+}
+void wd_alg_attrs_uninit(struct wd_init_attrs *attrs) +{
- struct wd_ctx_config *ctx_config = attrs->ctx_config;
- struct wd_sched *alg_sched = attrs->sched;
- if (ctx_config) {
wd_alg_ctx_uninit(ctx_config);
free(ctx_config);
- }
- wd_sched_rr_release(alg_sched);
+}
On 2022/11/30 9:52, fanghao (A) wrote:
在 2022/11/24 15:43, liulongfang 写道:
After the dynamic loading function is added, device resource initialization, driver acquisition, and scheduler initialization functions need to be extracted into the public framework.
so that the algorithm can quickly adapt to the dynamic loading function internally.
Signed-off-by: liulongfang liulongfang@huawei.com
include/wd_alg_common.h | 9 + include/wd_sched.h | 6 +- include/wd_util.h | 18 +- wd_comp.c | 2 +- wd_util.c | 447 +++++++++++++++++++++++++++++++++++++++- 5 files changed, 477 insertions(+), 5 deletions(-)
diff --git a/include/wd_alg_common.h b/include/wd_alg_common.h index 6938e50..b96e6d8 100644 --- a/include/wd_alg_common.h +++ b/include/wd_alg_common.h @@ -28,6 +28,12 @@ extern "C" { #define CTX_TYPE_INVALID 9999 #define POLL_TIME 1000 +enum alg_task_type { + TASK_MIX = 0x0, + TASK_HW, + TASK_INSTR, +};
/** * struct wd_ctx_nums - Define the ctx sets numbers. * @sync_ctx_num: The ctx numbers which are used for sync mode for each @@ -80,6 +86,9 @@ struct wd_sched { handle_t h_sched_ctx; }; +typedef int (*wd_alg_init)(struct wd_ctx_config *config, struct wd_sched *sched); +typedef int (*wd_alg_poll_ctx)(__u32 idx, __u32 expt, __u32 *count);
#ifdef __cplusplus } #endif diff --git a/include/wd_sched.h b/include/wd_sched.h index 2ae6103..a492d70 100644 --- a/include/wd_sched.h +++ b/include/wd_sched.h @@ -18,7 +18,11 @@ extern "C" { enum sched_policy_type { /* requests will be sent to ctxs one by one */ SCHED_POLICY_RR = 0, - SCHED_POLICY_BUTT + /* requests will no need ctxs */ + SCHED_POLICY_NONE, + /* requests will need a fixed ctx */ + SCHED_POLICY_SINGLE, + SCHED_POLICY_BUTT, }; struct sched_params { diff --git a/include/wd_util.h b/include/wd_util.h index 9b581d4..67d8b62 100644 --- a/include/wd_util.h +++ b/include/wd_util.h @@ -133,9 +133,12 @@ struct wd_msg_handle { struct wd_init_attrs { __u32 sched_type; char *alg; + struct wd_alg_driver *driver; struct wd_sched *sched; struct wd_ctx_params *ctx_params; struct wd_ctx_config *ctx_config; + wd_alg_init alg_init; + wd_alg_poll_ctx alg_poll_ctx; }; /* @@ -431,13 +434,24 @@ static inline void wd_alg_clear_init(enum wd_status *status) } /**
- wd_alg_pre_init() - Request the ctxs and initialize the sched_domain
- wd_alg_attrs_init() - Request the ctxs and initialize the sched_domain
* with the given devices list, ctxs number and numa mask. * @attrs: the algorithm initialization parameters. * * Return device if succeed and other error number if fail. */ -int wd_alg_pre_init(struct wd_init_attrs *attrs); +int wd_alg_attrs_init(struct wd_init_attrs *alg_init_attrs); +void wd_alg_attrs_uninit(struct wd_init_attrs *attrs);
+struct wd_alg_driver *wd_alg_drv_request(int task_type, char *alg_name); +int wd_alg_init_driver(struct wd_ctx_config_internal *config, + struct wd_alg_driver *driver, void **drv_priv); +void wd_alg_uninit_driver(struct wd_ctx_config_internal *config, + struct wd_alg_driver *driver, void *drv_priv);
+void *wd_dlopen_drv(const char *cust_lib_dir); +int wd_dlclose_drv(void *dlh_list); +void wd_get_alg_type(const char *alg_name, char *alg_type); /** * wd_dfx_msg_cnt() - Message counter interface for ctx diff --git a/wd_comp.c b/wd_comp.c index cd938d9..bb142a7 100644 --- a/wd_comp.c +++ b/wd_comp.c @@ -230,7 +230,7 @@ int wd_comp_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_par wd_comp_sched->name = SCHED_RR_NAME; wd_comp_init_attrs.sched = wd_comp_sched; - ret = wd_alg_pre_init(&wd_comp_init_attrs); + ret = wd_alg_attrs_init(&wd_comp_init_attrs); if (ret) goto out_freesched; diff --git a/wd_util.c b/wd_util.c index 433dd56..bc97f6e 100644 --- a/wd_util.c +++ b/wd_util.c @@ -5,6 +5,8 @@ */ #define _GNU_SOURCE +#include <dirent.h> +#include <dlfcn.h> #include <pthread.h> #include <semaphore.h> #include <string.h> @@ -23,6 +25,8 @@ #define WD_INIT_SLEEP_UTIME 1000 #define WD_INIT_RETRY_TIMES 10000 +#define DRV_LIB_DIR "/root/lib"
struct msg_pool { /* message array allocated dynamically */ void *msgs; @@ -64,6 +68,72 @@ struct async_task_queue { int (*alg_poll_ctx)(__u32, __u32, __u32 *); }; +struct drv_lib_list { + void *dlhandle; + struct drv_lib_list *next; +};
+struct acc_alg_item { + char *name; + char *algtype; +};
+static struct acc_alg_item alg_options[] = { + {"zlib", "zlib-deflate"}, + {"gzip", "gzip"}, + {"deflate", "deflate"}, + {"lz77_zstd", "lz77_zstd"},
+ {"rsa", "rsa"}, + {"dh", "dh"}, + {"ecdh", "ecdh"}, + {"x25519", "x25519"}, + {"x448", "x448"}, + {"ecdsa", "ecdsa"}, + {"sm2", "sm2"},
+ {"ecb(aes)", "cipher"}, + {"cbc(aes)", "cipher"}, + {"xts(aes)", "cipher"}, + {"ofb(aes)", "cipher"}, + {"cfb(aes)", "cipher"}, + {"ctr(aes)", "cipher"}, + {"cbc-cs1(aes)", "cipher"}, + {"cbc-cs2(aes)", "cipher"}, + {"cbc-cs3(aes)", "cipher"}, + {"ecb(sm4)", "cipher"}, + {"xts(sm4)", "cipher"}, + {"cbc(sm4)", "cipher"}, + {"ofb(sm4)", "cipher"}, + {"cfb(sm4)", "cipher"}, + {"ctr(sm4)", "cipher"}, + {"cbc-cs1(sm4)", "cipher"}, + {"cbc-cs2(sm4)", "cipher"}, + {"cbc-cs3(sm4)", "cipher"}, + {"ecb(des)", "cipher"}, + {"cbc(des)", "cipher"}, + {"ecb(des3_ede)", "cipher"}, + {"cbc(des3_ede)", "cipher"},
+ {"ccm(aes)", "aead"}, + {"gcm(aes)", "aead"}, + {"ccm(sm4)", "aead"}, + {"gcm(sm4)", "aead"}, + {"authenc(hmac(sha256),cbc(aes))", "aead"}, + {"authenc(hmac(sha256),cbc(sm4))", "aead"},
+ {"sm3", "digest"}, + {"md5", "digest"}, + {"sha1", "digest"}, + {"sha256", "digest"}, + {"sha224", "digest"}, + {"sha384", "digest"}, + {"sha512", "digest"}, + {"sha512-224", "digest"}, + {"sha512-256", "digest"}, + {"", ""} +};
static void clone_ctx_to_internal(struct wd_ctx *ctx, struct wd_ctx_internal *ctx_in) { @@ -1779,6 +1849,257 @@ int wd_init_param_check(struct wd_ctx_config *config, struct wd_sched *sched) return 0; } +void wd_get_alg_type(const char *alg_name, char *alg_type) +{ + int i;
+ for (i = 0; i < ARRAY_SIZE(alg_options); i++) { + if (strcmp(alg_name, alg_options[i].name) == 0) { + (void)strcpy(alg_type, alg_options[i].algtype); + break; + } + } +}
+/**
- There are many other .so files in this file directory (/root/lib/),
- and it is necessary to screen out valid uadk driver files
- through this function.
- */
+static int file_check_valid(char *lib_file) +{ +#define FILE_HEAD_SZ 4 +#define FILE_TAIL_SZ 4 + int file_len = strlen(lib_file); + char file_head[FILE_HEAD_SZ] = "lib"; + char file_tail[FILE_TAIL_SZ] = ".so"; + int i, j;
+ /* Lib file name is libxx_xxx.so */ + for (i = 0; i < FILE_HEAD_SZ - 1; i++) { + if (lib_file[i] != file_head[i]) + return -EINVAL; + }
+ for (i = file_len - (FILE_TAIL_SZ - 1), j = 0; + i < file_len && j < FILE_TAIL_SZ; i++, j++) { + if (lib_file[i] != file_tail[j]) + return -EINVAL; + }
+ return 0; +}
+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"); + return -WD_EINVAL; + }
+ fb_driver->init(NULL, NULL);
+ return 0; +}
+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"); + return; + }
+ fb_driver->exit(NULL); +}
+int wd_alg_init_driver(struct wd_ctx_config_internal *config, + struct wd_alg_driver *driver, void **drv_priv) +{ + void *priv; + int ret;
+ /* Init ctx related resources in specific driver */ + priv = calloc(1, driver->priv_size); + if (!priv) + return -WD_ENOMEM;
+ if (!driver->init) { + driver->fallback = 0; + WD_ERR("driver have no init interface.\n"); + ret = -WD_EINVAL; + goto err_alloc; + }
+ ret = driver->init(config, priv); + if (ret < 0) { + WD_ERR("driver init failed.\n"); + goto err_alloc; + }
+ if (driver->fallback) { + ret = wd_alg_init_fallback((struct wd_alg_driver *)driver->fallback); + if (ret) { + driver->fallback = 0; + WD_ERR("soft alg driver init failed.\n"); + } + } + *drv_priv = priv;
+ return 0;
+err_alloc: + free(priv); + return ret; +}
+void wd_alg_uninit_driver(struct wd_ctx_config_internal *config, + struct wd_alg_driver *driver, void *drv_priv) +{ + driver->exit(drv_priv); + /* Ctx config just need clear once */ + if (driver->priority == UADK_ALG_HW) + wd_clear_ctx_config(config);
+ if (driver->fallback) + wd_alg_uninit_fallback((struct wd_alg_driver *)driver->fallback); +}
+int wd_dlclose_drv(void *dlh_list) +{ + struct drv_lib_list *dlhead = (struct drv_lib_list *)dlh_list; + struct drv_lib_list *dlnode;
+ if (!dlhead) + return -WD_EINVAL;
+ while (dlhead) { + dlnode = dlhead; + dlhead = dlhead->next; + dlclose(dlnode); + }
+ return 0; +}
+static void add_lib_to_list(struct drv_lib_list *head, + struct drv_lib_list *node) +{ + struct drv_lib_list *tmp = head;
+ while (tmp->next) + tmp = tmp->next;
+ tmp->next = node; +}
+void *wd_dlopen_drv(const char *cust_lib_dir) +{ + typedef int (*alg_ops)(struct wd_alg_driver *drv); + struct drv_lib_list *node, *head = NULL; + char lib_dir_path[PATH_STR_SIZE]; + char lib_path[PATH_STR_SIZE]; + struct dirent *lib_dir; + alg_ops dl_func = NULL; + DIR *wd_dir; + int ret;
+ if (!cust_lib_dir) + strncpy(lib_dir_path, DRV_LIB_DIR, PATH_STR_SIZE - 1); + else + strncpy(lib_dir_path, cust_lib_dir, PATH_STR_SIZE - 1);
+ wd_dir = opendir(lib_dir_path); + if (!wd_dir) { + WD_ERR("UADK driver lib file: %s not exist!\n", lib_dir_path); + return NULL; + }
+ while ((lib_dir = readdir(wd_dir)) != NULL) { + if (!strncmp(lib_dir->d_name, ".", LINUX_CRTDIR_SIZE) || + !strncmp(lib_dir->d_name, "..", LINUX_PRTDIR_SIZE)) + continue;
+ ret = file_check_valid(lib_dir->d_name); + if (ret) + continue;
+ node = calloc(1, sizeof(*node)); + if (!node) + goto free_list;
+ ret = snprintf(lib_path, PATH_STR_SIZE, "%s/%s", lib_dir_path, lib_dir->d_name); + if (ret < 0) + goto free_list;
+ node->dlhandle = dlopen(lib_path, RTLD_NOW); + if (!node->dlhandle) { + WD_ERR("fail to open lib file: %s\n", dlerror()); + /* there are many other files need to skip */ + continue; + }
+ dl_func = dlsym(node->dlhandle, "wd_alg_driver_register"); + if (dl_func == NULL) { + dlclose(node->dlhandle); + continue; + }
+ if (!head) + head = node; + else + add_lib_to_list(head, node); + } + closedir(wd_dir);
+ return (void *)head;
+free_list: + closedir(wd_dir); + wd_dlclose_drv(head); + return NULL; +}
+struct wd_alg_driver *wd_alg_drv_request(int task_type, char *alg_name)
find the right driver and bind, "wd_alg_drv_bind" maybe more appropriate.
This is a good suggestion. Thanks, Longfang
+{ + struct wd_alg_driver *set_driver = NULL; + struct wd_alg_driver *drv;
+ /* Get alg driver and dev name */ + switch (task_type) { + case TASK_INSTR: + drv = wd_request_drv(alg_name, true); + if (!drv) { + WD_ERR("no soft %s driver support\n", alg_name); + return NULL; + } + set_driver = drv; + set_driver->fallback = 0; + break; + case TASK_HW: + case TASK_MIX: + drv = wd_request_drv(alg_name, false); + if (!drv) { + WD_ERR("no HW %s driver support\n", alg_name); + return NULL; + } + set_driver = drv; + set_driver->fallback = 0; + if (task_type == TASK_MIX) { + drv = wd_request_drv(alg_name, true); + if (!drv) { + set_driver->fallback = 0; + WD_ERR("no soft %s driver support\n", alg_name); + } else { + set_driver->fallback = (handle_t)drv; + WD_ERR("successful to get soft driver\n"); + } + } + break; + }
+ return set_driver; +}
bool wd_alg_try_init(enum wd_status *status) { enum wd_status expected; @@ -1960,7 +2281,7 @@ free_ctxs: return ret; } -int wd_alg_pre_init(struct wd_init_attrs *attrs) +static int wd_alg_ctx_init(struct wd_init_attrs *attrs) { struct wd_ctx_config *ctx_config = attrs->ctx_config; struct wd_ctx_params *ctx_params = attrs->ctx_params; @@ -2034,3 +2355,127 @@ out_freelist: return ret; }
+static void wd_alg_ctx_uninit(struct wd_ctx_config *ctx_config) +{ + int i;
+ for (i = 0; i < ctx_config->ctx_num; i++) + if (ctx_config->ctxs[i].ctx) { + wd_release_ctx(ctx_config->ctxs[i].ctx); + ctx_config->ctxs[i].ctx = 0; + }
+ free(ctx_config->ctxs); +}
+int wd_alg_attrs_init(struct wd_init_attrs *alg_init_attrs) +{ + wd_alg_poll_ctx alg_poll_func = alg_init_attrs->alg_poll_ctx; + wd_alg_init alg_init_func = alg_init_attrs->alg_init; + struct wd_alg_driver *driver = alg_init_attrs->driver; + __u32 sched_type = alg_init_attrs->sched_type; + char *alg = alg_init_attrs->alg; + struct wd_ctx_config *ctx_config = NULL; + struct wd_sched *alg_sched = NULL; + char alg_type[WD_NAME_SIZE]; + __u32 op_type_num; + int ret;
+ if (alg_init_attrs->ctx_params) + return -WD_EINVAL;
+ op_type_num = alg_init_attrs->ctx_params->op_type_num;
+ switch (driver->priority) {
This can named "driver->type", and driver->priority can defined a digit number.
static struct wd_alg_driver hisi_zlib_driver = { .drv_name = "hisi_zip", .alg_name = "zlib", + .priority = 900, /* The larger the number, the higher the priority.*/ + .alg_type = UADK_ALG_HW, .priv_size = sizeof(struct hisi_zip_ctx), .queue_num = ZIP_CTX_Q_NUM_DEF, .op_type_num = 2, .fallback = 0, .init = hisi_zip_init, .exit = hisi_zip_exit, .send = hisi_zip_comp_send, .recv = hisi_zip_comp_recv, };
+ case UADK_ALG_SOFT: + case UADK_ALG_CE_INSTR: + /* No need to alloc resource */ + if (sched_type != SCHED_POLICY_NONE) + return -WD_EINVAL;
+ alg_sched = wd_sched_rr_alloc(SCHED_POLICY_NONE, 1, 1, alg_poll_func); + if (!alg_sched) { + WD_ERR("fail to alloc scheduler\n"); + return -WD_EINVAL; + }
+ ret = wd_sched_rr_instance(alg_sched, NULL); + if (ret) { + WD_ERR("fail to instance scheduler\n"); + goto out_freesched; + } + break; + case UADK_ALG_SVE_INSTR: + /* Todo lock cpu core */ + if (sched_type != SCHED_POLICY_SINGLE) + return -WD_EINVAL;
+ alg_sched = wd_sched_rr_alloc(SCHED_POLICY_SINGLE, 1, 1, alg_poll_func); + if (!alg_sched) { + WD_ERR("fail to alloc scheduler\n"); + return -WD_EINVAL; + }
+ ret = wd_sched_rr_instance(alg_sched, NULL); + if (ret) { + WD_ERR("fail to instance scheduler\n"); + goto out_freesched; + } + break; + case UADK_ALG_HW: + wd_get_alg_type(alg, alg_type); + alg_init_attrs->alg = alg_type;
+ ctx_config = calloc(1, sizeof(*ctx_config)); + if (!ctx_config) { + WD_ERR("fail to alloc ctx config\n"); + return -WD_ENOMEM; + }
+ alg_sched = wd_sched_rr_alloc(sched_type, op_type_num, + numa_max_node() + 1, alg_poll_func); + if (!alg_sched) { + WD_ERR("fail to instance scheduler\n"); + ret = -WD_EINVAL; + goto out_ctx_config; + }
+ ret = wd_alg_ctx_init(alg_init_attrs); + if (ret) { + WD_ERR("fail to init ctx\n"); + goto out_freesched; + }
+ ret = alg_init_func(ctx_config, alg_sched); + if (ret) + goto out_pre_init; + }
+ alg_init_attrs->ctx_config = ctx_config; + alg_init_attrs->sched = alg_sched;
+ return 0;
+out_pre_init: + wd_alg_ctx_uninit(ctx_config); +out_freesched: + wd_sched_rr_release(alg_sched); +out_ctx_config: + if (ctx_config) + free(ctx_config); + return ret; +}
+void wd_alg_attrs_uninit(struct wd_init_attrs *attrs) +{ + struct wd_ctx_config *ctx_config = attrs->ctx_config; + struct wd_sched *alg_sched = attrs->sched;
+ if (ctx_config) { + wd_alg_ctx_uninit(ctx_config); + free(ctx_config); + } + wd_sched_rr_release(alg_sched); +}
.
After adding the zip module of the init2 interface, combine its initialization part with dynamic loading, and transform the HiSilicon driver of zip, and use the dynamic loading function to realize the connection between the driver and the algorithm layer.
Signed-off-by: liulongfang liulongfang@huawei.com --- drv/hisi_comp.c | 101 +++++++++++++++++++++--- include/drv/wd_comp_drv.h | 27 ------- wd_comp.c | 158 ++++++++++++++++++-------------------- 3 files changed, 163 insertions(+), 123 deletions(-)
diff --git a/drv/hisi_comp.c b/drv/hisi_comp.c index 2eede39..0038d6f 100644 --- a/drv/hisi_comp.c +++ b/drv/hisi_comp.c @@ -13,7 +13,7 @@
#define ZLIB_HEADER "\x78\x9c" #define ZLIB_HEADER_SZ 2 - +#define ZIP_CTX_Q_NUM_DEF 2 /* * We use a extra field for gzip block length. So the fourth byte is \x04. * This is necessary because our software don't know the size of block when @@ -771,8 +771,9 @@ static void hisi_zip_sqe_ops_adapt(handle_t h_qp) } }
-static int hisi_zip_init(struct wd_ctx_config_internal *config, void *priv) +static int hisi_zip_init(void *conf, void *priv) { + struct wd_ctx_config_internal *config = conf; struct hisi_zip_ctx *zip_ctx = (struct hisi_zip_ctx *)priv; struct hisi_qm_priv qm_priv; handle_t h_qp = 0; @@ -1055,14 +1056,92 @@ static int hisi_zip_comp_recv(handle_t ctx, void *comp_msg) return parse_zip_sqe(qp, &sqe, recv_msg); }
-struct wd_comp_driver hisi_zip = { - .drv_name = "hisi_zip", - .alg_name = "zlib\ngzip\ndeflate\nlz77_zstd", - .drv_ctx_size = sizeof(struct hisi_zip_ctx), - .init = hisi_zip_init, - .exit = hisi_zip_exit, - .comp_send = hisi_zip_comp_send, - .comp_recv = hisi_zip_comp_recv, +static struct wd_alg_driver hisi_zlib_driver = { + .drv_name = "hisi_zip", + .alg_name = "zlib", + .priority = UADK_ALG_HW, + .priv_size = sizeof(struct hisi_zip_ctx), + .queue_num = ZIP_CTX_Q_NUM_DEF, + .op_type_num = 2, + .fallback = 0, + .init = hisi_zip_init, + .exit = hisi_zip_exit, + .send = hisi_zip_comp_send, + .recv = hisi_zip_comp_recv, +}; + +static struct wd_alg_driver hisi_gzip_driver = { + .drv_name = "hisi_zip", + .alg_name = "gzip", + .priority = UADK_ALG_HW, + .priv_size = sizeof(struct hisi_zip_ctx), + .queue_num = ZIP_CTX_Q_NUM_DEF, + .op_type_num = 2, + .fallback = 0, + .init = hisi_zip_init, + .exit = hisi_zip_exit, + .send = hisi_zip_comp_send, + .recv = hisi_zip_comp_recv, +}; + +static struct wd_alg_driver hisi_deflate_driver = { + .drv_name = "hisi_zip", + .alg_name = "deflate", + .priority = UADK_ALG_HW, + .priv_size = sizeof(struct hisi_zip_ctx), + .queue_num = ZIP_CTX_Q_NUM_DEF, + .op_type_num = 2, + .fallback = 0, + .init = hisi_zip_init, + .exit = hisi_zip_exit, + .send = hisi_zip_comp_send, + .recv = hisi_zip_comp_recv, +}; + +static struct wd_alg_driver hisi_lz77_zstd_driver = { + .drv_name = "hisi_zip", + .alg_name = "lz77_zstd", + .priority = UADK_ALG_HW, + .priv_size = sizeof(struct hisi_zip_ctx), + .queue_num = ZIP_CTX_Q_NUM_DEF, + .op_type_num = 2, + .fallback = 0, + .init = hisi_zip_init, + .exit = hisi_zip_exit, + .send = hisi_zip_comp_send, + .recv = hisi_zip_comp_recv, };
-WD_COMP_SET_DRIVER(hisi_zip); +static void __attribute__((constructor)) hisi_zip_probe(void) +{ + int ret; + + WD_ERR("Info: register ZIP alg drivers!\n"); + ret = wd_alg_driver_register(&hisi_zlib_driver); + if (ret) + WD_ERR("Error: register zlib failed!\n"); + + ret = wd_alg_driver_register(&hisi_gzip_driver); + if (ret) + WD_ERR("Error: register gzip failed!\n"); + + ret = wd_alg_driver_register(&hisi_deflate_driver); + if (ret) + WD_ERR("Error: register deflate failed!\n"); + + ret = wd_alg_driver_register(&hisi_lz77_zstd_driver); + if (ret) + WD_ERR("Error: register lz77_zstd failed!\n"); +} + +static void __attribute__((destructor)) hisi_zip_remove(void) +{ + wd_alg_driver_unregister(&hisi_zlib_driver); + + wd_alg_driver_unregister(&hisi_gzip_driver); + + wd_alg_driver_unregister(&hisi_deflate_driver); + + wd_alg_driver_unregister(&hisi_lz77_zstd_driver); +} + diff --git a/include/drv/wd_comp_drv.h b/include/drv/wd_comp_drv.h index 4aeaee4..213cf2d 100644 --- a/include/drv/wd_comp_drv.h +++ b/include/drv/wd_comp_drv.h @@ -55,35 +55,8 @@ struct wd_comp_msg { __u32 tag; };
-struct wd_comp_driver { - const char *drv_name; - const char *alg_name; - __u32 drv_ctx_size; - int (*init)(struct wd_ctx_config_internal *config, void *priv); - void (*exit)(void *priv); - int (*comp_send)(handle_t ctx, void *comp_msg); - int (*comp_recv)(handle_t ctx, void *comp_msg); -}; - -void wd_comp_set_driver(struct wd_comp_driver *drv); -struct wd_comp_driver *wd_comp_get_driver(void); - struct wd_comp_msg *wd_comp_get_msg(__u32 idx, __u32 tag);
-#ifdef WD_STATIC_DRV -#define WD_COMP_SET_DRIVER(drv) \ -struct wd_comp_driver *wd_comp_get_driver(void) \ -{ \ - return &drv; \ -} -#else -#define WD_COMP_SET_DRIVER(drv) \ -static void __attribute__((constructor)) set_comp_driver(void) \ -{ \ - wd_comp_set_driver(&(drv)); \ -} -#endif - #ifdef __cplusplus } #endif diff --git a/wd_comp.c b/wd_comp.c index bb142a7..a12769a 100644 --- a/wd_comp.c +++ b/wd_comp.c @@ -19,8 +19,6 @@ #define HW_CTX_SIZE (64 * 1024) #define STREAM_CHUNK (128 * 1024)
-#define SCHED_RR_NAME "sched_rr" - #define swap_byte(x) \ ((((x) & 0x000000ff) << 24) | \ (((x) & 0x0000ff00) << 8) | \ @@ -45,17 +43,15 @@ struct wd_comp_setting { enum wd_status status2; struct wd_ctx_config_internal config; struct wd_sched sched; - struct wd_comp_driver *driver; + struct wd_async_msg_pool pool; + struct wd_alg_driver *driver; void *priv; void *dlhandle; - struct wd_async_msg_pool pool; + void *dlh_list; } wd_comp_setting;
struct wd_env_config wd_comp_env_config; - static struct wd_init_attrs wd_comp_init_attrs; -static struct wd_ctx_config wd_comp_ctx; -static struct wd_sched *wd_comp_sched;
static struct wd_ctx_nums wd_comp_ctx_num[] = { {1, 1}, {1, 1}, {} @@ -67,16 +63,11 @@ static struct wd_ctx_params wd_comp_ctx_params = { .bmp = NULL, };
-#ifdef WD_STATIC_DRV -static void wd_comp_set_static_drv(void) -{ - wd_comp_setting.driver = wd_comp_get_driver(); - if (!wd_comp_setting.driver) - WD_ERR("failed to get driver!\n"); -} -#else static void __attribute__((constructor)) wd_comp_open_driver(void) { + if (wd_comp_setting.dlh_list) + return; + wd_comp_setting.dlhandle = dlopen("libhisi_zip.so", RTLD_NOW); if (!wd_comp_setting.dlhandle) WD_ERR("failed to open libhisi_zip.so, %s\n", dlerror()); @@ -87,16 +78,27 @@ static void __attribute__((destructor)) wd_comp_close_driver(void) if (wd_comp_setting.dlhandle) dlclose(wd_comp_setting.dlhandle); } -#endif
-void wd_comp_set_driver(struct wd_comp_driver *drv) +static void wd_comp_get_driver(void) { - wd_comp_setting.driver = drv; + struct wd_alg_driver *driver = NULL; + const char *alg_name = "zlib"; + + /* + * Compatible with the normal acquisition of device + * drivers in the init interface + */ + driver = wd_request_drv(alg_name, false); + if (!driver) { + WD_ERR("failed to get %s driver support\n", alg_name); + return; + } + + wd_comp_setting.driver = driver; }
int wd_comp_init(struct wd_ctx_config *config, struct wd_sched *sched) { - void *priv; bool flag; int ret;
@@ -120,19 +122,6 @@ int wd_comp_init(struct wd_ctx_config *config, struct wd_sched *sched) ret = wd_init_sched(&wd_comp_setting.sched, sched); if (ret < 0) goto out_clear_ctx_config; - /* - * Fix me: ctx could be passed into wd_comp_set_static_drv to help to - * choose static compiled vendor driver. For dynamic vendor driver, - * wd_comp_open_driver will be called in the process of opening - * libwd_comp.so to load related driver dynamic library. Vendor driver - * pointer will be passed to wd_comp_setting.driver in the process of - * opening of vendor driver dynamic library. A configure file could be - * introduced to help to define which vendor driver lib should be - * loaded. - */ -#ifdef WD_STATIC_DRV - wd_comp_set_static_drv(); -#endif
/* fix me: sadly find we allocate async pool for every ctx */ ret = wd_init_async_request_pool(&wd_comp_setting.pool, @@ -142,25 +131,19 @@ int wd_comp_init(struct wd_ctx_config *config, struct wd_sched *sched) goto out_clear_sched;
/* init ctx related resources in specific driver */ - priv = calloc(1, wd_comp_setting.driver->drv_ctx_size); - if (!priv) { - ret = -WD_ENOMEM; + if (!wd_comp_setting.driver) + wd_comp_get_driver(); + + ret = wd_alg_init_driver(&wd_comp_setting.config, + wd_comp_setting.driver, + &wd_comp_setting.priv); + if (ret) goto out_clear_pool; - } - wd_comp_setting.priv = priv; - ret = wd_comp_setting.driver->init(&wd_comp_setting.config, priv); - if (ret < 0) { - WD_ERR("failed to do driver init, ret = %d!\n", ret); - goto out_free_priv; - }
wd_alg_set_init(&wd_comp_setting.status);
return 0;
-out_free_priv: - free(priv); - wd_comp_setting.priv = NULL; out_clear_pool: wd_uninit_async_request_pool(&wd_comp_setting.pool); out_clear_sched: @@ -179,16 +162,14 @@ void wd_comp_uninit(void) if (!priv) return;
- wd_comp_setting.driver->exit(priv); - free(priv); - wd_comp_setting.priv = NULL; - /* uninit async request pool */ wd_uninit_async_request_pool(&wd_comp_setting.pool);
/* unset config, sched, driver */ wd_clear_sched(&wd_comp_setting.sched); - wd_clear_ctx_config(&wd_comp_setting.config); + wd_alg_uninit_driver(&wd_comp_setting.config, + wd_comp_setting.driver, wd_comp_setting.priv); + wd_comp_setting.driver = NULL;
wd_alg_clear_init(&wd_comp_setting.status); } @@ -196,8 +177,8 @@ void wd_comp_uninit(void) int wd_comp_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params) { enum wd_status status; + int ret = 0; bool flag; - int ret;
wd_alg_get_init(&wd_comp_setting.status, &status); if (status == WD_INIT) { @@ -215,55 +196,62 @@ int wd_comp_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_par goto out_uninit; }
- wd_comp_init_attrs.alg = alg; - wd_comp_init_attrs.sched_type = sched_type; + /* + * Driver lib file could set by env param. + * Than open tham by wd_dlopen_drv() + * Find the /root/lib/xxx.so and then dlopen + */ + wd_comp_setting.dlh_list = wd_dlopen_drv(NULL); + if (!wd_comp_setting.dlh_list) { + WD_ERR("fail to open driver lib files.\n"); + goto out_uninit; + }
- wd_comp_init_attrs.ctx_params = ctx_params ? ctx_params : &wd_comp_ctx_params; - wd_comp_init_attrs.ctx_config = &wd_comp_ctx; +res_retry: + /* Get alg driver and dev name */ + wd_comp_setting.driver = wd_alg_drv_request(task_type, alg); + if (!wd_comp_setting.driver) + goto out_dlopen;
- wd_comp_sched = wd_sched_rr_alloc(sched_type, wd_comp_init_attrs.ctx_params->op_type_num, - numa_max_node() + 1, wd_comp_poll_ctx); - if (!wd_comp_sched) { - ret = -WD_EINVAL; - goto out_uninit; + if (sched_type < 0 || sched_type > SCHED_POLICY_BUTT) { + WD_ERR("fail to check sched_type: %d\n", sched_type); + goto out_dlopen; } - wd_comp_sched->name = SCHED_RR_NAME; - wd_comp_init_attrs.sched = wd_comp_sched; + memset(&wd_comp_setting.config, 0, sizeof(struct wd_ctx_config_internal));
+ 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 = ctx_params ? ctx_params : + &wd_comp_ctx_params; + wd_comp_init_attrs.alg_init = wd_comp_init; + wd_comp_init_attrs.alg_poll_ctx = wd_comp_poll_ctx; ret = wd_alg_attrs_init(&wd_comp_init_attrs); - if (ret) - goto out_freesched; - - ret = wd_comp_init(&wd_comp_ctx, wd_comp_sched); - if (ret) - goto out_freesched; + if (ret) { + if (ret == -WD_ENODEV) { + wd_disable_drv(wd_comp_setting.driver); + goto res_retry; + } + WD_ERR("fail to init alg attrs.\n"); + goto out_dlopen; + }
wd_alg_set_init(&wd_comp_setting.status2);
return 0;
-out_freesched: - wd_sched_rr_release(wd_comp_sched); - +out_dlopen: + wd_dlclose_drv(wd_comp_setting.dlh_list); out_uninit: wd_alg_clear_init(&wd_comp_setting.status2); - return ret; }
void wd_comp_uninit2(void) { - int i; - wd_comp_uninit();
- for (i = 0; i < wd_comp_ctx.ctx_num; i++) - if (wd_comp_ctx.ctxs[i].ctx) { - wd_release_ctx(wd_comp_ctx.ctxs[i].ctx); - wd_comp_ctx.ctxs[i].ctx = 0; - } - - wd_sched_rr_release(wd_comp_sched); + wd_alg_attrs_uninit(&wd_comp_init_attrs); wd_alg_clear_init(&wd_comp_setting.status2); }
@@ -297,7 +285,7 @@ int wd_comp_poll_ctx(__u32 idx, __u32 expt, __u32 *count) ctx = config->ctxs + idx;
do { - ret = wd_comp_setting.driver->comp_recv(ctx->ctx, &resp_msg); + ret = wd_comp_setting.driver->recv(ctx->ctx, &resp_msg); if (unlikely(ret < 0)) { if (ret == -WD_HW_EACCESS) WD_ERR("wd comp recv hw error!\n"); @@ -513,8 +501,8 @@ static int wd_comp_sync_job(struct wd_comp_sess *sess, wd_dfx_msg_cnt(config->msg_cnt, WD_CTX_CNT_NUM, idx); ctx = config->ctxs + idx;
- msg_handle.send = wd_comp_setting.driver->comp_send; - msg_handle.recv = wd_comp_setting.driver->comp_recv; + msg_handle.send = wd_comp_setting.driver->send; + msg_handle.recv = wd_comp_setting.driver->recv;
pthread_spin_lock(&ctx->lock); ret = wd_handle_msg_sync(&msg_handle, ctx->ctx, msg, @@ -773,7 +761,7 @@ int wd_do_comp_async(handle_t h_sess, struct wd_comp_req *req) msg->tag = tag; msg->stream_mode = WD_COMP_STATELESS;
- ret = wd_comp_setting.driver->comp_send(ctx->ctx, msg); + ret = wd_comp_setting.driver->send(ctx->ctx, msg); if (unlikely(ret < 0)) { WD_ERR("wd comp send error, ret = %d!\n", ret); goto fail_with_msg;
在 2022/11/24 15:43, liulongfang 写道:
After adding the zip module of the init2 interface, combine its initialization part with dynamic loading, and transform the HiSilicon driver of zip, and use the dynamic loading function to realize the connection between the driver and the algorithm layer.
Signed-off-by: liulongfang liulongfang@huawei.com
drv/hisi_comp.c | 101 +++++++++++++++++++++--- include/drv/wd_comp_drv.h | 27 ------- wd_comp.c | 158 ++++++++++++++++++-------------------- 3 files changed, 163 insertions(+), 123 deletions(-)
diff --git a/drv/hisi_comp.c b/drv/hisi_comp.c index 2eede39..0038d6f 100644 --- a/drv/hisi_comp.c +++ b/drv/hisi_comp.c @@ -13,7 +13,7 @@
#define ZLIB_HEADER "\x78\x9c" #define ZLIB_HEADER_SZ 2
+#define ZIP_CTX_Q_NUM_DEF 2 /*
- We use a extra field for gzip block length. So the fourth byte is \x04.
- This is necessary because our software don't know the size of block when
@@ -771,8 +771,9 @@ static void hisi_zip_sqe_ops_adapt(handle_t h_qp) } }
-static int hisi_zip_init(struct wd_ctx_config_internal *config, void *priv) +static int hisi_zip_init(void *conf, void *priv) {
- struct wd_ctx_config_internal *config = conf; struct hisi_zip_ctx *zip_ctx = (struct hisi_zip_ctx *)priv; struct hisi_qm_priv qm_priv; handle_t h_qp = 0;
@@ -1055,14 +1056,92 @@ static int hisi_zip_comp_recv(handle_t ctx, void *comp_msg) return parse_zip_sqe(qp, &sqe, recv_msg); }
-struct wd_comp_driver hisi_zip = {
- .drv_name = "hisi_zip",
- .alg_name = "zlib\ngzip\ndeflate\nlz77_zstd",
- .drv_ctx_size = sizeof(struct hisi_zip_ctx),
- .init = hisi_zip_init,
- .exit = hisi_zip_exit,
- .comp_send = hisi_zip_comp_send,
- .comp_recv = hisi_zip_comp_recv,
+static struct wd_alg_driver hisi_zlib_driver = {
.drv_name = "hisi_zip",
.alg_name = "zlib",
.priority = UADK_ALG_HW,
.priv_size = sizeof(struct hisi_zip_ctx),
.queue_num = ZIP_CTX_Q_NUM_DEF,
.op_type_num = 2,
.fallback = 0,
.init = hisi_zip_init,
.exit = hisi_zip_exit,
.send = hisi_zip_comp_send,
.recv = hisi_zip_comp_recv,
+};
+static struct wd_alg_driver hisi_gzip_driver = {
.drv_name = "hisi_zip",
.alg_name = "gzip",
.priority = UADK_ALG_HW,
.priv_size = sizeof(struct hisi_zip_ctx),
.queue_num = ZIP_CTX_Q_NUM_DEF,
.op_type_num = 2,
.fallback = 0,
.init = hisi_zip_init,
.exit = hisi_zip_exit,
.send = hisi_zip_comp_send,
.recv = hisi_zip_comp_recv,
+};
+static struct wd_alg_driver hisi_deflate_driver = {
.drv_name = "hisi_zip",
.alg_name = "deflate",
.priority = UADK_ALG_HW,
.priv_size = sizeof(struct hisi_zip_ctx),
.queue_num = ZIP_CTX_Q_NUM_DEF,
.op_type_num = 2,
.fallback = 0,
.init = hisi_zip_init,
.exit = hisi_zip_exit,
.send = hisi_zip_comp_send,
.recv = hisi_zip_comp_recv,
+};
+static struct wd_alg_driver hisi_lz77_zstd_driver = {
.drv_name = "hisi_zip",
.alg_name = "lz77_zstd",
.priority = UADK_ALG_HW,
.priv_size = sizeof(struct hisi_zip_ctx),
.queue_num = ZIP_CTX_Q_NUM_DEF,
.op_type_num = 2,
.fallback = 0,
.init = hisi_zip_init,
.exit = hisi_zip_exit,
.send = hisi_zip_comp_send,
};.recv = hisi_zip_comp_recv,
-WD_COMP_SET_DRIVER(hisi_zip); +static void __attribute__((constructor)) hisi_zip_probe(void) +{
- int ret;
- WD_ERR("Info: register ZIP alg drivers!\n");
- ret = wd_alg_driver_register(&hisi_zlib_driver);
- if (ret)
WD_ERR("Error: register zlib failed!\n");
- ret = wd_alg_driver_register(&hisi_gzip_driver);
- if (ret)
WD_ERR("Error: register gzip failed!\n");
- ret = wd_alg_driver_register(&hisi_deflate_driver);
- if (ret)
WD_ERR("Error: register deflate failed!\n");
- ret = wd_alg_driver_register(&hisi_lz77_zstd_driver);
- if (ret)
WD_ERR("Error: register lz77_zstd failed!\n");
+}
optimize the duplicate code,can combine to one driver?
.alg_name = "zlib\ngzip\ndeflate\nlz77_zstd",
and in the wd alg layer can search a alg name from a string.
+static void __attribute__((destructor)) hisi_zip_remove(void) +{
- wd_alg_driver_unregister(&hisi_zlib_driver);
- wd_alg_driver_unregister(&hisi_gzip_driver);
- wd_alg_driver_unregister(&hisi_deflate_driver);
- wd_alg_driver_unregister(&hisi_lz77_zstd_driver);
+}
diff --git a/include/drv/wd_comp_drv.h b/include/drv/wd_comp_drv.h index 4aeaee4..213cf2d 100644 --- a/include/drv/wd_comp_drv.h +++ b/include/drv/wd_comp_drv.h @@ -55,35 +55,8 @@ struct wd_comp_msg { __u32 tag; };
-struct wd_comp_driver {
- const char *drv_name;
- const char *alg_name;
- __u32 drv_ctx_size;
- int (*init)(struct wd_ctx_config_internal *config, void *priv);
- void (*exit)(void *priv);
- int (*comp_send)(handle_t ctx, void *comp_msg);
- int (*comp_recv)(handle_t ctx, void *comp_msg);
-};
-void wd_comp_set_driver(struct wd_comp_driver *drv); -struct wd_comp_driver *wd_comp_get_driver(void);
- struct wd_comp_msg *wd_comp_get_msg(__u32 idx, __u32 tag);
-#ifdef WD_STATIC_DRV -#define WD_COMP_SET_DRIVER(drv) \ -struct wd_comp_driver *wd_comp_get_driver(void) \ -{ \
- return &drv; \
-} -#else -#define WD_COMP_SET_DRIVER(drv) \ -static void __attribute__((constructor)) set_comp_driver(void) \ -{ \
- wd_comp_set_driver(&(drv)); \
-} -#endif
- #ifdef __cplusplus } #endif
diff --git a/wd_comp.c b/wd_comp.c index bb142a7..a12769a 100644 --- a/wd_comp.c +++ b/wd_comp.c @@ -19,8 +19,6 @@ #define HW_CTX_SIZE (64 * 1024) #define STREAM_CHUNK (128 * 1024)
-#define SCHED_RR_NAME "sched_rr"
- #define swap_byte(x) \ ((((x) & 0x000000ff) << 24) | \ (((x) & 0x0000ff00) << 8) | \
@@ -45,17 +43,15 @@ struct wd_comp_setting { enum wd_status status2; struct wd_ctx_config_internal config; struct wd_sched sched;
- struct wd_comp_driver *driver;
- struct wd_async_msg_pool pool;
- struct wd_alg_driver *driver; void *priv; void *dlhandle;
- struct wd_async_msg_pool pool;
void *dlh_list; } wd_comp_setting;
struct wd_env_config wd_comp_env_config;
- static struct wd_init_attrs wd_comp_init_attrs;
-static struct wd_ctx_config wd_comp_ctx; -static struct wd_sched *wd_comp_sched;
static struct wd_ctx_nums wd_comp_ctx_num[] = { {1, 1}, {1, 1}, {} @@ -67,16 +63,11 @@ static struct wd_ctx_params wd_comp_ctx_params = { .bmp = NULL, };
-#ifdef WD_STATIC_DRV -static void wd_comp_set_static_drv(void) -{
- wd_comp_setting.driver = wd_comp_get_driver();
- if (!wd_comp_setting.driver)
WD_ERR("failed to get driver!\n");
-} -#else static void __attribute__((constructor)) wd_comp_open_driver(void) {
- if (wd_comp_setting.dlh_list)
return;
- wd_comp_setting.dlhandle = dlopen("libhisi_zip.so", RTLD_NOW); if (!wd_comp_setting.dlhandle) WD_ERR("failed to open libhisi_zip.so, %s\n", dlerror());
@@ -87,16 +78,27 @@ static void __attribute__((destructor)) wd_comp_close_driver(void) if (wd_comp_setting.dlhandle) dlclose(wd_comp_setting.dlhandle); } -#endif
-void wd_comp_set_driver(struct wd_comp_driver *drv) +static void wd_comp_get_driver(void) {
- wd_comp_setting.driver = drv;
struct wd_alg_driver *driver = NULL;
const char *alg_name = "zlib";
/*
* Compatible with the normal acquisition of device
* drivers in the init interface
*/
driver = wd_request_drv(alg_name, false);
if (!driver) {
WD_ERR("failed to get %s driver support\n", alg_name);
return;
}
wd_comp_setting.driver = driver; }
int wd_comp_init(struct wd_ctx_config *config, struct wd_sched *sched) {
- void *priv; bool flag; int ret;
@@ -120,19 +122,6 @@ int wd_comp_init(struct wd_ctx_config *config, struct wd_sched *sched) ret = wd_init_sched(&wd_comp_setting.sched, sched); if (ret < 0) goto out_clear_ctx_config;
- /*
* Fix me: ctx could be passed into wd_comp_set_static_drv to help to
* choose static compiled vendor driver. For dynamic vendor driver,
* wd_comp_open_driver will be called in the process of opening
* libwd_comp.so to load related driver dynamic library. Vendor driver
* pointer will be passed to wd_comp_setting.driver in the process of
* opening of vendor driver dynamic library. A configure file could be
* introduced to help to define which vendor driver lib should be
* loaded.
*/
-#ifdef WD_STATIC_DRV
- wd_comp_set_static_drv();
-#endif
/* fix me: sadly find we allocate async pool for every ctx */ ret = wd_init_async_request_pool(&wd_comp_setting.pool, @@ -142,25 +131,19 @@ int wd_comp_init(struct wd_ctx_config *config, struct wd_sched *sched) goto out_clear_sched;
/* init ctx related resources in specific driver */
- priv = calloc(1, wd_comp_setting.driver->drv_ctx_size);
- if (!priv) {
ret = -WD_ENOMEM;
- if (!wd_comp_setting.driver)
wd_comp_get_driver();
- ret = wd_alg_init_driver(&wd_comp_setting.config,
wd_comp_setting.driver,
&wd_comp_setting.priv);
- if (ret) goto out_clear_pool;
}
wd_comp_setting.priv = priv;
ret = wd_comp_setting.driver->init(&wd_comp_setting.config, priv);
if (ret < 0) {
WD_ERR("failed to do driver init, ret = %d!\n", ret);
goto out_free_priv;
}
wd_alg_set_init(&wd_comp_setting.status);
return 0;
-out_free_priv:
- free(priv);
- wd_comp_setting.priv = NULL; out_clear_pool: wd_uninit_async_request_pool(&wd_comp_setting.pool); out_clear_sched:
@@ -179,16 +162,14 @@ void wd_comp_uninit(void) if (!priv) return;
wd_comp_setting.driver->exit(priv);
free(priv);
wd_comp_setting.priv = NULL;
/* uninit async request pool */ wd_uninit_async_request_pool(&wd_comp_setting.pool);
/* unset config, sched, driver */ wd_clear_sched(&wd_comp_setting.sched);
wd_clear_ctx_config(&wd_comp_setting.config);
wd_alg_uninit_driver(&wd_comp_setting.config,
wd_comp_setting.driver, wd_comp_setting.priv);
wd_comp_setting.driver = NULL;
wd_alg_clear_init(&wd_comp_setting.status); }
@@ -196,8 +177,8 @@ void wd_comp_uninit(void) int wd_comp_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params) { enum wd_status status;
- int ret = 0; bool flag;
int ret;
wd_alg_get_init(&wd_comp_setting.status, &status); if (status == WD_INIT) {
@@ -215,55 +196,62 @@ int wd_comp_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_par goto out_uninit; }
- wd_comp_init_attrs.alg = alg;
- wd_comp_init_attrs.sched_type = sched_type;
- /*
* Driver lib file could set by env param.
* Than open tham by wd_dlopen_drv()
* Find the /root/lib/xxx.so and then dlopen
*/
- wd_comp_setting.dlh_list = wd_dlopen_drv(NULL);
- if (!wd_comp_setting.dlh_list) {
WD_ERR("fail to open driver lib files.\n");
goto out_uninit;
- }
- wd_comp_init_attrs.ctx_params = ctx_params ? ctx_params : &wd_comp_ctx_params;
- wd_comp_init_attrs.ctx_config = &wd_comp_ctx;
+res_retry:
- /* Get alg driver and dev name */
- wd_comp_setting.driver = wd_alg_drv_request(task_type, alg);
- if (!wd_comp_setting.driver)
goto out_dlopen;
- wd_comp_sched = wd_sched_rr_alloc(sched_type, wd_comp_init_attrs.ctx_params->op_type_num,
numa_max_node() + 1, wd_comp_poll_ctx);
- if (!wd_comp_sched) {
ret = -WD_EINVAL;
goto out_uninit;
- if (sched_type < 0 || sched_type > SCHED_POLICY_BUTT) {
WD_ERR("fail to check sched_type: %d\n", sched_type);
}goto out_dlopen;
- wd_comp_sched->name = SCHED_RR_NAME;
- wd_comp_init_attrs.sched = wd_comp_sched;
memset(&wd_comp_setting.config, 0, sizeof(struct wd_ctx_config_internal));
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 = ctx_params ? ctx_params :
&wd_comp_ctx_params;
wd_comp_init_attrs.alg_init = wd_comp_init;
wd_comp_init_attrs.alg_poll_ctx = wd_comp_poll_ctx; ret = wd_alg_attrs_init(&wd_comp_init_attrs);
- if (ret)
goto out_freesched;
- ret = wd_comp_init(&wd_comp_ctx, wd_comp_sched);
- if (ret)
goto out_freesched;
if (ret) {
if (ret == -WD_ENODEV) {
wd_disable_drv(wd_comp_setting.driver);
goto res_retry;
}
WD_ERR("fail to init alg attrs.\n");
goto out_dlopen;
}
wd_alg_set_init(&wd_comp_setting.status2);
return 0;
-out_freesched:
- wd_sched_rr_release(wd_comp_sched);
+out_dlopen:
- wd_dlclose_drv(wd_comp_setting.dlh_list); out_uninit: wd_alg_clear_init(&wd_comp_setting.status2);
return ret; }
void wd_comp_uninit2(void) {
int i;
wd_comp_uninit();
for (i = 0; i < wd_comp_ctx.ctx_num; i++)
if (wd_comp_ctx.ctxs[i].ctx) {
wd_release_ctx(wd_comp_ctx.ctxs[i].ctx);
wd_comp_ctx.ctxs[i].ctx = 0;
}
wd_sched_rr_release(wd_comp_sched);
- wd_alg_attrs_uninit(&wd_comp_init_attrs); wd_alg_clear_init(&wd_comp_setting.status2); }
@@ -297,7 +285,7 @@ int wd_comp_poll_ctx(__u32 idx, __u32 expt, __u32 *count) ctx = config->ctxs + idx;
do {
ret = wd_comp_setting.driver->comp_recv(ctx->ctx, &resp_msg);
if (unlikely(ret < 0)) { if (ret == -WD_HW_EACCESS) WD_ERR("wd comp recv hw error!\n");ret = wd_comp_setting.driver->recv(ctx->ctx, &resp_msg);
@@ -513,8 +501,8 @@ static int wd_comp_sync_job(struct wd_comp_sess *sess, wd_dfx_msg_cnt(config->msg_cnt, WD_CTX_CNT_NUM, idx); ctx = config->ctxs + idx;
- msg_handle.send = wd_comp_setting.driver->comp_send;
- msg_handle.recv = wd_comp_setting.driver->comp_recv;
msg_handle.send = wd_comp_setting.driver->send;
msg_handle.recv = wd_comp_setting.driver->recv;
pthread_spin_lock(&ctx->lock); ret = wd_handle_msg_sync(&msg_handle, ctx->ctx, msg,
@@ -773,7 +761,7 @@ int wd_do_comp_async(handle_t h_sess, struct wd_comp_req *req) msg->tag = tag; msg->stream_mode = WD_COMP_STATELESS;
- ret = wd_comp_setting.driver->comp_send(ctx->ctx, msg);
- ret = wd_comp_setting.driver->send(ctx->ctx, msg); if (unlikely(ret < 0)) { WD_ERR("wd comp send error, ret = %d!\n", ret); goto fail_with_msg;
在 2022/11/26 10:38, fanghao (A) 写道:
在 2022/11/24 15:43, liulongfang 写道:
After adding the zip module of the init2 interface, combine its initialization part with dynamic loading, and transform the HiSilicon driver of zip, and use the dynamic loading function to realize the connection between the driver and the algorithm layer.
Signed-off-by: liulongfang liulongfang@huawei.com
drv/hisi_comp.c | 101 +++++++++++++++++++++--- include/drv/wd_comp_drv.h | 27 ------- wd_comp.c | 158 ++++++++++++++++++-------------------- 3 files changed, 163 insertions(+), 123 deletions(-)
diff --git a/drv/hisi_comp.c b/drv/hisi_comp.c index 2eede39..0038d6f 100644 --- a/drv/hisi_comp.c +++ b/drv/hisi_comp.c @@ -13,7 +13,7 @@ #define ZLIB_HEADER "\x78\x9c" #define ZLIB_HEADER_SZ 2
+#define ZIP_CTX_Q_NUM_DEF 2 /* * We use a extra field for gzip block length. So the fourth byte is \x04. * This is necessary because our software don't know the size of block when @@ -771,8 +771,9 @@ static void hisi_zip_sqe_ops_adapt(handle_t h_qp) } } -static int hisi_zip_init(struct wd_ctx_config_internal *config, void *priv) +static int hisi_zip_init(void *conf, void *priv) { + struct wd_ctx_config_internal *config = conf; struct hisi_zip_ctx *zip_ctx = (struct hisi_zip_ctx *)priv; struct hisi_qm_priv qm_priv; handle_t h_qp = 0; @@ -1055,14 +1056,92 @@ static int hisi_zip_comp_recv(handle_t ctx, void *comp_msg) return parse_zip_sqe(qp, &sqe, recv_msg); } -struct wd_comp_driver hisi_zip = { - .drv_name = "hisi_zip", - .alg_name = "zlib\ngzip\ndeflate\nlz77_zstd", - .drv_ctx_size = sizeof(struct hisi_zip_ctx), - .init = hisi_zip_init, - .exit = hisi_zip_exit, - .comp_send = hisi_zip_comp_send, - .comp_recv = hisi_zip_comp_recv, +static struct wd_alg_driver hisi_zlib_driver = { + .drv_name = "hisi_zip", + .alg_name = "zlib", + .priority = UADK_ALG_HW, + .priv_size = sizeof(struct hisi_zip_ctx), + .queue_num = ZIP_CTX_Q_NUM_DEF, + .op_type_num = 2, + .fallback = 0, + .init = hisi_zip_init, + .exit = hisi_zip_exit, + .send = hisi_zip_comp_send, + .recv = hisi_zip_comp_recv, +};
+static struct wd_alg_driver hisi_gzip_driver = { + .drv_name = "hisi_zip", + .alg_name = "gzip", + .priority = UADK_ALG_HW, + .priv_size = sizeof(struct hisi_zip_ctx), + .queue_num = ZIP_CTX_Q_NUM_DEF, + .op_type_num = 2, + .fallback = 0, + .init = hisi_zip_init, + .exit = hisi_zip_exit, + .send = hisi_zip_comp_send, + .recv = hisi_zip_comp_recv, +};
+static struct wd_alg_driver hisi_deflate_driver = { + .drv_name = "hisi_zip", + .alg_name = "deflate", + .priority = UADK_ALG_HW, + .priv_size = sizeof(struct hisi_zip_ctx), + .queue_num = ZIP_CTX_Q_NUM_DEF, + .op_type_num = 2, + .fallback = 0, + .init = hisi_zip_init, + .exit = hisi_zip_exit, + .send = hisi_zip_comp_send, + .recv = hisi_zip_comp_recv, +};
+static struct wd_alg_driver hisi_lz77_zstd_driver = { + .drv_name = "hisi_zip", + .alg_name = "lz77_zstd", + .priority = UADK_ALG_HW, + .priv_size = sizeof(struct hisi_zip_ctx), + .queue_num = ZIP_CTX_Q_NUM_DEF, + .op_type_num = 2, + .fallback = 0, + .init = hisi_zip_init, + .exit = hisi_zip_exit, + .send = hisi_zip_comp_send, + .recv = hisi_zip_comp_recv, }; -WD_COMP_SET_DRIVER(hisi_zip); +static void __attribute__((constructor)) hisi_zip_probe(void) +{ + int ret;
+ WD_ERR("Info: register ZIP alg drivers!\n"); + ret = wd_alg_driver_register(&hisi_zlib_driver); + if (ret) + WD_ERR("Error: register zlib failed!\n");
+ ret = wd_alg_driver_register(&hisi_gzip_driver); + if (ret) + WD_ERR("Error: register gzip failed!\n");
+ ret = wd_alg_driver_register(&hisi_deflate_driver); + if (ret) + WD_ERR("Error: register deflate failed!\n");
+ ret = wd_alg_driver_register(&hisi_lz77_zstd_driver); + if (ret) + WD_ERR("Error: register lz77_zstd failed!\n"); +}
optimize the duplicate code,can combine to one driver?
.alg_name = "zlib\ngzip\ndeflate\nlz77_zstd",
This should not be done, because after this assignment, the query algorithm in alg_list will become more complicated and unclear. I understand that your purpose is to reduce repetitive code, which can actually be handled by defining function macros.
Thansk. Longfang.
and in the wd alg layer can search a alg name from a string.
+static void __attribute__((destructor)) hisi_zip_remove(void) +{ + wd_alg_driver_unregister(&hisi_zlib_driver);
+ wd_alg_driver_unregister(&hisi_gzip_driver);
+ wd_alg_driver_unregister(&hisi_deflate_driver);
+ wd_alg_driver_unregister(&hisi_lz77_zstd_driver); +}
diff --git a/include/drv/wd_comp_drv.h b/include/drv/wd_comp_drv.h index 4aeaee4..213cf2d 100644 --- a/include/drv/wd_comp_drv.h +++ b/include/drv/wd_comp_drv.h @@ -55,35 +55,8 @@ struct wd_comp_msg { __u32 tag; }; -struct wd_comp_driver { - const char *drv_name; - const char *alg_name; - __u32 drv_ctx_size; - int (*init)(struct wd_ctx_config_internal *config, void *priv); - void (*exit)(void *priv); - int (*comp_send)(handle_t ctx, void *comp_msg); - int (*comp_recv)(handle_t ctx, void *comp_msg); -};
-void wd_comp_set_driver(struct wd_comp_driver *drv); -struct wd_comp_driver *wd_comp_get_driver(void);
struct wd_comp_msg *wd_comp_get_msg(__u32 idx, __u32 tag); -#ifdef WD_STATIC_DRV -#define WD_COMP_SET_DRIVER(drv) \ -struct wd_comp_driver *wd_comp_get_driver(void) \ -{ \ - return &drv; \ -} -#else -#define WD_COMP_SET_DRIVER(drv) \ -static void __attribute__((constructor)) set_comp_driver(void) \ -{ \ - wd_comp_set_driver(&(drv)); \ -} -#endif
#ifdef __cplusplus } #endif diff --git a/wd_comp.c b/wd_comp.c index bb142a7..a12769a 100644 --- a/wd_comp.c +++ b/wd_comp.c @@ -19,8 +19,6 @@ #define HW_CTX_SIZE (64 * 1024) #define STREAM_CHUNK (128 * 1024) -#define SCHED_RR_NAME "sched_rr"
#define swap_byte(x) \ ((((x) & 0x000000ff) << 24) | \ (((x) & 0x0000ff00) << 8) | \ @@ -45,17 +43,15 @@ struct wd_comp_setting { enum wd_status status2; struct wd_ctx_config_internal config; struct wd_sched sched; - struct wd_comp_driver *driver; + struct wd_async_msg_pool pool; + struct wd_alg_driver *driver; void *priv; void *dlhandle; - struct wd_async_msg_pool pool; + void *dlh_list; } wd_comp_setting; struct wd_env_config wd_comp_env_config;
static struct wd_init_attrs wd_comp_init_attrs; -static struct wd_ctx_config wd_comp_ctx; -static struct wd_sched *wd_comp_sched; static struct wd_ctx_nums wd_comp_ctx_num[] = { {1, 1}, {1, 1}, {} @@ -67,16 +63,11 @@ static struct wd_ctx_params wd_comp_ctx_params = { .bmp = NULL, }; -#ifdef WD_STATIC_DRV -static void wd_comp_set_static_drv(void) -{ - wd_comp_setting.driver = wd_comp_get_driver(); - if (!wd_comp_setting.driver) - WD_ERR("failed to get driver!\n"); -} -#else static void __attribute__((constructor)) wd_comp_open_driver(void) { + if (wd_comp_setting.dlh_list) + return;
wd_comp_setting.dlhandle = dlopen("libhisi_zip.so", RTLD_NOW); if (!wd_comp_setting.dlhandle) WD_ERR("failed to open libhisi_zip.so, %s\n", dlerror()); @@ -87,16 +78,27 @@ static void __attribute__((destructor)) wd_comp_close_driver(void) if (wd_comp_setting.dlhandle) dlclose(wd_comp_setting.dlhandle); } -#endif -void wd_comp_set_driver(struct wd_comp_driver *drv) +static void wd_comp_get_driver(void) { - wd_comp_setting.driver = drv; + struct wd_alg_driver *driver = NULL; + const char *alg_name = "zlib";
+ /* + * Compatible with the normal acquisition of device + * drivers in the init interface + */ + driver = wd_request_drv(alg_name, false); + if (!driver) { + WD_ERR("failed to get %s driver support\n", alg_name); + return; + }
+ wd_comp_setting.driver = driver; } int wd_comp_init(struct wd_ctx_config *config, struct wd_sched *sched) { - void *priv; bool flag; int ret; @@ -120,19 +122,6 @@ int wd_comp_init(struct wd_ctx_config *config, struct wd_sched *sched) ret = wd_init_sched(&wd_comp_setting.sched, sched); if (ret < 0) goto out_clear_ctx_config; - /* - * Fix me: ctx could be passed into wd_comp_set_static_drv to help to - * choose static compiled vendor driver. For dynamic vendor driver, - * wd_comp_open_driver will be called in the process of opening - * libwd_comp.so to load related driver dynamic library. Vendor driver - * pointer will be passed to wd_comp_setting.driver in the process of - * opening of vendor driver dynamic library. A configure file could be - * introduced to help to define which vendor driver lib should be - * loaded. - */ -#ifdef WD_STATIC_DRV - wd_comp_set_static_drv(); -#endif /* fix me: sadly find we allocate async pool for every ctx */ ret = wd_init_async_request_pool(&wd_comp_setting.pool, @@ -142,25 +131,19 @@ int wd_comp_init(struct wd_ctx_config *config, struct wd_sched *sched) goto out_clear_sched; /* init ctx related resources in specific driver */ - priv = calloc(1, wd_comp_setting.driver->drv_ctx_size); - if (!priv) { - ret = -WD_ENOMEM; + if (!wd_comp_setting.driver) + wd_comp_get_driver();
+ ret = wd_alg_init_driver(&wd_comp_setting.config, + wd_comp_setting.driver, + &wd_comp_setting.priv); + if (ret) goto out_clear_pool; - } - wd_comp_setting.priv = priv; - ret = wd_comp_setting.driver->init(&wd_comp_setting.config, priv); - if (ret < 0) { - WD_ERR("failed to do driver init, ret = %d!\n", ret); - goto out_free_priv; - } wd_alg_set_init(&wd_comp_setting.status); return 0; -out_free_priv: - free(priv); - wd_comp_setting.priv = NULL; out_clear_pool: wd_uninit_async_request_pool(&wd_comp_setting.pool); out_clear_sched: @@ -179,16 +162,14 @@ void wd_comp_uninit(void) if (!priv) return; - wd_comp_setting.driver->exit(priv); - free(priv); - wd_comp_setting.priv = NULL;
/* uninit async request pool */ wd_uninit_async_request_pool(&wd_comp_setting.pool); /* unset config, sched, driver */ wd_clear_sched(&wd_comp_setting.sched); - wd_clear_ctx_config(&wd_comp_setting.config); + wd_alg_uninit_driver(&wd_comp_setting.config, + wd_comp_setting.driver, wd_comp_setting.priv); + wd_comp_setting.driver = NULL; wd_alg_clear_init(&wd_comp_setting.status); } @@ -196,8 +177,8 @@ void wd_comp_uninit(void) int wd_comp_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params) { enum wd_status status; + int ret = 0; bool flag; - int ret; wd_alg_get_init(&wd_comp_setting.status, &status); if (status == WD_INIT) { @@ -215,55 +196,62 @@ int wd_comp_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_par goto out_uninit; } - wd_comp_init_attrs.alg = alg; - wd_comp_init_attrs.sched_type = sched_type; + /* + * Driver lib file could set by env param. + * Than open tham by wd_dlopen_drv() + * Find the /root/lib/xxx.so and then dlopen + */ + wd_comp_setting.dlh_list = wd_dlopen_drv(NULL); + if (!wd_comp_setting.dlh_list) { + WD_ERR("fail to open driver lib files.\n"); + goto out_uninit; + } - wd_comp_init_attrs.ctx_params = ctx_params ? ctx_params : &wd_comp_ctx_params; - wd_comp_init_attrs.ctx_config = &wd_comp_ctx; +res_retry: + /* Get alg driver and dev name */ + wd_comp_setting.driver = wd_alg_drv_request(task_type, alg); + if (!wd_comp_setting.driver) + goto out_dlopen; - wd_comp_sched = wd_sched_rr_alloc(sched_type, wd_comp_init_attrs.ctx_params->op_type_num, - numa_max_node() + 1, wd_comp_poll_ctx); - if (!wd_comp_sched) { - ret = -WD_EINVAL; - goto out_uninit; + if (sched_type < 0 || sched_type > SCHED_POLICY_BUTT) { + WD_ERR("fail to check sched_type: %d\n", sched_type); + goto out_dlopen; } - wd_comp_sched->name = SCHED_RR_NAME; - wd_comp_init_attrs.sched = wd_comp_sched; + memset(&wd_comp_setting.config, 0, sizeof(struct wd_ctx_config_internal)); + 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 = ctx_params ? ctx_params : + &wd_comp_ctx_params; + wd_comp_init_attrs.alg_init = wd_comp_init; + wd_comp_init_attrs.alg_poll_ctx = wd_comp_poll_ctx; ret = wd_alg_attrs_init(&wd_comp_init_attrs); - if (ret) - goto out_freesched;
- ret = wd_comp_init(&wd_comp_ctx, wd_comp_sched); - if (ret) - goto out_freesched; + if (ret) { + if (ret == -WD_ENODEV) { + wd_disable_drv(wd_comp_setting.driver); + goto res_retry; + } + WD_ERR("fail to init alg attrs.\n"); + goto out_dlopen; + } wd_alg_set_init(&wd_comp_setting.status2); return 0; -out_freesched: - wd_sched_rr_release(wd_comp_sched);
+out_dlopen: + wd_dlclose_drv(wd_comp_setting.dlh_list); out_uninit: wd_alg_clear_init(&wd_comp_setting.status2);
return ret; } void wd_comp_uninit2(void) { - int i;
wd_comp_uninit(); - for (i = 0; i < wd_comp_ctx.ctx_num; i++) - if (wd_comp_ctx.ctxs[i].ctx) { - wd_release_ctx(wd_comp_ctx.ctxs[i].ctx); - wd_comp_ctx.ctxs[i].ctx = 0; - }
- wd_sched_rr_release(wd_comp_sched); + wd_alg_attrs_uninit(&wd_comp_init_attrs); wd_alg_clear_init(&wd_comp_setting.status2); } @@ -297,7 +285,7 @@ int wd_comp_poll_ctx(__u32 idx, __u32 expt, __u32 *count) ctx = config->ctxs + idx; do { - ret = wd_comp_setting.driver->comp_recv(ctx->ctx, &resp_msg); + ret = wd_comp_setting.driver->recv(ctx->ctx, &resp_msg); if (unlikely(ret < 0)) { if (ret == -WD_HW_EACCESS) WD_ERR("wd comp recv hw error!\n"); @@ -513,8 +501,8 @@ static int wd_comp_sync_job(struct wd_comp_sess *sess, wd_dfx_msg_cnt(config->msg_cnt, WD_CTX_CNT_NUM, idx); ctx = config->ctxs + idx; - msg_handle.send = wd_comp_setting.driver->comp_send; - msg_handle.recv = wd_comp_setting.driver->comp_recv; + msg_handle.send = wd_comp_setting.driver->send; + msg_handle.recv = wd_comp_setting.driver->recv; pthread_spin_lock(&ctx->lock); ret = wd_handle_msg_sync(&msg_handle, ctx->ctx, msg, @@ -773,7 +761,7 @@ int wd_do_comp_async(handle_t h_sess, struct wd_comp_req *req) msg->tag = tag; msg->stream_mode = WD_COMP_STATELESS; - ret = wd_comp_setting.driver->comp_send(ctx->ctx, msg); + ret = wd_comp_setting.driver->send(ctx->ctx, msg); if (unlikely(ret < 0)) { WD_ERR("wd comp send error, ret = %d!\n", ret); goto fail_with_msg;
.
On 2022/11/24 下午3:43, liulongfang wrote:
Update the current driver adaptation and usage method, from fixed use of Hisilicon device driver to automatic registration of the driver according to the algorithm.
When the algorithm API layer uses the driver, it is no longer bound to the fixed device driver, but dynamically obtained and stored according to the algorithm query to use.
Any example? not understand this. Does applications need to be changed?
On 2022/11/24 17:32, zhangfei.gao@foxmail.com wrote:
On 2022/11/24 下午3:43, liulongfang wrote:
Update the current driver adaptation and usage method, from fixed use of Hisilicon device driver to automatic registration of the driver according to the algorithm.
When the algorithm API layer uses the driver, it is no longer bound to the fixed device driver, but dynamically obtained and stored according to the algorithm query to use.
Any example? not understand this. Does applications need to be changed?
.
For example: static void __attribute__((constructor)) wd_cipher_open_driver(void) { wd_cipher_setting.dlhandle = dlopen("libhisi_sec.so", RTLD_NOW); if (!wd_cipher_setting.dlhandle) WD_ERR("failed to open libhisi_sec.so, %s\n", dlerror()); }
This is the fixed code writing method between the cipher API and the corresponding HiSilicon sec accelerator driver.
It is recommended that you apply the patch to the latest uadk mainline branch. Here, the zip API and driver are modified together. It is very clear to look at the code directly.
Thanks, Longfang.
On 2022/11/24 下午8:27, liulongfang wrote:
On 2022/11/24 17:32, zhangfei.gao@foxmail.com wrote:
On 2022/11/24 下午3:43, liulongfang wrote:
Update the current driver adaptation and usage method, from fixed use of Hisilicon device driver to automatic registration of the driver according to the algorithm.
When the algorithm API layer uses the driver, it is no longer bound to the fixed device driver, but dynamically obtained and stored according to the algorithm query to use.
Any example? not understand this. Does applications need to be changed?
.
For example: static void __attribute__((constructor)) wd_cipher_open_driver(void) { wd_cipher_setting.dlhandle = dlopen("libhisi_sec.so", RTLD_NOW); if (!wd_cipher_setting.dlhandle) WD_ERR("failed to open libhisi_sec.so, %s\n", dlerror()); }
This is the fixed code writing method between the cipher API and the corresponding HiSilicon sec accelerator driver.
It is recommended that you apply the patch to the latest uadk mainline branch. Here, the zip API and driver are modified together. It is very clear to look at the code directly.
Thanks Not find pull request? https://github.com/Linaro/uadk/pulls
We can do ci test based on the pull request.
On 2022/11/24 下午8:27, liulongfang wrote:
On 2022/11/24 17:32, zhangfei.gao@foxmail.com wrote:
On 2022/11/24 下午3:43, liulongfang wrote:
Update the current driver adaptation and usage method, from fixed use of Hisilicon device driver to automatic registration of the driver according to the algorithm.
When the algorithm API layer uses the driver, it is no longer bound to the fixed device driver, but dynamically obtained and stored according to the algorithm query to use.
Any example? not understand this. Does applications need to be changed?
.
For example: static void __attribute__((constructor)) wd_cipher_open_driver(void) { wd_cipher_setting.dlhandle = dlopen("libhisi_sec.so", RTLD_NOW); if (!wd_cipher_setting.dlhandle) WD_ERR("failed to open libhisi_sec.so, %s\n", dlerror()); }
This is the fixed code writing method between the cipher API and the corresponding HiSilicon sec accelerator driver.
It is recommended that you apply the patch to the latest uadk mainline branch. Here, the zip API and driver are modified together. It is very clear to look at the code directly.
The patches can not be applied to the uadk master branch Could you send pr, or push to some branch for easier review.
I still don't understand how to use it. For example. there are two algorithms registered, one hardware, one software, The big package go to hardware, while the small package go to software. Can we handle this case.
We can refer dpdk crypto scheduler. http://doc.dpdk.org/guides/cryptodevs/scheduler.html
"A typical use case in this mode is with the QAT cryptodev as the primary and a software cryptodev as the secondary worker. This may help applications to process additional crypto workload than what the QAT cryptodev can handle on its own, by making use of the available CPU cycles to deal with smaller crypto workloads."
But dpdk crypto scheduler is cut off the call back, so it has to handler differently with different algorithms such as crypto and compress.
Normal case app -> crypto's call back
When crypto scheduler is registered app -> crypto scheduler's call back -> distribution mechanism -> uadk crypto's call back -> software crypto's call back
Thanks