From fa82f460d194605e9ad149ba36dcb1e557102197 Mon Sep 17 00:00:00 2001 From: Dariusz Stojaczyk Date: Fri, 26 Jan 2018 15:29:20 +0100 Subject: [PATCH] vhost: remove vdev->type field This field was only required to check if we can safely upcast vdev object. We can just as well check vdev->backend instead. The vdev->type is not needed here. Change-Id: I525350957406d4299151e0557b9025ca7bea5371 Signed-off-by: Dariusz Stojaczyk Reviewed-on: https://review.gerrithub.io/396584 Tested-by: SPDK Automated Test System Reviewed-by: Pawel Wodkowski Reviewed-by: Reviewed-by: Jim Harris --- lib/vhost/vhost.c | 3 +-- lib/vhost/vhost_blk.c | 11 ++++++----- lib/vhost/vhost_internal.h | 8 +------- lib/vhost/vhost_scsi.c | 9 ++++----- test/unit/lib/vhost/test_vhost.c | 3 ++- test/unit/lib/vhost/vhost.c/vhost_ut.c | 10 +++++----- test/unit/lib/vhost/vhost_blk.c/vhost_blk_ut.c | 6 +++--- test/unit/lib/vhost/vhost_scsi.c/vhost_scsi_ut.c | 1 + 8 files changed, 23 insertions(+), 28 deletions(-) diff --git a/lib/vhost/vhost.c b/lib/vhost/vhost.c index cbe2efa057..5be2225510 100644 --- a/lib/vhost/vhost.c +++ b/lib/vhost/vhost.c @@ -610,7 +610,7 @@ spdk_vhost_parse_core_mask(const char *mask, struct spdk_cpuset *cpumask) int spdk_vhost_dev_register(struct spdk_vhost_dev *vdev, const char *name, const char *mask_str, - enum spdk_vhost_dev_type type, const struct spdk_vhost_dev_backend *backend) + const struct spdk_vhost_dev_backend *backend) { unsigned ctrlr_num; char path[PATH_MAX]; @@ -708,7 +708,6 @@ spdk_vhost_dev_register(struct spdk_vhost_dev *vdev, const char *name, const cha vdev->lcore = -1; vdev->cpumask = cpumask; vdev->registered = true; - vdev->type = type; vdev->backend = backend; spdk_vhost_set_coalescing(vdev, SPDK_VHOST_COALESCING_DELAY_BASE_US, diff --git a/lib/vhost/vhost_blk.c b/lib/vhost/vhost_blk.c index 97cbf3d8ac..1cd8871091 100644 --- a/lib/vhost/vhost_blk.c +++ b/lib/vhost/vhost_blk.c @@ -71,6 +71,9 @@ struct spdk_vhost_blk_dev { bool readonly; }; +/* forward declaration */ +static const struct spdk_vhost_dev_backend vhost_blk_device_backend; + static void blk_task_finish(struct spdk_vhost_blk_task *task) { @@ -377,9 +380,8 @@ to_blk_dev(struct spdk_vhost_dev *vdev) return NULL; } - if (vdev->type != SPDK_VHOST_DEV_T_BLK) { - SPDK_ERRLOG("Controller %s: expected block controller (%d) but got %d\n", - vdev->name, SPDK_VHOST_DEV_T_BLK, vdev->type); + if (vdev->backend != &vhost_blk_device_backend) { + SPDK_ERRLOG("%s: not a vhost-blk device\n", vdev->name); return NULL; } @@ -705,8 +707,7 @@ spdk_vhost_blk_construct(const char *name, const char *cpumask, const char *dev_ bvdev->bdev = bdev; bvdev->readonly = readonly; - ret = spdk_vhost_dev_register(&bvdev->vdev, name, cpumask, SPDK_VHOST_DEV_T_BLK, - &vhost_blk_device_backend); + ret = spdk_vhost_dev_register(&bvdev->vdev, name, cpumask, &vhost_blk_device_backend); if (ret != 0) { spdk_bdev_close(bvdev->bdev_desc); ret = -1; diff --git a/lib/vhost/vhost_internal.h b/lib/vhost/vhost_internal.h index 9e06d540ed..0e62d8239a 100644 --- a/lib/vhost/vhost_internal.h +++ b/lib/vhost/vhost_internal.h @@ -93,11 +93,6 @@ #define SPDK_VHOST_DISABLED_FEATURES ((1ULL << VIRTIO_RING_F_EVENT_IDX) | \ (1ULL << VIRTIO_RING_F_INDIRECT_DESC)) -enum spdk_vhost_dev_type { - SPDK_VHOST_DEV_T_SCSI,//!< SPDK_VHOST_DEV_T_SCSI - SPDK_VHOST_DEV_T_BLK, //!< SPDK_VHOST_DEV_T_BLK -}; - struct spdk_vhost_virtqueue { struct rte_vhost_vring vring; void *tasks; @@ -148,7 +143,6 @@ struct spdk_vhost_dev { struct spdk_cpuset *cpumask; bool registered; - enum spdk_vhost_dev_type type; const struct spdk_vhost_dev_backend *backend; uint32_t coalescing_delay_time_base; @@ -241,7 +235,7 @@ int spdk_vhost_vring_desc_to_iov(struct spdk_vhost_dev *vdev, struct iovec *iov, bool spdk_vhost_dev_has_feature(struct spdk_vhost_dev *vdev, unsigned feature_id); int spdk_vhost_dev_register(struct spdk_vhost_dev *vdev, const char *name, const char *mask_str, - enum spdk_vhost_dev_type type, const struct spdk_vhost_dev_backend *backend); + const struct spdk_vhost_dev_backend *backend); int spdk_vhost_dev_unregister(struct spdk_vhost_dev *vdev); int spdk_vhost_scsi_controller_construct(void); diff --git a/lib/vhost/vhost_scsi.c b/lib/vhost/vhost_scsi.c index edb71f77c3..a7b9ca3683 100644 --- a/lib/vhost/vhost_scsi.c +++ b/lib/vhost/vhost_scsi.c @@ -677,13 +677,12 @@ to_scsi_dev(struct spdk_vhost_dev *ctrlr) 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); + if (ctrlr->backend != &spdk_vhost_scsi_device_backend) { + SPDK_ERRLOG("%s: not a vhost-scsi device.\n", ctrlr->name); return NULL; } - return (struct spdk_vhost_scsi_dev *)ctrlr; + return SPDK_CONTAINEROF(ctrlr, struct spdk_vhost_scsi_dev, vdev); } int @@ -698,7 +697,7 @@ spdk_vhost_scsi_dev_construct(const char *name, const char *cpumask) } spdk_vhost_lock(); - rc = spdk_vhost_dev_register(&svdev->vdev, name, cpumask, SPDK_VHOST_DEV_T_SCSI, + rc = spdk_vhost_dev_register(&svdev->vdev, name, cpumask, &spdk_vhost_scsi_device_backend); if (rc) { diff --git a/test/unit/lib/vhost/test_vhost.c b/test/unit/lib/vhost/test_vhost.c index fcacdcf80c..9e3e2c41a8 100644 --- a/test/unit/lib/vhost/test_vhost.c +++ b/test/unit/lib/vhost/test_vhost.c @@ -108,12 +108,13 @@ DEFINE_STUB(spdk_vhost_dev_register_fail, bool, (void), false); static struct spdk_vhost_dev *g_spdk_vhost_device; int spdk_vhost_dev_register(struct spdk_vhost_dev *vdev, const char *name, const char *mask_str, - enum spdk_vhost_dev_type type, const struct spdk_vhost_dev_backend *backend) + const struct spdk_vhost_dev_backend *backend) { if (spdk_vhost_dev_register_fail()) { return -1; } + vdev->backend = backend; g_spdk_vhost_device = vdev; vdev->registered = true; return 0; diff --git a/test/unit/lib/vhost/vhost.c/vhost_ut.c b/test/unit/lib/vhost/vhost.c/vhost_ut.c index 3bae466b87..c8bda33a2c 100644 --- a/test/unit/lib/vhost/vhost.c/vhost_ut.c +++ b/test/unit/lib/vhost/vhost.c/vhost_ut.c @@ -222,25 +222,25 @@ create_controller_test(void) /* Create device with no name */ vdev = alloc_vdev(); - ret = spdk_vhost_dev_register(vdev, NULL, "0x1", SPDK_VHOST_DEV_T_BLK, &backend); + ret = spdk_vhost_dev_register(vdev, NULL, "0x1", &backend); CU_ASSERT(ret != 0); /* Create device with incorrect cpumask */ - ret = spdk_vhost_dev_register(vdev, "vdev_name_0", "0x2", SPDK_VHOST_DEV_T_BLK, &backend); + ret = spdk_vhost_dev_register(vdev, "vdev_name_0", "0x2", &backend); CU_ASSERT(ret != 0); /* Create device with too long name and path */ memset(long_name, 'x', sizeof(long_name)); long_name[PATH_MAX - 1] = 0; snprintf(dev_dirname, sizeof(dev_dirname), "some_path/"); - ret = spdk_vhost_dev_register(vdev, long_name, "0x1", SPDK_VHOST_DEV_T_BLK, &backend); + ret = spdk_vhost_dev_register(vdev, long_name, "0x1", &backend); CU_ASSERT(ret != 0); dev_dirname[0] = 0; /* Create device when device name is already taken */ vdev->name = strdup("vdev_name_0"); g_spdk_vhost_devices[0] = vdev; - ret = spdk_vhost_dev_register(vdev, "vdev_name_0", "0x1", SPDK_VHOST_DEV_T_BLK, &backend); + ret = spdk_vhost_dev_register(vdev, "vdev_name_0", "0x1", &backend); CU_ASSERT(ret != 0); /* Create device when max number of devices is reached */ @@ -248,7 +248,7 @@ create_controller_test(void) g_spdk_vhost_devices[ctrlr_num] = vdev; } - ret = spdk_vhost_dev_register(vdev, "vdev_name_1", "0x1", SPDK_VHOST_DEV_T_BLK, &backend); + ret = spdk_vhost_dev_register(vdev, "vdev_name_1", "0x1", &backend); CU_ASSERT(ret != 0); free_vdev(vdev); diff --git a/test/unit/lib/vhost/vhost_blk.c/vhost_blk_ut.c b/test/unit/lib/vhost/vhost_blk.c/vhost_blk_ut.c index b9810c86d2..65d5f29506 100644 --- a/test/unit/lib/vhost/vhost_blk.c/vhost_blk_ut.c +++ b/test/unit/lib/vhost/vhost_blk.c/vhost_blk_ut.c @@ -88,7 +88,7 @@ alloc_bvdev(void) SPDK_CACHE_LINE_SIZE, NULL); SPDK_CU_ASSERT_FATAL(bvdev != NULL); - bvdev->vdev.type = SPDK_VHOST_DEV_T_BLK; + bvdev->vdev.backend = &vhost_blk_device_backend; return bvdev; } @@ -138,12 +138,12 @@ vhost_blk_destroy_test(void) bvdev = alloc_bvdev(); /* Device has an incorrect type */ - bvdev->vdev.type = SPDK_VHOST_DEV_T_SCSI; + bvdev->vdev.backend = NULL;; rc = spdk_vhost_blk_destroy(&bvdev->vdev); CU_ASSERT(rc == -EINVAL); /* Failed to remove device */ - bvdev->vdev.type = SPDK_VHOST_DEV_T_BLK; + bvdev->vdev.backend = &vhost_blk_device_backend; MOCK_SET(spdk_vhost_dev_unregister_fail, bool, true); rc = spdk_vhost_blk_destroy(&bvdev->vdev); CU_ASSERT(rc == -1); diff --git a/test/unit/lib/vhost/vhost_scsi.c/vhost_scsi_ut.c b/test/unit/lib/vhost/vhost_scsi.c/vhost_scsi_ut.c index d62af6867b..1205100db2 100644 --- a/test/unit/lib/vhost/vhost_scsi.c/vhost_scsi_ut.c +++ b/test/unit/lib/vhost/vhost_scsi.c/vhost_scsi_ut.c @@ -105,6 +105,7 @@ alloc_svdev(void) SPDK_CU_ASSERT_FATAL(svdev != NULL); svdev->vdev.registered = true; + svdev->vdev.backend = &spdk_vhost_scsi_device_backend; return svdev; }