net: add asynchronous initialization and finish
Change-Id: Ic67818adf28ffc58a029cb8a551c74e51abde381 Signed-off-by: Tomasz Zawadzki <tomasz.zawadzki@intel.com> Signed-off-by: Tomasz Kulasek <tomaszx.kulasek@intel.com> Reviewed-on: https://review.gerrithub.io/426829 Chandler-Test-Pool: SPDK Automated Test System <sys_sgsw@intel.com> Tested-by: SPDK CI Jenkins <sys_sgci@intel.com> Reviewed-by: Ben Walker <benjamin.walker@intel.com> Reviewed-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
This commit is contained in:
parent
21bd942751
commit
9b91f9c47c
@ -72,6 +72,8 @@ struct hello_context_t {
|
|||||||
struct spdk_poller *poller_in;
|
struct spdk_poller *poller_in;
|
||||||
struct spdk_poller *poller_out;
|
struct spdk_poller *poller_out;
|
||||||
struct spdk_poller *time_out;
|
struct spdk_poller *time_out;
|
||||||
|
|
||||||
|
int rc;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -106,6 +108,13 @@ static void hello_sock_parse_arg(int ch, char *arg)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
hello_sock_net_fini_cb(void *cb_arg)
|
||||||
|
{
|
||||||
|
struct hello_context_t *ctx = cb_arg;
|
||||||
|
spdk_app_stop(ctx->rc);
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
hello_sock_close_timeout_poll(void *arg)
|
hello_sock_close_timeout_poll(void *arg)
|
||||||
{
|
{
|
||||||
@ -115,8 +124,19 @@ hello_sock_close_timeout_poll(void *arg)
|
|||||||
spdk_poller_unregister(&ctx->time_out);
|
spdk_poller_unregister(&ctx->time_out);
|
||||||
spdk_poller_unregister(&ctx->poller_in);
|
spdk_poller_unregister(&ctx->poller_in);
|
||||||
spdk_sock_close(&ctx->sock);
|
spdk_sock_close(&ctx->sock);
|
||||||
|
spdk_sock_group_close(&ctx->group);
|
||||||
|
|
||||||
spdk_app_stop(0);
|
spdk_net_framework_fini(hello_sock_net_fini_cb, arg);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
hello_sock_quit(struct hello_context_t *ctx, int rc)
|
||||||
|
{
|
||||||
|
ctx->rc = rc;
|
||||||
|
spdk_poller_unregister(&ctx->poller_out);
|
||||||
|
ctx->time_out = spdk_poller_register(hello_sock_close_timeout_poll, ctx,
|
||||||
|
CLOSE_TIMEOUT_US);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -164,11 +184,7 @@ hello_sock_writev_poll(void *arg)
|
|||||||
if (n == 0 || !g_is_running) {
|
if (n == 0 || !g_is_running) {
|
||||||
/* EOF */
|
/* EOF */
|
||||||
SPDK_NOTICELOG("Closing connection...\n");
|
SPDK_NOTICELOG("Closing connection...\n");
|
||||||
|
hello_sock_quit(ctx, 0);
|
||||||
ctx->time_out = spdk_poller_register(hello_sock_close_timeout_poll, ctx,
|
|
||||||
CLOSE_TIMEOUT_US);
|
|
||||||
|
|
||||||
spdk_poller_unregister(&ctx->poller_out);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (n > 0) {
|
if (n > 0) {
|
||||||
@ -266,18 +282,19 @@ hello_sock_accept_poll(void *arg)
|
|||||||
uint16_t cport, sport;
|
uint16_t cport, sport;
|
||||||
|
|
||||||
if (!g_is_running) {
|
if (!g_is_running) {
|
||||||
spdk_poller_unregister(&ctx->poller_in);
|
hello_sock_quit(ctx, 0);
|
||||||
spdk_poller_unregister(&ctx->poller_out);
|
|
||||||
spdk_sock_close(&ctx->sock);
|
|
||||||
spdk_sock_group_close(&ctx->group);
|
|
||||||
spdk_app_stop(0);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
sock = spdk_sock_accept(ctx->sock);
|
sock = spdk_sock_accept(ctx->sock);
|
||||||
if (sock != NULL) {
|
if (sock != NULL) {
|
||||||
spdk_sock_getaddr(sock, saddr, sizeof(saddr), &sport, caddr, sizeof(caddr), &cport);
|
rc = spdk_sock_getaddr(sock, saddr, sizeof(saddr), &sport, caddr, sizeof(caddr), &cport);
|
||||||
|
if (rc < 0) {
|
||||||
|
SPDK_ERRLOG("Cannot get connection addresses\n");
|
||||||
|
spdk_sock_close(&ctx->sock);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
SPDK_NOTICELOG("Accepting a new connection from (%s, %hu) to (%s, %hu)\n",
|
SPDK_NOTICELOG("Accepting a new connection from (%s, %hu) to (%s, %hu)\n",
|
||||||
caddr, cport, saddr, sport);
|
caddr, cport, saddr, sport);
|
||||||
@ -350,14 +367,20 @@ hello_sock_shutdown_cb(void)
|
|||||||
{
|
{
|
||||||
g_is_running = false;
|
g_is_running = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Our initial event that kicks off everything from main().
|
* Our initial event that kicks off everything from main().
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
hello_start(void *arg1, void *arg2)
|
hello_start(void *arg1, int rc)
|
||||||
{
|
{
|
||||||
struct hello_context_t *ctx = arg1;
|
struct hello_context_t *ctx = arg1;
|
||||||
int rc = 0;
|
|
||||||
|
if (rc) {
|
||||||
|
SPDK_ERRLOG("ERROR starting application\n");
|
||||||
|
spdk_app_stop(-1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
SPDK_NOTICELOG("Successfully started the application\n");
|
SPDK_NOTICELOG("Successfully started the application\n");
|
||||||
|
|
||||||
@ -373,6 +396,12 @@ hello_start(void *arg1, void *arg2)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
start_net_framework(void *arg1, void *arg2)
|
||||||
|
{
|
||||||
|
spdk_net_framework_start(hello_start, arg1);
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char **argv)
|
main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
@ -395,18 +424,11 @@ main(int argc, char **argv)
|
|||||||
hello_context.port = g_port;
|
hello_context.port = g_port;
|
||||||
hello_context.verbose = g_verbose;
|
hello_context.verbose = g_verbose;
|
||||||
|
|
||||||
rc = spdk_net_framework_start();
|
rc = spdk_app_start(&opts, start_net_framework, &hello_context, NULL);
|
||||||
if (rc) {
|
|
||||||
SPDK_ERRLOG("ERROR starting application\n");
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = spdk_app_start(&opts, hello_start, &hello_context, NULL);
|
|
||||||
if (rc) {
|
if (rc) {
|
||||||
SPDK_ERRLOG("ERROR starting application\n");
|
SPDK_ERRLOG("ERROR starting application\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
end:
|
|
||||||
SPDK_NOTICELOG("Exiting from application\n");
|
SPDK_NOTICELOG("Exiting from application\n");
|
||||||
|
|
||||||
if (hello_context.verbose) {
|
if (hello_context.verbose) {
|
||||||
@ -414,8 +436,6 @@ end:
|
|||||||
hello_context.bytes_in, hello_context.bytes_out);
|
hello_context.bytes_in, hello_context.bytes_out);
|
||||||
}
|
}
|
||||||
|
|
||||||
spdk_net_framework_fini();
|
|
||||||
|
|
||||||
/* Gracefully close out all of the SPDK subsystems. */
|
/* Gracefully close out all of the SPDK subsystems. */
|
||||||
spdk_app_fini();
|
spdk_app_fini();
|
||||||
return rc;
|
return rc;
|
||||||
|
@ -82,17 +82,36 @@ int spdk_interface_init(void);
|
|||||||
*/
|
*/
|
||||||
void spdk_interface_destroy(void);
|
void spdk_interface_destroy(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Net framework initialization callback.
|
||||||
|
*
|
||||||
|
* \param cb_arg Callback argument.
|
||||||
|
* \param rc 0 if net framework initialized successfully or negative errno if it failed.
|
||||||
|
*/
|
||||||
|
typedef void (*spdk_net_init_cb)(void *cb_arg, int rc);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Net framework finish callback.
|
||||||
|
*
|
||||||
|
* \param cb_arg Callback argument.
|
||||||
|
*/
|
||||||
|
typedef void (*spdk_net_fini_cb)(void *cb_arg);
|
||||||
|
|
||||||
|
void spdk_net_framework_init_next(int rc);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start all registered frameworks.
|
* Start all registered frameworks.
|
||||||
*
|
*
|
||||||
* \return 0 on success.
|
* \return 0 on success.
|
||||||
*/
|
*/
|
||||||
int spdk_net_framework_start(void);
|
void spdk_net_framework_start(spdk_net_init_cb cb_fn, void *cb_arg);
|
||||||
|
|
||||||
|
void spdk_net_framework_fini_next(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stop all registered frameworks.
|
* Stop all registered frameworks.
|
||||||
*/
|
*/
|
||||||
void spdk_net_framework_fini(void);
|
void spdk_net_framework_fini(spdk_net_fini_cb cb_fn, void *cb_arg);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
@ -63,21 +63,28 @@ static struct spdk_subsystem g_spdk_subsystem_interface = {
|
|||||||
|
|
||||||
SPDK_SUBSYSTEM_REGISTER(g_spdk_subsystem_interface);
|
SPDK_SUBSYSTEM_REGISTER(g_spdk_subsystem_interface);
|
||||||
|
|
||||||
|
static void
|
||||||
|
spdk_net_start_complete(void *cb_arg, int rc)
|
||||||
|
{
|
||||||
|
spdk_subsystem_init_next(rc);
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
spdk_net_subsystem_start(void)
|
spdk_net_subsystem_start(void)
|
||||||
{
|
{
|
||||||
int rc;
|
spdk_net_framework_start(spdk_net_start_complete, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
rc = spdk_net_framework_start();
|
static void
|
||||||
|
spdk_net_fini_done(void *cb_arg)
|
||||||
spdk_subsystem_init_next(rc);
|
{
|
||||||
|
spdk_subsystem_fini_next();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
spdk_net_subsystem_fini(void)
|
spdk_net_subsystem_fini(void)
|
||||||
{
|
{
|
||||||
spdk_net_framework_fini();
|
spdk_net_framework_fini(spdk_net_fini_done, NULL);
|
||||||
spdk_subsystem_fini_next();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct spdk_subsystem g_spdk_subsystem_net_framework = {
|
static struct spdk_subsystem g_spdk_subsystem_net_framework = {
|
||||||
|
@ -38,29 +38,70 @@
|
|||||||
static STAILQ_HEAD(, spdk_net_framework) g_net_frameworks =
|
static STAILQ_HEAD(, spdk_net_framework) g_net_frameworks =
|
||||||
STAILQ_HEAD_INITIALIZER(g_net_frameworks);
|
STAILQ_HEAD_INITIALIZER(g_net_frameworks);
|
||||||
|
|
||||||
int spdk_net_framework_start(void)
|
static spdk_net_init_cb g_init_cb_fn = NULL;
|
||||||
{
|
static void *g_init_cb_arg = NULL;
|
||||||
struct spdk_net_framework *net_framework = NULL;
|
|
||||||
int rc;
|
|
||||||
|
|
||||||
STAILQ_FOREACH_FROM(net_framework, &g_net_frameworks, link) {
|
static spdk_net_fini_cb g_fini_cb_fn = NULL;
|
||||||
rc = net_framework->init();
|
static void *g_fini_cb_arg = NULL;
|
||||||
if (rc != 0) {
|
|
||||||
SPDK_ERRLOG("Net framework %s failed to initalize\n", net_framework->name);
|
struct spdk_net_framework *g_next_net_framework = NULL;
|
||||||
return rc;
|
|
||||||
}
|
void
|
||||||
|
spdk_net_framework_init_next(int rc)
|
||||||
|
{
|
||||||
|
if (rc) {
|
||||||
|
SPDK_ERRLOG("Net framework %s failed to initalize with error %d\n", g_next_net_framework->name, rc);
|
||||||
|
g_init_cb_fn(g_init_cb_arg, rc);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
if (g_next_net_framework == NULL) {
|
||||||
|
g_next_net_framework = STAILQ_FIRST(&g_net_frameworks);
|
||||||
|
} else {
|
||||||
|
g_next_net_framework = STAILQ_NEXT(g_next_net_framework, link);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (g_next_net_framework == NULL) {
|
||||||
|
g_init_cb_fn(g_init_cb_arg, 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
g_next_net_framework->init();
|
||||||
}
|
}
|
||||||
|
|
||||||
void spdk_net_framework_fini(void)
|
void
|
||||||
|
spdk_net_framework_start(spdk_net_init_cb cb_fn, void *cb_arg)
|
||||||
{
|
{
|
||||||
struct spdk_net_framework *net_framework = NULL;
|
g_init_cb_fn = cb_fn;
|
||||||
|
g_init_cb_arg = cb_arg;
|
||||||
|
|
||||||
STAILQ_FOREACH_FROM(net_framework, &g_net_frameworks, link) {
|
spdk_net_framework_init_next(0);
|
||||||
net_framework->fini();
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
spdk_net_framework_fini_next(void)
|
||||||
|
{
|
||||||
|
if (g_next_net_framework == NULL) {
|
||||||
|
g_next_net_framework = STAILQ_FIRST(&g_net_frameworks);
|
||||||
|
} else {
|
||||||
|
g_next_net_framework = STAILQ_NEXT(g_next_net_framework, link);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (g_next_net_framework == NULL) {
|
||||||
|
g_fini_cb_fn(g_fini_cb_arg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
g_next_net_framework->fini();
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
spdk_net_framework_fini(spdk_net_fini_cb cb_fn, void *cb_arg)
|
||||||
|
{
|
||||||
|
g_fini_cb_fn = cb_fn;
|
||||||
|
g_fini_cb_arg = cb_arg;
|
||||||
|
|
||||||
|
spdk_net_framework_fini_next();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
Loading…
Reference in New Issue
Block a user