libwd/0019-uadk-v1-add-the-cookie-depth-configuration.patch
2023-09-28 09:15:32 +08:00

477 lines
14 KiB
Diff

From 85269d2fddbe03c53763a2c3214f41a3c19f9318 Mon Sep 17 00:00:00 2001
From: Weili Qian <qianweili@huawei.com>
Date: Mon, 31 Jul 2023 14:50:55 +0800
Subject: [PATCH 19/26] uadk/v1: add the cookie depth configuration
Add the cookie depth configuration. Users can use
the 'flags 0~15bits' in 'struct wd_capa' to input the
depth of cookie. If the input value ranges from 1 to 1024,
the input value is used to initialize cookie. Otherwise,
the default value is used to initialize cookie.
Signed-off-by: Weili Qian <qianweili@huawei.com>
---
v1/wd.h | 8 +++++++-
v1/wd_aead.c | 26 +++++++++++++++++---------
v1/wd_cipher.c | 27 +++++++++++++++++----------
v1/wd_comp.c | 4 +++-
v1/wd_dh.c | 8 +++++---
v1/wd_digest.c | 28 ++++++++++++++++++----------
v1/wd_ecc.c | 29 +++++++++++++++++++----------
v1/wd_rng.c | 14 ++++++++------
v1/wd_rsa.c | 12 +++++++-----
v1/wd_util.c | 16 ++++++++++++++++
v1/wd_util.h | 11 ++++++-----
11 files changed, 123 insertions(+), 60 deletions(-)
diff --git a/v1/wd.h b/v1/wd.h
index 1bd9669..176f2ef 100644
--- a/v1/wd.h
+++ b/v1/wd.h
@@ -174,7 +174,13 @@ struct wd_capa {
int throughput;
/* latency capability */
int latency;
- /* other capabilities */
+ /*
+ * Other capabilities.
+ * 0~15 bits: number of cookies that the user wants to allocate.
+ * Optional, user can set value based on the number of requests and system memory,
+ * 1~1024 is valid. If the value is not set or invalid, the default value 64 (rng is 256)
+ * is used to initialize cookies.
+ */
__u32 flags;
/* For algorithm parameters, now it is defined in extending notions */
diff --git a/v1/wd_aead.c b/v1/wd_aead.c
index f81f327..a82d51d 100644
--- a/v1/wd_aead.c
+++ b/v1/wd_aead.c
@@ -142,13 +142,23 @@ static int create_ctx_para_check(struct wd_queue *q,
return WD_SUCCESS;
}
-static void init_aead_cookie(struct wcrypto_aead_ctx *ctx,
+static int init_aead_cookie(struct wcrypto_aead_ctx *ctx,
struct wcrypto_aead_ctx_setup *setup)
{
struct wcrypto_aead_cookie *cookie;
- __u32 i;
+ __u32 flags = ctx->q->capa.flags;
+ __u32 cookies_num, i;
+ int ret;
+
+ cookies_num = wd_get_ctx_cookies_num(flags, WD_CTX_COOKIES_NUM);
+ ret = wd_init_cookie_pool(&ctx->pool,
+ sizeof(struct wcrypto_aead_cookie), cookies_num);
+ if (ret) {
+ WD_ERR("failed to init cookie pool!\n");
+ return ret;
+ }
- for (i = 0; i < ctx->pool.cookies_num; i++) {
+ for (i = 0; i < cookies_num; i++) {
cookie = (void *)((uintptr_t)ctx->pool.cookies +
i * ctx->pool.cookies_size);
cookie->msg.alg_type = WCRYPTO_AEAD;
@@ -161,6 +171,8 @@ static void init_aead_cookie(struct wcrypto_aead_ctx *ctx,
cookie->tag.wcrypto_tag.ctx_id = ctx->ctx_id;
cookie->msg.usr_data = (uintptr_t)&cookie->tag;
}
+
+ return 0;
}
static int wcrypto_setup_qinfo(struct wcrypto_aead_ctx_setup *setup,
@@ -236,13 +248,9 @@ void *wcrypto_create_aead_ctx(struct wd_queue *q,
}
ctx->iv_blk_size = get_iv_block_size(setup->cmode);
- ret = wd_init_cookie_pool(&ctx->pool,
- sizeof(struct wcrypto_aead_cookie), WD_CTX_MSG_NUM);
- if (ret) {
- WD_ERR("fail to init cookie pool!\n");
+ ret = init_aead_cookie(ctx, setup);
+ if (ret)
goto free_ctx_akey;
- }
- init_aead_cookie(ctx, setup);
return ctx;
diff --git a/v1/wd_cipher.c b/v1/wd_cipher.c
index 3d7d140..55bfbe2 100644
--- a/v1/wd_cipher.c
+++ b/v1/wd_cipher.c
@@ -122,13 +122,23 @@ static int create_ctx_para_check(struct wd_queue *q,
return WD_SUCCESS;
}
-static void init_cipher_cookie(struct wcrypto_cipher_ctx *ctx,
+static int init_cipher_cookie(struct wcrypto_cipher_ctx *ctx,
struct wcrypto_cipher_ctx_setup *setup)
{
struct wcrypto_cipher_cookie *cookie;
- __u32 i;
+ __u32 flags = ctx->q->capa.flags;
+ __u32 cookies_num, i;
+ int ret;
+
+ cookies_num = wd_get_ctx_cookies_num(flags, WD_CTX_COOKIES_NUM);
+ ret = wd_init_cookie_pool(&ctx->pool,
+ sizeof(struct wcrypto_cipher_cookie), cookies_num);
+ if (ret) {
+ WD_ERR("failed to init cookie pool!\n");
+ return ret;
+ }
- for (i = 0; i < ctx->pool.cookies_num; i++) {
+ for (i = 0; i < cookies_num; i++) {
cookie = (void *)((uintptr_t)ctx->pool.cookies +
i * ctx->pool.cookies_size);
cookie->msg.alg_type = WCRYPTO_CIPHER;
@@ -139,6 +149,8 @@ static void init_cipher_cookie(struct wcrypto_cipher_ctx *ctx,
cookie->tag.wcrypto_tag.ctx_id = ctx->ctx_id;
cookie->msg.usr_data = (uintptr_t)&cookie->tag;
}
+
+ return 0;
}
static int setup_qinfo(struct wcrypto_cipher_ctx_setup *setup,
@@ -209,14 +221,9 @@ void *wcrypto_create_cipher_ctx(struct wd_queue *q,
}
ctx->iv_blk_size = get_iv_block_size(setup->alg, setup->mode);
-
- ret = wd_init_cookie_pool(&ctx->pool,
- sizeof(struct wcrypto_cipher_cookie), WD_CTX_MSG_NUM);
- if (ret) {
- WD_ERR("fail to init cookie pool!\n");
+ ret = init_cipher_cookie(ctx, setup);
+ if (ret)
goto free_ctx_key;
- }
- init_cipher_cookie(ctx, setup);
return ctx;
diff --git a/v1/wd_comp.c b/v1/wd_comp.c
index 9e7ec5a..f898c1d 100644
--- a/v1/wd_comp.c
+++ b/v1/wd_comp.c
@@ -122,6 +122,7 @@ void *wcrypto_create_comp_ctx(struct wd_queue *q,
{
struct wcrypto_comp_ctx *ctx;
struct q_info *qinfo;
+ __u32 cookies_num;
__u32 ctx_id = 0;
int ret;
@@ -169,8 +170,9 @@ void *wcrypto_create_comp_ctx(struct wd_queue *q,
goto free_ctx_id;
}
+ cookies_num = wd_get_ctx_cookies_num(q->capa.flags, WD_CTX_COOKIES_NUM);
ret = wd_init_cookie_pool(&ctx->pool,
- sizeof(struct wcrypto_comp_cookie), WD_CTX_MSG_NUM);
+ sizeof(struct wcrypto_comp_cookie), cookies_num);
if (ret) {
WD_ERR("fail to init cookie pool!\n");
goto free_ctx_buf;
diff --git a/v1/wd_dh.c b/v1/wd_dh.c
index 714ae71..9ed0e0d 100644
--- a/v1/wd_dh.c
+++ b/v1/wd_dh.c
@@ -90,17 +90,19 @@ static int wcrypto_init_dh_cookie(struct wcrypto_dh_ctx *ctx)
{
struct wcrypto_dh_ctx_setup *setup = &ctx->setup;
struct wcrypto_dh_cookie *cookie;
+ __u32 flags = ctx->q->capa.flags;
+ __u32 cookies_num, i;
int ret;
- __u32 i;
+ cookies_num = wd_get_ctx_cookies_num(flags, WD_CTX_COOKIES_NUM);
ret = wd_init_cookie_pool(&ctx->pool,
- sizeof(struct wcrypto_dh_cookie), WD_HPRE_CTX_MSG_NUM);
+ sizeof(struct wcrypto_dh_cookie), cookies_num);
if (ret) {
WD_ERR("fail to init cookie pool!\n");
return ret;
}
- for (i = 0; i < ctx->pool.cookies_num; i++) {
+ for (i = 0; i < cookies_num; i++) {
cookie = (void *)((uintptr_t)ctx->pool.cookies +
i * ctx->pool.cookies_size);
cookie->msg.is_g2 = (__u8)setup->is_g2;
diff --git a/v1/wd_digest.c b/v1/wd_digest.c
index 809aecd..cb0d4d8 100644
--- a/v1/wd_digest.c
+++ b/v1/wd_digest.c
@@ -98,13 +98,23 @@ static int create_ctx_para_check(struct wd_queue *q,
return WD_SUCCESS;
}
-static void init_digest_cookie(struct wcrypto_digest_ctx *ctx,
- struct wcrypto_digest_ctx_setup *setup)
+static int init_digest_cookie(struct wcrypto_digest_ctx *ctx,
+ struct wcrypto_digest_ctx_setup *setup)
{
struct wcrypto_digest_cookie *cookie;
- __u32 i;
+ __u32 flags = ctx->q->capa.flags;
+ __u32 cookies_num, i;
+ int ret;
+
+ cookies_num = wd_get_ctx_cookies_num(flags, WD_CTX_COOKIES_NUM);
+ ret = wd_init_cookie_pool(&ctx->pool,
+ sizeof(struct wcrypto_digest_cookie), cookies_num);
+ if (ret) {
+ WD_ERR("failed to init cookie pool!\n");
+ return ret;
+ }
- for (i = 0; i < ctx->pool.cookies_num; i++) {
+ for (i = 0; i < cookies_num; i++) {
cookie = (void *)((uintptr_t)ctx->pool.cookies +
i * ctx->pool.cookies_size);
cookie->msg.alg_type = WCRYPTO_DIGEST;
@@ -117,6 +127,8 @@ static void init_digest_cookie(struct wcrypto_digest_ctx *ctx,
cookie->tag.wcrypto_tag.ctx_id = ctx->ctx_id;
cookie->msg.usr_data = (uintptr_t)&cookie->tag;
}
+
+ return 0;
}
static int setup_qinfo(struct wcrypto_digest_ctx_setup *setup,
@@ -192,13 +204,9 @@ void *wcrypto_create_digest_ctx(struct wd_queue *q,
else
ctx->align_sz = SEC_SHA1_ALIGN_SZ;
- ret = wd_init_cookie_pool(&ctx->pool,
- sizeof(struct wcrypto_digest_cookie), WD_CTX_MSG_NUM);
- if (ret) {
- WD_ERR("fail to init cookie pool!\n");
+ ret = init_digest_cookie(ctx, setup);
+ if (ret)
goto free_ctx_key;
- }
- init_digest_cookie(ctx, setup);
return ctx;
diff --git a/v1/wd_ecc.c b/v1/wd_ecc.c
index 93b4e1b..c4fab63 100644
--- a/v1/wd_ecc.c
+++ b/v1/wd_ecc.c
@@ -1062,15 +1062,25 @@ static void del_ctx_key(struct wd_mm_br *br,
}
}
-static void init_ctx_cookies(struct wcrypto_ecc_ctx *ctx,
- struct wcrypto_ecc_ctx_setup *setup)
+static int init_ctx_cookies(struct wcrypto_ecc_ctx *ctx,
+ struct wcrypto_ecc_ctx_setup *setup)
{
__u32 hsz = get_hw_keysize(ctx->key_size);
struct q_info *qinfo = ctx->q->qinfo;
struct wcrypto_ecc_cookie *cookie;
- __u32 i;
+ __u32 flags = ctx->q->capa.flags;
+ __u32 cookies_num, i;
+ int ret;
+
+ cookies_num = wd_get_ctx_cookies_num(flags, WD_CTX_COOKIES_NUM);
+ ret = wd_init_cookie_pool(&ctx->pool,
+ sizeof(struct wcrypto_ecc_cookie), cookies_num);
+ if (ret) {
+ WD_ERR("fail to init cookie pool!\n");
+ return ret;
+ }
- for (i = 0; i < ctx->pool.cookies_num; i++) {
+ for (i = 0; i < cookies_num; i++) {
cookie = (void *)((uintptr_t)ctx->pool.cookies +
i * ctx->pool.cookies_size);
cookie->msg.curve_id = setup->cv.cfg.id;
@@ -1082,6 +1092,8 @@ static void init_ctx_cookies(struct wcrypto_ecc_ctx *ctx,
cookie->tag.ctx_id = ctx->ctx_id;
cookie->msg.usr_data = (uintptr_t)&cookie->tag;
}
+
+ return 0;
}
static int setup_qinfo(struct wcrypto_ecc_ctx_setup *setup,
@@ -1147,13 +1159,10 @@ void *wcrypto_create_ecc_ctx(struct wd_queue *q,
ctx->key_size = BITS_TO_BYTES(setup->key_bits);
ctx->q = q;
ctx->ctx_id = cid + 1;
- ret = wd_init_cookie_pool(&ctx->pool,
- sizeof(struct wcrypto_ecc_cookie), WD_HPRE_CTX_MSG_NUM);
- if (ret) {
- WD_ERR("fail to init cookie pool!\n");
+
+ ret = init_ctx_cookies(ctx, setup);
+ if (ret)
goto free_ctx;
- }
- init_ctx_cookies(ctx, setup);
ret = create_ctx_key(setup, ctx);
if (unlikely(ret)) {
diff --git a/v1/wd_rng.c b/v1/wd_rng.c
index a783f99..cc8a594 100644
--- a/v1/wd_rng.c
+++ b/v1/wd_rng.c
@@ -26,9 +26,9 @@
#include "wd_util.h"
#include "wd_rng.h"
-#define MAX_NUM 10
-#define RNG_RESEND_CNT 8
-#define RNG_RECV_CNT 8
+#define RNG_RESEND_CNT 8
+#define RNG_RECV_CNT 8
+#define WD_RNG_CTX_COOKIE_NUM 256
struct wcrypto_rng_cookie {
struct wcrypto_cb_tag tag;
@@ -84,7 +84,8 @@ void *wcrypto_create_rng_ctx(struct wd_queue *q,
struct wcrypto_rng_cookie *cookie;
struct wcrypto_rng_ctx *ctx;
struct q_info *qinfo;
- __u32 i, ctx_id = 0;
+ __u32 cookies_num, i;
+ __u32 ctx_id = 0;
int ret;
if (wcrypto_setup_qinfo(setup, q, &ctx_id))
@@ -99,14 +100,15 @@ void *wcrypto_create_rng_ctx(struct wd_queue *q,
ctx->q = q;
ctx->ctx_id = ctx_id + 1;
+ cookies_num = wd_get_ctx_cookies_num(q->capa.flags, WD_RNG_CTX_COOKIE_NUM);
ret = wd_init_cookie_pool(&ctx->pool,
- sizeof(struct wcrypto_rng_cookie), WD_RNG_CTX_MSG_NUM);
+ sizeof(struct wcrypto_rng_cookie), cookies_num);
if (ret) {
WD_ERR("fail to init cookie pool!\n");
free(ctx);
goto free_ctx_id;
}
- for (i = 0; i < ctx->pool.cookies_num; i++) {
+ for (i = 0; i < cookies_num; i++) {
cookie = (void *)((uintptr_t)ctx->pool.cookies +
i * ctx->pool.cookies_size);
cookie->msg.alg_type = WCRYPTO_RNG;
diff --git a/v1/wd_rsa.c b/v1/wd_rsa.c
index 61de3cc..4a2a5b5 100644
--- a/v1/wd_rsa.c
+++ b/v1/wd_rsa.c
@@ -496,19 +496,21 @@ static void del_ctx_key(struct wcrypto_rsa_ctx_setup *setup,
}
}
-struct wcrypto_rsa_ctx *create_ctx(struct wcrypto_rsa_ctx_setup *setup, int ctx_id)
+static struct wcrypto_rsa_ctx *create_ctx(struct wcrypto_rsa_ctx_setup *setup,
+ int ctx_id, __u32 flags)
{
struct wcrypto_rsa_cookie *cookie;
struct wcrypto_rsa_ctx *ctx;
- __u32 i;
+ __u32 cookies_num, i;
int ret;
ctx = calloc(1, sizeof(struct wcrypto_rsa_ctx));
if (!ctx)
return ctx;
+ cookies_num = wd_get_ctx_cookies_num(flags, WD_CTX_COOKIES_NUM);
ret = wd_init_cookie_pool(&ctx->pool,
- sizeof(struct wcrypto_rsa_cookie), WD_HPRE_CTX_MSG_NUM);
+ sizeof(struct wcrypto_rsa_cookie), cookies_num);
if (ret) {
WD_ERR("fail to init cookie pool!\n");
free(ctx);
@@ -518,7 +520,7 @@ struct wcrypto_rsa_ctx *create_ctx(struct wcrypto_rsa_ctx_setup *setup, int ctx_
memcpy(&ctx->setup, setup, sizeof(*setup));
ctx->ctx_id = ctx_id;
ctx->key_size = setup->key_bits >> BYTE_BITS_SHIFT;
- for (i = 0; i < ctx->pool.cookies_num; i++) {
+ for (i = 0; i < cookies_num; i++) {
cookie = (void *)((uintptr_t)ctx->pool.cookies +
i * ctx->pool.cookies_size);
if (setup->is_crt)
@@ -611,7 +613,7 @@ void *wcrypto_create_rsa_ctx(struct wd_queue *q, struct wcrypto_rsa_ctx_setup *s
qinfo->ctx_num++;
wd_unspinlock(&qinfo->qlock);
- ctx = create_ctx(setup, cid + 1);
+ ctx = create_ctx(setup, cid + 1, q->capa.flags);
if (!ctx) {
WD_ERR("create rsa ctx fail!\n");
goto free_ctx_id;
diff --git a/v1/wd_util.c b/v1/wd_util.c
index 26f46d4..6b8f944 100644
--- a/v1/wd_util.c
+++ b/v1/wd_util.c
@@ -157,6 +157,22 @@ put_cookies:
return -WD_EBUSY;
}
+__u32 wd_get_ctx_cookies_num(__u32 usr_cookies_num, __u32 def_num)
+{
+ __u32 usr_num = usr_cookies_num & WD_CTX_COOKIES_NUM_MASK;
+
+ if (!usr_num)
+ return def_num;
+
+ if (usr_num > WD_MAX_CTX_COOKIES_NUM) {
+ WD_ERR("user msg num %u is invalid, use default value: %u!\n",
+ usr_num, def_num);
+ return def_num;
+ }
+
+ return usr_num;
+}
+
int wd_burst_send(struct wd_queue *q, void **req, __u32 num)
{
return drv_send(q, req, num);
diff --git a/v1/wd_util.h b/v1/wd_util.h
index 617af3b..a8c6b15 100644
--- a/v1/wd_util.h
+++ b/v1/wd_util.h
@@ -36,11 +36,11 @@
#include "v1/wd_ecc.h"
#include "v1/wd_adapter.h"
-#define WD_CTX_MSG_NUM 64
-#define WD_HPRE_CTX_MSG_NUM 64
-#define WD_RNG_CTX_MSG_NUM 256
-#define WD_MAX_CTX_NUM 256
-#define BYTE_BITS 8
+#define WD_CTX_COOKIES_NUM 64
+#define WD_MAX_CTX_COOKIES_NUM 1024
+#define WD_CTX_COOKIES_NUM_MASK 0xffff
+#define WD_MAX_CTX_NUM 256
+#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)
@@ -393,6 +393,7 @@ int wd_alloc_id(__u8 *buf, __u32 size, __u32 *id, __u32 last_id, __u32 id_max);
void wd_free_id(__u8 *buf, __u32 size, __u32 id, __u32 id_max);
int wd_get_cookies(struct wd_cookie_pool *pool, void **cookies, __u32 num);
void wd_put_cookies(struct wd_cookie_pool *pool, void **cookies, __u32 num);
+__u32 wd_get_ctx_cookies_num(__u32 usr_cookies_num, __u32 def_num);
const char *wd_get_drv(struct wd_queue *q);
int wd_burst_send(struct wd_queue *q, void **req, __u32 num);
int wd_burst_recv(struct wd_queue *q, void **resp, __u32 num);
--
2.25.1