From 79d876716ca88f9bbd78abaa76df5513d9b79b43 Mon Sep 17 00:00:00 2001 From: Seth Howell Date: Thu, 15 Aug 2019 11:32:11 -0700 Subject: [PATCH] nvmf: add spdk_nvmf_get_tgt function This function will allow applications (and RPCs) to obtain an spdk_nvmf_tgt pointer by name. Change-Id: I82792e06a819e06d9fddb5429830008653d92cd1 Signed-off-by: Seth Howell Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/465349 Tested-by: SPDK CI Jenkins Reviewed-by: Changpeng Liu Reviewed-by: Broadcom SPDK FC-NVMe CI Reviewed-by: Jim Harris --- CHANGELOG.md | 5 +++++ include/spdk/nvmf.h | 14 ++++++++++++++ lib/nvmf/nvmf.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 78eeee36d..326d87088 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,11 @@ The `spdk_nvmf_tgt_create` function now accepts an object of type `spdk_nvmf_tar as its only parameter. This new structure contains the max_subsystems parameter previously passed into that function. +A new public API function `spdk_nvmf_get_tgt` has been added which allows users to +retrieve a pointer to an `spdk_nvmf_tgt` object by supplying its name. In the special +case where an RPC or application only creates a single target, this function can accept +a null name parameter and will return the only available target. + ### nvme Added `no_shn_notification` to NVMe controller initialization options, users can enable diff --git a/include/spdk/nvmf.h b/include/spdk/nvmf.h index 588e8e342..17f060212 100644 --- a/include/spdk/nvmf.h +++ b/include/spdk/nvmf.h @@ -136,6 +136,20 @@ void spdk_nvmf_tgt_destroy(struct spdk_nvmf_tgt *tgt, spdk_nvmf_tgt_destroy_done_fn cb_fn, void *cb_arg); +/** + * Get a pointer to an NVMe-oF target. + * + * In order to support some legacy applications and RPC methods that may rely on the + * concept that there is only one target, the name parameter can be passed as NULL. + * If there is only one available target, that target will be returned. + * Otherwise, name is a required parameter. + * + * \param name The name provided when the target was created. + * + * \return The target with the given name, or NULL if no match was found. + */ +struct spdk_nvmf_tgt *spdk_nvmf_get_tgt(const char *name); + /** * Write NVMe-oF target configuration into provided JSON context. * \param w JSON write context diff --git a/lib/nvmf/nvmf.c b/lib/nvmf/nvmf.c index b90e838c8..35c040cc8 100644 --- a/lib/nvmf/nvmf.c +++ b/lib/nvmf/nvmf.c @@ -320,6 +320,34 @@ spdk_nvmf_tgt_destroy(struct spdk_nvmf_tgt *tgt, spdk_io_device_unregister(tgt, spdk_nvmf_tgt_destroy_cb); } +struct spdk_nvmf_tgt * +spdk_nvmf_get_tgt(const char *name) +{ + struct spdk_nvmf_tgt *tgt; + uint32_t num_targets = 0; + + TAILQ_FOREACH(tgt, &g_nvmf_tgts, link) { + if (name) { + if (!strncmp(tgt->name, name, strlen(tgt->name))) { + return tgt; + } + } + num_targets++; + } + + /* + * special case. If there is only one target and + * no name was specified, return the only available + * target. If there is more than one target, name must + * be specified. + */ + if (!name && num_targets == 1) { + return TAILQ_FIRST(&g_nvmf_tgts); + } + + return NULL; +} + static void spdk_nvmf_write_subsystem_config_json(struct spdk_json_write_ctx *w, struct spdk_nvmf_subsystem *subsystem)