From bd4ecea5055e49768d81e44d7045b37b0ad02f5c Mon Sep 17 00:00:00 2001 From: Pawel Wodkowski Date: Mon, 26 Feb 2018 20:16:59 +0100 Subject: [PATCH] subsystem: add per module configuration dump Change-Id: I5c4e51cd9cd97b05ab9a95cbe084ff2741f6ef58 Signed-off-by: Pawel Wodkowski Reviewed-on: https://review.gerrithub.io/402323 Reviewed-by: Shuhei Matsumoto Tested-by: SPDK Automated Test System Reviewed-by: Jim Harris --- include/spdk_internal/event.h | 13 +++ lib/event/rpc/subsystem_rpc.c | 43 ++++++++++ lib/event/subsystem.c | 12 ++- scripts/rpc.py | 4 + scripts/rpc/subsystem.py | 5 ++ .../unit/lib/event/subsystem.c/subsystem_ut.c | 1 + test/unit/lib/json_mock.c | 81 +++++++++++++++++++ 7 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 test/unit/lib/json_mock.c diff --git a/include/spdk_internal/event.h b/include/spdk_internal/event.h index 643a44b9f..6d72ea3e7 100644 --- a/include/spdk_internal/event.h +++ b/include/spdk_internal/event.h @@ -37,6 +37,7 @@ #include "spdk/stdinc.h" #include "spdk/event.h" +#include "spdk/json.h" struct spdk_event { uint32_t lcore; @@ -57,12 +58,15 @@ struct spdk_subsystem { void (*init)(void); void (*fini)(void); void (*config)(FILE *fp); + int (*write_config_json)(struct spdk_json_write_ctx *w); TAILQ_ENTRY(spdk_subsystem) tailq; }; TAILQ_HEAD(spdk_subsystem_list, spdk_subsystem); extern struct spdk_subsystem_list g_subsystems; +struct spdk_subsystem *spdk_subsystem_find(struct spdk_subsystem_list *list, const char *name); + struct spdk_subsystem_depend { const char *name; const char *depends_on; @@ -81,6 +85,15 @@ void spdk_subsystem_init_next(int rc); void spdk_subsystem_fini_next(void); void spdk_subsystem_config(FILE *fp); +/** + * Save pointed subsystem configuration to the JSON write context. In case of + * error \c null is written to the JSON context. + * + * \param w JSON write context + * \param subsystem the subsystem to query + */ +void spdk_subsystem_config_json(struct spdk_json_write_ctx *w, struct spdk_subsystem *subsystem); + void spdk_rpc_initialize(const char *listen_addr); void spdk_rpc_finish(void); diff --git a/lib/event/rpc/subsystem_rpc.c b/lib/event/rpc/subsystem_rpc.c index 73498bc8c..36635e2bb 100644 --- a/lib/event/rpc/subsystem_rpc.c +++ b/lib/event/rpc/subsystem_rpc.c @@ -33,6 +33,8 @@ #include "spdk_internal/event.h" #include "spdk/rpc.h" +#include "spdk/string.h" +#include "spdk/util.h" static void spdk_rpc_get_subsystems(struct spdk_jsonrpc_request *request, @@ -73,3 +75,44 @@ spdk_rpc_get_subsystems(struct spdk_jsonrpc_request *request, } SPDK_RPC_REGISTER("get_subsystems", spdk_rpc_get_subsystems) + +struct rpc_get_subsystem_config { + char *name; +}; + +static const struct spdk_json_object_decoder rpc_get_subsystem_config[] = { + {"name", offsetof(struct rpc_get_subsystem_config, name), spdk_json_decode_string}, +}; + +static void +spdk_rpc_get_subsystem_config(struct spdk_jsonrpc_request *request, + const struct spdk_json_val *params) +{ + struct rpc_get_subsystem_config req = {}; + struct spdk_json_write_ctx *w; + struct spdk_subsystem *subsystem; + + if (spdk_json_decode_object(params, rpc_get_subsystem_config, + SPDK_COUNTOF(rpc_get_subsystem_config), &req)) { + spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid arguments"); + return; + } + + subsystem = spdk_subsystem_find(&g_subsystems, req.name); + if (!subsystem) { + spdk_jsonrpc_send_error_response_fmt(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, + "Subsystem '%s' not found", req.name); + goto out; + } + + w = spdk_jsonrpc_begin_result(request); + if (w) { + spdk_subsystem_config_json(w, subsystem); + spdk_jsonrpc_end_result(request, w); + } + +out: + free(req.name); +} + +SPDK_RPC_REGISTER("get_subsystem_config", spdk_rpc_get_subsystem_config) diff --git a/lib/event/subsystem.c b/lib/event/subsystem.c index a318882ae..4c34544da 100644 --- a/lib/event/subsystem.c +++ b/lib/event/subsystem.c @@ -58,7 +58,7 @@ spdk_add_subsystem_depend(struct spdk_subsystem_depend *depend) TAILQ_INSERT_TAIL(&g_subsystems_deps, depend, tailq); } -static struct spdk_subsystem * +struct spdk_subsystem * spdk_subsystem_find(struct spdk_subsystem_list *list, const char *name) { struct spdk_subsystem *iter; @@ -242,3 +242,13 @@ spdk_subsystem_config(FILE *fp) } } } + +void +spdk_subsystem_config_json(struct spdk_json_write_ctx *w, struct spdk_subsystem *subsystem) +{ + if (subsystem && subsystem->write_config_json) { + subsystem->write_config_json(w); + } else { + spdk_json_write_null(w); + } +} diff --git a/scripts/rpc.py b/scripts/rpc.py index 949de97ab..d75bebafe 100755 --- a/scripts/rpc.py +++ b/scripts/rpc.py @@ -437,6 +437,10 @@ if __name__ == "__main__": entry contain (unsorted) array of subsystems it depends on.""") p.set_defaults(func=rpc.subsystem.get_subsystems) + p = subparsers.add_parser('get_subsystem_config', help=""""Print subsystem configuration""") + p.add_argument('name', help='Name of subsystem to query') + p.set_defaults(func=rpc.subsystem.get_subsystem_config) + # vhost p = subparsers.add_parser('set_vhost_controller_coalescing', help='Set vhost controller coalescing') p.add_argument('ctrlr', help='controller name') diff --git a/scripts/rpc/subsystem.py b/scripts/rpc/subsystem.py index 416546dde..7b01a0d1b 100755 --- a/scripts/rpc/subsystem.py +++ b/scripts/rpc/subsystem.py @@ -3,3 +3,8 @@ from client import print_dict, print_array, int_arg def get_subsystems(args): print_dict(args.client.call('get_subsystems')) + + +def get_subsystem_config(args): + params = {'name': args.name} + print_dict(args.client.call('get_subsystem_config', params)) diff --git a/test/unit/lib/event/subsystem.c/subsystem_ut.c b/test/unit/lib/event/subsystem.c/subsystem_ut.c index 44bceee63..75ae1f1e7 100644 --- a/test/unit/lib/event/subsystem.c/subsystem_ut.c +++ b/test/unit/lib/event/subsystem.c/subsystem_ut.c @@ -35,6 +35,7 @@ #include "spdk_cunit.h" +#include "unit/lib/json_mock.c" #include "event/subsystem.c" static struct spdk_subsystem g_ut_subsystems[8]; diff --git a/test/unit/lib/json_mock.c b/test/unit/lib/json_mock.c new file mode 100644 index 000000000..b9cee171e --- /dev/null +++ b/test/unit/lib/json_mock.c @@ -0,0 +1,81 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "spdk/json.h" +#include "spdk_internal/mock.h" + +DEFINE_STUB(spdk_json_write_begin, struct spdk_json_write_ctx *, (spdk_json_write_cb write_cb, + void *cb_ctx, uint32_t flags), NULL); + +DEFINE_STUB(spdk_json_write_end, int, (struct spdk_json_write_ctx *w), 0); +DEFINE_STUB(spdk_json_write_null, int, (struct spdk_json_write_ctx *w), 0); +DEFINE_STUB(spdk_json_write_bool, int, (struct spdk_json_write_ctx *w, bool val), 0); +DEFINE_STUB(spdk_json_write_int32, int, (struct spdk_json_write_ctx *w, int32_t val), 0); +DEFINE_STUB(spdk_json_write_uint32, int, (struct spdk_json_write_ctx *w, uint32_t val), 0); +DEFINE_STUB(spdk_json_write_int64, int, (struct spdk_json_write_ctx *w, int64_t val), 0); +DEFINE_STUB(spdk_json_write_uint64, int, (struct spdk_json_write_ctx *w, uint64_t val), 0); +DEFINE_STUB(spdk_json_write_string, int, (struct spdk_json_write_ctx *w, const char *val), 0); +DEFINE_STUB(spdk_json_write_string_raw, int, (struct spdk_json_write_ctx *w, const char *val, + size_t len), 0); + +DEFINE_STUB(spdk_json_write_array_begin, int, (struct spdk_json_write_ctx *w), 0); +DEFINE_STUB(spdk_json_write_array_end, int, (struct spdk_json_write_ctx *w), 0); +DEFINE_STUB(spdk_json_write_object_begin, int, (struct spdk_json_write_ctx *w), 0); +DEFINE_STUB(spdk_json_write_object_end, int, (struct spdk_json_write_ctx *w), 0); +DEFINE_STUB(spdk_json_write_name, int, (struct spdk_json_write_ctx *w, const char *name), 0); +DEFINE_STUB(spdk_json_write_name_raw, int, (struct spdk_json_write_ctx *w, const char *name, + size_t len), 0); + +/* Utility functions */ +DEFINE_STUB(spdk_json_write_named_null, int, (struct spdk_json_write_ctx *w, const char *name), 0); +DEFINE_STUB(spdk_json_write_named_bool, int, (struct spdk_json_write_ctx *w, const char *name, + bool val), 0); +DEFINE_STUB(spdk_json_write_named_int32, int, (struct spdk_json_write_ctx *w, const char *name, + int32_t val), 0); +DEFINE_STUB(spdk_json_write_named_uint32, int, (struct spdk_json_write_ctx *w, const char *name, + uint32_t val), 0); +DEFINE_STUB(spdk_json_write_named_uint64, int, (struct spdk_json_write_ctx *w, const char *name, + uint64_t val), 0); +DEFINE_STUB(spdk_json_write_named_int64, int, (struct spdk_json_write_ctx *w, const char *name, + int64_t val), 0); +DEFINE_STUB(spdk_json_write_named_string, int, (struct spdk_json_write_ctx *w, const char *name, + const char *val), 0); +DEFINE_STUB(spdk_json_write_named_string_fmt, int, (struct spdk_json_write_ctx *w, const char *name, + const char *fmt, ...), 0); +DEFINE_STUB(spdk_json_write_named_string_fmt_v, int, (struct spdk_json_write_ctx *w, + const char *name, const char *fmt, va_list args), 0); + +DEFINE_STUB(spdk_json_write_named_array_begin, int, (struct spdk_json_write_ctx *w, + const char *name), 0); +DEFINE_STUB(spdk_json_write_named_object_begin, int, (struct spdk_json_write_ctx *w, + const char *name), 0);