Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapter 5.25 #557

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
14 changes: 13 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ nobase_pkginclude_HEADERS = v1/wd.h v1/wd_cipher.h v1/wd_aead.h v1/uacce.h v1/wd

lib_LTLIBRARIES=libwd.la libwd_comp.la libwd_crypto.la libhisi_zip.la \
libhisi_hpre.la libhisi_sec.la
if HAVE_ZLIB
lib_LTLIBRARIES+=libhisi_zlib.la
endif

libwd_la_SOURCES=wd.c wd_mempool.c wd.h wd_alg.c wd_alg.h \
v1/wd.c v1/wd.h v1/wd_adapter.c v1/wd_adapter.h \
Expand All @@ -59,14 +62,18 @@ libwd_la_SOURCES=wd.c wd_mempool.c wd.h wd_alg.c wd_alg.h \
v1/drv/hisi_zip_udrv.c v1/drv/hisi_zip_udrv.h \
v1/drv/hisi_hpre_udrv.c v1/drv/hisi_hpre_udrv.h \
v1/drv/hisi_sec_udrv.c v1/drv/hisi_sec_udrv.h \
v1/drv/hisi_rng_udrv.c v1/drv/hisi_rng_udrv.h
v1/drv/hisi_rng_udrv.c v1/drv/hisi_rng_udrv.h \
adapter/adapter.c adapter/adapter_roundrobin.c \
adapter/adapter_threshold.c

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.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

libhisi_zlib_la_SOURCES=drv/hisi_zlib.c

libwd_crypto_la_SOURCES=wd_cipher.c wd_cipher.h wd_cipher_drv.h \
wd_aead.c wd_aead.h wd_aead_drv.h \
wd_rsa.c wd_rsa.h wd_rsa_drv.h \
Expand Down Expand Up @@ -94,6 +101,8 @@ libwd_comp_la_DEPENDENCIES = libwd.la

libhisi_zip_la_LIBADD = -ldl

libhisi_zlib_la_LIBADD = -ldl -lz

libwd_crypto_la_LIBADD = $(libwd_la_OBJECTS) -ldl -lnuma
libwd_crypto_la_DEPENDENCIES = libwd.la

Expand Down Expand Up @@ -123,6 +132,9 @@ libhisi_zip_la_LIBADD= -lwd -ldl
libhisi_zip_la_LDFLAGS=$(UADK_VERSION)
libhisi_zip_la_DEPENDENCIES= libwd.la

libhisi_zlib_la_LIBADD= -ldl -lz
libhisi_zlib_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
Expand Down
259 changes: 259 additions & 0 deletions adapter/adapter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
/* SPDX-License-Identifier: Apache-2.0 */
/*
* Copyright 2023-2024 Huawei Technologies Co.,Ltd. All rights reserved.
* Copyright 2023-2024 Linaro ltd.
*/

#include <dlfcn.h>

#include "adapter_private.h"

int uadk_adapter_attach_worker(struct wd_alg_driver *adapter,
struct wd_alg_driver *drv, void *dlhandle)
{
struct uadk_adapter_ctx *ctx = (struct uadk_adapter_ctx *)adapter->priv;
struct uadk_adapter_worker *worker;
int idx = ctx->workers_nb, i;

if (idx >= UADK_MAX_NB_WORKERS) {
fprintf(stderr, "%s too many workers\n", __func__);
return -EINVAL;
}

for (i = 0; i < idx; i++) {
if (strcmp(drv->drv_name, ctx->workers[i].driver->drv_name) == 0) {
fprintf(stderr, "%s attach same driver\n", __func__);
return -EINVAL;
}
}

worker = &ctx->workers[idx];
worker->driver = drv;
worker->dlhandle = dlhandle;
ctx->workers_nb++;

return 0;
}

int uadk_adapter_parse(struct wd_alg_driver *adapter, char *lib_path,
char *drv_name, char *alg_name)
{
struct wd_alg_driver *drv;
void *dlhandle = NULL;
int ret;

if (lib_path) {
dlhandle = dlopen(lib_path, RTLD_NOW);
if (!dlhandle) {
fprintf(stderr, "%s failed to dlopen %s\n", __func__, dlerror());
return -EINVAL;
}
}

drv = wd_find_drv(drv_name, alg_name, 0);
if (!drv) {
fprintf(stderr, "%s failed to find driver\n", __func__);
ret = -EINVAL;
goto fail;
}

ret = uadk_adapter_attach_worker(adapter, drv, dlhandle);
if (ret)
goto fail;

return 0;
fail:
if (dlhandle)
dlclose(dlhandle);
return ret;
}

static int uadk_adapter_load_user_adapter(struct wd_alg_driver *adapter,
struct uadk_user_adapter *user_adapter)
{
struct uadk_adapter_ctx *ctx = (struct uadk_adapter_ctx *)adapter->priv;
int ret;

/* load scheduler instance operations functions */
ctx->ops.init = user_adapter->ops->init;
ctx->ops.exit = user_adapter->ops->exit;
ctx->ops.send = user_adapter->ops->send;
ctx->ops.recv = user_adapter->ops->recv;

if (ctx->priv) {
free(ctx->priv);
ctx->priv = NULL;
}

if (ctx->ops.init) {
ret = ctx->ops.init(adapter);
if (ret)
return ret;
}

ctx->mode = user_adapter->mode;

return 0;
}

int uadk_adapter_set_mode(struct wd_alg_driver *adapter, enum uadk_adapter_mode mode)
{
struct uadk_adapter_ctx *ctx = (struct uadk_adapter_ctx *)adapter->priv;
int ret;

if (mode == ctx->mode)
return 0;

switch (mode) {
case UADK_ADAPT_MODE_ROUNDROBIN:
ret = uadk_adapter_load_user_adapter(adapter, uadk_user_adapter_roundrobin);
if (ret)
return ret;

break;

case UADK_ADAPT_MODE_THRESHOLD:
ret = uadk_adapter_load_user_adapter(adapter, uadk_user_adapter_threshold);
if (ret)
return ret;

break;
default:
fprintf(stderr, "Not support this mode\n");
return -ENOTSUP;
}

return 0;
}

int uadk_adapter_config(struct wd_alg_driver *adapter, void *cfg)
{
struct uadk_adapter_ctx *ctx = (struct uadk_adapter_ctx *)adapter->priv;

if (ctx && ctx->ops.cfg)
return ctx->ops.cfg(adapter, ctx->mode, cfg);

return -EINVAL;
}
static int uadk_adapter_init(struct wd_alg_driver *adapter, void *conf)
{
struct uadk_adapter_ctx *ctx = (struct uadk_adapter_ctx *)adapter->priv;
int ret, i;

for (i = 0; i < ctx->workers_nb; i++) {
struct uadk_adapter_worker *worker = &ctx->workers[i];

if (worker->inited)
continue;

ret = wd_alg_driver_init(worker->driver, conf);
if (ret)
continue;
worker->inited = true;
}

/* can be reenter, considering attach_worker after set_mode */
if (ctx->ops.init)
ctx->ops.init(adapter);

return 0;
}

static void uadk_adapter_exit(struct wd_alg_driver *adapter)
{
struct uadk_adapter_ctx *ctx = (struct uadk_adapter_ctx *)adapter->priv;
int i;

for (i = 0; i < ctx->workers_nb; i++) {
struct uadk_adapter_worker *worker = &ctx->workers[i];

if (!worker->inited)
continue;

wd_alg_driver_exit(worker->driver);
worker->inited = false;

if (worker->dlhandle) {
dlclose(worker->dlhandle);
worker->dlhandle = NULL;
}
}

if (ctx->ops.exit)
ctx->ops.exit(adapter);
}

static int uadk_adapter_send(struct wd_alg_driver *adapter, handle_t handle, void *msg)
{
struct uadk_adapter_ctx *ctx = (struct uadk_adapter_ctx *)adapter->priv;

if (unlikely(ctx->workers_nb == 0)) {
fprintf(stderr, "%s failed since no worker\n", __func__);
return -EINVAL;
}

/* Just forward if only one worker */
if (ctx->workers_nb == 1)
return wd_alg_driver_send(ctx->workers[0].driver, handle, msg);

/* dispatch according to policy */
if (ctx->ops.send)
return ctx->ops.send(adapter, handle, msg);

fprintf(stderr, "Not set mode for multi-workers\n");

return -EINVAL;
}

static int uadk_adapter_recv(struct wd_alg_driver *adapter, handle_t handle, void *msg)
{
struct uadk_adapter_ctx *ctx = (struct uadk_adapter_ctx *)adapter->priv;

if (unlikely(ctx->workers_nb == 0)) {
fprintf(stderr, "%s failed since no worker\n", __func__);
return -EINVAL;
}

/* Just forward if only one worker */
if (ctx->workers_nb == 1)
return wd_alg_driver_recv(ctx->workers[0].driver, handle, msg);

/* dispatch according to policy */
if (ctx->ops.recv)
return ctx->ops.recv(adapter, handle, msg);

fprintf(stderr, "Not set mode for multi-workers\n");

return -EINVAL;
}

struct wd_alg_driver *uadk_adapter_alloc(void)
{
struct wd_alg_driver *adapter;

adapter = calloc(1, sizeof(*adapter));
if (adapter == NULL)
return NULL;

adapter->priv = calloc(1, sizeof(struct uadk_adapter_ctx));
if (adapter->priv == NULL) {
free(adapter);
return NULL;
}

adapter->priority = UADK_ALG_ADAPT;
adapter->init = uadk_adapter_init;
adapter->exit = uadk_adapter_exit;
adapter->send = uadk_adapter_send;
adapter->recv = uadk_adapter_recv;

return adapter;
}

void uadk_adapter_free(struct wd_alg_driver *adapter)
{
if (adapter)
free(adapter->priv);

free(adapter);
}
52 changes: 52 additions & 0 deletions adapter/adapter_private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* SPDX-License-Identifier: Apache-2.0 */
/*
* Copyright 2023-2024 Huawei Technologies Co.,Ltd. All rights reserved.
* Copyright 2023-2024 Linaro ltd.
*/

#include <stdio.h>
#include <stdlib.h>
#include "adapter.h"
#include "wd.h"

/* Maximum number of bonded drv per adapter */
#ifndef UADK_MAX_NB_WORKERS
#define UADK_MAX_NB_WORKERS (8)
#endif

struct uadk_adapter_ops {
int (*init)(struct wd_alg_driver *drv);
void (*exit)(struct wd_alg_driver *drv);
int (*send)(struct wd_alg_driver *drv, handle_t handle, void *msg);
int (*recv)(struct wd_alg_driver *drv, handle_t handle, void *msg);
int (*cfg)(struct wd_alg_driver *adapter, enum uadk_adapter_mode mode, void *cfg);
};

struct uadk_user_adapter {
const char *name; /* adapter name */
const char *description; /* adapter description */
enum uadk_adapter_mode mode; /* adapter mode */
struct uadk_adapter_ops *ops; /* adapter operation */
};

struct uadk_adapter_worker {
struct wd_alg_driver *driver;
/* handle of shared library */
void *dlhandle;
bool inited;
uint32_t inflight_pkts;
};

struct uadk_adapter_ctx {
/* priviate ctx */
void *priv;
/* worker number */
unsigned int workers_nb;
enum uadk_adapter_mode mode;
/* workers attached to the adapter */
struct uadk_adapter_worker workers[UADK_MAX_NB_WORKERS];
struct uadk_adapter_ops ops;
};

extern struct uadk_user_adapter *uadk_user_adapter_roundrobin;
extern struct uadk_user_adapter *uadk_user_adapter_threshold;
Loading