From 1b93872dbbd2bef1f09686a4d1ac8c9fb2bf7aaf Mon Sep 17 00:00:00 2001 From: Pawel Wodkowski Date: Fri, 2 Jun 2017 18:00:22 +0200 Subject: [PATCH] vhost: don't expose backend structures outside backend implementation Theres no need to have vhost SCSI (and incoming vhost block) structures available in public API. Generic spdk_vhost_dev might be used to communicate with backedns. Backends are responsible to check if type of passed vdev valid. Change-Id: I850a1cc661166ee146d513b0069bb40edc9a761a Signed-off-by: Pawel Wodkowski Reviewed-on: https://review.gerrithub.io/363581 Tested-by: SPDK Automated Test System Reviewed-by: Daniel Verkamp Reviewed-by: Ben Walker --- include/spdk/vhost.h | 10 ++++++- lib/vhost/vhost.c | 1 - lib/vhost/vhost_internal.h | 2 ++ lib/vhost/vhost_rpc.c | 9 +++--- lib/vhost/vhost_scsi.c | 60 +++++++++++++++++++++++++++++--------- lib/vhost/vhost_scsi.h | 51 -------------------------------- 6 files changed, 61 insertions(+), 72 deletions(-) delete mode 100644 lib/vhost/vhost_scsi.h diff --git a/include/spdk/vhost.h b/include/spdk/vhost.h index d0e668352..426763e3b 100644 --- a/include/spdk/vhost.h +++ b/include/spdk/vhost.h @@ -51,7 +51,6 @@ void spdk_vhost_shutdown_cb(void); /* Forward declaration */ struct spdk_vhost_dev; -struct spdk_vhost_scsi_dev; /** * Get handle to next controller. @@ -64,4 +63,13 @@ const char *spdk_vhost_dev_get_name(struct spdk_vhost_dev *ctrl); uint64_t spdk_vhost_dev_get_cpumask(struct spdk_vhost_dev *ctrl); int spdk_vhost_parse_core_mask(const char *mask, uint64_t *cpumask); +int spdk_vhost_scsi_controller_construct(void); +int spdk_vhost_scsi_dev_construct(const char *name, uint64_t cpumask); +int spdk_vhost_scsi_dev_remove(struct spdk_vhost_dev *vdev); +struct spdk_scsi_dev *spdk_vhost_scsi_dev_get_dev(struct spdk_vhost_dev *ctrl, + uint8_t num); +int spdk_vhost_scsi_dev_add_dev(const char *name, unsigned scsi_dev_num, const char *lun_name); +int spdk_vhost_scsi_dev_remove_dev(struct spdk_vhost_dev *vdev, unsigned scsi_dev_num); + + #endif /* SPDK_VHOST_H */ diff --git a/lib/vhost/vhost.c b/lib/vhost/vhost.c index 8900ce3a2..8cd381e94 100644 --- a/lib/vhost/vhost.c +++ b/lib/vhost/vhost.c @@ -39,7 +39,6 @@ #include "spdk/vhost.h" #include "vhost_internal.h" -#include "vhost_scsi.h" #include "task.h" #include "vhost_iommu.h" diff --git a/lib/vhost/vhost_internal.h b/lib/vhost/vhost_internal.h index fbb6416b4..58c6d8aa9 100644 --- a/lib/vhost/vhost_internal.h +++ b/lib/vhost/vhost_internal.h @@ -53,6 +53,8 @@ #define MAX_VHOST_VRINGS 256 +#define SPDK_VHOST_SCSI_CTRLR_MAX_DEVS 8 + enum spdk_vhost_dev_type { SPDK_VHOST_DEV_T_SCSI, }; diff --git a/lib/vhost/vhost_rpc.c b/lib/vhost/vhost_rpc.c index 73e6ab107..9e0bd9a81 100644 --- a/lib/vhost/vhost_rpc.c +++ b/lib/vhost/vhost_rpc.c @@ -32,14 +32,13 @@ */ #include "spdk/stdinc.h" - #include "spdk_internal/log.h" #include "spdk/rpc.h" #include "spdk/util.h" -#include "vhost_scsi.h" #include "spdk/vhost.h" #include "task.h" +#include "vhost_internal.h" static void json_scsi_dev_write(struct spdk_json_write_ctx *ctx, struct spdk_scsi_dev *dev) @@ -108,7 +107,7 @@ spdk_rpc_get_vhost_scsi_controllers(struct spdk_jsonrpc_server_conn *conn, spdk_json_write_array_begin(w); for (i = 0; i < SPDK_VHOST_SCSI_CTRLR_MAX_DEVS; i++) { - dev = spdk_vhost_scsi_dev_get_dev((struct spdk_vhost_scsi_dev *) vdev, i); + dev = spdk_vhost_scsi_dev_get_dev(vdev, i); if (!dev) continue; @@ -224,7 +223,7 @@ spdk_rpc_remove_vhost_scsi_controller(struct spdk_jsonrpc_server_conn *conn, goto invalid; } - rc = spdk_vhost_scsi_dev_remove((struct spdk_vhost_scsi_dev *) vdev); + rc = spdk_vhost_scsi_dev_remove(vdev); if (rc < 0) { goto invalid; } @@ -340,7 +339,7 @@ spdk_rpc_remove_vhost_scsi_dev(struct spdk_jsonrpc_server_conn *conn, goto invalid; } - rc = spdk_vhost_scsi_dev_remove_dev((struct spdk_vhost_scsi_dev *) vdev, req.scsi_dev_num); + rc = spdk_vhost_scsi_dev_remove_dev(vdev, req.scsi_dev_num); if (rc < 0) { goto invalid; } diff --git a/lib/vhost/vhost_scsi.c b/lib/vhost/vhost_scsi.c index 6021326af..9d3680174 100644 --- a/lib/vhost/vhost_scsi.c +++ b/lib/vhost/vhost_scsi.c @@ -37,12 +37,12 @@ #include "spdk/env.h" #include "spdk/scsi.h" +#include "spdk/scsi_spec.h" #include "spdk/conf.h" #include "spdk/event.h" #include "spdk/vhost.h" #include "vhost_internal.h" -#include "vhost_scsi.h" #include "task.h" /* Features supported by SPDK VHOST lib. */ @@ -566,6 +566,22 @@ remove_vdev_cb(void *arg) spdk_vhost_dev_mem_unregister(&svdev->vdev); } +static struct spdk_vhost_scsi_dev * +to_scsi_dev(struct spdk_vhost_dev *ctrlr) +{ + if (ctrlr == NULL) { + return NULL; + } + + if (ctrlr->type != SPDK_VHOST_DEV_T_SCSI) { + SPDK_ERRLOG("Controller %s: expected SCSI controller (%d) but got %d\n", + ctrlr->name, SPDK_VHOST_DEV_T_SCSI, ctrlr->type); + return NULL; + } + + return (struct spdk_vhost_scsi_dev *)ctrlr; +} + int spdk_vhost_scsi_dev_construct(const char *name, uint64_t cpumask) { @@ -588,12 +604,15 @@ spdk_vhost_scsi_dev_construct(const char *name, uint64_t cpumask) } int -spdk_vhost_scsi_dev_remove(struct spdk_vhost_scsi_dev *svdev) +spdk_vhost_scsi_dev_remove(struct spdk_vhost_dev *vdev) { - struct spdk_vhost_dev *vdev; + struct spdk_vhost_scsi_dev *svdev = to_scsi_dev(vdev); int i; - vdev = &svdev->vdev; + if (svdev == NULL) { + return -EINVAL; + } + for (i = 0; i < SPDK_VHOST_SCSI_CTRLR_MAX_DEVS; ++i) { if (svdev->scsi_dev[i]) { SPDK_ERRLOG("Trying to remove non-empty controller: %s.\n", vdev->name); @@ -610,11 +629,14 @@ spdk_vhost_scsi_dev_remove(struct spdk_vhost_scsi_dev *svdev) } struct spdk_scsi_dev * -spdk_vhost_scsi_dev_get_dev(struct spdk_vhost_scsi_dev *svdev, uint8_t num) +spdk_vhost_scsi_dev_get_dev(struct spdk_vhost_dev *vdev, uint8_t num) { - assert(svdev != NULL); + struct spdk_vhost_scsi_dev *svdev; + assert(num < SPDK_VHOST_SCSI_CTRLR_MAX_DEVS); - return svdev->scsi_dev[num]; + svdev = to_scsi_dev(vdev); + + return svdev ? svdev->scsi_dev[num] : NULL; } int @@ -645,13 +667,16 @@ spdk_vhost_scsi_dev_add_dev(const char *ctrlr_name, unsigned scsi_dev_num, const return -1; } - svdev = (struct spdk_vhost_scsi_dev *) spdk_vhost_dev_find(ctrlr_name); - if (svdev == NULL) { - SPDK_ERRLOG("Controller %s is not defined\n", ctrlr_name); + vdev = spdk_vhost_dev_find(ctrlr_name); + if (vdev == NULL) { + SPDK_ERRLOG("Controller %s is not defined.\n", ctrlr_name); return -ENODEV; } - vdev = &svdev->vdev; + svdev = to_scsi_dev(vdev); + if (svdev == NULL) { + return -EINVAL; + } if (vdev->lcore != -1) { SPDK_ERRLOG("Controller %s is in use and hotplug is not supported\n", ctrlr_name); @@ -686,15 +711,21 @@ spdk_vhost_scsi_dev_add_dev(const char *ctrlr_name, unsigned scsi_dev_num, const } int -spdk_vhost_scsi_dev_remove_dev(struct spdk_vhost_scsi_dev *svdev, unsigned scsi_dev_num) +spdk_vhost_scsi_dev_remove_dev(struct spdk_vhost_dev *vdev, unsigned scsi_dev_num) { - struct spdk_vhost_dev *vdev = &svdev->vdev; + struct spdk_vhost_scsi_dev *svdev; + assert(vdev != NULL); if (vdev->lcore != -1) { SPDK_ERRLOG("Controller %s is in use and hotremove is not supported\n", vdev->name); return -EBUSY; } + svdev = to_scsi_dev(vdev); + if (svdev == NULL) { + return -ENODEV; + } + if (svdev->scsi_dev[scsi_dev_num] == NULL) { SPDK_ERRLOG("Controller %s dev %u is not occupied\n", vdev->name, scsi_dev_num); return -ENODEV; @@ -803,7 +834,8 @@ destroy_device(int vid) if (vdev == NULL) { rte_panic("Couldn't find device with vid %d to stop.\n", vid); } - svdev = (struct spdk_vhost_scsi_dev *) vdev; + svdev = to_scsi_dev(vdev); + assert(svdev); spdk_vhost_timed_event_init(&event, vdev->lcore, NULL, NULL, 1); spdk_poller_unregister(&svdev->requestq_poller, event.spdk_event); diff --git a/lib/vhost/vhost_scsi.h b/lib/vhost/vhost_scsi.h deleted file mode 100644 index 91aef5b1e..000000000 --- a/lib/vhost/vhost_scsi.h +++ /dev/null @@ -1,51 +0,0 @@ -/*- - * 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. - */ - -#ifndef SPDK_VHOST_SCSI_H -#define SPDK_VHOST_SCSI_H - -#include "spdk/scsi_spec.h" - -#define SPDK_VHOST_SCSI_CTRLR_MAX_DEVS 8 - -struct spdk_vhost_scsi_dev; - -int spdk_vhost_scsi_controller_construct(void); -int spdk_vhost_scsi_dev_construct(const char *name, uint64_t cpumask); -int spdk_vhost_scsi_dev_remove(struct spdk_vhost_scsi_dev *vdev); -struct spdk_scsi_dev *spdk_vhost_scsi_dev_get_dev(struct spdk_vhost_scsi_dev *ctrl, - uint8_t num); -int spdk_vhost_scsi_dev_add_dev(const char *name, unsigned scsi_dev_num, const char *lun_name); -int spdk_vhost_scsi_dev_remove_dev(struct spdk_vhost_scsi_dev *vdev, unsigned scsi_dev_num); - -#endif /* SPDK_VHOST_SCSI_H */