From: Weili Qian qianweili@huawei.com
Support performance test for TRNG.
Signed-off-by: Weili Qian qianweili@huawei.com --- uadk_tool/Makefile.am | 1 + uadk_tool/benchmark/trng_wd_benchmark.c | 324 ++++++++++++++++++++++++ uadk_tool/benchmark/trng_wd_benchmark.h | 7 + uadk_tool/benchmark/uadk_benchmark.c | 14 + uadk_tool/benchmark/uadk_benchmark.h | 2 + 5 files changed, 348 insertions(+) create mode 100644 uadk_tool/benchmark/trng_wd_benchmark.c create mode 100644 uadk_tool/benchmark/trng_wd_benchmark.h
diff --git a/uadk_tool/Makefile.am b/uadk_tool/Makefile.am index 62372d8..6b787f9 100644 --- a/uadk_tool/Makefile.am +++ b/uadk_tool/Makefile.am @@ -15,6 +15,7 @@ uadk_tool_SOURCES=uadk_tool.c dfx/uadk_dfx.c dfx/uadk_dfx.h \ benchmark/hpre_wd_benchmark.c hpre_wd_benchmark.h \ benchmark/zip_uadk_benchmark.c benchmark/zip_uadk_benchmark.h \ benchmark/zip_wd_benchmark.c benchmark/zip_wd_benchmark.h \ + benchmark/trng_wd_benchmark.c benchmark/trng_wd_benchmark.h \ test/uadk_test.c test/uadk_test.h \ test/test_sec.c test/test_sec.h test/sec_template_tv.h
diff --git a/uadk_tool/benchmark/trng_wd_benchmark.c b/uadk_tool/benchmark/trng_wd_benchmark.c new file mode 100644 index 0000000..64942f0 --- /dev/null +++ b/uadk_tool/benchmark/trng_wd_benchmark.c @@ -0,0 +1,324 @@ +/* SPDX-License-Identifier: Apache-2.0 */ + +#include <numa.h> +#include "uadk_benchmark.h" + +#include "trng_wd_benchmark.h" +#include "v1/wd.h" +#include "v1/wd_rng.h" + +struct thread_bd_res { + struct wd_queue *queue; + void *out; + __u32 in_bytes; +}; + +struct thread_queue_res { + struct thread_bd_res *bd_res; +}; + +struct wd_thread_res { + u32 td_id; + u32 pollid; +}; + +struct trng_async_tag { + void *ctx; + int optype; +}; + +static unsigned int g_thread_num; +static struct thread_queue_res g_thread_queue; + +static int init_trng_wd_queue(struct acc_option *options) +{ + int i, ret; + + g_thread_queue.bd_res = malloc(g_thread_num * sizeof(struct thread_bd_res)); + if (!g_thread_queue.bd_res) { + printf("failed to malloc thread res memory!\n"); + return -ENOMEM; + } + + for (i = 0; i < g_thread_num; i++) { + g_thread_queue.bd_res[i].queue = malloc(sizeof(struct wd_queue)); + if (!g_thread_queue.bd_res[i].queue) { + ret = -ENOMEM; + goto free_mem; + } + + g_thread_queue.bd_res[i].queue->capa.alg = options->algclass; + /* nodemask need to be clean */ + g_thread_queue.bd_res[i].queue->node_mask = 0x0; + memset(g_thread_queue.bd_res[i].queue->dev_path, 0x0, PATH_STR_SIZE); + + g_thread_queue.bd_res[i].in_bytes = options->pktlen; + g_thread_queue.bd_res[i].out = malloc(options->pktlen); + if (!g_thread_queue.bd_res[i].queue) { + free(g_thread_queue.bd_res[i].queue); + ret = -ENOMEM; + goto free_mem; + } + + ret = wd_request_queue(g_thread_queue.bd_res[i].queue); + if (ret) { + printf("failed to request queue %d, ret = %d!\n", i, ret); + free(g_thread_queue.bd_res[i].out); + free(g_thread_queue.bd_res[i].queue); + goto free_mem; + } + } + + return 0; + +free_mem: + for (i = i - 1; i >= 0; i--) { + wd_release_queue(g_thread_queue.bd_res[i].queue); + free(g_thread_queue.bd_res[i].out); + free(g_thread_queue.bd_res[i].queue); + } + + free(g_thread_queue.bd_res); + return ret; +} + +static void uninit_trng_wd_queue(void) +{ + int j; + + for (j = 0; j < g_thread_num; j++) { + wd_release_queue(g_thread_queue.bd_res[j].queue); + free(g_thread_queue.bd_res[j].out); + free(g_thread_queue.bd_res[j].queue); + } + + free(g_thread_queue.bd_res); +} + +static void *trng_wd_sync_run(void *arg) +{ + struct wd_thread_res *pdata = (struct wd_thread_res *)arg; + struct wcrypto_rng_ctx_setup trng_setup; + struct wcrypto_rng_op_data opdata; + struct wd_queue *queue; + void *ctx = NULL; + u32 count = 0; + int ret; + + queue = g_thread_queue.bd_res[pdata->td_id].queue; + ctx = wcrypto_create_rng_ctx(queue, &trng_setup); + if (!ctx) + return NULL; + + memset(&opdata, 0, sizeof(opdata)); + opdata.in_bytes = g_thread_queue.bd_res[pdata->td_id].in_bytes; + opdata.out = g_thread_queue.bd_res[pdata->td_id].out; + opdata.op_type = WCRYPTO_TRNG_GEN; + + do { + ret = wcrypto_do_rng(ctx, &opdata, NULL); + if (ret) { + printf("failed to do rng task, ret: %d\n", ret); + goto ctx_release; + } + + count++; + if (get_run_state() == 0) + break; + } while (true); + +ctx_release: + wcrypto_del_rng_ctx(ctx); + add_recv_data(count, opdata.in_bytes); + + return NULL; +} + +static void trng_wd_sync_threads(void) +{ + struct wd_thread_res threads_args[THREADS_NUM]; + pthread_t tdid[THREADS_NUM]; + int i, ret; + + for (i = 0; i < g_thread_num; i++) { + threads_args[i].td_id = i; + ret = pthread_create(&tdid[i], NULL, trng_wd_sync_run, &threads_args[i]); + if (ret) { + printf("failed to create sync thread!\n"); + return; + } + } + + /* join thread */ + for (i = 0; i < g_thread_num; i++) { + ret = pthread_join(tdid[i], NULL); + if (ret) { + printf("failed to join sync thread!\n"); + return; + } + } +} + +void *wd_trng_poll(void *data) +{ + struct wd_thread_res *pdata = (struct wd_thread_res *)data; + struct wd_queue *queue; + u32 last_time = 2; // poll need one more recv time + u32 count = 0; + u32 in_bytes; + int recv; + + in_bytes = g_thread_queue.bd_res[pdata->pollid].in_bytes; + queue = g_thread_queue.bd_res[pdata->pollid].queue; + + while (last_time) { + recv = wcrypto_rng_poll(queue, ACC_QUEUE_SIZE); + if (recv < 0) { + printf("failed to recv bd, ret: %d!\n", recv); + goto recv_error; + } + count += recv; + + if (get_run_state() == 0) + last_time--; + } + +recv_error: + add_recv_data(count, in_bytes); + + return NULL; +} + +static void *trng_async_cb(const void *msg, void *tag) +{ + return NULL; +} + +static void *wd_trng_async_run(void *arg) +{ + struct wd_thread_res *pdata = (struct wd_thread_res *)arg; + struct wcrypto_rng_ctx_setup trng_setup; + struct wcrypto_rng_op_data opdata; + struct trng_async_tag *tag = NULL; + struct wd_queue *queue; + void *ctx = NULL; + int ret, i; + + memset(&opdata, 0, sizeof(opdata)); + + queue = g_thread_queue.bd_res[pdata->td_id].queue; + trng_setup.cb = (void *)trng_async_cb; + + ctx = wcrypto_create_rng_ctx(queue, &trng_setup); + if (!ctx) + return NULL; + + opdata.in_bytes = g_thread_queue.bd_res[pdata->td_id].in_bytes; + opdata.out = g_thread_queue.bd_res[pdata->td_id].out; + opdata.op_type = WCRYPTO_TRNG_GEN; + + tag = malloc(sizeof(*tag) * MAX_POOL_LENTH); + if (!tag) { + printf("failed to malloc dh tag!\n"); + goto free_ctx; + } + tag->ctx = ctx; + + do { + ret = wcrypto_do_rng(ctx, &opdata, tag); + if (ret && ret != -WD_EBUSY) { + printf("failed to send trng task, ret = %d!\n", ret); + break; + } + + if (get_run_state() == 0) + break; + } while (true); + + /* Release memory after all tasks are complete. */ + i = 0; + while (get_recv_time() != g_thread_num) { + if (i++ >= MAX_TRY_CNT) { + printf("failed to wait poll thread finish!\n"); + break; + } + + usleep(SEND_USLEEP); + } + + if (tag) + free(tag); +free_ctx: + wcrypto_del_rng_ctx(ctx); + add_send_complete(); + + return NULL; +} + +static void trng_wd_async_threads(void) +{ + struct wd_thread_res threads_args[THREADS_NUM]; + pthread_t tdid[THREADS_NUM]; + pthread_t pollid[THREADS_NUM]; + int i, ret; + + for (i = 0; i < g_thread_num; i++) { + threads_args[i].pollid = i; + /* poll thread */ + ret = pthread_create(&pollid[i], NULL, wd_trng_poll, &threads_args[i]); + if (ret) { + printf("failed to create poll thread!\n"); + return; + } + } + + for (i = 0; i < g_thread_num; i++) { + threads_args[i].td_id = i; + ret = pthread_create(&tdid[i], NULL, wd_trng_async_run, &threads_args[i]); + if (ret) { + printf("failed to create async thread!\n"); + return; + } + } + + /* join thread */ + for (i = 0; i < g_thread_num; i++) { + ret = pthread_join(tdid[i], NULL); + if (ret) { + printf("failed to join async thread!\n"); + return; + } + } + + for (i = 0; i < g_thread_num; i++) { + ret = pthread_join(pollid[i], NULL); + if (ret) { + printf("failed to join poll thread!\n"); + return; + } + } +} + +int trng_wd_benchmark(struct acc_option *options) +{ + u32 ptime; + int ret; + + g_thread_num = options->threads; + + ret = init_trng_wd_queue(options); + if (ret) + return ret; + + get_pid_cpu_time(&ptime); + time_start(options->times); + if (options->syncmode) + trng_wd_async_threads(); + else + trng_wd_sync_threads(); + cal_perfermance_data(options, ptime); + + uninit_trng_wd_queue(); + + return 0; +} diff --git a/uadk_tool/benchmark/trng_wd_benchmark.h b/uadk_tool/benchmark/trng_wd_benchmark.h new file mode 100644 index 0000000..49453c8 --- /dev/null +++ b/uadk_tool/benchmark/trng_wd_benchmark.h @@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: Apache-2.0 */ + +#ifndef TRNG_WD_BENCHMARK_H +#define TRNG_WD_BENCHMARK_H + +extern int trng_wd_benchmark(struct acc_option *options); +#endif /* TRNG_WD_BENCHMARK_H */ diff --git a/uadk_tool/benchmark/uadk_benchmark.c b/uadk_tool/benchmark/uadk_benchmark.c index f36cc4b..f903829 100644 --- a/uadk_tool/benchmark/uadk_benchmark.c +++ b/uadk_tool/benchmark/uadk_benchmark.c @@ -14,6 +14,8 @@ #include "zip_uadk_benchmark.h" #include "zip_wd_benchmark.h"
+#include "trng_wd_benchmark.h" + #define TABLE_SPACE_SIZE 8
/*----------------------------------------head struct--------------------------------------------------------*/ @@ -131,6 +133,7 @@ static struct acc_alg_item alg_options[] = { {"sha512", SHA512_ALG}, {"sha512-224", SHA512_224}, {"sha512-256", SHA512_256}, + {"trng", TRNG}, {"", ALG_MAX} };
@@ -345,6 +348,11 @@ static void parse_alg_param(struct acc_option *option) option->acctype = HPRE_TYPE; option->subtype = X448_TYPE; break; + case TRNG: + snprintf(option->algclass, MAX_ALG_NAME, "%s", "trng"); + option->acctype = TRNG_TYPE; + option->subtype = DEFAULT_TYPE; + break; default: if (option->algtype <= RSA_4096_CRT) { snprintf(option->algclass, MAX_ALG_NAME, "%s", "rsa"); @@ -456,6 +464,12 @@ static int benchmark_run(struct acc_option *option) } else if (option->modetype & NOSVA_MODE) { ret = zip_wd_benchmark(option); } + case TRNG_TYPE: + if (option->modetype & SVA_MODE) + ACC_TST_PRT("TRNG not support sva mode..\n"); + else if (option->modetype & NOSVA_MODE) + ret = trng_wd_benchmark(option); + break; }
diff --git a/uadk_tool/benchmark/uadk_benchmark.h b/uadk_tool/benchmark/uadk_benchmark.h index 2c8de11..44e2dd9 100644 --- a/uadk_tool/benchmark/uadk_benchmark.h +++ b/uadk_tool/benchmark/uadk_benchmark.h @@ -78,6 +78,7 @@ enum acc_type { SEC_TYPE, HPRE_TYPE, ZIP_TYPE, + TRNG_TYPE, };
enum alg_type { @@ -174,6 +175,7 @@ enum test_alg { SHA512_ALG, SHA512_224, SHA512_256, // digest key all set 4 Bytes + TRNG, ALG_MAX, };