diff --git a/uadk/Makefile.am b/uadk/Makefile.am index 7b81168a5cb73091be4e31a7f8b86f36bf0b4273..3e2c1e96780c3b3ad390464a08e93b99fca036c5 100644 --- a/uadk/Makefile.am +++ b/uadk/Makefile.am @@ -20,12 +20,12 @@ endif # WITH_LOG_FILE # y = minor # z = revision MAJOR = 2 -MINOR = 3 -REVISION = 38 +MINOR = 4 +REVISION = 0 UADK_VERSION = -version-number ${MAJOR}:${MINOR}:${REVISION} -DAY = 11 -MONTH = Jun +DAY = 12 +MONTH = Dec YEAR = 2022 AM_CFLAGS+= -DUADK_VERSION_NUMBER="\"UADK version: ${MAJOR}.${MINOR}.${REVISION}\"" AM_CFLAGS+= -DUADK_RELEASED_TIME="\"Released ${MONTH} ${DAY}, ${YEAR}\"" @@ -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_common.h include/wd_ecc.h include/wd_sched.h + include/wd_ecc.h include/wd_sched.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 v1/wd_comp.h @@ -62,7 +62,7 @@ libwd_la_SOURCES=wd.c wd_mempool.c wd.h \ v1/drv/hisi_rng_udrv.c v1/drv/hisi_rng_udrv.h libwd_comp_la_SOURCES=wd_comp.c wd_comp.h wd_comp_drv.h wd_util.c wd_util.h \ - wd_sched.c wd_sched.h + wd_sched.c wd_sched.h wd.c wd.h wd_zlibwrapper.c wd_zlibwrapper.h libhisi_zip_la_SOURCES=drv/hisi_comp.c hisi_comp.h drv/hisi_qm_udrv.c \ hisi_qm_udrv.h wd_comp_drv.h @@ -74,7 +74,8 @@ libwd_crypto_la_SOURCES=wd_cipher.c wd_cipher.h wd_cipher_drv.h \ wd_ecc.c wd_ecc.h wd_ecc_drv.h \ wd_digest.c wd_digest.h wd_digest_drv.h \ wd_util.c wd_util.h \ - wd_sched.c wd_sched.h + wd_sched.c wd_sched.h \ + wd.c wd.h libhisi_sec_la_SOURCES=drv/hisi_sec.c drv/hisi_qm_udrv.c \ hisi_qm_udrv.h wd_cipher_drv.h wd_aead_drv.h @@ -87,7 +88,7 @@ AM_CFLAGS += -DWD_NO_LOG libwd_la_LIBADD = $(libwd_la_OBJECTS) -lnuma -libwd_comp_la_LIBADD = $(libwd_la_OBJECTS) -ldl +libwd_comp_la_LIBADD = $(libwd_la_OBJECTS) -ldl -lnuma libwd_comp_la_DEPENDENCIES = libwd.la libhisi_zip_la_LIBADD = -ldl @@ -101,20 +102,25 @@ libhisi_sec_la_DEPENDENCIES = libwd.la libwd_crypto.la libhisi_hpre_la_LIBADD = $(libwd_la_OBJECTS) $(libwd_crypto_la_OBJECTS) libhisi_hpre_la_DEPENDENCIES = libwd.la libwd_crypto.la else -libwd_la_LDFLAGS=$(UADK_VERSION) +UADK_WD_SYMBOL= -Wl,--version-script,$(top_srcdir)/libwd.map +UADK_CRYPTO_SYMBOL= -Wl,--version-script,$(top_srcdir)/libwd_crypto.map +UADK_COMP_SYMBOL= -Wl,--version-script,$(top_srcdir)/libwd_comp.map +UADK_V1_SYMBOL= -Wl,--version-script,$(top_srcdir)/v1/libwd.map + +libwd_la_LDFLAGS=$(UADK_VERSION) $(UADK_WD_SYMBOL) $(UADK_V1_SYMBOL) libwd_la_LIBADD= -lnuma -libwd_comp_la_LIBADD= -lwd -ldl -libwd_comp_la_LDFLAGS=$(UADK_VERSION) +libwd_comp_la_LIBADD= -lwd -ldl -lnuma +libwd_comp_la_LDFLAGS=$(UADK_VERSION) $(UADK_COMP_SYMBOL) libwd_comp_la_DEPENDENCIES= libwd.la -libhisi_zip_la_LIBADD= -ldl -libhisi_zip_la_LDFLAGS=$(UADK_VERSION) - libwd_crypto_la_LIBADD= -lwd -ldl -lnuma -libwd_crypto_la_LDFLAGS=$(UADK_VERSION) +libwd_crypto_la_LDFLAGS=$(UADK_VERSION) $(UADK_CRYPTO_SYMBOL) libwd_crypto_la_DEPENDENCIES= libwd.la +libhisi_zip_la_LIBADD= -ldl +libhisi_zip_la_LDFLAGS=$(UADK_VERSION) + libhisi_sec_la_LIBADD= -lwd -lwd_crypto libhisi_sec_la_LDFLAGS=$(UADK_VERSION) libhisi_sec_la_DEPENDENCIES= libwd.la libwd_crypto.la diff --git a/uadk/configure.ac b/uadk/configure.ac index 6991fe4ef102dd98eecf1cbdb3ede22a7dabb4d6..3eb853f289a1e5258a021a822c47177c033009db 100644 --- a/uadk/configure.ac +++ b/uadk/configure.ac @@ -1,5 +1,5 @@ AC_PREREQ([2.69]) -AC_INIT([uadk], [2.3.38], [liguozhu@hisilicon.com]) +AC_INIT([uadk], [2.4], [liguozhu@hisilicon.com]) AC_CONFIG_SRCDIR([wd.c]) AM_INIT_AUTOMAKE([1.10 no-define]) @@ -64,12 +64,6 @@ AC_ARG_WITH(static_drv, AC_SUBST(with_static_drv) AM_CONDITIONAL([WD_STATIC_DRV], [test -n "$with_static_drv"]) -AC_ARG_WITH(openssl_dir, - AS_HELP_STRING([--with-openssl_dir], [Path to OpenSSL source code])) -AC_SUBST(with_openssl_dir) - -AM_CONDITIONAL([WITH_OPENSSL_DIR], [test -n "$with_openssl_dir"]) - AC_ARG_WITH(zlib_fse_dir, AS_HELP_STRING([--with-zlib_fse_dir], [Path to Zlib FSE library])) AC_SUBST(with_zlib_fse_dir) diff --git a/uadk/docs/ReleaseNotes.md b/uadk/docs/ReleaseNotes.md new file mode 100644 index 0000000000000000000000000000000000000000..959405693534b4ee9ba25465f1b4d1476bb44c96 --- /dev/null +++ b/uadk/docs/ReleaseNotes.md @@ -0,0 +1,15 @@ + +# UADK Release v2.4 December 2022 + +## Features: +- Support symmetric encryption algorithms, including AES, SM4, DES/3DES. +- Support asymmetric encryption algorithms, including RSA, DH, SM2, ECDH, ECDSA, x25519, and x448. +- Support compression algorithms, including zlib, gzip, deflate, and lz77_zstd. +- Support custom memory management. +- Support set configuration via environment variables. +- Support queue scheduling. +- Support device query and resource management. +- Support user state epoll. +- Support log query and management. + +## Fixes: diff --git a/uadk/docs/maintenance.md b/uadk/docs/maintenance.md index b0fda8257aff93588e8c862cbfcd575f003ec820..e9888260690ec8b6c894b677914fb80635f0e7ea 100644 --- a/uadk/docs/maintenance.md +++ b/uadk/docs/maintenance.md @@ -3,7 +3,7 @@ ## Getting Started -Make sure you have registered in the mailing list "linux-accelerators@lists.ozlabs.org". +Make sure you have registered in the mailing list "acc@lists.linaro.org". Clone UADK from [Github](https://github.com/Linaro/uadk). @@ -15,8 +15,8 @@ libwd is adopting Apache License 2.0. ### Include statement ording -All header files that are included by a source file must use the following, -grouped ordering. This is to improve readability (by making it easier to +All header files that are included by a source file must use the following,\ +grouped ordering. This is to improve readability (by making it easier to\ quickly read through the list of headers) and maintainability. **System** includes: Head files from the standard *C* library, such as @@ -28,7 +28,7 @@ quickly read through the list of headers) and maintainability. **Internal** includes: Head files relating to an internal component within UADK. -Within each group, **\#include** statements must be in alphabetical order, +Within each group, **\#include** statements must be in alphabetical order,\ taking both the file and directory names into account. Groups must be separated by a single blank line for clarity. @@ -49,8 +49,8 @@ is better written as: }; ``` -This allows function declarations in other header files that depend on the -struct/enum to forward declare the struct/enum instead of including the entire +This allows function declarations in other header files that depend on the\ +struct/enum to forward declare the struct/enum instead of including the entire\ header: ``` @@ -65,18 +65,18 @@ instead of: ## Making Changes -Keep the commits on topic. +Keep the commits on topic.\ Please test your changes. ## Submitting Changes -Ensure that each commit in the series has at least one **Signed-off-by:** line, -using your real name and email address. The names in the **Signed-off-by:** -and **Author:** lines must match. If anyone else contributes to the commit, +Ensure that each commit in the series has at least one **Signed-off-by:** line,\ +using your real name and email address. The names in the **Signed-off-by:**\ +and **Author:** lines must match. If anyone else contributes to the commit,\ they must also add their own **Signed-off-by:** line. -Submit your changes for review at the mailing list -"linux-accelerators@lists.ozlabs.org" targeting the **master** branch. +Submit your changes for review at the mailing list\ +"acc@lists.linaro.org" targeting the **master** branch. Or submit your changes for review in [Github](https://github.com/Linaro/uadk). @@ -84,20 +84,30 @@ When the changes are accepted, the maintainers will integrate them. ## Library Versions -UADK could be built in dynamic library that is only linked for execution. -Because of this, multiple different UADK dynamic libraries could coexist in -system if the library versions are different. And application could link -any library with specified library version. +UADK could be built in dynamic libraries that are only linked for execution.\ +Because of this, multiple different UADK dynamic libraries could coexist in\ +the system if the library versions are different. And application could link\ +any library with a specified library version. The library version likes libNAME.so.{x}.{y}.{z} ``` -{x} stand for primary version, should change when APIs are changed which - making things incompatible. -{y} stand for sub version, accumulated for three monthes. -{z} stand for mirror version, accumulated for each work week. +{x} stands for the primary version, and should be changed when APIs are + changed which makes things incompatible. +{y} stands for the release version, likely released twice each year. +{z} stands for minor version, for the major bug fix. ``` +### UADK Release + +Likely two releases each year in May and November.\ +Tag {x}.{y} is for release, while {z} is for the major bug fixes.\ +In the meantime, ReleaseNotes is required to describe release contents. + +ReleasesNotes:\ +Features:\ +Fixes: + ## Working Branch @@ -107,24 +117,25 @@ Clone kernel from [Github](https://github.com/Linaro/uadk). Current working branch: uacce-devel - This branch is based on current mainline kernel Linux X.X-rcX. Patches which - are under reviewed in community will be added into this branch. + This branch is based on the current mainline kernel Linux X.X-rcX.\ + Patches that are under review in the community will be added to this branch. Release branch: uacce-devel-X.X - Current working branch will be changed to Release branch once the mainline - kernel which current working branch is based on is released. + The current working branch will be changed to the Release branch once the\ + mainline kernel which the current working branch is based is released. ### UADK Branch Current working branch: master - tags like wd-X.X will be added to match with kernel release branch - uacce-devel-X.X. However, UADK should be alway compatible with + tags like wd-X.X will be added to match with the kernel release branch\ + uacce-devel-X.X. However, UADK should be always compatible with\ former kernel versions. ## Main maintainers - +``` Haojian Zhuang Zhou Wang Longfang Liu +``` diff --git a/uadk/docs/uadk_file_relationship.svg b/uadk/docs/uadk_file_relationship.svg new file mode 100644 index 0000000000000000000000000000000000000000..9a62988284086204d6d1d8f68c54ab03d0c6a310 --- /dev/null +++ b/uadk/docs/uadk_file_relationship.svg @@ -0,0 +1,40 @@ + + +wd.h +wd_util.h +wd.c +wd_alg_common.h +wd_util.c +wd_sched.h +wd_sched.c +wd_aead.c +wd_cipher.c +wd_digest.c +wd_comp.c +wd_rsa.c +wd_dh.c +wd_ecc.c +wd_mempool.c +wd_aead.h +wd_aead_drv.h +wd_cipher.h +wd_cipher_drv.h +wd_digest.h +wd_digest_drv.h +wd_comp.h +wd_comp_drv.h +config.h +wd_rsa_drv.h +wd_dh.h +wd_dh_drv.h +wd_ecc.h +wd_ecc_drv.h +wd_ecc_curve.h +hisi_qm_udrv.h +hisi_qm_udrv.c +uacce.h +hisi_comp.c +hisi_sec.c +hisi_hpre.c +wd_rsa.h \ No newline at end of file diff --git a/uadk/docs/wd_alg_init2.md b/uadk/docs/wd_alg_init2.md new file mode 100644 index 0000000000000000000000000000000000000000..e1d168da250fcf063fa977fbe1e2f06613f4d79c --- /dev/null +++ b/uadk/docs/wd_alg_init2.md @@ -0,0 +1,159 @@ +# wd_alg_init2 + +## Preface + +The current uadk initialization process is: +1.Call wd_request_ctx() to request ctxs from devices. +2.Call wd_sched_rr_alloc() to create a sched(or some other scheduler alloc function if exits). +3.Initialize the sched. +4.Call wd_alg_init() with ctx_config and sched. + +```flow +st=>start: Start +o1=>operation: request ctxs +o2=>operation: create uadk_sched and instance ctxs to sched region +o3=>operation: call wd_alg_init +e=>end +st->o1->o2->o3->e +``` + +Logic is reasonable. But in practice, the step of wd_request_ctx() +and wd_sched_rr_alloc() are very tedious. This makes it difficult +for users to use the interface. One of the main reasons for this is +that uadk has made a lot of configurations in the scheduler in order +to provide users with better performance. Based on this consideration, +the current uadk requires the user to arrange the division of hardware +resources according to the device topology during initialization. +Therefore, as a high-level interface, this scheme can provide customized +scheme configuration for users with deep needs. + +## wd_alg_init2 + +### Design + +Is there any way to simplify these steps? Not currently. Because the +architecture model designed by uadk is to manage hardware resources +through a scheduler, users can no longer perceive after specifying +hardware resources, and all subsequent tasks are handled by the scheduler. +The original intention of this design is to make the scenarios supported +by uadk more flexible. Because the resource requirements of different +business scenarios are different from the task model of the business +itself, the best performance experience can be obtained through the +scheduler to match. + +But we can try to provide a layer of encapsulation. The original design +intention of this layer of encapsulation is that users only need to +specify available resources and requirements, and the configuration of +resources is completed internally by the interface. Because the previous +interface complexity mainly lies in the parameter configuration of CTX +and scheduler, it is easy for users to make configuration errors and +generate bugs because of their misunderstanding of parameters. + +All algorithms have the same input parameters and initialization logic. + +```c +struct wd_ctx_config { + __u32 ctx_num; + struct wd_ctx *ctxs; + void *priv; +}; + +struct wd_sched { + const char *name; + int sched_policy; + handle_t (*sched_init)(handle_t h_sched_ctx, void *sched_param); + __u32 (*pick_next_ctx)(handle_t h_sched_ctx, void *sched_key, + const int sched_mode); + int (*poll_policy)(handle_t h_sched_ctx, __u32 expect, __u32 *count); + handle_t h_sched_ctx; +}; + +int wd_alg_init(struct wd_ctx_config *config, struct wd_sched *sched); +``` + +`wd_ctx_config` is the requested ctxs descriptor, and the attributes +of ctxs are contained in their own structure. The attributes will be +used in scheduler for picking ctx according to request type. The main +difficulty in this step is that users need to apply for CTXs from the +appropriate device nodes according to their own business distribution. +If the user does not consider the appropriate device distribution, +it may lead to cross chip or cross numa node which will affect +performance. + +`wd_sched` is the scheduler descriptor of the request. It will create +the scheduling domain based parameters passed by the users. User needs +to allocate the ctxs applied to the scheduling domain that meets the +attribute, so that uadk can select the appropriate ctxs according to +the issued business. The main difficulty in this step is that the user +needs to initialize the correct scheduling domain according to the ctxs +attributes previously applied. However, there are many attributes of +ctxs here, which should be divided by multiple dimensions. If the +parameters are not understood enough, it is easy to make queue +allocation errors, resulting in the scheduling of the wrong ctxs when +the task is finally issued, and cause unexpected errors. + +Therefore, the next thing to be done is to use limited and easy-to-use +input parameters to describe users' requirements on the two input +parameters, ensuring that the functions of the new interface init2 +are the same as those of init. For ease of description, v1 is used +to refer to the existing interface, and v2 is used to refer to the +layer of encapsulation. + +Let's clarify the following logic first: all uacce devices under a +numa node can be regarded as the same. So although we request for +ctxs from the device, we manage ctxs according to numa nodes. +That means if users want to get the same performance for all cpu, +the uadk configure should be same for all numa node. + +At present, at least 4 parameters are required to meet the user +configuration requirements with the V1 interface function remains +unchanged. + +@alg: The algorithm users wanted. + +@sched_type: Scheduling type the user wants to use. + +@task_type: Reserved. + +@wd_ctx_params: op_type_num and ctx_set_num means the requested ctx +number for each numa node. Due to users may have different requirements +for different types of ctx numbers, needs a two-dimensional array as +input. The bitmask provided by libnuma. Users can use this parameter +to control requesting ctxs devices in the bind NUMA scenario. +This parameter is mainly convenient for users to use in the binding +cpu scenario. It can avoid resource waste or initialization failure +caused by insufficient resources. Libnuma provides a complete operation +interface which can be found in numa.h. + +To sum up, the wd_alg_init2_() is as follows + +```c +struct wd_ctx_nums { + __u32 sync_ctx_num; + __u32 async_ctx_num; +}; + +struct wd_ctx_params { + __u32 op_type_num; + struct wd_ctx_nums *ctx_set_num; + struct bitmask *bmp; +}; + +init wd_alg_init2_(char *alg, __u32 sched_type, int task_type, + struct wd_ctx_params *ctx_params); +``` + +Somebody may say that the wd_alg_init2_() is still complex for three +input parameters are structure. So the interface support default value +for some parameters. The @bmp can be set as NULL, and then it will be +initialized according to device list. The @cparams can be set as NULL, +and it has a default value in wd_alg.c. So there is a simpler interface +wd_alg_init2(). + +```c +#define wd_alg_init2(alg, sched_type, task_type) \ + wd_alg_init2_(alg, sched_type, task_type, NULL) +``` + +Please do not use this interface with wd_comp_init() together, +or some resources may be leak. diff --git a/uadk/docs/wd_design.md b/uadk/docs/wd_design.md index ba5a5b9d59171d87055f92b813a5a5fcf5be23cf..3e5297e61e9c8a49da4f72381b07ec0ef283efa2 100644 --- a/uadk/docs/wd_design.md +++ b/uadk/docs/wd_design.md @@ -81,6 +81,7 @@ | | |2) Change *user* layer to *sched* layer since | | | | sample_sched is moved from user space into UADK | | | | framework. | +| 1.4 | |1) Update *wd_alg_init* reentrancy. | ## Terminology @@ -493,7 +494,9 @@ device. Return 0 if it succeeds. And return error number if it fails. In *wd_comp_init()*, context resources, user scheduler and vendor driver are -initialized. +initialized. This function supports multi-threaded concurrent calls and +reentrant. When one thread is initializing, other threads will wait for +completion. ***void wd_comp_uninit(void)*** diff --git a/uadk/drv/hisi_comp.c b/uadk/drv/hisi_comp.c index c940c3f2064dfe920c269ea830d0ee1e33d12c24..2eede391fe61e996485286d35edc9ee064f7bf33 100644 --- a/uadk/drv/hisi_comp.c +++ b/uadk/drv/hisi_comp.c @@ -4,7 +4,6 @@ #include #include "drv/wd_comp_drv.h" #include "hisi_qm_udrv.h" -#include "wd.h" #define ZLIB 0 #define GZIP 1 diff --git a/uadk/drv/hisi_hpre.c b/uadk/drv/hisi_hpre.c index 552a5650c4502fb9872f2cac387efdf6a7b3d943..d8b212d9619b6f8a08532383b06ad572e3de7a45 100644 --- a/uadk/drv/hisi_hpre.c +++ b/uadk/drv/hisi_hpre.c @@ -1054,11 +1054,16 @@ static void ecc_get_io_len(__u32 atype, __u32 hsz, size_t *ilen, } } -static bool is_all_zero(struct wd_dtb *e, struct wd_ecc_msg *msg) +static bool is_all_zero(struct wd_dtb *e) { int i; - for (i = 0; i < e->dsize && i < msg->key_bytes; i++) { + if (!e || !e->data) { + WD_ERR("invalid: e or e->data is NULL\n"); + return true; + } + + for (i = 0; i < e->bsize; i++) { if (e->data[i]) return false; } @@ -1094,12 +1099,12 @@ static int ecc_prepare_sign_in(struct wd_ecc_msg *msg, return -WD_EINVAL; } hw_msg->sm2_ksel = 1; - } else if (is_all_zero(k, msg)) { + } else if (is_all_zero(k)) { WD_ERR("invalid: ecc sign in k all zero!\n"); return -WD_EINVAL; } - if (is_all_zero(e, msg)) { + if (is_all_zero(e)) { WD_ERR("invalid: ecc sign in e all zero!\n"); return -WD_EINVAL; } @@ -1141,7 +1146,7 @@ static int ecc_prepare_verf_in(struct wd_ecc_msg *msg, s = &vin->s; r = &vin->r; - if (is_all_zero(e, msg)) { + if (is_all_zero(e)) { WD_ERR("invalid: ecc verf in e all zero!\n"); return -WD_EINVAL; } @@ -1282,7 +1287,7 @@ static int u_is_in_p(struct wd_ecc_msg *msg) return -WD_EINVAL; } - if (is_all_zero(&pbk->x, msg)) { + if (is_all_zero(&pbk->x)) { WD_ERR("invalid: ux is zero!\n"); return -WD_EINVAL; } @@ -2191,11 +2196,11 @@ static int sm2_convert_dec_out(struct wd_ecc_msg *src, struct wd_sm2_dec_out *dout = &out->param.dout; struct wd_ecc_in *in = (void *)src->req.src; struct wd_sm2_dec_in *din = &in->param.din; + char buff[MAX_HASH_LENS] = {0}; struct wd_ecc_dh_out *dh_out; __u32 ksz = dst->key_bytes; struct wd_ecc_point x2y2; - struct wd_dtb tmp = {0}; - char buff[64] = {0}; + struct wd_dtb u = {0}; int ret; /* @@ -2211,8 +2216,6 @@ static int sm2_convert_dec_out(struct wd_ecc_msg *src, x2y2.x.dsize = ksz; x2y2.y.dsize = ksz; - tmp.data = buff; - /* t = KDF(x2 || y2, klen) */ ret = sm2_kdf(&dout->plaintext, &x2y2, din->c2.dsize, &src->hash); if (unlikely(ret)) { @@ -2220,20 +2223,21 @@ static int sm2_convert_dec_out(struct wd_ecc_msg *src, return ret; } - /* M' = C2 XOR t */ + /* M' = c2 XOR t */ sm2_xor(&dout->plaintext, &din->c2); - /* u = hash(x2 || M' || y2), save u to din->c2 */ - ret = sm2_hash(&tmp, &x2y2, &dout->plaintext, &src->hash); + /* u = hash(x2 || M' || y2) */ + u.data = buff; + ret = sm2_hash(&u, &x2y2, &dout->plaintext, &src->hash); if (unlikely(ret)) { WD_ERR("failed to compute c3, ret = %d!\n", ret); return ret; } - /* u == c3 */ - ret = is_equal(&tmp, &din->c3); + /* Judge whether u == c3 */ + ret = is_equal(&u, &din->c3); if (ret) - WD_ERR("failed to decode sm2, u != C3!\n"); + WD_ERR("failed to decode sm2, u != c3!\n"); return ret; } diff --git a/uadk/drv/hisi_qm_udrv.c b/uadk/drv/hisi_qm_udrv.c index 6647840863ead5845f19faecfb62fc76071613d2..a3b5dc19810b10c881c60edffd3213f149940b08 100644 --- a/uadk/drv/hisi_qm_udrv.c +++ b/uadk/drv/hisi_qm_udrv.c @@ -8,6 +8,7 @@ #include #include "hisi_qm_udrv.h" +#include "wd_util.h" #define QM_DBELL_CMD_SQ 0 #define QM_DBELL_CMD_CQ 1 diff --git a/uadk/drv/hisi_sec.c b/uadk/drv/hisi_sec.c index 5b676e9cf58d2d6f6e20181b264c91286d4e3c0e..c30b653b8d00e6bbeed47b01520c264da8a06646 100644 --- a/uadk/drv/hisi_sec.c +++ b/uadk/drv/hisi_sec.c @@ -8,10 +8,6 @@ #include "../include/drv/wd_digest_drv.h" #include "../include/drv/wd_aead_drv.h" #include "hisi_qm_udrv.h" -#include "wd_cipher.h" -#include "wd_digest.h" -#include "wd_aead.h" -#include "wd.h" #define SEC_DIGEST_ALG_OFFSET 11 #define WORD_ALIGNMENT_MASK 0x3 diff --git a/uadk/include/drv/wd_aead_drv.h b/uadk/include/drv/wd_aead_drv.h index de4a7d7ee153fa1c589644d2f00ac6cb3c315dec..7c657f66f402c780f4d50e6261d03347fc10413c 100644 --- a/uadk/include/drv/wd_aead_drv.h +++ b/uadk/include/drv/wd_aead_drv.h @@ -4,9 +4,8 @@ #ifndef __WD_AEAD_DRV_H #define __WD_AEAD_DRV_H -#include "../wd_common.h" -#include "../wd_alg_common.h" #include "../wd_aead.h" +#include "../wd_util.h" #ifdef __cplusplus extern "C" { diff --git a/uadk/include/drv/wd_cipher_drv.h b/uadk/include/drv/wd_cipher_drv.h index f2417a816e110a4206e56e7085bffc7d39ac87ed..82fb89a472769101017c9ae2a47f880fce50051c 100644 --- a/uadk/include/drv/wd_cipher_drv.h +++ b/uadk/include/drv/wd_cipher_drv.h @@ -5,9 +5,9 @@ #define __WD_CIPHER_DRV_H #include -#include "../wd_common.h" -#include "../wd_alg_common.h" + #include "../wd_cipher.h" +#include "../wd_util.h" #ifdef __cplusplus extern "C" { diff --git a/uadk/include/drv/wd_comp_drv.h b/uadk/include/drv/wd_comp_drv.h index 4c37116180ddd38709f0e89719d63666eb0f90ea..4aeaee487b057ea9e9f3eec752611ca6ac290623 100644 --- a/uadk/include/drv/wd_comp_drv.h +++ b/uadk/include/drv/wd_comp_drv.h @@ -6,9 +6,9 @@ #include #include -#include "../wd_common.h" -#include "../wd_alg_common.h" + #include "../wd_comp.h" +#include "../wd_util.h" #ifdef __cplusplus extern "C" { diff --git a/uadk/include/drv/wd_dh_drv.h b/uadk/include/drv/wd_dh_drv.h index 57f774d6292ef2f31e87a5a44cc949811feed619..64144e1f6cfdaa6e2cfbf4785a7481f3a3668361 100644 --- a/uadk/include/drv/wd_dh_drv.h +++ b/uadk/include/drv/wd_dh_drv.h @@ -5,8 +5,9 @@ #define __WD_DH_DRV_H #include -#include "../wd_alg_common.h" + #include "../wd_dh.h" +#include "../wd_util.h" #ifdef __cplusplus extern "C" { diff --git a/uadk/include/drv/wd_digest_drv.h b/uadk/include/drv/wd_digest_drv.h index 7ba1c30df1a1a4dd4fe001ddca26f77d594119d6..586588b28678682d5034d5f30e7da613db9bf7e2 100644 --- a/uadk/include/drv/wd_digest_drv.h +++ b/uadk/include/drv/wd_digest_drv.h @@ -3,9 +3,8 @@ #ifndef __WD_DIGEST_DRV_H #define __WD_DIGEST_DRV_H -#include "../wd_common.h" -#include "../wd_alg_common.h" #include "../wd_digest.h" +#include "../wd_util.h" #ifdef __cplusplus extern "C" { diff --git a/uadk/include/drv/wd_ecc_drv.h b/uadk/include/drv/wd_ecc_drv.h index ddf5e5b3b5ccc70053f7e3507341f3e4a43b584a..4d27ccbaa4ce1456d5b06aa73e90a7f7fabccfff 100644 --- a/uadk/include/drv/wd_ecc_drv.h +++ b/uadk/include/drv/wd_ecc_drv.h @@ -7,8 +7,8 @@ #include #include -#include "../wd.h" #include "../wd_ecc.h" +#include "../wd_util.h" #ifdef __cplusplus extern "C" { diff --git a/uadk/include/drv/wd_rsa_drv.h b/uadk/include/drv/wd_rsa_drv.h index 108c2a3987d41d904256993cf7796d3ee66f51d7..baf112f46fad4d1c6caa6c0969ad03652de2115e 100644 --- a/uadk/include/drv/wd_rsa_drv.h +++ b/uadk/include/drv/wd_rsa_drv.h @@ -6,6 +6,7 @@ #include #include "../wd_rsa.h" +#include "../wd_util.h" #ifdef __cplusplus extern "C" { diff --git a/uadk/include/hisi_qm_udrv.h b/uadk/include/hisi_qm_udrv.h index dda320d26268031f6bd0a124dba1278b0a6a07f5..32f81838f6f6761577a634cf66014b53201d33c3 100644 --- a/uadk/include/hisi_qm_udrv.h +++ b/uadk/include/hisi_qm_udrv.h @@ -10,8 +10,7 @@ #include #include "config.h" -#include "wd.h" -#include "wd_alg_common.h" +#include "wd_util.h" #ifdef __cplusplus extern "C" { diff --git a/uadk/include/wd.h b/uadk/include/wd.h index b0580ba6b00fb12b2b32c8762e9eea3392ec4246..e102fe29746e0223f9f20692a93228550abac30a 100644 --- a/uadk/include/wd.h +++ b/uadk/include/wd.h @@ -30,6 +30,13 @@ extern "C" { #define WD_CTX_CNT_NUM 1024 #define WD_IPC_KEY 0x500011 +/* Required compiler attributes */ +#define likely(x) __builtin_expect(!!(x), 1) +#define unlikely(x) __builtin_expect(!!(x), 0) + +#define handle_t uintptr_t +typedef struct wd_dev_mask wd_dev_mask_t; + typedef void (*wd_log)(const char *format, ...); #ifndef WD_NO_LOG @@ -91,12 +98,12 @@ typedef void (*wd_log)(const char *format, ...); #define WD_IS_ERR(h) ((uintptr_t)(h) > \ (uintptr_t)(-1000)) -static inline void *WD_ERR_PTR(uintptr_t error) -{ - return (void *)error; -} +enum wd_buff_type { + WD_FLAT_BUF, + WD_SGL_BUF, +}; -enum wcrypto_type { +enum wd_alg_type { WD_CIPHER, WD_DIGEST, WD_AEAD, @@ -141,9 +148,6 @@ struct wd_dev_mask { unsigned int magic; }; -#define handle_t uintptr_t -typedef struct wd_dev_mask wd_dev_mask_t; - #if defined(__AARCH64_CMODEL_SMALL__) && __AARCH64_CMODEL_SMALL__ #define dsb(opt) { asm volatile("dsb " #opt : : : "memory"); } #define rmb() dsb(ld) /* read fence */ @@ -185,6 +189,16 @@ static inline void wd_iowrite64(void *addr, uint64_t value) *((volatile uint64_t *)addr) = value; } +static inline void *WD_ERR_PTR(uintptr_t error) +{ + return (void *)error; +} + +static inline long WD_PTR_ERR(const void *ptr) +{ + return (long)ptr; +} + /** * wd_request_ctx() - Request a communication context from a device. * @dev: Indicate one device. @@ -343,6 +357,16 @@ int wd_get_avail_ctx(struct uacce_dev *dev); */ struct uacce_dev_list *wd_get_accel_list(const char *alg_name); +/** + * wd_find_dev_by_numa() - get device with max available ctx number from an + * device list according to numa id. + * @list: The device list. + * @numa_id: The numa_id. + * + * Return device if succeed and other error number if fail. + */ +struct uacce_dev *wd_find_dev_by_numa(struct uacce_dev_list *list, int numa_id); + /** * wd_get_accel_dev() - Get device supporting the algorithm with smallest numa distance to current numa node. @@ -503,6 +527,35 @@ void wd_mempool_stats(handle_t mempool, struct wd_mempool_stats *stats); */ void wd_blockpool_stats(handle_t blkpool, struct wd_blockpool_stats *stats); +/** + * wd_clone_dev() - clone a new uacce device. + * @dev: The source device. + * + * Return a pointer value if succeed, and NULL if fail. + */ +struct uacce_dev *wd_clone_dev(struct uacce_dev *dev); + +/** + * wd_add_dev_to_list() - add a node to end of list. + * @head: The list head. + * @node: The node need to be add. + */ +void wd_add_dev_to_list(struct uacce_dev_list *head, struct uacce_dev_list *node); + +/** + * wd_create_device_nodemask() - create a numa node mask of device list. + * @list: The devices list. + * + * Return a pointer value if succeed, and error number if fail. + */ +struct bitmask *wd_create_device_nodemask(struct uacce_dev_list *list); + +/** + * wd_free_device_nodemask() - free a numa node mask. + * @bmp: A numa node mask. + */ +void wd_free_device_nodemask(struct bitmask *bmp); + /** * wd_ctx_get_dev_name() - Get the device name about task. * @h_ctx: The handle of context. diff --git a/uadk/include/wd_aead.h b/uadk/include/wd_aead.h index 3be8e8ad61488a6c03b7544ba2e0577cbb48b7f0..ba7d0623ac7d31947f0b476ad29056a28e4928f6 100644 --- a/uadk/include/wd_aead.h +++ b/uadk/include/wd_aead.h @@ -8,8 +8,6 @@ #define __WD_AEAD_H #include -#include "config.h" -#include "wd.h" #include "wd_alg_common.h" #include "wd_cipher.h" #include "wd_digest.h" diff --git a/uadk/include/wd_alg_common.h b/uadk/include/wd_alg_common.h index c455dc355e742b0de626e59562d13b82a5ff1bbc..56539cc6100291753325ec1e15efb72e9d5f1831 100644 --- a/uadk/include/wd_alg_common.h +++ b/uadk/include/wd_alg_common.h @@ -10,17 +10,15 @@ #include #include #include "wd.h" -#include "wd_common.h" #ifdef __cplusplus extern "C" { #endif -/* Required compiler attributes */ -#define likely(x) __builtin_expect(!!(x), 1) -#define unlikely(x) __builtin_expect(!!(x), 0) +#define BYTE_BITS 8 +#define BYTE_BITS_SHIFT 3 +#define GET_NEGATIVE(val) (0 - (val)) -#define BYTE_BITS_SHIFT 3 #define BITS_TO_BYTES(bits) (((bits) + 7) >> 3) #define BYTES_TO_BITS(bytes) ((bytes) << 3) @@ -63,6 +61,33 @@ struct wd_ctx_config { void *priv; }; +/** + * struct wd_ctx_nums - Define the ctx sets numbers. + * @sync_ctx_num: The ctx numbers which are used for sync mode for each + * ctx sets. + * @async_ctx_num: The ctx numbers which are used for async mode for each + * ctx sets. + */ +struct wd_ctx_nums { + __u32 sync_ctx_num; + __u32 async_ctx_num; +}; + +/** + * struct wd_ctx_params - Define the ctx sets params which are used for init + * algorithms. + * @op_type_num: Used for index of ctx_set_num, the order is the same as + * wd__op_type. + * @ctx_set_num: Each operation type ctx sets numbers. + * @bmp: Ctxs distribution. Means users want to run business process on these + * numa or request ctx from devices located in these numa. + */ +struct wd_ctx_params { + __u32 op_type_num; + struct wd_ctx_nums *ctx_set_num; + struct bitmask *bmp; +}; + struct wd_ctx_internal { handle_t ctx; __u8 op_type; @@ -83,7 +108,7 @@ struct wd_ctx_config_internal { * struct wd_comp_sched - Define a scheduler. * @name: Name of this scheduler. * @sched_policy: Method for scheduler to perform scheduling - * @sched_init: inited the scheduler input parameters. + * @sched_init: inited the scheduler input parameters. * @pick_next_ctx: Pick the proper ctx which a request will be sent to. * config points to the ctx config; sched_ctx points to * scheduler context; req points to the request. Return diff --git a/uadk/include/wd_cipher.h b/uadk/include/wd_cipher.h index dfd6e60c84cc0134957e5c88aa20627854996943..8e6985293ec89b743ebe7930ba909ebdb856210f 100644 --- a/uadk/include/wd_cipher.h +++ b/uadk/include/wd_cipher.h @@ -8,7 +8,6 @@ #define __WD_CIPHER_H #include -#include "wd.h" #include "wd_alg_common.h" #ifdef __cplusplus diff --git a/uadk/include/wd_common.h b/uadk/include/wd_common.h deleted file mode 100644 index 91dd066eb82d63495e72781fd74541b7dcce358b..0000000000000000000000000000000000000000 --- a/uadk/include/wd_common.h +++ /dev/null @@ -1,23 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -/* - * Copyright 2020-2021 Huawei Technologies Co.,Ltd. All rights reserved. - * Copyright 2020-2021 Linaro ltd. - */ - -#ifndef __WD_COMMON_H -#define __WD_COMMON_H - -#ifdef __cplusplus -extern "C" { -#endif - -enum wd_buff_type { - WD_FLAT_BUF, - WD_SGL_BUF, -}; - -#ifdef __cplusplus -} -#endif - -#endif /* __WD_COMMON_H */ diff --git a/uadk/include/wd_comp.h b/uadk/include/wd_comp.h index e043a834af5a54a31795118b257c88eb78c83f47..45b0d0b48506885c954dc4dfbf22684e34da5a4a 100644 --- a/uadk/include/wd_comp.h +++ b/uadk/include/wd_comp.h @@ -7,7 +7,8 @@ #ifndef __WD_COMP_H #define __WD_COMP_H -#include "wd.h" +#include + #include "wd_alg_common.h" #ifdef __cplusplus @@ -113,6 +114,35 @@ int wd_comp_init(struct wd_ctx_config *config, struct wd_sched *sched); */ void wd_comp_uninit(void); +/** + * wd_comp_init2_() - A simplify interface to initializate uadk + * compression/decompression. This interface keeps most functions of + * wd_comp_init(). Users just need to descripe the deployment of + * business scenarios. Then the initialization will request appropriate + * resources to support the business scenarios. + * To make the initializate simpler, ctx_params support set NULL. + * And then the function will set them as default. + * Please do not use this interface with wd_comp_init() together, or + * some resources may be leak. + * + * @alg: The algorithm users want to use. + * @sched_type: The scheduling type users want to use. + * @task_type: Reserved. + * @ctx_params: The ctxs resources users want to use. Include per operation + * type ctx numbers and business process run numa. + * + * Return 0 if succeed and others if fail. + */ +int wd_comp_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params); + +#define wd_comp_init2(alg, sched_type, task_type) \ + wd_comp_init2_(alg, sched_type, task_type, NULL) + +/** + * wd_comp_uninit2() - Uninitialise ctx configuration and scheduler. + */ +void wd_comp_uninit2(void); + struct wd_comp_sess_setup { enum wd_comp_alg_type alg_type; /* Denoted by enum wd_comp_alg_type */ enum wd_comp_level comp_lv; /* Denoted by enum wd_comp_level */ @@ -128,11 +158,18 @@ struct wd_comp_sess_setup { handle_t wd_comp_alloc_sess(struct wd_comp_sess_setup *setup); /** - * wd_comp_free_sess() - Free a wd comp session. + * wd_comp_free_sess() - Free a wd comp session. * @h_sess: The sess to be freed. */ void wd_comp_free_sess(handle_t h_sess); +/** + * wd_comp_reset_sess() - Reset a wd comp session. After reset h_sess, it can + * used for a new stream request. + * @h_sess: The sess to be reset. + */ +int wd_comp_reset_sess(handle_t h_sess); + /** * wd_do_comp_sync() - Send a sync compression request. * @h_sess: The session which request will be sent to. diff --git a/uadk/include/wd_dh.h b/uadk/include/wd_dh.h index f3427221a7b12169f4a2ab9de5a6a16fde0d8072..391268076397602075a1bc86890f382a829abcd3 100644 --- a/uadk/include/wd_dh.h +++ b/uadk/include/wd_dh.h @@ -9,17 +9,12 @@ #include -#include "wd.h" #include "wd_alg_common.h" #ifdef __cplusplus extern "C" { #endif -#define BYTE_BITS 8 -#define BYTE_BITS_SHIFT 3 -#define GET_NEGATIVE(val) (0 - (val)) - typedef void (*wd_dh_cb_t)(void *cb_param); enum wd_dh_op_type { diff --git a/uadk/include/wd_digest.h b/uadk/include/wd_digest.h index eb764ce052fa0498aebf59c2c119b3994b806f0d..a44328ea5a9cfaefef16151755e3215d36f0c3a9 100644 --- a/uadk/include/wd_digest.h +++ b/uadk/include/wd_digest.h @@ -8,7 +8,6 @@ #define __WD_DIGEST_H #include -#include "wd.h" #include "wd_alg_common.h" #ifdef __cplusplus diff --git a/uadk/include/wd_ecc.h b/uadk/include/wd_ecc.h index 05a776dd8b8edeeaa1803a54ef38ca3ed8119dec..a92bb4bbae099f8be47e8f805c2a3ae19bb25bd6 100644 --- a/uadk/include/wd_ecc.h +++ b/uadk/include/wd_ecc.h @@ -10,7 +10,6 @@ #include #include -#include "wd.h" #include "wd_alg_common.h" #ifdef __cplusplus diff --git a/uadk/include/wd_rsa.h b/uadk/include/wd_rsa.h index ef7a12c968671a727f0c030c4f5b917762802c06..e16171f22b72017c1fb0d3673c4dacd9f9f1714e 100644 --- a/uadk/include/wd_rsa.h +++ b/uadk/include/wd_rsa.h @@ -9,20 +9,16 @@ #include -#include "wd.h" #include "wd_alg_common.h" #ifdef __cplusplus extern "C" { #endif -#define BYTE_BITS 8 -#define BYTE_BITS_SHIFT 3 #define CRT_PARAMS_SZ(key_size) ((5 * (key_size)) >> 1) #define CRT_GEN_PARAMS_SZ(key_size) ((7 * (key_size)) >> 1) #define GEN_PARAMS_SZ(key_size) ((key_size) << 1) #define CRT_PARAM_SZ(key_size) ((key_size) >> 1) -#define GET_NEGATIVE(val) (0 - (val)) typedef void (*wd_rsa_cb_t)(void *cb_param); diff --git a/uadk/include/wd_util.h b/uadk/include/wd_util.h index 83ac5f8491bfdb5bb3f8161b56e156bbfce48b9f..d4b2814f11687160cae719d65b9ff2d7d3e5468b 100644 --- a/uadk/include/wd_util.h +++ b/uadk/include/wd_util.h @@ -7,11 +7,13 @@ #ifndef __WD_UTIL_H #define __WD_UTIL_H +#include #include #include #include #include -#include "wd_alg_common.h" + +#include "wd_sched.h" #ifdef __cplusplus extern "C" { @@ -21,6 +23,12 @@ extern "C" { for ((i) = 0, (config_numa) = (config)->config_per_numa; \ (i) < (config)->numa_num; (config_numa)++, (i)++) +enum wd_status { + WD_UNINIT, + WD_INITING, + WD_INIT, +}; + struct wd_async_msg_pool { struct msg_pool *pools; __u32 pool_num; @@ -106,6 +114,14 @@ struct wd_msg_handle { int (*recv)(handle_t sess, void *msg); }; +struct wd_init_attrs { + __u32 sched_type; + char *alg; + struct wd_sched *sched; + struct wd_ctx_params *ctx_params; + struct wd_ctx_config *ctx_config; +}; + /* * wd_init_ctx_config() - Init internal ctx configuration. * @in: ctx configuration in global setting. @@ -356,6 +372,57 @@ int wd_handle_msg_sync(struct wd_msg_handle *msg_handle, handle_t ctx, */ int wd_init_param_check(struct wd_ctx_config *config, struct wd_sched *sched); +/** + * wd_alg_try_init() - Check the algorithm status and set it as WD_INITING + * if need initialization. + * @status: algorithm initialization status. + * + * Return true if need initialization and false if initialized, otherwise will wait + * last initialization result. + */ +bool wd_alg_try_init(enum wd_status *status); + +/** + * wd_alg_set_init() - Set the algorithm status as WD_INIT. + * @status: algorithm initialization status. + */ +static inline void wd_alg_set_init(enum wd_status *status) +{ + enum wd_status setting = WD_INIT; + + __atomic_store(status, &setting, __ATOMIC_RELAXED); +} + +/** + * wd_alg_get_init() - Get the algorithm status. + * @status: algorithm initialization status. + * @value: value of algorithm initialization status. + */ +static inline void wd_alg_get_init(enum wd_status *status, enum wd_status *value) +{ + __atomic_load(status, value, __ATOMIC_RELAXED); +} + +/** + * wd_alg_clear_init() - Set the algorithm status as WD_UNINIT. + * @status: algorithm initialization status. + */ +static inline void wd_alg_clear_init(enum wd_status *status) +{ + enum wd_status setting = WD_UNINIT; + + __atomic_store(status, &setting, __ATOMIC_RELAXED); +} + +/** + * wd_alg_pre_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); + /** * wd_dfx_msg_cnt() - Message counter interface for ctx * @msg: Shared memory addr. diff --git a/uadk/libwd.map b/uadk/libwd.map new file mode 100644 index 0000000000000000000000000000000000000000..459f9ba47a0ad3ecfa3921771c37a94db0acfedd --- /dev/null +++ b/uadk/libwd.map @@ -0,0 +1,45 @@ +UADK_2.0 { +global: + wd_request_ctx; + wd_release_ctx; + wd_ctx_get_api; + wd_ctx_wait; + wd_is_sva; + wd_is_isolate; + wd_get_accel_name; + wd_get_numa_id; + wd_get_avail_ctx; + wd_get_accel_list; + wd_get_accel_dev; + wd_free_list_accels; + + wd_release_ctx_force; + wd_ctx_set_priv; + wd_ctx_get_priv; + wd_ctx_start; + wd_ctx_mmap_qfr; + wd_ctx_unmap_qfr; + wd_ctx_set_io_cmd; + wd_ctx_get_region_size; + wd_ctx_get_dev_name; + + wd_block_alloc; + wd_block_free; + wd_blockpool_create; + wd_blockpool_destroy; + wd_mempool_create; + wd_mempool_destroy; + wd_mempool_stats; + wd_blockpool_stats; + wd_get_version; + wd_need_debug; + wd_need_info; + + wd_create_device_nodemask; + wd_free_device_nodemask; + wd_clone_dev; + wd_add_dev_to_list; + wd_find_dev_by_numa; + +local: *; +}; diff --git a/uadk/libwd_comp.map b/uadk/libwd_comp.map new file mode 100644 index 0000000000000000000000000000000000000000..ce3e0511f57f783b5fd88408244b94192860b915 --- /dev/null +++ b/uadk/libwd_comp.map @@ -0,0 +1,41 @@ +UADK_COMP_2.0 { +global: + wd_comp_init; + wd_comp_uninit; + wd_comp_init2; + wd_comp_init2_; + wd_comp_uninit2; + wd_comp_alloc_sess; + wd_comp_free_sess; + wd_do_comp_sync; + wd_do_comp_strm; + wd_do_comp_async; + wd_comp_poll_ctx; + wd_comp_poll; + wd_do_comp_sync2; + wd_comp_env_init; + wd_comp_env_uninit; + wd_comp_ctx_num_init; + wd_comp_ctx_num_uninit; + wd_comp_get_env_param; + wd_comp_set_driver; + wd_comp_get_driver; + wd_comp_get_msg; + + wd_sched_rr_instance; + wd_sched_rr_alloc; + wd_sched_rr_release; + + wd_deflateInit_; + wd_deflateInit2_; + wd_deflate; + wd_deflateReset; + wd_deflateEnd; + wd_inflateInit_; + wd_inflateInit2_; + wd_inflate; + wd_inflateReset; + wd_inflateEnd; + +local: *; +}; diff --git a/uadk/libwd_crypto.map b/uadk/libwd_crypto.map new file mode 100644 index 0000000000000000000000000000000000000000..5fadc5316b629b36bfe9aaf45bb2d0edf7901745 --- /dev/null +++ b/uadk/libwd_crypto.map @@ -0,0 +1,173 @@ +UADK_CRYPTO_2.0 { +global: + wd_cipher_init; + wd_cipher_uninit; + wd_cipher_alloc_sess; + wd_cipher_free_sess; + wd_cipher_set_key; + wd_do_cipher_sync; + wd_do_cipher_async; + wd_cipher_poll_ctx; + wd_cipher_poll; + wd_cipher_env_init; + wd_cipher_env_uninit; + wd_cipher_ctx_num_init; + wd_cipher_ctx_num_uninit; + wd_cipher_get_env_param; + wd_cipher_set_driver; + wd_cipher_get_driver; + wd_cipher_get_msg; + + wd_aead_init; + wd_aead_uninit; + wd_aead_alloc_sess; + wd_aead_free_sess; + wd_aead_set_ckey; + wd_aead_set_akey; + wd_do_aead_sync; + wd_do_aead_async; + wd_aead_set_authsize; + wd_aead_get_authsize; + wd_aead_get_maxauthsize; + wd_aead_poll_ctx; + wd_aead_poll; + wd_aead_env_init; + wd_aead_env_uninit; + wd_aead_ctx_num_init; + wd_aead_ctx_num_uninit; + wd_aead_get_env_param; + wd_aead_set_driver; + wd_aead_get_driver; + wd_aead_get_msg; + + wd_digest_init; + wd_digest_uninit; + wd_digest_alloc_sess; + wd_digest_free_sess; + wd_do_digest_sync; + wd_do_digest_async; + wd_digest_set_key; + wd_digest_poll_ctx; + wd_digest_poll; + wd_digest_env_init; + wd_digest_env_uninit; + wd_digest_ctx_num_init; + wd_digest_ctx_num_uninit; + wd_digest_get_env_param; + wd_digest_set_driver; + wd_digest_get_driver; + wd_digest_get_msg; + + wd_rsa_is_crt; + wd_rsa_get_key_bits; + wd_rsa_get_pubkey; + wd_rsa_get_prikey; + wd_rsa_set_pubkey_params; + wd_rsa_get_pubkey_params; + wd_rsa_set_prikey_params; + wd_rsa_get_prikey_params; + wd_rsa_set_crt_prikey_params; + wd_rsa_get_crt_prikey_params; + wd_rsa_new_kg_in; + wd_rsa_del_kg_in; + wd_rsa_get_kg_in_params; + wd_rsa_new_kg_out; + wd_rsa_del_kg_out; + wd_rsa_get_kg_out_params; + wd_rsa_get_kg_out_crt_params; + wd_rsa_kg_in_data; + wd_rsa_kg_out_data; + wd_rsa_set_kg_out_crt_psz; + wd_rsa_set_kg_out_psz; + wd_rsa_init; + wd_rsa_uninit; + wd_rsa_alloc_sess; + wd_rsa_free_sess; + wd_do_rsa_async; + wd_rsa_poll; + wd_do_rsa_sync; + wd_do_rsa_async; + wd_rsa_poll_ctx; + wd_rsa_env_init; + wd_rsa_env_uninit; + wd_rsa_ctx_num_init; + wd_rsa_ctx_num_uninit; + wd_rsa_get_env_param; + wd_rsa_set_driver; + wd_rsa_get_driver; + wd_rsa_get_msg; + + wd_dh_get_mode; + wd_dh_key_bits; + wd_dh_set_g; + wd_dh_get_g; + wd_dh_alloc_sess; + wd_dh_free_sess; + wd_do_dh_async; + wd_do_dh_sync; + wd_dh_poll_ctx; + wd_dh_poll; + wd_dh_init; + wd_dh_uninit; + wd_dh_env_init; + wd_dh_env_uninit; + wd_dh_ctx_num_init; + wd_dh_ctx_num_uninit; + wd_dh_get_env_param; + wd_dh_set_driver; + wd_dh_get_driver; + wd_dh_get_msg; + + wd_ecc_get_key_bits; + wd_ecc_get_key; + wd_ecc_set_prikey; + wd_ecc_get_prikey; + wd_ecc_set_pubkey; + wd_ecc_get_pubkey; + wd_ecc_del_in; + wd_ecc_del_out; + wd_ecc_get_prikey_params; + wd_ecc_get_pubkey_params; + wd_ecxdh_new_in; + wd_ecxdh_new_out; + wd_ecxdh_get_out_params; + wd_ecc_set_driver; + wd_ecc_get_driver; + wd_ecc_get_msg; + + wd_sm2_new_sign_in; + wd_sm2_new_verf_in; + wd_sm2_new_enc_in; + wd_sm2_new_dec_in; + wd_sm2_new_sign_out; + wd_sm2_new_enc_out; + wd_sm2_new_dec_out; + wd_sm2_new_kg_out; + wd_sm2_get_sign_out_params; + wd_sm2_get_kg_out_params; + wd_sm2_get_enc_out_params; + wd_sm2_get_dec_out_params; + + wd_ecdsa_new_sign_in; + wd_ecdsa_new_verf_in; + wd_ecdsa_new_sign_out; + wd_ecdsa_get_sign_out_params; + wd_ecc_init; + wd_ecc_uninit; + wd_ecc_alloc_sess; + wd_ecc_free_sess; + wd_ecc_poll; + wd_do_ecc_sync; + wd_do_ecc_async; + wd_ecc_poll_ctx; + wd_ecc_env_init; + wd_ecc_env_uninit; + wd_ecc_ctx_num_init; + wd_ecc_ctx_num_uninit; + wd_ecc_get_env_param; + + wd_sched_rr_instance; + wd_sched_rr_alloc; + wd_sched_rr_release; +local: *; +}; diff --git a/uadk/test/Makefile.am b/uadk/test/Makefile.am index 81e2d2c1241556de2855a7662e5312d32a2b8e4b..860a103c1d3ed8399d93f3808acee1bd577a2060 100644 --- a/uadk/test/Makefile.am +++ b/uadk/test/Makefile.am @@ -1,4 +1,4 @@ -AM_CFLAGS=-Wall -O0 -Werror -fno-strict-aliasing -I$(top_srcdir)/include -I$(top_srcdir) -lpthread +AM_CFLAGS=-Wall -O0 -Werror -fno-strict-aliasing -I$(top_srcdir)/include -I$(top_srcdir) bin_PROGRAMS=wd_mempool_test wd_mempool_test_SOURCES=wd_mempool_test.c @@ -6,9 +6,9 @@ wd_mempool_test_SOURCES=wd_mempool_test.c if WD_STATIC_DRV AM_CFLAGS+=-Bstatic wd_mempool_test_LDADD=../.libs/libwd.a ../.libs/libwd_crypto.a \ - ../.libs/libhisi_sec.a -lnuma + ../.libs/libhisi_sec.a -lnuma -lpthread else -wd_mempool_test_LDADD=-L../.libs -l:libwd.so.2 -l:libwd_crypto.so.2 -lnuma +wd_mempool_test_LDADD=-L../.libs -lwd -lwd_crypto -lnuma -lpthread endif wd_mempool_test_LDFLAGS=-Wl,-rpath,'/usr/local/lib' diff --git a/uadk/test/hisi_hpre_test/hpre_test_sample.h b/uadk/test/hisi_hpre_test/hpre_test_sample.h index 921ae49fdd3ea4edeabd469ded58b2fc96b40ab1..e4966d1ee3f15c2c0c6cba5e39c3149e162dd6d1 100644 --- a/uadk/test/hisi_hpre_test/hpre_test_sample.h +++ b/uadk/test/hisi_hpre_test/hpre_test_sample.h @@ -2,7 +2,7 @@ #ifndef __HPRE_TEST_SAMPLE_H #define __HPRE_TEST_SAMPLE_H -#ifndef WITH_OPENSSL_DIR +#ifndef HAVE_CRYPTO static unsigned char rsa_e_1024[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, diff --git a/uadk/test/hisi_hpre_test/test_hisi_hpre.c b/uadk/test/hisi_hpre_test/test_hisi_hpre.c index 57d76fda5c468cd40e66db9762059fe3f8871925..fbefef87c701ce891b9eae0c0304e0cc56e9467e 100644 --- a/uadk/test/hisi_hpre_test/test_hisi_hpre.c +++ b/uadk/test/hisi_hpre_test/test_hisi_hpre.c @@ -2,7 +2,6 @@ /* Copyright 2020-2021 Huawei Technologies Co.,Ltd. All rights reserved. */ //#define DEBUG -//#define WITH_OPENSSL_DIR #include #include @@ -248,7 +247,7 @@ static struct hpre_test_config g_config = { .k_len = INVALID_LEN, .hash_type = HASH_SM3, .rand_type = RAND_CB, - #ifdef WITH_OPENSSL_DIR + #ifdef HAVE_CRYPTO .check = 1, #else .check = 0, @@ -810,7 +809,7 @@ const EVP_MD *EVP_md5(void); EVP_PKEY_CTX_ctrl(ctx, -1, -1, \ EVP_PKEY_CTRL_SET1_ID, (int)id_len, (void *)id) -#ifndef WITH_OPENSSL_DIR +#ifndef HAVE_CRYPTO BIGNUM *BN_new(void) { return NULL; @@ -6773,7 +6772,7 @@ int hpre_test_write_to_file(__u8 *out, int size, char *out_file, return fd; } -#ifndef WITH_OPENSSL_DIR +#ifndef HAVE_CRYPTO static int get_rsa_key_from_test_sample(handle_t sess, char *pubkey_file, char *privkey_file, char *crt_privkey_file, int is_file) @@ -7599,7 +7598,7 @@ new_test_again: memset(key_info, 0, key_size * 16); - #ifdef WITH_OPENSSL_DIR + #ifdef HAVE_CRYPTO ret = test_rsa_key_gen(sess, NULL, key_info, key_info, 0); if (ret) { HPRE_TST_PRT("thrd-%d:Openssl key gen fail!\n", thread_id); @@ -8045,7 +8044,7 @@ void *_rsa_async_op_test_thread(void *data) wd_rsa_get_pubkey(sess, &pubkey); wd_rsa_get_pubkey_params(pubkey, &wd_e, &wd_n); -#ifdef WITH_OPENSSL_DIR +#ifdef HAVE_CRYPTO wd_e->dsize = BN_bn2bin(ssl_params.e, (unsigned char *)wd_e->data); if (wd_e->dsize > wd_e->bsize) { HPRE_TST_PRT("e bn to bin overflow!\n"); @@ -8066,7 +8065,7 @@ void *_rsa_async_op_test_thread(void *data) if (wd_rsa_is_crt(sess)) { wd_rsa_get_crt_prikey_params(prikey, &wd_dq, &wd_dp, &wd_qinv, &wd_q, &wd_p); -#ifdef WITH_OPENSSL_DIR +#ifdef HAVE_CRYPTO /* CRT mode private key */ wd_dq->dsize = BN_bn2bin(ssl_params.dq, (unsigned char *)wd_dq->data); if (wd_dq->dsize > wd_dq->bsize) { @@ -8109,7 +8108,7 @@ void *_rsa_async_op_test_thread(void *data) } else { wd_rsa_get_prikey_params(prikey, &wd_d, &wd_n); -#ifdef WITH_OPENSSL_DIR +#ifdef HAVE_CRYPTO wd_d->dsize = BN_bn2bin(ssl_params.d, (unsigned char *)wd_d->data); wd_n->dsize = BN_bn2bin(ssl_params.n, (unsigned char *)wd_n->data); #else @@ -8124,7 +8123,7 @@ void *_rsa_async_op_test_thread(void *data) memset(rsa_key_in->e, 0, key_size); memset(rsa_key_in->p, 0, key_size >> 1); memset(rsa_key_in->q, 0, key_size >> 1); -#ifdef WITH_OPENSSL_DIR +#ifdef HAVE_CRYPTO rsa_key_in->e_size = BN_bn2bin(ssl_params.e, (unsigned char *)rsa_key_in->e); rsa_key_in->p_size = BN_bn2bin(ssl_params.p, (unsigned char *)rsa_key_in->p); rsa_key_in->q_size = BN_bn2bin(ssl_params.q, (unsigned char *)rsa_key_in->q); @@ -8280,7 +8279,7 @@ static int set_ssl_plantext(void) return 0; } -#ifdef WITH_OPENSSL_DIR +#ifdef HAVE_CRYPTO static int rsa_openssl_key_gen_for_async_test(void) { int ret; @@ -8544,7 +8543,7 @@ static int rsa_async_test(int thread_num, __u64 lcore_mask, return ret; } - #ifdef WITH_OPENSSL_DIR + #ifdef HAVE_CRYPTO ret = rsa_openssl_key_gen_for_async_test(); if(ret) { HPRE_TST_PRT("openssl genkey for async thread test fail!"); diff --git a/uadk/uadk_tool/benchmark/uadk_benchmark.c b/uadk/uadk_tool/benchmark/uadk_benchmark.c index d3b9ef37914ea59780aa46de9fb46eeba77385c0..752553d05ce8d7b786982aa7ddf87c03cb3b989d 100644 --- a/uadk/uadk_tool/benchmark/uadk_benchmark.c +++ b/uadk/uadk_tool/benchmark/uadk_benchmark.c @@ -422,7 +422,7 @@ static int benchmark_run(struct acc_option *option) ret = sec_wd_benchmark(option); } usleep(20000); -#ifdef WITH_OPENSSL_DIR +#ifdef HAVE_CRYPTO if (option->modetype & SOFT_MODE) { ret = sec_soft_benchmark(option); } diff --git a/uadk/v1/drv/hisi_hpre_udrv.c b/uadk/v1/drv/hisi_hpre_udrv.c index 4a97917af91be6805cbbe0139676386da63762a1..7ce8f47c3e8946b16c8f903de7fe7f358a6595df 100644 --- a/uadk/v1/drv/hisi_hpre_udrv.c +++ b/uadk/v1/drv/hisi_hpre_udrv.c @@ -1111,17 +1111,21 @@ static void correct_random(struct wd_dtb *k) k->data[lens] = 0; } -static bool is_all_zero(struct wd_dtb *e, struct wcrypto_ecc_msg *msg, - const char *p_name) +static bool is_all_zero(struct wd_dtb *e, const char *p_name) { int i; - for (i = 0; i < e->dsize && i < msg->key_bytes; i++) { + if (!e || !e->data) { + WD_ERR("invalid: %s is NULL!\n", p_name); + return true; + } + + for (i = 0; i < e->bsize; i++) { if (e->data[i]) return false; } - WD_ERR("error: %s all zero!\n", p_name); + WD_ERR("invalid: %s all zero!\n", p_name); return true; } @@ -1144,15 +1148,15 @@ static int ecc_prepare_sign_in(struct wcrypto_ecc_msg *msg, e = &in->dgst; if (!in->k_set) { if (msg->op_type != WCRYPTO_SM2_SIGN) { - WD_ERR("random k not set!\n"); + WD_ERR("invalid: random k not set!\n"); return -WD_EINVAL; } hw_msg->sm2_ksel = 1; - } else if (is_all_zero(k, msg, "ecc sgn k")) { + } else if (is_all_zero(k, "ecc sgn k")) { return -WD_EINVAL; } - if (is_all_zero(e, msg, "ecc sgn e")) + if (is_all_zero(e, "ecc sgn e")) return -WD_EINVAL; ret = qm_crypto_bin_to_hpre_bin(e->data, (const char *)e->data, @@ -1192,7 +1196,7 @@ static int ecc_prepare_verf_in(struct wcrypto_ecc_msg *msg, void **data) s = &vin->s; r = &vin->r; - if (is_all_zero(e, msg, "ecc vrf e")) + if (is_all_zero(e, "ecc vrf e")) return -WD_EINVAL; ret = qm_crypto_bin_to_hpre_bin(e->data, (const char *)e->data, @@ -1274,7 +1278,7 @@ static int ecc_prepare_sm2_enc_in(struct wcrypto_ecc_msg *msg, int ret; if (ein->k_set) { - if (is_all_zero(k, msg, "sm2 enc k")) + if (is_all_zero(k, "sm2 enc k")) return -WD_EINVAL; ret = qm_crypto_bin_to_hpre_bin(k->data, (const char *)k->data, @@ -1969,7 +1973,6 @@ static int fill_sm2_dec_sqe(void *message, struct qm_queue_info *info, __u16 i) /* compute d * c1 */ memcpy(dst, req_src, sizeof(*dst)); - dst->op_type = HPRE_SM2_DEC; *(struct wcrypto_ecc_msg **)(dst + 1) = req_src; dst->in = (void *)&din->c1; @@ -2047,8 +2050,8 @@ static int qm_parse_ecc_sqe_general(void *msg, const struct qm_queue_info *info, dma_out = DMA_ADDR(hw_msg->hi_out, hw_msg->low_out); dma_key = DMA_ADDR(hw_msg->hi_key, hw_msg->low_key); - dma_in = DMA_ADDR(hw_msg->hi_in, hw_msg->hi_in); - drv_iova_unmap(q, NULL, (void *)(uintptr_t)dma_in, olen); + dma_in = DMA_ADDR(hw_msg->hi_in, hw_msg->low_in); + drv_iova_unmap(q, NULL, (void *)(uintptr_t)dma_in, ilen); drv_iova_unmap(q, NULL, (void *)(uintptr_t)dma_out, olen); drv_iova_unmap(q, NULL, (void *)(uintptr_t)dma_key, kbytes); @@ -2265,8 +2268,8 @@ static int is_equal(struct wd_dtb *src, struct wd_dtb *dst) static int sm2_hash(struct wd_dtb *out, struct wcrypto_ecc_point *x2y2, struct wd_dtb *msg, struct q_info *q_info) { - struct wcrypto_hash_mt *hash = &q_info->hash; __u64 lens = (__u64)msg->dsize + 2 * (__u64)x2y2->x.dsize; + struct wcrypto_hash_mt *hash = &q_info->hash; char hash_out[MAX_HASH_LENS] = {0}; __u64 in_len = 0; __u32 h_bytes; @@ -2353,8 +2356,10 @@ static int sm2_convert_dec_out(struct wcrypto_ecc_msg *src, struct wcrypto_sm2_dec_out *dout = &out->param.dout; struct wcrypto_ecc_in *in = (void *)src->in; struct wcrypto_sm2_dec_in *din = &in->param.din; + char buff[MAX_HASH_LENS] = {0}; struct wcrypto_ecc_point x2y2; __u32 ksz = dst->key_bytes; + struct wd_dtb u = {0}; int ret; /* dec origin out data fmt: @@ -2362,8 +2367,6 @@ static int sm2_convert_dec_out(struct wcrypto_ecc_msg *src, * final out data fmt: * | plaintext | */ - - /* x2y2 :copy x2y2 into din->c1 */ x2y2.x.data = (void *)dst->out; x2y2.y.data = (void *)(dst->out + ksz); x2y2.x.dsize = ksz; @@ -2376,20 +2379,21 @@ static int sm2_convert_dec_out(struct wcrypto_ecc_msg *src, return ret; } - /* M' = C2 XOR t */ + /* M' = c2 XOR t */ sm2_xor(&dout->plaintext, &din->c2); - /* u = hash(x2 || M' || y2), save u to din->c2 */ - ret = sm2_hash(&din->c1.x, &x2y2, &dout->plaintext, q_info); + /* u = hash(x2 || M' || y2) */ + u.data = buff; + ret = sm2_hash(&u, &x2y2, &dout->plaintext, q_info); if (unlikely(ret)) { WD_ERR("failed to compute c3, ret = %d!\n", ret); return ret; } - /* u == c3 */ - ret = is_equal(&din->c1.x, &din->c3); + /* Judge whether u == c3 */ + ret = is_equal(&u, &din->c3); if (ret) - WD_ERR("failed to dec sm2, u != C3!\n"); + WD_ERR("failed to dec sm2, u != c3!\n"); return ret; } diff --git a/uadk/v1/libwd.map b/uadk/v1/libwd.map new file mode 100644 index 0000000000000000000000000000000000000000..4e6b63905bb0cfbe31df089bbfe0081e137a11b0 --- /dev/null +++ b/uadk/v1/libwd.map @@ -0,0 +1,165 @@ +UADK_1.0 { +global: + wd_request_queue; + wd_release_queue; + wd_send; + wd_recv; + wd_wait; + wd_recv_sync; + wd_reserve_memory; + wd_share_reserved_memory; + wd_get_available_dev_num; + wd_get_node_id; + wd_iova_map; + wd_iova_unmap; + wd_dma_to_va; + wd_register_log; + log_out; + + wd_blkpool_create; + wd_blkpool_destroy; + wd_alloc_blk; + wd_free_blk; + wd_get_free_blk_num; + wd_blk_alloc_failures; + wd_blk_iova_map; + wd_blk_iova_unmap; + wd_blksize; + + wcrypto_create_aead_ctx; + wcrypto_set_aead_ckey; + wcrypto_set_aead_akey; + wcrypto_aead_setauthsize; + wcrypto_aead_getauthsize; + wcrypto_aead_get_maxauthsize; + wcrypto_do_aead; + wcrypto_burst_aead; + wcrypto_aead_poll; + wcrypto_del_aead_ctx; + + wcrypto_create_cipher_ctx; + wcrypto_set_cipher_key; + wcrypto_do_cipher; + wcrypto_cipher_poll; + wcrypto_del_cipher_ctx; + wcrypto_burst_cipher; + + wcrypto_create_digest_ctx; + wcrypto_set_digest_key; + wcrypto_do_digest; + wcrypto_digest_poll; + wcrypto_del_digest_ctx; + wcrypto_burst_digest; + + wcrypto_create_comp_ctx; + wcrypto_do_comp; + wcrypto_comp_poll; + wcrypto_del_comp_ctx; + + wcrypto_create_dh_ctx; + wcrypto_dh_is_g2; + wcrypto_dh_key_bits; + wcrypto_do_dh; + wcrypto_dh_poll; + wcrypto_del_dh_ctx; + wcrypto_set_dh_g; + wcrypto_get_dh_g; + + wcrypto_get_ecc_key_bits; + wcrypto_create_ecc_ctx; + wcrypto_del_ecc_ctx; + wcrypto_get_ecc_key; + wcrypto_set_ecc_prikey; + wcrypto_get_ecc_prikey; + wcrypto_set_ecc_pubkey; + wcrypto_get_ecc_pubkey; + wcrypto_del_ecc_in; + wcrypto_del_ecc_out; + wcrypto_get_ecc_prikey_params; + wcrypto_get_ecc_pubkey_params; + wcrypto_get_ecxdh_in_params; + wcrypto_get_ecxdh_out_params; + wcrypto_new_ecxdh_in; + wcrypto_new_ecxdh_out; + wcrypto_do_ecxdh; + wcrypto_ecxdh_poll; + wcrypto_new_ecdsa_sign_in; + wcrypto_new_ecdsa_verf_in; + wcrypto_new_ecdsa_sign_out; + wcrypto_get_ecdsa_sign_in_params; + wcrypto_get_ecdsa_verf_in_params; + wcrypto_get_ecdsa_sign_out_params; + wcrypto_do_ecdsa; + wcrypto_ecdsa_poll; + + wcrypto_new_sm2_sign_in; + wcrypto_new_sm2_verf_in; + wcrypto_new_sm2_sign_out; + wcrypto_get_sm2_sign_out_params; + wcrypto_new_sm2_enc_in; + wcrypto_new_sm2_dec_in; + wcrypto_new_sm2_enc_out; + wcrypto_new_sm2_dec_out; + wcrypto_new_sm2_kg_out; + wcrypto_get_sm2_kg_out_params; + wcrypto_get_sm2_enc_out_params; + wcrypto_get_sm2_dec_out_params; + wcrypto_do_sm2; + wcrypto_sm2_poll; + + wcrypto_rsa_is_crt; + wcrypto_rsa_key_bits; + wcrypto_create_rsa_ctx; + wcrypto_get_rsa_pubkey; + wcrypto_get_rsa_prikey; + wcrypto_set_rsa_pubkey_params; + wcrypto_get_rsa_pubkey_params; + wcrypto_set_rsa_prikey_params; + wcrypto_get_rsa_prikey_params; + wcrypto_set_rsa_crt_prikey_params; + wcrypto_get_rsa_crt_prikey_params; + wcrypto_new_kg_in; + wcrypto_del_kg_in; + wcrypto_get_rsa_kg_in_params; + wcrypto_new_kg_out; + wcrypto_del_kg_out; + wcrypto_get_rsa_kg_out_params; + wcrypto_get_rsa_kg_out_crt_params; + wcrypto_rsa_kg_in_data; + wcrypto_rsa_kg_out_data; + wcrypto_set_rsa_kg_out_crt_psz; + wcrypto_set_rsa_kg_out_psz; + wcrypto_do_rsa; + wcrypto_rsa_poll; + wcrypto_del_rsa_ctx; + + wcrypto_create_rng_ctx; + wcrypto_del_rng_ctx; + wcrypto_do_rng; + wcrypto_rng_poll; + + wd_sglpool_create; + wd_sglpool_destroy; + wd_alloc_sgl; + wd_free_sgl; + wd_sgl_merge; + wd_sgl_cp_to_pbuf; + wd_sgl_cp_from_pbuf; + wd_sgl_iova_map; + wd_sgl_iova_unmap; + wd_get_last_sge_buf; + wd_get_first_sge_buf; + wd_get_sgl_sge_num; + wd_get_sgl_buf_num; + wd_get_sge_buf; + wd_get_sgl_buf_sum; + wd_get_sgl_mem_size; + wd_get_free_sgl_num; + wd_get_free_sgl_sge_num; + wd_get_free_buf_num; + wd_get_sgl_datalen; + wd_get_sge_datalen; + wd_get_sgl_bufsize; + +local: *; +}; diff --git a/uadk/v1/test/bmm_test/Makefile.am b/uadk/v1/test/bmm_test/Makefile.am index 43467207057e50ca2b3be901f74600cca597e1f5..85895bc63dd6e383060554e0c5c7874c4eee3993 100644 --- a/uadk/v1/test/bmm_test/Makefile.am +++ b/uadk/v1/test/bmm_test/Makefile.am @@ -1,6 +1,6 @@ AM_CFLAGS=-Wall -O0 -fno-strict-aliasing -I$(top_srcdir)/v1/include -pthread -if WITH_OPENSSL_DIR +if HAVE_CRYPTO bin_PROGRAMS=bmm_test bmm_test_SOURCES=bmm_test.c bmm_test.h diff --git a/uadk/v1/test/hisi_hpre_test/Makefile.am b/uadk/v1/test/hisi_hpre_test/Makefile.am index c124bd6e55415a8bc4efa6ea7ae5108367dfe0d4..26f6e98ca8119781b46e09af7abeb27e34c31e77 100644 --- a/uadk/v1/test/hisi_hpre_test/Makefile.am +++ b/uadk/v1/test/hisi_hpre_test/Makefile.am @@ -1,23 +1,23 @@ AM_CFLAGS=-Wall -O0 -fno-strict-aliasing -I$(top_srcdir) -I$(top_srcdir)/v1/internal -pthread -if WITH_OPENSSL_DIR -bin_PROGRAMS=test_hisi_hpre hpre_test_tools test_hisi_hpre_times +if HAVE_CRYPTO +bin_PROGRAMS=test_hisi_hpre_v1 hpre_test_tools_v1 test_hisi_hpre_times_v1 -test_hisi_hpre_SOURCES=test_hisi_hpre.c test_hisi_hpre.h -hpre_test_tools_SOURCES=hpre_test_tools.c -test_hisi_hpre_times_SOURCES=test_hisi_hpre_times.c test_hisi_hpre.h hpre_test_sample.h +test_hisi_hpre_v1_SOURCES=test_hisi_hpre.c test_hisi_hpre.h +hpre_test_tools_v1_SOURCES=hpre_test_tools.c +test_hisi_hpre_times_v1_SOURCES=test_hisi_hpre_times.c test_hisi_hpre.h hpre_test_sample.h if WD_STATIC_DRV -test_hisi_hpre_LDADD=../../../.libs/libwd.la -hpre_test_tools_LDADD=../../../.libs/libwd.la -test_hisi_hpre_times_LDADD=../../../.libs/libwd.la +test_hisi_hpre_v1_LDADD=../../../.libs/libwd.la +hpre_test_tools_v1_LDADD=../../../.libs/libwd.la +test_hisi_hpre_times_v1_LDADD=../../../.libs/libwd.la else -test_hisi_hpre_LDADD=../../../.libs/libwd.so -hpre_test_tools_LDADD=../../../.libs/libwd.so -test_hisi_hpre_times_LDADD=../../../.libs/libwd.so +test_hisi_hpre_v1_LDADD=../../../.libs/libwd.so +hpre_test_tools_v1_LDADD=../../../.libs/libwd.so +test_hisi_hpre_times_v1_LDADD=../../../.libs/libwd.so endif -test_hisi_hpre_LDADD+=$(with_openssl_dir)/libcrypto_wd.so -hpre_test_tools_LDADD+=$(with_openssl_dir)/libcrypto_wd.so -test_hisi_hpre_times_LDADD+=$(with_openssl_dir)/libcrypto_wd.so +test_hisi_hpre_v1_LDADD+=$(libcrypto_LIBS) +hpre_test_tools_v1_LDADD+=$(libcrypto_LIBS) +test_hisi_hpre_times_v1_LDADD+=$(libcrypto_LIBS) endif diff --git a/uadk/v1/test/hisi_hpre_test/hpre_test_tools.c b/uadk/v1/test/hisi_hpre_test/hpre_test_tools.c index d966a9e7073a0ad71b9cb8e68707784837bc3f49..7f562f34ed462f99cc12597a6a64c592f72cc1b9 100644 --- a/uadk/v1/test/hisi_hpre_test/hpre_test_tools.c +++ b/uadk/v1/test/hisi_hpre_test/hpre_test_tools.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "../../wd.h" #include "../../drv/hisi_qm_udrv.h" @@ -66,7 +67,6 @@ struct bn_gencb_st { int xxx; }; typedef struct dh_st DH; -typedef struct big_number BIGNUM; typedef struct bn_gencb_st BN_GENCB; /* struct hpre_queue_mempool { @@ -84,10 +84,10 @@ struct hpre_queue_mempool { int dev; }; */ -static int key_bits = 2048; static int openssl_check; -char* s2b(char* s) { +void s2b(char *s) +{ int i; for(i = 0; i < 128; i++) { printf("%x ", s[i]); @@ -95,7 +95,8 @@ char* s2b(char* s) { printf("\n"); } -char* s2c(char* a, char* b) { +void s2c(char *a, char *b) +{ int i; for(i = 0; i < 128; i++) { printf("%d %x %x\n", i, a[i], b[i]); @@ -1061,15 +1062,15 @@ int hpre_blkpool_operating(char *dev, unsigned int blk_sz, unsigned int blk_num, printf("create ctx pool success!\n"); if (wd_get_free_blk_num(pool, &blk_count) != WD_SUCCESS) { - printf("wd_get_free_blk_num fail, blk count:%u\n", blk_count); - ret = -WD_EINVAL; - goto release_q; - } - if (BLK_NUM_TIMES(blk_num, blk_count) < BLK_NUM_VALUE) { - printf("%u is 87% ;pwer than %u\n", blk_count, blk_num); - ret = -WD_EINVAL; - goto release_q; - } + printf("wd_get_free_blk_num fail, blk count:%u\n", blk_count); + ret = -WD_EINVAL; + goto release_q; + } + if (BLK_NUM_TIMES(blk_num, blk_count) < BLK_NUM_VALUE) { + printf("%u is lower than %u\n", blk_count, blk_num); + ret = -WD_EINVAL; + goto release_q; + } for(i = 0; i < blk_count; i++) { @@ -1623,11 +1624,12 @@ int hpre_node_mask_do(char *dev, unsigned int node, char *alg) } void *hpre_node_mask_thread(void *pthreadid) { - PTHDATA tid = (PTHDATA)pthreadid; + PTHDATA tid = (PTHDATA)pthreadid; - printf("info: thread %d\n dev %s\n node %d\n alg %s\n", tid->threadid, tid->dev, tid->node, tid->alg); + printf("info: thread %d\n dev %s\n node %d\n alg %s\n", + tid->threadid, tid->dev, tid->node, tid->alg); hpre_node_mask_do(tid->dev, tid->node, tid->alg); - // return 0; + return 0; } /*** diff --git a/uadk/v1/test/hisi_sec_test/Makefile.am b/uadk/v1/test/hisi_sec_test/Makefile.am index 713fb0be7acadb352924c599e2829032723f964f..b0ba722fdac32ffbe091a6ca98684b02eb5be2b9 100644 --- a/uadk/v1/test/hisi_sec_test/Makefile.am +++ b/uadk/v1/test/hisi_sec_test/Makefile.am @@ -1,14 +1,15 @@ AM_CFLAGS=-Wall -O0 -fno-strict-aliasing -I$(top_srcdir) -I$(top_srcdir)/include -I$(srcdir) -pthread -if WITH_OPENSSL_DIR -bin_PROGRAMS=test_hisi_sec +if HAVE_CRYPTO +bin_PROGRAMS=test_hisi_sec_v1 -test_hisi_sec_SOURCES=test_hisi_sec.c test_hisi_sec.h +test_hisi_sec_v1_SOURCES=test_hisi_sec.c test_hisi_sec.h if WD_STATIC_DRV -test_hisi_sec_LDADD=../../../.libs/libwd.la $(with_openssl_dir)/libcrypto_wd.so +test_hisi_sec_v1_LDADD=../../../.libs/libwd.la $(libcrypto_LIBS) else -test_hisi_sec_LDADD=../../../.libs/libwd.so $(with_openssl_dir)/libcrypto_wd.so +test_hisi_sec_v1_LDADD=../../../.libs/libwd.so $(libcrypto_LIBS) + endif endif diff --git a/uadk/v1/test/hisi_sec_test/test_hisi_sec.c b/uadk/v1/test/hisi_sec_test/test_hisi_sec.c index be4ee9dd3bed7b1b5e8509a755659532e5466b45..824fe9ebe16b67fcdb2660054cb1a8076dbe11ca 100644 --- a/uadk/v1/test/hisi_sec_test/test_hisi_sec.c +++ b/uadk/v1/test/hisi_sec_test/test_hisi_sec.c @@ -1006,7 +1006,6 @@ static int sec_cipher_sync_test(int thread_num, __u64 lcore_mask, { struct wd_blkpool_setup setup; int i, ret, cnt = 0, j; - int block_num = 128; struct wd_queue *q; void **pool; int qidx; @@ -1054,7 +1053,7 @@ static int sec_cipher_sync_test(int thread_num, __u64 lcore_mask, setup.block_num = MAX_BLOCK_NM; setup.align_size = SQE_SIZE; - SEC_TST_PRT("create pool memory: %lld\n", MAX_BLOCK_NM * setup.block_size); + SEC_TST_PRT("create pool memory: %d\n", MAX_BLOCK_NM * setup.block_size); pool[j] = wd_blkpool_create(&q[j], &setup); if (!pool[j]) { SEC_TST_PRT("%s(): create %dth pool fail!\n", __func__, j); @@ -2258,7 +2257,6 @@ static int test_sec_default_case(void) { unsigned int node_msk = 0; int thread_num = 1; - int direction = 0; pktlen = 16; g_keylen = 16; @@ -2359,7 +2357,6 @@ int main(int argc, char *argv[]) int ret = 0; SEC_TST_PRT("this is a hisi sec test.\n"); - int thread_num = 1; if (!argv[1]) return test_sec_default_case(); diff --git a/uadk/v1/test/hisi_sec_test/test_hisi_sec.h b/uadk/v1/test/hisi_sec_test/test_hisi_sec.h index 337b5ba214f786472f3ed8f7d1e057a1c83b606a..eeeb6261d1524a41db4c88388ab660ae7cc5c017 100644 --- a/uadk/v1/test/hisi_sec_test/test_hisi_sec.h +++ b/uadk/v1/test/hisi_sec_test/test_hisi_sec.h @@ -49,7 +49,7 @@ enum cipher_mode { }; struct cipher_testvec { - const char *key; + char *key; int klen; const char *iv; int ivlen; @@ -67,8 +67,8 @@ enum sec_digest_state { }; struct hash_testvec { - const char *key; - const char *plaintext; + char *key; + char *plaintext; const char *digest; unsigned int psize; unsigned short ksize; diff --git a/uadk/v1/test/hisi_sec_test_sgl/Makefile.am b/uadk/v1/test/hisi_sec_test_sgl/Makefile.am index f7f1ffd7443814b534d3ebca731d2077eac47834..95b977db705bf34a621d5b8b9c4ee69d57cb7847 100644 --- a/uadk/v1/test/hisi_sec_test_sgl/Makefile.am +++ b/uadk/v1/test/hisi_sec_test_sgl/Makefile.am @@ -1,14 +1,14 @@ AM_CFLAGS=-Wall -O0 -fno-strict-aliasing -I$(top_srcdir) -I$(top_srcdir)/include -I$(srcdir) -pthread -if WITH_OPENSSL_DIR -bin_PROGRAMS=test_hisi_sec_sgl +if HAVE_CRYPTO +bin_PROGRAMS=test_hisi_sec_sgl_v1 -test_hisi_sec_sgl_SOURCES=test_hisi_sec_sgl.c test_hisi_sec_sgl.h +test_hisi_sec_sgl_v1_SOURCES=test_hisi_sec_sgl.c test_hisi_sec_sgl.h if WD_STATIC_DRV -test_hisi_sec_sgl_LDADD=../../../.libs/libwd.la $(with_openssl_dir)/libcrypto_wd.so +test_hisi_sec_sgl_v1_LDADD=../../../.libs/libwd.la $(libcrypto_LIBS) else -test_hisi_sec_sgl_LDADD=../../../.libs/libwd.so $(with_openssl_dir)/libcrypto_wd.so +test_hisi_sec_sgl_v1_LDADD=../../../.libs/libwd.so $(libcrypto_LIBS) endif endif diff --git a/uadk/v1/test/hisi_trng_test/Makefile.am b/uadk/v1/test/hisi_trng_test/Makefile.am index fe75651bb3111fecd848e75e52656dc61940b5fe..b561585d4f92b5e0fbc8c4253f3d8b2f4acb071d 100644 --- a/uadk/v1/test/hisi_trng_test/Makefile.am +++ b/uadk/v1/test/hisi_trng_test/Makefile.am @@ -1,20 +1,20 @@ AM_CFLAGS=-Wall -Werror -O0 -fno-strict-aliasing -I$(top_srcdir)/include -I$(srcdir) -pthread -if WITH_OPENSSL_DIR -bin_PROGRAMS=test_hisi_trngu test_hisi_trngk test_hisi_trngp +if HAVE_CRYPTO +bin_PROGRAMS=test_hisi_trngu_v1 test_hisi_trngk_v1 test_hisi_trngp_v1 -test_hisi_trngu_SOURCES=test_hisi_trngu.c -test_hisi_trngk_SOURCES=test_hisi_trngk.c -test_hisi_trngp_SOURCES=test_hisi_trngp.c +test_hisi_trngu_v1_SOURCES=test_hisi_trngu.c +test_hisi_trngk_v1_SOURCES=test_hisi_trngk.c +test_hisi_trngp_v1_SOURCES=test_hisi_trngp.c if WD_STATIC_DRV -test_hisi_trngu_LDADD=../../../.libs/libwd.la -test_hisi_trngk_LDADD=../../../.libs/libwd.la -test_hisi_trngp_LDADD=../../../.libs/libwd.la +test_hisi_trngu_v1_LDADD=../../../.libs/libwd.la +test_hisi_trngk_v1_LDADD=../../../.libs/libwd.la +test_hisi_trngp_v1_LDADD=../../../.libs/libwd.la else -test_hisi_trngu_LDADD=../../../.libs/libwd.so -test_hisi_trngk_LDADD=../../../.libs/libwd.so -test_hisi_trngp_LDADD=../../../.libs/libwd.so +test_hisi_trngu_v1_LDADD=../../../.libs/libwd.so +test_hisi_trngk_v1_LDADD=../../../.libs/libwd.so +test_hisi_trngp_v1_LDADD=../../../.libs/libwd.so endif endif diff --git a/uadk/v1/test/hisi_zip_test/Makefile.am b/uadk/v1/test/hisi_zip_test/Makefile.am index dae3c59dc3694ddb3a1c2e01f3d7ab1be9552302..6cbfd3f2a157c7ca5f8379aa74c29bfc61bdde63 100644 --- a/uadk/v1/test/hisi_zip_test/Makefile.am +++ b/uadk/v1/test/hisi_zip_test/Makefile.am @@ -2,7 +2,7 @@ AM_CFLAGS=-Wall -O0 -fno-strict-aliasing -I$(top_srcdir)/v1 -I$(srcdir)/v1/test AUTOMAKE_OPTIONS = subdir-objects bin_PROGRAMS=test_hisi_zip test_hisi_zlib wd_zip_test wd_zip_test_1630 test_hisi_zip_perf -test_hisi_zip_SOURCES=test_hisi_zip.c ../wd_sched.c ../smm.c +test_hisi_zip_SOURCES=test_hisi_zip.c ../wd_sched.c if WD_STATIC_DRV test_hisi_zip_LDADD=../../../.libs/libwd.a else diff --git a/uadk/v1/test/hisi_zip_test/test_hisi_zip.c b/uadk/v1/test/hisi_zip_test/test_hisi_zip.c index 69ba5908b8719ae75042d902b235bf6c996e84b0..2e92e3968cbe3b8f9f87d1fb495ba827a23cb7a1 100644 --- a/uadk/v1/test/hisi_zip_test/test_hisi_zip.c +++ b/uadk/v1/test/hisi_zip_test/test_hisi_zip.c @@ -30,6 +30,7 @@ #include #include #include "v1/wd_comp.h" +#include "../smm.c" #define SYS_ERR_COND(cond, msg, ...) \ do { \ diff --git a/uadk/v1/test/hisi_zip_test_sgl/Makefile.am b/uadk/v1/test/hisi_zip_test_sgl/Makefile.am index 7ffd760fa20d947151d995888032dabfb69096a3..b9240357f1fab914cf72b9b1a7ace849dc1234ef 100644 --- a/uadk/v1/test/hisi_zip_test_sgl/Makefile.am +++ b/uadk/v1/test/hisi_zip_test_sgl/Makefile.am @@ -2,7 +2,7 @@ AM_CFLAGS=-Wall -O0 -fno-strict-aliasing -I$(top_srcdir)/v1 -I$(top_srcdir) -I$( AUTOMAKE_OPTIONS = subdir-objects bin_PROGRAMS=wd_zip_test_sgl sgl_test -wd_zip_test_sgl_SOURCES=wd_zip_test_sgl.c zip_alg_sgl.c ../smm.c zip_alg_sgl.h wd_sched_sgl.c wd_sched_sgl.h +wd_zip_test_sgl_SOURCES=wd_zip_test_sgl.c zip_alg_sgl.c zip_alg_sgl.h wd_sched_sgl.c wd_sched_sgl.h sgl_test_SOURCES=sgl_test.c sgl_test.h if WD_STATIC_DRV diff --git a/uadk/v1/test/hisi_zip_test_sgl/wd_zip_test_sgl.c b/uadk/v1/test/hisi_zip_test_sgl/wd_zip_test_sgl.c index 331b11bc8beddc3c1bb9ddf4baea41521aabec8e..2e65c561ceb30fcb680367149e0359e00149296b 100644 --- a/uadk/v1/test/hisi_zip_test_sgl/wd_zip_test_sgl.c +++ b/uadk/v1/test/hisi_zip_test_sgl/wd_zip_test_sgl.c @@ -29,6 +29,7 @@ #include "../../wd_comp.h" #include "../../wd_sgl.h" #include "../smm.h" +#include "../smm.c" #ifdef DEBUG_LOG #define dbg(msg, ...) fprintf(stderr, msg, ##__VA_ARGS__) diff --git a/uadk/v1/wd_dh.c b/uadk/v1/wd_dh.c index 25483c130e560be72507d7d901ff659127f249b5..dff8a025da9d72b11002afa02ec1b44d08b050e3 100644 --- a/uadk/v1/wd_dh.c +++ b/uadk/v1/wd_dh.c @@ -30,6 +30,12 @@ #define DH_BALANCE_THRHD 1280 #define DH_RESEND_CNT 8 #define DH_RECV_MAX_CNT 60000000 // 1 min +#define DH_KEYSIZE_768 768 +#define DH_KEYSIZE_1024 1024 +#define DH_KEYSIZE_1536 1536 +#define DH_KEYSIZE_2048 2048 +#define DH_KEYSIZE_3072 3072 +#define DH_KEYSIZE_4096 4096 static __thread int balance; @@ -65,7 +71,19 @@ static int create_ctx_param_check(struct wd_queue *q, return -WD_EINVAL; } - return 0; + /* Key width check */ + switch (setup->key_bits) { + case DH_KEYSIZE_768: + case DH_KEYSIZE_1024: + case DH_KEYSIZE_1536: + case DH_KEYSIZE_2048: + case DH_KEYSIZE_3072: + case DH_KEYSIZE_4096: + return WD_SUCCESS; + default: + WD_ERR("invalid: dh key_bits %u is error!\n", setup->key_bits); + return -WD_EINVAL; + } } static int wcrypto_init_dh_cookie(struct wcrypto_dh_ctx *ctx) diff --git a/uadk/v1/wd_ecc.c b/uadk/v1/wd_ecc.c index e108051e651843bc3f90d3b18268b213c7e69891..e6b771af6f1a5a3150f7646041974df27e8ca9f0 100644 --- a/uadk/v1/wd_ecc.c +++ b/uadk/v1/wd_ecc.c @@ -544,6 +544,7 @@ static struct wcrypto_ecc_in *create_ecc_sign_in(struct wcrypto_ecc_ctx *ctx, { if (is_dgst) return create_ecc_in(ctx, ECC_SIGN_IN_PARAM_NUM); + return create_sm2_sign_in(ctx, m_len); } @@ -1489,7 +1490,6 @@ static int ecc_request_init(struct wcrypto_ecc_msg *req, if (req->op_type == WCRYPTO_ECXDH_GEN_KEY || req->op_type == WCRYPTO_SM2_KG) { struct wcrypto_ecc_point *g = NULL; - wcrypto_get_ecc_prikey_params((void *)key, NULL, NULL, NULL, NULL, &g, NULL); req->in = (void *)g; @@ -1715,11 +1715,11 @@ static bool less_than_latter(struct wd_dtb *d, struct wd_dtb *n) return ret < 0; } -static bool is_all_zero(struct wd_dtb *p, struct wcrypto_ecc_ctx *ctx) +static bool is_all_zero(struct wd_dtb *p) { int i; - for (i = 0; i < p->dsize && i < ctx->key_size; i++) { + for (i = 0; i < p->bsize; i++) { if (p->data[i]) return false; } @@ -1733,7 +1733,7 @@ static bool check_k_param(struct wd_dtb *k, struct wcrypto_ecc_ctx *ctx) int ret; if (unlikely(!k->data)) { - WD_ERR("error: k->data NULL!\n"); + WD_ERR("invalid: k->data NULL!\n"); return false; } @@ -1744,12 +1744,12 @@ static bool check_k_param(struct wd_dtb *k, struct wcrypto_ecc_ctx *ctx) } if (unlikely(!less_than_latter(k, &cv->n))) { - WD_ERR("error: k >= n\n"); + WD_ERR("invalid: k >= n!\n"); return false; } - if (unlikely(is_all_zero(k, ctx))) { - WD_ERR("error: k all zero\n"); + if (unlikely(is_all_zero(k))) { + WD_ERR("invalid: k all zero!\n"); return false; } diff --git a/uadk/v1/wd_rsa.c b/uadk/v1/wd_rsa.c index f0cdda7562d05a58aa862225900a5e8bfae78637..250f47913117cad7dfcdef5791aba78d9e0fccd7 100644 --- a/uadk/v1/wd_rsa.c +++ b/uadk/v1/wd_rsa.c @@ -30,7 +30,10 @@ #define RSA_RESEND_CNT 8 #define RSA_MAX_KEY_SIZE 512 #define RSA_RECV_MAX_CNT 60000000 // 1 min - +#define RSA_KEYSIZE_1024 1024 +#define RSA_KEYSIZE_2048 2048 +#define RSA_KEYSIZE_3072 3072 +#define RSA_KEYSIZE_4096 4096 static __thread int balance; @@ -558,7 +561,17 @@ static int check_q_setup(struct wd_queue *q, struct wcrypto_rsa_ctx_setup *setup return -WD_EINVAL; } - return 0; + /* Key width check */ + switch (setup->key_bits) { + case RSA_KEYSIZE_1024: + case RSA_KEYSIZE_2048: + case RSA_KEYSIZE_3072: + case RSA_KEYSIZE_4096: + return WD_SUCCESS; + default: + WD_ERR("invalid: rsa key_bits %u is error!\n", setup->key_bits); + return -WD_EINVAL; + } } /* Before initiate this context, we should get a queue from WD */ diff --git a/uadk/wd.c b/uadk/wd.c index 6ea17f32311c02556ecd9c62922d14747bae28bf..629c0dfabef158630f764a61476a10864ba723d1 100644 --- a/uadk/wd.c +++ b/uadk/wd.c @@ -18,7 +18,6 @@ #include #include -#include "wd_alg_common.h" #include "wd.h" #define SYS_CLASS_DIR "/sys/class/uacce" @@ -342,7 +341,13 @@ out: return strndup(name, len); } -static struct uacce_dev *clone_uacce_dev(struct uacce_dev *dev) +static void wd_ctx_init_qfrs_offs(struct wd_ctx_h *ctx) +{ + memcpy(&ctx->qfrs_offs, &ctx->dev->qfrs_offs, + sizeof(ctx->qfrs_offs)); +} + +struct uacce_dev *wd_clone_dev(struct uacce_dev *dev) { struct uacce_dev *new; @@ -355,10 +360,14 @@ static struct uacce_dev *clone_uacce_dev(struct uacce_dev *dev) return new; } -static void wd_ctx_init_qfrs_offs(struct wd_ctx_h *ctx) +void wd_add_dev_to_list(struct uacce_dev_list *head, struct uacce_dev_list *node) { - memcpy(&ctx->qfrs_offs, &ctx->dev->qfrs_offs, - sizeof(ctx->qfrs_offs)); + struct uacce_dev_list *tmp = head; + + while (tmp->next) + tmp = tmp->next; + + tmp->next = node; } handle_t wd_request_ctx(struct uacce_dev *dev) @@ -393,7 +402,7 @@ handle_t wd_request_ctx(struct uacce_dev *dev) if (!ctx->drv_name) goto free_dev_name; - ctx->dev = clone_uacce_dev(dev); + ctx->dev = wd_clone_dev(dev); if (!ctx->dev) goto free_drv_name; @@ -633,17 +642,6 @@ static bool dev_has_alg(const char *dev_alg_name, const char *alg_name) return false; } -static void add_uacce_dev_to_list(struct uacce_dev_list *head, - struct uacce_dev_list *node) -{ - struct uacce_dev_list *tmp = head; - - while (tmp->next) - tmp = tmp->next; - - tmp->next = node; -} - static int check_alg_name(const char *alg_name) { int i = 0; @@ -715,7 +713,7 @@ struct uacce_dev_list *wd_get_accel_list(const char *alg_name) if (!head) head = node; else - add_uacce_dev_to_list(head, node); + wd_add_dev_to_list(head, node); } closedir(wd_class); @@ -728,6 +726,35 @@ free_list: return NULL; } +struct uacce_dev *wd_find_dev_by_numa(struct uacce_dev_list *list, int numa_id) +{ + struct uacce_dev *dev = WD_ERR_PTR(-WD_ENODEV); + struct uacce_dev_list *p = list; + int ctx_num, ctx_max = 0; + + if (!list) { + WD_ERR("invalid: list is NULL!\n"); + return WD_ERR_PTR(-WD_EINVAL); + } + + while (p) { + if (numa_id != p->dev->numa_id) { + p = p->next; + continue; + } + + ctx_num = wd_get_avail_ctx(p->dev); + if (ctx_num > ctx_max) { + dev = p->dev; + ctx_max = ctx_num; + } + + p = p->next; + } + + return dev; +} + void wd_free_list_accels(struct uacce_dev_list *list) { struct uacce_dev_list *curr, *next; @@ -774,7 +801,7 @@ struct uacce_dev *wd_get_accel_dev(const char *alg_name) } if (dev) - target = clone_uacce_dev(dev); + target = wd_clone_dev(dev); wd_free_list_accels(head); @@ -794,6 +821,39 @@ int wd_ctx_set_io_cmd(handle_t h_ctx, unsigned long cmd, void *arg) return ioctl(ctx->fd, cmd, arg); } +struct bitmask *wd_create_device_nodemask(struct uacce_dev_list *list) +{ + struct uacce_dev_list *p; + struct bitmask *bmp; + + if (!list) { + WD_ERR("invalid: list is NULL!\n"); + return WD_ERR_PTR(-WD_EINVAL); + } + + bmp = numa_allocate_nodemask(); + if (!bmp) { + WD_ERR("failed to alloc bitmask(%d)!\n", errno); + return WD_ERR_PTR(-WD_ENOMEM); + } + + p = list; + while (p) { + numa_bitmask_setbit(bmp, p->dev->numa_id); + p = p->next; + } + + return bmp; +} + +void wd_free_device_nodemask(struct bitmask *bmp) +{ + if (!bmp) + return; + + numa_free_nodemask(bmp); +} + void wd_get_version(void) { const char *wd_released_time = UADK_RELEASED_TIME; diff --git a/uadk/wd_aead.c b/uadk/wd_aead.c index d6c238005e394172e2b28477121f423f7c828673..9b80922748288e738760fc8d5206f7474b48e28e 100644 --- a/uadk/wd_aead.c +++ b/uadk/wd_aead.c @@ -6,7 +6,6 @@ #include #include -#include "wd_util.h" #include "include/drv/wd_aead_drv.h" #include "wd_aead.h" @@ -31,6 +30,7 @@ static int g_aead_mac_len[WD_DIGEST_TYPE_MAX] = { }; struct wd_aead_setting { + enum wd_status status; struct wd_ctx_config_internal config; struct wd_sched sched; struct wd_aead_driver *driver; @@ -69,7 +69,7 @@ static void __attribute__((constructor)) wd_aead_open_driver(void) { wd_aead_setting.dlhandle = dlopen("libhisi_sec.so", RTLD_NOW); if (!wd_aead_setting.dlhandle) - WD_ERR("failed to open libhisi_sec.so!\n"); + WD_ERR("failed to open libhisi_sec.so, %s\n", dlerror()); } static void __attribute__((destructor)) wd_aead_close_driver(void) @@ -389,27 +389,39 @@ static int wd_aead_param_check(struct wd_aead_sess *sess, return 0; } +static void wd_aead_clear_status(void) +{ + wd_alg_clear_init(&wd_aead_setting.status); +} + int wd_aead_init(struct wd_ctx_config *config, struct wd_sched *sched) { void *priv; + bool flag; int ret; + pthread_atfork(NULL, NULL, wd_aead_clear_status); + + flag = wd_alg_try_init(&wd_aead_setting.status); + if (!flag) + return 0; + ret = wd_init_param_check(config, sched); if (ret) - return ret; + goto out_clear_init; ret = wd_set_epoll_en("WD_AEAD_EPOLL_EN", &wd_aead_setting.config.epoll_en); if (ret < 0) - return ret; + goto out_clear_init; ret = wd_init_ctx_config(&wd_aead_setting.config, config); if (ret) - return ret; + goto out_clear_init; ret = wd_init_sched(&wd_aead_setting.sched, sched); if (ret < 0) - goto out; + goto out_clear_ctx_config; /* set driver */ #ifdef WD_STATIC_DRV @@ -421,33 +433,37 @@ int wd_aead_init(struct wd_ctx_config *config, struct wd_sched *sched) config->ctx_num, WD_POOL_MAX_ENTRIES, sizeof(struct wd_aead_msg)); if (ret < 0) - goto out_sched; + goto out_clear_sched; /* init ctx related resources in specific driver */ priv = calloc(1, wd_aead_setting.driver->drv_ctx_size); if (!priv) { ret = -WD_ENOMEM; - goto out_priv; + goto out_clear_pool; } wd_aead_setting.priv = priv; ret = wd_aead_setting.driver->init(&wd_aead_setting.config, priv); if (ret < 0) { WD_ERR("failed to init aead dirver!\n"); - goto out_init; + goto out_free_priv; } + wd_alg_set_init(&wd_aead_setting.status); + return 0; -out_init: +out_free_priv: free(priv); wd_aead_setting.priv = NULL; -out_priv: +out_clear_pool: wd_uninit_async_request_pool(&wd_aead_setting.pool); -out_sched: +out_clear_sched: wd_clear_sched(&wd_aead_setting.sched); -out: +out_clear_ctx_config: wd_clear_ctx_config(&wd_aead_setting.config); +out_clear_init: + wd_alg_clear_init(&wd_aead_setting.status); return ret; } @@ -465,6 +481,7 @@ void wd_aead_uninit(void) wd_uninit_async_request_pool(&wd_aead_setting.pool); wd_clear_sched(&wd_aead_setting.sched); wd_clear_ctx_config(&wd_aead_setting.config); + wd_alg_clear_init(&wd_aead_setting.status); } static void fill_request_msg(struct wd_aead_msg *msg, struct wd_aead_req *req, diff --git a/uadk/wd_cipher.c b/uadk/wd_cipher.c index 8ce975aca0aa476494db32c4e328c5d81cc4207d..8af2e4b82847b4b66c6426fee3ba490cb0cfa568 100644 --- a/uadk/wd_cipher.c +++ b/uadk/wd_cipher.c @@ -7,7 +7,6 @@ #include #include #include -#include "wd_util.h" #include "include/drv/wd_cipher_drv.h" #include "wd_cipher.h" @@ -45,6 +44,7 @@ static const unsigned char des_weak_keys[DES_WEAK_KEY_NUM][DES_KEY_SIZE] = { }; struct wd_cipher_setting { + enum wd_status status; struct wd_ctx_config_internal config; struct wd_sched sched; void *sched_ctx; @@ -80,7 +80,7 @@ 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!\n"); + WD_ERR("failed to open libhisi_sec.so, %s\n", dlerror()); } static void __attribute__((destructor)) wd_cipher_close_driver(void) @@ -228,27 +228,39 @@ void wd_cipher_free_sess(handle_t h_sess) free(sess); } +static void wd_cipher_clear_status(void) +{ + wd_alg_clear_init(&wd_cipher_setting.status); +} + int wd_cipher_init(struct wd_ctx_config *config, struct wd_sched *sched) { void *priv; + bool flag; int ret; + pthread_atfork(NULL, NULL, wd_cipher_clear_status); + + flag = wd_alg_try_init(&wd_cipher_setting.status); + if (!flag) + return 0; + ret = wd_init_param_check(config, sched); if (ret) - return ret; + goto out_clear_init; ret = wd_set_epoll_en("WD_CIPHER_EPOLL_EN", &wd_cipher_setting.config.epoll_en); if (ret < 0) - return ret; + goto out_clear_init; ret = wd_init_ctx_config(&wd_cipher_setting.config, config); if (ret < 0) - return ret; + goto out_clear_init; ret = wd_init_sched(&wd_cipher_setting.sched, sched); if (ret < 0) - goto out; + goto out_clear_ctx_config; #ifdef WD_STATIC_DRV /* set driver */ @@ -260,33 +272,37 @@ int wd_cipher_init(struct wd_ctx_config *config, struct wd_sched *sched) config->ctx_num, WD_POOL_MAX_ENTRIES, sizeof(struct wd_cipher_msg)); if (ret < 0) - goto out_sched; + goto out_clear_sched; /* init ctx related resources in specific driver */ priv = calloc(1, wd_cipher_setting.driver->drv_ctx_size); if (!priv) { ret = -WD_ENOMEM; - goto out_priv; + goto out_clear_pool; } wd_cipher_setting.priv = priv; ret = wd_cipher_setting.driver->init(&wd_cipher_setting.config, priv); if (ret < 0) { - WD_ERR("hisi sec init failed.\n"); - goto out_init; + WD_ERR("failed to do dirver init, ret = %d.\n", ret); + goto out_free_priv; } + wd_alg_set_init(&wd_cipher_setting.status); + return 0; -out_init: +out_free_priv: free(priv); wd_cipher_setting.priv = NULL; -out_priv: +out_clear_pool: wd_uninit_async_request_pool(&wd_cipher_setting.pool); -out_sched: +out_clear_sched: wd_clear_sched(&wd_cipher_setting.sched); -out: +out_clear_ctx_config: wd_clear_ctx_config(&wd_cipher_setting.config); +out_clear_init: + wd_alg_clear_init(&wd_cipher_setting.status); return ret; } @@ -304,6 +320,7 @@ void wd_cipher_uninit(void) wd_uninit_async_request_pool(&wd_cipher_setting.pool); wd_clear_sched(&wd_cipher_setting.sched); wd_clear_ctx_config(&wd_cipher_setting.config); + wd_alg_clear_init(&wd_cipher_setting.status); } static void fill_request_msg(struct wd_cipher_msg *msg, diff --git a/uadk/wd_comp.c b/uadk/wd_comp.c index eacebd33808148406413b4f528fda8f25d6794b5..cca6eb90700fe07c64876d41c36dd58d7cb8a579 100644 --- a/uadk/wd_comp.c +++ b/uadk/wd_comp.c @@ -12,15 +12,15 @@ #include #include -#include "config.h" #include "drv/wd_comp_drv.h" -#include "wd_util.h" #include "wd_comp.h" #define WD_POOL_MAX_ENTRIES 1024 #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) | \ @@ -41,6 +41,8 @@ struct wd_comp_sess { }; struct wd_comp_setting { + enum wd_status status; + enum wd_status status2; struct wd_ctx_config_internal config; struct wd_sched sched; struct wd_comp_driver *driver; @@ -51,6 +53,20 @@ struct 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}, {} +}; + +static struct wd_ctx_params wd_comp_ctx_params = { + .op_type_num = WD_DIR_MAX, + .ctx_set_num = wd_comp_ctx_num, + .bmp = NULL, +}; + #ifdef WD_STATIC_DRV static void wd_comp_set_static_drv(void) { @@ -63,7 +79,7 @@ static void __attribute__((constructor)) wd_comp_open_driver(void) { wd_comp_setting.dlhandle = dlopen("libhisi_zip.so", RTLD_NOW); if (!wd_comp_setting.dlhandle) - WD_ERR("failed to open libhisi_zip.so!\n"); + WD_ERR("failed to open libhisi_zip.so, %s\n", dlerror()); } static void __attribute__((destructor)) wd_comp_close_driver(void) @@ -78,7 +94,12 @@ void wd_comp_set_driver(struct wd_comp_driver *drv) wd_comp_setting.driver = drv; } -int wd_comp_init(struct wd_ctx_config *config, struct wd_sched *sched) +static void wd_comp_clear_status(void) +{ + wd_alg_clear_init(&wd_comp_setting.status); +} + +static int wd_comp_init_nolock(struct wd_ctx_config *config, struct wd_sched *sched) { void *priv; int ret; @@ -98,7 +119,7 @@ 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; + 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, @@ -118,35 +139,36 @@ int wd_comp_init(struct wd_ctx_config *config, struct wd_sched *sched) config->ctx_num, WD_POOL_MAX_ENTRIES, sizeof(struct wd_comp_msg)); if (ret < 0) - goto out_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; - goto out_priv; + 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_init; + goto out_free_priv; } + return 0; -out_init: +out_free_priv: free(priv); wd_comp_setting.priv = NULL; -out_priv: +out_clear_pool: wd_uninit_async_request_pool(&wd_comp_setting.pool); -out_sched: +out_clear_sched: wd_clear_sched(&wd_comp_setting.sched); -out: +out_clear_ctx_config: wd_clear_ctx_config(&wd_comp_setting.config); return ret; } -void wd_comp_uninit(void) +static void wd_comp_uninit_nolock(void) { void *priv = wd_comp_setting.priv; @@ -165,6 +187,104 @@ void wd_comp_uninit(void) wd_clear_ctx_config(&wd_comp_setting.config); } +int wd_comp_init(struct wd_ctx_config *config, struct wd_sched *sched) +{ + bool flag; + int ret; + + pthread_atfork(NULL, NULL, wd_comp_clear_status); + + flag = wd_alg_try_init(&wd_comp_setting.status); + if (!flag) + return 0; + + ret = wd_comp_init_nolock(config, sched); + if (ret) { + wd_alg_clear_init(&wd_comp_setting.status); + goto out; + } + + wd_alg_set_init(&wd_comp_setting.status); + +out: + return ret; +} + +void wd_comp_uninit(void) +{ + wd_comp_uninit_nolock(); + wd_alg_clear_init(&wd_comp_setting.status); +} + +int wd_comp_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params) +{ + bool flag; + int ret; + + pthread_atfork(NULL, NULL, wd_comp_clear_status); + + flag = wd_alg_try_init(&wd_comp_setting.status); + if (!flag) + return 0; + + if (!alg) { + WD_ERR("invalid: alg is NULL!\n"); + ret = -WD_EINVAL; + goto out_uninit; + } + + wd_comp_init_attrs.alg = alg; + wd_comp_init_attrs.sched_type = sched_type; + + wd_comp_init_attrs.ctx_params = ctx_params ? ctx_params : &wd_comp_ctx_params; + wd_comp_init_attrs.ctx_config = &wd_comp_ctx; + + 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; + } + wd_comp_sched->name = SCHED_RR_NAME; + wd_comp_init_attrs.sched = wd_comp_sched; + + ret = wd_alg_pre_init(&wd_comp_init_attrs); + if (ret) + goto out_freesched; + + ret = wd_comp_init_nolock(&wd_comp_ctx, wd_comp_sched); + if (ret) + goto out_freesched; + + wd_alg_set_init(&wd_comp_setting.status); + + return 0; + +out_freesched: + wd_sched_rr_release(wd_comp_sched); + +out_uninit: + wd_alg_clear_init(&wd_comp_setting.status); + + return ret; +} + +void wd_comp_uninit2(void) +{ + int i; + + wd_comp_uninit_nolock(); + + 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_clear_init(&wd_comp_setting.status); +} + struct wd_comp_msg *wd_comp_get_msg(__u32 idx, __u32 tag) { return wd_find_msg_in_pool(&wd_comp_setting.pool, idx, tag); @@ -276,6 +396,7 @@ handle_t wd_comp_alloc_sess(struct wd_comp_sess_setup *setup) sess->comp_lv = setup->comp_lv; sess->win_sz = setup->win_sz; sess->stream_pos = WD_COMP_STREAM_NEW; + /* Some simple scheduler don't need scheduling parameters */ sess->sched_key = (void *)wd_comp_setting.sched.sched_init( wd_comp_setting.sched.h_sched_ctx, setup->sched_param); @@ -305,9 +426,25 @@ void wd_comp_free_sess(handle_t h_sess) if (sess->sched_key) free(sess->sched_key); + free(sess); } +int wd_comp_reset_sess(handle_t h_sess) +{ + struct wd_comp_sess *sess = (struct wd_comp_sess *)h_sess; + + if (!sess) { + WD_ERR("invalid: sess is NULL!\n"); + return -WD_EINVAL; + } + + sess->stream_pos = WD_COMP_STREAM_NEW; + memset(sess->ctx_buf, 0, HW_CTX_SIZE); + + return 0; +} + static void fill_comp_msg(struct wd_comp_sess *sess, struct wd_comp_msg *msg, struct wd_comp_req *req) { diff --git a/uadk/wd_dh.c b/uadk/wd_dh.c index 0bf770dfa71b0c81484e3a5e3930e5ecc4764676..4cb5c2635496b860c40e12a4c367a842864a2b1f 100644 --- a/uadk/wd_dh.c +++ b/uadk/wd_dh.c @@ -12,10 +12,8 @@ #include #include -#include "config.h" #include "include/drv/wd_dh_drv.h" #include "wd_util.h" -#include "wd_dh.h" #define WD_POOL_MAX_ENTRIES 1024 #define DH_MAX_KEY_SIZE 512 @@ -32,6 +30,7 @@ struct wd_dh_sess { }; static struct wd_dh_setting { + enum wd_status status; struct wd_ctx_config_internal config; struct wd_sched sched; void *sched_ctx; @@ -55,7 +54,7 @@ static void __attribute__((constructor)) wd_dh_open_driver(void) { wd_dh_setting.dlhandle = dlopen("libhisi_hpre.so", RTLD_NOW); if (!wd_dh_setting.dlhandle) - WD_ERR("failed to open libhisi_hpre.so!\n"); + WD_ERR("failed to open libhisi_hpre.so, %s\n", dlerror()); } static void __attribute__((destructor)) wd_dh_close_driver(void) @@ -75,27 +74,39 @@ void wd_dh_set_driver(struct wd_dh_driver *drv) wd_dh_setting.driver = drv; } +static void wd_dh_clear_status(void) +{ + wd_alg_clear_init(&wd_dh_setting.status); +} + int wd_dh_init(struct wd_ctx_config *config, struct wd_sched *sched) { void *priv; + bool flag; int ret; + pthread_atfork(NULL, NULL, wd_dh_clear_status); + + flag = wd_alg_try_init(&wd_dh_setting.status); + if (!flag) + return 0; + ret = wd_init_param_check(config, sched); if (ret) - return ret; + goto out_clear_init; ret = wd_set_epoll_en("WD_DH_EPOLL_EN", &wd_dh_setting.config.epoll_en); if (ret < 0) - return ret; + goto out_clear_init; ret = wd_init_ctx_config(&wd_dh_setting.config, config); if (ret) - return ret; + goto out_clear_init; ret = wd_init_sched(&wd_dh_setting.sched, sched); if (ret) - goto out; + goto out_clear_ctx_config; #ifdef WD_STATIC_DRV wd_dh_set_static_drv(); @@ -106,13 +117,13 @@ int wd_dh_init(struct wd_ctx_config *config, struct wd_sched *sched) config->ctx_num, WD_POOL_MAX_ENTRIES, sizeof(struct wd_dh_msg)); if (ret) - goto out_sched; + goto out_clear_sched; /* initialize ctx related resources in specific driver */ priv = calloc(1, wd_dh_setting.driver->drv_ctx_size); if (!priv) { ret = -WD_ENOMEM; - goto out_priv; + goto out_clear_pool; } wd_dh_setting.priv = priv; @@ -120,21 +131,24 @@ int wd_dh_init(struct wd_ctx_config *config, struct wd_sched *sched) wd_dh_setting.driver->alg_name); if (ret < 0) { WD_ERR("failed to init dh driver, ret= %d!\n", ret); - goto out_init; + goto out_free_priv; } + wd_alg_set_init(&wd_dh_setting.status); + return 0; -out_init: +out_free_priv: free(priv); wd_dh_setting.priv = NULL; -out_priv: +out_clear_pool: wd_uninit_async_request_pool(&wd_dh_setting.pool); -out_sched: +out_clear_sched: wd_clear_sched(&wd_dh_setting.sched); -out: +out_clear_ctx_config: wd_clear_ctx_config(&wd_dh_setting.config); - +out_clear_init: + wd_alg_clear_init(&wd_dh_setting.status); return ret; } @@ -156,6 +170,7 @@ void wd_dh_uninit(void) /* unset config, sched, driver */ wd_clear_sched(&wd_dh_setting.sched); wd_clear_ctx_config(&wd_dh_setting.config); + wd_alg_clear_init(&wd_dh_setting.status); } static int fill_dh_msg(struct wd_dh_msg *msg, struct wd_dh_req *req, diff --git a/uadk/wd_digest.c b/uadk/wd_digest.c index f56be0c203da8ce347db1ccaa44dfab957ffe982..8c01709b1b75088b8e9786b7dd529e536531798b 100644 --- a/uadk/wd_digest.c +++ b/uadk/wd_digest.c @@ -6,7 +6,6 @@ #include #include -#include "wd_util.h" #include "include/drv/wd_digest_drv.h" #include "wd_digest.h" @@ -39,6 +38,7 @@ static int g_digest_mac_full_len[WD_DIGEST_TYPE_MAX] = { }; struct wd_digest_setting { + enum wd_status status; struct wd_ctx_config_internal config; struct wd_sched sched; struct wd_digest_driver *driver; @@ -80,7 +80,7 @@ static void __attribute__((constructor)) wd_digest_open_driver(void) /* Fix me: vendor driver should be put in /usr/lib/wd/ */ wd_digest_setting.dlhandle = dlopen("libhisi_sec.so", RTLD_NOW); if (!wd_digest_setting.dlhandle) - WD_ERR("failed to open libhisi_sec.so!\n"); + WD_ERR("failed to open libhisi_sec.so, %s\n", dlerror()); } static void __attribute__((destructor)) wd_digest_close_driver(void) @@ -183,27 +183,39 @@ void wd_digest_free_sess(handle_t h_sess) free(sess); } +static void wd_digest_clear_status(void) +{ + wd_alg_clear_init(&wd_digest_setting.status); +} + int wd_digest_init(struct wd_ctx_config *config, struct wd_sched *sched) { void *priv; + bool flag; int ret; + pthread_atfork(NULL, NULL, wd_digest_clear_status); + + flag = wd_alg_try_init(&wd_digest_setting.status); + if (!flag) + return 0; + ret = wd_init_param_check(config, sched); if (ret) - return ret; + goto out_clear_init; ret = wd_set_epoll_en("WD_DIGEST_EPOLL_EN", &wd_digest_setting.config.epoll_en); if (ret < 0) - return ret; + goto out_clear_init; ret = wd_init_ctx_config(&wd_digest_setting.config, config); if (ret < 0) - return ret; + goto out_clear_init; ret = wd_init_sched(&wd_digest_setting.sched, sched); if (ret < 0) - goto out; + goto out_clear_ctx_config; /* set driver */ #ifdef WD_STATIC_DRV @@ -215,33 +227,37 @@ int wd_digest_init(struct wd_ctx_config *config, struct wd_sched *sched) config->ctx_num, WD_POOL_MAX_ENTRIES, sizeof(struct wd_digest_msg)); if (ret < 0) - goto out_sched; + goto out_clear_sched; /* init ctx related resources in specific driver */ priv = calloc(1, wd_digest_setting.driver->drv_ctx_size); if (!priv) { ret = -WD_ENOMEM; - goto out_priv; + goto out_clear_pool; } wd_digest_setting.priv = priv; ret = wd_digest_setting.driver->init(&wd_digest_setting.config, priv); if (ret < 0) { WD_ERR("failed to init digest dirver!\n"); - goto out_init; + goto out_free_priv; } + wd_alg_set_init(&wd_digest_setting.status); + return 0; -out_init: +out_free_priv: free(priv); wd_digest_setting.priv = NULL; -out_priv: +out_clear_pool: wd_uninit_async_request_pool(&wd_digest_setting.pool); -out_sched: +out_clear_sched: wd_clear_sched(&wd_digest_setting.sched); -out: +out_clear_ctx_config: wd_clear_ctx_config(&wd_digest_setting.config); +out_clear_init: + wd_alg_clear_init(&wd_digest_setting.status); return ret; } @@ -260,6 +276,7 @@ void wd_digest_uninit(void) wd_clear_sched(&wd_digest_setting.sched); wd_clear_ctx_config(&wd_digest_setting.config); + wd_alg_clear_init(&wd_digest_setting.status); } static int wd_aes_hmac_length_check(struct wd_digest_sess *sess, diff --git a/uadk/wd_ecc.c b/uadk/wd_ecc.c index 2266b1dd6d9165a605570300414bdc597cb07667..99811b5d815c2d51a1176250d33adbda928d654a 100644 --- a/uadk/wd_ecc.c +++ b/uadk/wd_ecc.c @@ -13,7 +13,6 @@ #include #include -#include "wd_util.h" #include "include/drv/wd_ecc_drv.h" #include "include/wd_ecc_curve.h" #include "wd_ecc.h" @@ -64,6 +63,7 @@ struct wd_ecc_curve_list { }; static struct wd_ecc_setting { + enum wd_status status; struct wd_ctx_config_internal config; struct wd_sched sched; void *sched_ctx; @@ -110,7 +110,7 @@ static void __attribute__((constructor)) wd_ecc_open_driver(void) { wd_ecc_setting.dlhandle = dlopen("libhisi_hpre.so", RTLD_NOW); if (!wd_ecc_setting.dlhandle) - WD_ERR("failed to open libhisi_hpre.so!\n"); + WD_ERR("failed to open libhisi_hpre.so, %s\n", dlerror()); } static void __attribute__((destructor)) wd_ecc_close_driver(void) @@ -130,27 +130,39 @@ void wd_ecc_set_driver(struct wd_ecc_driver *drv) wd_ecc_setting.driver = drv; } +static void wd_ecc_clear_status(void) +{ + wd_alg_clear_init(&wd_ecc_setting.status); +} + int wd_ecc_init(struct wd_ctx_config *config, struct wd_sched *sched) { void *priv; + bool flag; int ret; + pthread_atfork(NULL, NULL, wd_ecc_clear_status); + + flag = wd_alg_try_init(&wd_ecc_setting.status); + if (!flag) + return 0; + ret = wd_init_param_check(config, sched); if (ret) - return ret; + goto out_clear_init; ret = wd_set_epoll_en("WD_ECC_EPOLL_EN", &wd_ecc_setting.config.epoll_en); if (ret < 0) - return ret; + goto out_clear_init; ret = wd_init_ctx_config(&wd_ecc_setting.config, config); if (ret < 0) - return ret; + goto out_clear_init; ret = wd_init_sched(&wd_ecc_setting.sched, sched); if (ret < 0) - goto out; + goto out_clear_ctx_config; #ifdef WD_STATIC_DRV wd_ecc_set_static_drv(); @@ -161,13 +173,13 @@ int wd_ecc_init(struct wd_ctx_config *config, struct wd_sched *sched) config->ctx_num, WD_POOL_MAX_ENTRIES, sizeof(struct wd_ecc_msg)); if (ret < 0) - goto out_sched; + goto out_clear_sched; /* initialize ctx related resources in specific driver */ priv = calloc(1, wd_ecc_setting.driver->drv_ctx_size); if (!priv) { ret = -WD_ENOMEM; - goto out_priv; + goto out_clear_pool; } wd_ecc_setting.priv = priv; @@ -175,20 +187,24 @@ int wd_ecc_init(struct wd_ctx_config *config, struct wd_sched *sched) wd_ecc_setting.driver->alg_name); if (ret < 0) { WD_ERR("failed to init ecc driver, ret = %d!\n", ret); - goto out_init; + goto out_free_priv; } + wd_alg_set_init(&wd_ecc_setting.status); + return 0; -out_init: +out_free_priv: free(priv); wd_ecc_setting.priv = NULL; -out_priv: +out_clear_pool: wd_uninit_async_request_pool(&wd_ecc_setting.pool); -out_sched: +out_clear_sched: wd_clear_sched(&wd_ecc_setting.sched); -out: +out_clear_ctx_config: wd_clear_ctx_config(&wd_ecc_setting.config); +out_clear_init: + wd_alg_clear_init(&wd_ecc_setting.status); return ret; } @@ -210,6 +226,7 @@ void wd_ecc_uninit(void) /* unset config, sched, driver */ wd_clear_sched(&wd_ecc_setting.sched); wd_clear_ctx_config(&wd_ecc_setting.config); + wd_alg_clear_init(&wd_ecc_setting.status); } static int trans_to_binpad(char *dst, const char *src, diff --git a/uadk/wd_rsa.c b/uadk/wd_rsa.c index 489833eeafc22cd90bb8f03fd7acd1887e8af028..0b76c4808b3a8204069cc709f011a97c851f420a 100644 --- a/uadk/wd_rsa.c +++ b/uadk/wd_rsa.c @@ -12,9 +12,8 @@ #include #include -#include "config.h" #include "include/drv/wd_rsa_drv.h" -#include "wd_util.h" +#include "wd_rsa.h" #define WD_POOL_MAX_ENTRIES 1024 #define WD_HW_EACCESS 62 @@ -72,6 +71,7 @@ struct wd_rsa_sess { }; static struct wd_rsa_setting { + enum wd_status status; struct wd_ctx_config_internal config; struct wd_sched sched; void *sched_ctx; @@ -95,7 +95,7 @@ static void __attribute__((constructor)) wd_rsa_open_driver(void) { wd_rsa_setting.dlhandle = dlopen("libhisi_hpre.so", RTLD_NOW); if (!wd_rsa_setting.dlhandle) - WD_ERR("failed to open libhisi_hpre.so!\n"); + WD_ERR("failed to open libhisi_hpre.so, %s\n", dlerror()); } static void __attribute__((destructor)) wd_rsa_close_driver(void) @@ -115,27 +115,39 @@ void wd_rsa_set_driver(struct wd_rsa_driver *drv) wd_rsa_setting.driver = drv; } +static void wd_rsa_clear_status(void) +{ + wd_alg_clear_init(&wd_rsa_setting.status); +} + int wd_rsa_init(struct wd_ctx_config *config, struct wd_sched *sched) { void *priv; + bool flag; int ret; + pthread_atfork(NULL, NULL, wd_rsa_clear_status); + + flag = wd_alg_try_init(&wd_rsa_setting.status); + if (!flag) + return 0; + ret = wd_init_param_check(config, sched); if (ret) - return ret; + goto out_clear_init; ret = wd_set_epoll_en("WD_RSA_EPOLL_EN", &wd_rsa_setting.config.epoll_en); if (ret < 0) - return ret; + goto out_clear_init; ret = wd_init_ctx_config(&wd_rsa_setting.config, config); if (ret < 0) - return ret; + goto out_clear_init; ret = wd_init_sched(&wd_rsa_setting.sched, sched); if (ret < 0) - goto out; + goto out_clear_ctx_config; #ifdef WD_STATIC_DRV wd_rsa_set_static_drv(); @@ -146,13 +158,13 @@ int wd_rsa_init(struct wd_ctx_config *config, struct wd_sched *sched) config->ctx_num, WD_POOL_MAX_ENTRIES, sizeof(struct wd_rsa_msg)); if (ret < 0) - goto out_sched; + goto out_clear_sched; /* initialize ctx related resources in specific driver */ priv = calloc(1, wd_rsa_setting.driver->drv_ctx_size); if (!priv) { ret = -WD_ENOMEM; - goto out_priv; + goto out_clear_pool; } wd_rsa_setting.priv = priv; @@ -160,20 +172,24 @@ int wd_rsa_init(struct wd_ctx_config *config, struct wd_sched *sched) wd_rsa_setting.driver->alg_name); if (ret < 0) { WD_ERR("failed to init rsa driver, ret = %d!\n", ret); - goto out_init; + goto out_free_priv; } + wd_alg_set_init(&wd_rsa_setting.status); + return 0; -out_init: +out_free_priv: free(priv); wd_rsa_setting.priv = NULL; -out_priv: +out_clear_pool: wd_uninit_async_request_pool(&wd_rsa_setting.pool); -out_sched: +out_clear_sched: wd_clear_sched(&wd_rsa_setting.sched); -out: +out_clear_ctx_config: wd_clear_ctx_config(&wd_rsa_setting.config); +out_clear_init: + wd_alg_clear_init(&wd_rsa_setting.status); return ret; } @@ -195,6 +211,7 @@ void wd_rsa_uninit(void) /* unset config, sched, driver */ wd_clear_sched(&wd_rsa_setting.sched); wd_clear_ctx_config(&wd_rsa_setting.config); + wd_alg_clear_init(&wd_rsa_setting.status); } static int fill_rsa_msg(struct wd_rsa_msg *msg, struct wd_rsa_req *req, diff --git a/uadk/wd_util.c b/uadk/wd_util.c index bd8207513103675da88a8af45c8c8b2e4525fe63..433dd56e87e4e950520945754a8f1eb24eb0bb9e 100644 --- a/uadk/wd_util.c +++ b/uadk/wd_util.c @@ -5,12 +5,10 @@ */ #define _GNU_SOURCE -#include #include #include #include #include -#include "wd_alg_common.h" #include "wd_sched.h" #include "wd_util.h" @@ -22,6 +20,9 @@ #define PRIVILEGE_FLAG 600 #define MIN(a, b) ((a) > (b) ? (b) : (a)) +#define WD_INIT_SLEEP_UTIME 1000 +#define WD_INIT_RETRY_TIMES 10000 + struct msg_pool { /* message array allocated dynamically */ void *msgs; @@ -1777,3 +1778,259 @@ int wd_init_param_check(struct wd_ctx_config *config, struct wd_sched *sched) return 0; } + +bool wd_alg_try_init(enum wd_status *status) +{ + enum wd_status expected; + int count = 0; + bool ret; + + do { + expected = WD_UNINIT; + ret = __atomic_compare_exchange_n(status, &expected, WD_INITING, true, + __ATOMIC_RELAXED, __ATOMIC_RELAXED); + if (expected == WD_INIT) + return false; + usleep(WD_INIT_SLEEP_UTIME); + if (!(++count % WD_INIT_RETRY_TIMES)) + WD_ERR("The algorithm initizalite has been waiting for %ds!\n", + WD_INIT_SLEEP_UTIME * count / 1000000); + } while (!ret); + + return true; +} + +static __u32 wd_get_ctx_numbers(struct wd_ctx_params ctx_params, int end) +{ + __u32 count = 0; + int i; + + for (i = 0; i < end; i++) { + count += ctx_params.ctx_set_num[i].sync_ctx_num; + count += ctx_params.ctx_set_num[i].async_ctx_num; + } + + return count; +} + +struct uacce_dev_list *wd_get_usable_list(struct uacce_dev_list *list, struct bitmask *bmp) +{ + struct uacce_dev_list *p, *node, *result = NULL; + struct uacce_dev *dev; + int numa_id, ret; + + if (!bmp) { + WD_ERR("invalid: bmp is NULL!\n"); + return WD_ERR_PTR(-WD_EINVAL); + } + + p = list; + while (p) { + dev = p->dev; + numa_id = dev->numa_id; + ret = numa_bitmask_isbitset(bmp, numa_id); + if (!ret) { + p = p->next; + continue; + } + + node = calloc(1, sizeof(*node)); + if (!node) { + result = WD_ERR_PTR(-WD_ENOMEM); + goto out_free_list; + } + + node->dev = wd_clone_dev(dev); + if (!node->dev) { + result = WD_ERR_PTR(-WD_ENOMEM); + goto out_free_node; + } + + if (!result) + result = node; + else + wd_add_dev_to_list(result, node); + + p = p->next; + } + + return result ? result : WD_ERR_PTR(-WD_ENODEV); + +out_free_node: + free(node); +out_free_list: + wd_free_list_accels(result); + return result; +} + +static int wd_init_ctx_set(struct wd_init_attrs *attrs, struct uacce_dev_list *list, + int idx, int numa_id, int op_type) +{ + struct wd_ctx_nums ctx_nums = attrs->ctx_params->ctx_set_num[op_type]; + __u32 ctx_set_num = ctx_nums.sync_ctx_num + ctx_nums.async_ctx_num; + struct wd_ctx_config *ctx_config = attrs->ctx_config; + struct uacce_dev *dev; + int i; + + dev = wd_find_dev_by_numa(list, numa_id); + if (WD_IS_ERR(dev)) + return WD_PTR_ERR(dev); + + for (i = idx; i < idx + ctx_set_num; i++) { + ctx_config->ctxs[i].ctx = wd_request_ctx(dev); + if (errno == WD_EBUSY) { + dev = wd_find_dev_by_numa(list, numa_id); + if (WD_IS_ERR(dev)) + return WD_PTR_ERR(dev); + i--; + } + ctx_config->ctxs[i].op_type = op_type; + ctx_config->ctxs[i].ctx_mode = + ((i - idx) < ctx_nums.sync_ctx_num) ? + CTX_MODE_SYNC : CTX_MODE_ASYNC; + } + + return 0; +} + +static void wd_release_ctx_set(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; + } +} + +static int wd_instance_sched_set(struct wd_sched *sched, struct wd_ctx_nums ctx_nums, + int idx, int numa_id, int op_type) +{ + struct sched_params sparams; + int i, ret = 0; + + for (i = 0; i < CTX_MODE_MAX; i++) { + sparams.numa_id = numa_id; + sparams.type = op_type; + sparams.mode = i; + sparams.begin = idx + ctx_nums.sync_ctx_num * i; + sparams.end = idx - 1 + ctx_nums.sync_ctx_num + ctx_nums.async_ctx_num * i; + if (sparams.begin > sparams.end) + continue; + ret = wd_sched_rr_instance(sched, &sparams); + if (ret) + goto out; + } + +out: + return ret; +} + +static int wd_init_ctx_and_sched(struct wd_init_attrs *attrs, struct bitmask *bmp, + struct uacce_dev_list *list) +{ + struct wd_ctx_params *ctx_params = attrs->ctx_params; + __u32 op_type_num = ctx_params->op_type_num; + int max_node = numa_max_node() + 1; + struct wd_ctx_nums ctx_nums; + int i, j, ret; + int idx = 0; + + for (i = 0; i < max_node; i++) { + if (!numa_bitmask_isbitset(bmp, i)) + continue; + for (j = 0; j < op_type_num; j++) { + ctx_nums = ctx_params->ctx_set_num[j]; + ret = wd_init_ctx_set(attrs, list, idx, i, j); + if (ret) + goto free_ctxs; + ret = wd_instance_sched_set(attrs->sched, ctx_nums, idx, i, j); + if (ret) + goto free_ctxs; + idx += (ctx_nums.sync_ctx_num + ctx_nums.async_ctx_num); + } + } + + return 0; + +free_ctxs: + wd_release_ctx_set(attrs->ctx_config); + + return ret; +} + +int wd_alg_pre_init(struct wd_init_attrs *attrs) +{ + struct wd_ctx_config *ctx_config = attrs->ctx_config; + struct wd_ctx_params *ctx_params = attrs->ctx_params; + struct bitmask *used_bmp, *bmp = ctx_params->bmp; + struct uacce_dev_list *list, *used_list = NULL; + __u32 ctx_set_num, op_type_num; + int numa_cnt, ret; + + list = wd_get_accel_list(attrs->alg); + if (!list) { + WD_ERR("failed to get devices!\n"); + return -WD_ENODEV; + } + + op_type_num = ctx_params->op_type_num; + ctx_set_num = wd_get_ctx_numbers(*ctx_params, op_type_num); + if (!ctx_set_num || !op_type_num) { + WD_ERR("invalid: ctx_set_num is %d, op_type_num is %d!\n", + ctx_set_num, op_type_num); + ret = -WD_EINVAL; + goto out_freelist; + } + + /* + * Not every numa has a device. Therefore, the first thing is to + * filter the devices in the selected numa node, and the second + * thing is to obtain the distribution of devices. + */ + if (bmp) { + used_list = wd_get_usable_list(list, bmp); + if (WD_IS_ERR(used_list)) { + ret = WD_PTR_ERR(used_list); + WD_ERR("failed to get usable devices(%d)!\n", ret); + goto out_freelist; + } + } + + used_bmp = wd_create_device_nodemask(used_list ? used_list : list); + if (WD_IS_ERR(used_bmp)) { + ret = WD_PTR_ERR(used_bmp); + goto out_freeusedlist; + } + + numa_cnt = numa_bitmask_weight(used_bmp); + if (!numa_cnt) { + ret = numa_cnt; + WD_ERR("invalid: bmp is clear!\n"); + goto out_freenodemask; + } + + ctx_config->ctx_num = ctx_set_num * numa_cnt; + ctx_config->ctxs = calloc(ctx_config->ctx_num, sizeof(struct wd_ctx)); + if (!ctx_config->ctxs) { + ret = -WD_ENOMEM; + WD_ERR("failed to alloc ctxs!\n"); + goto out_freenodemask; + } + + ret = wd_init_ctx_and_sched(attrs, used_bmp, used_list ? used_list : list); + if (ret) + free(ctx_config->ctxs); + +out_freenodemask: + wd_free_device_nodemask(used_bmp); + +out_freeusedlist: + wd_free_list_accels(used_list); + +out_freelist: + wd_free_list_accels(list); + + return ret; +} diff --git a/uadk/wd_zlibwrapper.c b/uadk/wd_zlibwrapper.c new file mode 100644 index 0000000000000000000000000000000000000000..6a0dfbaf019c821271069684170221c2f23c6a4d --- /dev/null +++ b/uadk/wd_zlibwrapper.c @@ -0,0 +1,343 @@ +// SPDX-License-Identifier: Apache-2.0 +/* + * Copyright 2022 Huawei Technologies Co.,Ltd. All rights reserved. + */ + +/* === Dependencies === */ +#define _GNU_SOURCE + +#include +#include +#include +#include +#include + +#include "wd.h" +#include "wd_comp.h" +#include "wd_sched.h" +#include "wd_util.h" +#include "wd_zlibwrapper.h" +#include "drv/wd_comp_drv.h" + +#define max(a, b) ((a) > (b) ? (a) : (b)) + +enum uadk_init_status { + WD_ZLIB_UNINIT, + WD_ZLIB_INIT, +}; + +struct wd_zlibwrapper_config { + int count; + int status; +}; + +static pthread_mutex_t wd_zlib_mutex = PTHREAD_MUTEX_INITIALIZER; +static struct wd_zlibwrapper_config zlib_config = {0}; + +static void wd_zlib_unlock(void) +{ + pthread_mutex_unlock(&wd_zlib_mutex); + zlib_config.status = WD_ZLIB_UNINIT; +} + +static int wd_zlib_init(void) +{ + struct wd_ctx_nums *ctx_set_num; + struct wd_ctx_params cparams; + int ret, i; + + if (zlib_config.status == WD_ZLIB_INIT) + return 0; + + ctx_set_num = calloc(WD_DIR_MAX, sizeof(*ctx_set_num)); + if (!ctx_set_num) { + WD_ERR("failed to alloc ctx_set_size!\n"); + return -WD_ENOMEM; + } + + cparams.op_type_num = WD_DIR_MAX; + cparams.ctx_set_num = ctx_set_num; + cparams.bmp = numa_allocate_nodemask(); + if (!cparams.bmp) { + WD_ERR("failed to create nodemask!\n"); + ret = -WD_ENOMEM; + goto out_freectx; + } + + numa_bitmask_setall(cparams.bmp); + + for (i = 0; i < WD_DIR_MAX; i++) + ctx_set_num[i].sync_ctx_num = 2; + + ret = wd_comp_init2_("zlib", 0, 0, &cparams); + if (ret) + goto out_freebmp; + + zlib_config.status = WD_ZLIB_INIT; + +out_freebmp: + numa_free_nodemask(cparams.bmp); + +out_freectx: + free(ctx_set_num); + + return ret; +} + +static void wd_zlib_uninit(void) +{ + wd_comp_uninit2(); + zlib_config.status = WD_ZLIB_UNINIT; +} + +static int wd_zlib_analy_alg(int windowbits, int *alg, int *windowsize) +{ + static const int ZLIB_MAX_WBITS = 15; + static const int ZLIB_MIN_WBITS = 8; + static const int GZIP_MAX_WBITS = 31; + static const int GZIP_MIN_WBITS = 24; + static const int DEFLATE_MAX_WBITS = -8; + static const int DEFLATE_MIN_WBITS = -15; + static const int WBINS_ZLIB_4K = 12; + static const int WBINS_GZIP_4K = 27; + static const int WBINS_DEFLATE_4K = -12; + int ret = Z_STREAM_ERROR; + + if ((windowbits >= ZLIB_MIN_WBITS) && (windowbits <= ZLIB_MAX_WBITS)) { + *alg = WD_ZLIB; + *windowsize = max(windowbits - WBINS_ZLIB_4K, WD_COMP_WS_4K); + ret = Z_OK; + } else if ((windowbits >= GZIP_MIN_WBITS) && (windowbits <= GZIP_MAX_WBITS)) { + *alg = WD_GZIP; + *windowsize = max(windowbits - WBINS_GZIP_4K, WD_COMP_WS_4K); + ret = Z_OK; + } else if ((windowbits >= DEFLATE_MIN_WBITS) && (windowbits <= DEFLATE_MAX_WBITS)) { + *alg = WD_DEFLATE; + *windowsize = max(windowbits - WBINS_DEFLATE_4K, WD_COMP_WS_4K); + ret = Z_OK; + } + + return ret; +} + +static int wd_zlib_alloc_sess(z_streamp strm, int level, int windowbits, enum wd_comp_op_type type) +{ + struct wd_comp_sess_setup setup = {0}; + struct sched_params sparams = {0}; + int windowsize, alg, ret; + handle_t h_sess; + + ret = wd_zlib_analy_alg(windowbits, &alg, &windowsize); + if (ret < 0) { + WD_ERR("invalid: windowbits is %d!\n", windowbits); + return ret; + } + + setup.comp_lv = level; + setup.alg_type = alg; + setup.win_sz = windowsize; + setup.op_type = type; + sparams.type = type; + setup.sched_param = &sparams; + + h_sess = wd_comp_alloc_sess(&setup); + if (!h_sess) { + WD_ERR("failed to alloc comp sess!\n"); + return Z_STREAM_ERROR; + } + strm->reserved = (__u64)h_sess; + + return Z_OK; +} + +static void wd_zlib_free_sess(z_streamp strm) +{ + wd_comp_free_sess((handle_t)strm->reserved); +} + +static int wd_zlib_do_request(z_streamp strm, int flush, enum wd_comp_op_type type) +{ + handle_t h_sess = strm->reserved; + struct wd_comp_req req = {0}; + int src_len = strm->avail_in; + int dst_len = strm->avail_out; + int ret; + + if (unlikely(flush != Z_SYNC_FLUSH && flush != Z_FINISH)) { + WD_ERR("invalid: flush is %d!\n", flush); + return Z_STREAM_ERROR; + } + + req.src = (void *)strm->next_in; + req.src_len = strm->avail_in; + req.dst = (void *)strm->next_out; + req.dst_len = strm->avail_out; + req.op_type = type; + req.data_fmt = WD_FLAT_BUF; + req.last = (flush == Z_FINISH) ? 1 : 0; + + ret = wd_do_comp_strm(h_sess, &req); + if (unlikely(ret)) { + WD_ERR("failed to do compress(%d)!\n", ret); + return Z_STREAM_ERROR; + } + + strm->avail_in = src_len - req.src_len; + strm->avail_out = dst_len - req.dst_len; + strm->total_in += req.src_len; + strm->total_out += req.dst_len; + + if (flush == Z_FINISH && req.src_len == src_len) + ret = Z_STREAM_END; + + return ret; +} + +/* === Compression === */ +int wd_deflateInit_(z_streamp strm, int level, const char *version, int stream_size) + +{ + return wd_deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, + Z_DEFAULT_STRATEGY, version, stream_size); +} + +int wd_deflateInit2_(z_streamp strm, int level, int method, int windowBits, + int memLevel, int strategy, const char *version, int stream_size) +{ + int ret; + + pthread_atfork(NULL, NULL, wd_zlib_unlock); + + pthread_mutex_lock(&wd_zlib_mutex); + ret = wd_zlib_init(); + if (unlikely(ret < 0)) + goto out_unlock; + + strm->total_in = 0; + strm->total_out = 0; + + ret = wd_zlib_alloc_sess(strm, level, windowBits, WD_DIR_COMPRESS); + if (unlikely(ret < 0)) + goto out_uninit; + + __atomic_add_fetch(&zlib_config.count, 1, __ATOMIC_RELAXED); + pthread_mutex_unlock(&wd_zlib_mutex); + + return 0; + +out_uninit: + wd_zlib_uninit(); + +out_unlock: + pthread_mutex_unlock(&wd_zlib_mutex); + + return ret; +} + +int wd_deflate(z_streamp strm, int flush) +{ + return wd_zlib_do_request(strm, flush, WD_DIR_COMPRESS); +} + +int wd_deflateReset(z_streamp strm) +{ + wd_comp_reset_sess((handle_t)strm->reserved); + + strm->total_in = 0; + strm->total_out = 0; + + return Z_OK; +} + +int wd_deflateEnd(z_streamp strm) +{ + int ret; + + wd_zlib_free_sess(strm); + + pthread_mutex_lock(&wd_zlib_mutex); + + ret = __atomic_sub_fetch(&zlib_config.count, 1, __ATOMIC_RELAXED); + if (ret != 0) + goto out_unlock; + + wd_zlib_uninit(); + +out_unlock: + pthread_mutex_unlock(&wd_zlib_mutex); + + return Z_OK; +} + +/* === Decompression === */ +int wd_inflateInit_(z_streamp strm, const char *version, int stream_size) +{ + return wd_inflateInit2_(strm, MAX_WBITS, version, stream_size); +} + +int wd_inflateInit2_(z_streamp strm, int windowBits, const char *version, int stream_size) +{ + int ret; + + pthread_atfork(NULL, NULL, wd_zlib_unlock); + + pthread_mutex_lock(&wd_zlib_mutex); + ret = wd_zlib_init(); + if (unlikely(ret < 0)) + goto out_unlock; + + strm->total_in = 0; + strm->total_out = 0; + + ret = wd_zlib_alloc_sess(strm, 0, windowBits, WD_DIR_DECOMPRESS); + if (unlikely(ret < 0)) + goto out_uninit; + + __atomic_add_fetch(&zlib_config.count, 1, __ATOMIC_RELAXED); + pthread_mutex_unlock(&wd_zlib_mutex); + + return 0; + +out_uninit: + wd_zlib_uninit(); + +out_unlock: + pthread_mutex_unlock(&wd_zlib_mutex); + + return ret; +} + +int wd_inflate(z_streamp strm, int flush) +{ + return wd_zlib_do_request(strm, flush, WD_DIR_DECOMPRESS); +} + +int wd_inflateReset(z_streamp strm) +{ + wd_comp_reset_sess((handle_t)strm->reserved); + + strm->total_in = 0; + strm->total_out = 0; + + return Z_OK; +} + +int wd_inflateEnd(z_streamp strm) +{ + int ret; + + wd_zlib_free_sess(strm); + + pthread_mutex_lock(&wd_zlib_mutex); + + ret = __atomic_sub_fetch(&zlib_config.count, 1, __ATOMIC_RELAXED); + if (ret != 0) + goto out_unlock; + + wd_zlib_uninit(); + +out_unlock: + pthread_mutex_unlock(&wd_zlib_mutex); + + return Z_OK; +} diff --git a/uadk/wd_zlibwrapper.h b/uadk/wd_zlibwrapper.h new file mode 100644 index 0000000000000000000000000000000000000000..a409f3696a73ad3ff2300dd9cccbca45717253d5 --- /dev/null +++ b/uadk/wd_zlibwrapper.h @@ -0,0 +1,121 @@ +/* SPDX-License-Identifier: Apache-2.0 */ +/* + * Copyright 2022 Huawei Technologies Co.,Ltd. All rights reserved. + */ + +#ifndef UADK_ZLIBWRAPPER_H +#define UADK_ZLIBWRAPPER_H + +/* + * These APIs are used to replace the ZLIB library. So if you don't use them. + * Please do not use these. These APIs provide limited function, while the + * wd_comp.h provide full function. + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#define ZLIB_VERSION "1.2.11" + +/* Allowed flush values; the same as zlib library */ +#define Z_NO_FLUSH 0 +#define Z_PARTIAL_FLUSH 1 +#define Z_SYNC_FLUSH 2 +#define Z_FULL_FLUSH 3 +#define Z_FINISH 4 + +/* + * Return codes for the compression/decompression functions. Negative values + * are errors, positive values are used for special but normal events. + */ +#define Z_OK 0 +#define Z_STREAM_END 1 +#define Z_NEED_DICT 2 +#define Z_ERRNO (-1) +#define Z_STREAM_ERROR (-2) +#define Z_DATA_ERROR (-3) +#define Z_MEM_ERROR (-4) +#define Z_BUF_ERROR (-5) +#define Z_VERSION_ERROR (-6) + +#define Z_DEFLATED 0 +#define MAX_WBITS 15 +#define DEF_MEM_LEVEL 0 +#define Z_DEFAULT_STRATEGY 0 + +struct internal_state {}; + +typedef void (*alloc_func) (void *opaque, __u32 items, __u32 size); +typedef void (*free_func) (void *opaque, void *address); + +typedef struct z_stream_s { + /* next input byte */ + const __u8 *next_in; + /* number of bytes available at next_in */ + __u32 avail_in; + /* total number of input bytes read so far */ + __u64 total_in; + + /* next output byte will go here */ + __u8 *next_out; + /* remaining free space at next_out */ + __u32 avail_out; + /* total number of bytes output so far */ + __u64 total_out; + + /* last error message, NULL if no error */ + const char *msg; + /* not visible by applications */ + struct internal_state *state; + + /* used to allocate the internal state */ + alloc_func zalloc; + /* used to free the internal state */ + free_func zfree; + /* private data object passed to zalloc and zfree */ + void *opaque; + + /* + * Best guess about the data type: binary or text + * for deflate, or the decoding state for inflate. + */ + int data_type; + /* Adler-32 or CRC-32 value of the uncompressed data */ + __u64 adler; + /* reserved the wd_comp_sess */ + __u64 reserved; +} z_stream; + +typedef z_stream * z_streamp; + +int wd_deflateInit_(z_streamp strm, int level, const char *version, int stream_size); +int wd_deflateInit2_(z_streamp strm, int level, int method, int windowBits, + int memLevel, int strategy, const char *version, int stream_size); +/* + * The flush support Z_SYNC_FLUSH and Z_FINISH only. + */ +int wd_deflate(z_streamp strm, int flush); +int wd_deflateReset(z_streamp strm); +int wd_deflateEnd(z_streamp strm); + +int wd_inflateInit_(z_streamp strm, const char *version, int stream_size); +int wd_inflateInit2_(z_streamp strm, int windowBits, const char *version, int stream_size); +int wd_inflate(z_streamp strm, int flush); +int wd_inflateReset(z_streamp strm); +int wd_inflateEnd(z_streamp strm); + +#define wd_deflateInit(strm, level) \ + wd_deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream)) + +#define wd_deflateInit2(strm, level, method, windowBits, memLevel, strategy) \ + wd_deflateInit2_((strm), (level), (method), (windowBits), (memLevel),\ + (strategy), ZLIB_VERSION, (int)sizeof(z_stream)) + +#define wd_inflateInit(strm) \ + wd_inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream)) + +#define wd_inflateInit2(strm, windowBits) \ + wd_inflateInit2_((strm), (windowBits), ZLIB_VERSION, (int)sizeof(z_stream)) + +#endif /* UADK_ZLIBWRAPPER_H */