From f9fccbae637ce12a3ae38432fd7e73a3b15a6507 Mon Sep 17 00:00:00 2001 From: Tomasz Zawadzki Date: Thu, 3 Mar 2022 11:24:34 +0100 Subject: [PATCH] lib/vhost: separate out vhost_user backend callbacks Previously spdk_vhost_dev_backend held callbacks for vhost_blk and vhost_scsi functionality, along with ones that are called by the vhost_user backend. This patch separates out those callbacks into two structures: - spdk_vhost_dev_backend - to be implemented by vhost_blk and vhost_scsi - spdk_vhost_user_dev_backend - is only implemented by vhost_user backend, callbacks for session managment specific to that transport Signed-off-by: Tomasz Zawadzki Change-Id: I348090df5dddeb2b1945b082b85aec53d03c781b Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/11812 Tested-by: SPDK CI Jenkins Community-CI: Mellanox Build Bot Reviewed-by: Jim Harris Reviewed-by: Changpeng Liu --- lib/vhost/rte_vhost_user.c | 12 ++++++------ lib/vhost/vhost.c | 7 +++++-- lib/vhost/vhost_blk.c | 8 ++++++-- lib/vhost/vhost_internal.h | 10 +++++++--- lib/vhost/vhost_scsi.c | 8 ++++++-- test/unit/lib/vhost/vhost.c/vhost_ut.c | 3 ++- 6 files changed, 32 insertions(+), 16 deletions(-) diff --git a/lib/vhost/rte_vhost_user.c b/lib/vhost/rte_vhost_user.c index b6311e2ad..88d4623a0 100644 --- a/lib/vhost/rte_vhost_user.c +++ b/lib/vhost/rte_vhost_user.c @@ -833,7 +833,7 @@ _stop_session(struct spdk_vhost_session *vsession) int rc; uint16_t i; - rc = vdev->backend->stop_session(vsession); + rc = vdev->user_backend->stop_session(vsession); if (rc != 0) { SPDK_ERRLOG("Couldn't stop device with vid %d.\n", vsession->vid); return rc; @@ -908,12 +908,12 @@ new_connection(int vid) } if (posix_memalign((void **)&vsession, SPDK_CACHE_LINE_SIZE, sizeof(*vsession) + - vdev->backend->session_ctx_size)) { + vdev->user_backend->session_ctx_size)) { SPDK_ERRLOG("vsession alloc failed\n"); spdk_vhost_unlock(); return -1; } - memset(vsession, 0, sizeof(*vsession) + vdev->backend->session_ctx_size); + memset(vsession, 0, sizeof(*vsession) + vdev->user_backend->session_ctx_size); vsession->vdev = vdev; vsession->vid = vid; @@ -1058,7 +1058,7 @@ start_device(int vid) vhost_user_session_set_coalescing(vdev, vsession, NULL); vhost_session_mem_register(vsession->mem); vsession->initialized = true; - rc = vdev->backend->start_session(vsession); + rc = vdev->user_backend->start_session(vsession); if (rc != 0) { vhost_session_mem_unregister(vsession->mem); free(vsession->mem); @@ -1712,7 +1712,7 @@ vhost_dev_thread_exit(void *arg1) int vhost_user_dev_register(struct spdk_vhost_dev *vdev, const char *name, struct spdk_cpuset *cpumask, - const struct spdk_vhost_dev_backend *backend) + const struct spdk_vhost_user_dev_backend *user_backend) { char path[PATH_MAX]; @@ -1735,7 +1735,7 @@ vhost_user_dev_register(struct spdk_vhost_dev *vdev, const char *name, struct sp } vdev->registered = true; - vdev->backend = backend; + vdev->user_backend = user_backend; TAILQ_INIT(&vdev->vsessions); vhost_user_dev_set_coalescing(vdev, SPDK_VHOST_COALESCING_DELAY_BASE_US, diff --git a/lib/vhost/vhost.c b/lib/vhost/vhost.c index 09176dc3c..39680a106 100644 --- a/lib/vhost/vhost.c +++ b/lib/vhost/vhost.c @@ -116,7 +116,8 @@ vhost_parse_core_mask(const char *mask, struct spdk_cpuset *cpumask) int vhost_dev_register(struct spdk_vhost_dev *vdev, const char *name, const char *mask_str, - const struct spdk_vhost_dev_backend *backend) + const struct spdk_vhost_dev_backend *backend, + const struct spdk_vhost_user_dev_backend *user_backend) { struct spdk_cpuset cpumask = {}; int rc; @@ -143,7 +144,9 @@ vhost_dev_register(struct spdk_vhost_dev *vdev, const char *name, const char *ma return -EIO; } - rc = vhost_user_dev_register(vdev, name, &cpumask, backend); + vdev->backend = backend; + + rc = vhost_user_dev_register(vdev, name, &cpumask, user_backend); if (rc != 0) { free(vdev->name); return rc; diff --git a/lib/vhost/vhost_blk.c b/lib/vhost/vhost_blk.c index e02632052..359d4b687 100644 --- a/lib/vhost/vhost_blk.c +++ b/lib/vhost/vhost_blk.c @@ -1534,10 +1534,13 @@ vhost_blk_get_config(struct spdk_vhost_dev *vdev, uint8_t *config, return 0; } -static const struct spdk_vhost_dev_backend vhost_blk_device_backend = { +static const struct spdk_vhost_user_dev_backend vhost_blk_user_device_backend = { .session_ctx_size = sizeof(struct spdk_vhost_blk_session) - sizeof(struct spdk_vhost_session), .start_session = vhost_blk_start, .stop_session = vhost_blk_stop, +}; + +static const struct spdk_vhost_dev_backend vhost_blk_device_backend = { .vhost_get_config = vhost_blk_get_config, .dump_info_json = vhost_blk_dump_info_json, .write_config_json = vhost_blk_write_config_json, @@ -1617,7 +1620,8 @@ spdk_vhost_blk_construct(const char *name, const char *cpumask, const char *dev_ bvdev->bdev = bdev; bvdev->readonly = req.readonly; - ret = vhost_dev_register(vdev, name, cpumask, &vhost_blk_device_backend); + ret = vhost_dev_register(vdev, name, cpumask, &vhost_blk_device_backend, + &vhost_blk_user_device_backend); if (ret != 0) { spdk_put_io_channel(bvdev->dummy_io_channel); spdk_bdev_close(bvdev->bdev_desc); diff --git a/lib/vhost/vhost_internal.h b/lib/vhost/vhost_internal.h index 7fe9ffc50..386cb09c9 100644 --- a/lib/vhost/vhost_internal.h +++ b/lib/vhost/vhost_internal.h @@ -185,6 +185,7 @@ struct spdk_vhost_dev { bool packed_ring_recovery; const struct spdk_vhost_dev_backend *backend; + const struct spdk_vhost_user_dev_backend *user_backend; /* Saved original values used to setup coalescing to avoid integer * rounding issues during save/load config. @@ -226,7 +227,7 @@ typedef int (*spdk_vhost_session_fn)(struct spdk_vhost_dev *vdev, */ typedef void (*spdk_vhost_dev_fn)(struct spdk_vhost_dev *vdev, void *arg); -struct spdk_vhost_dev_backend { +struct spdk_vhost_user_dev_backend { /** * Size of additional per-session context data * allocated whenever a new client connects. @@ -235,7 +236,9 @@ struct spdk_vhost_dev_backend { int (*start_session)(struct spdk_vhost_session *vsession); int (*stop_session)(struct spdk_vhost_session *vsession); +}; +struct spdk_vhost_dev_backend { int (*vhost_get_config)(struct spdk_vhost_dev *vdev, uint8_t *config, uint32_t len); int (*vhost_set_config)(struct spdk_vhost_dev *vdev, uint8_t *config, uint32_t offset, uint32_t size, uint32_t flags); @@ -404,7 +407,8 @@ vhost_dev_has_feature(struct spdk_vhost_session *vsession, unsigned feature_id) } int vhost_dev_register(struct spdk_vhost_dev *vdev, const char *name, const char *mask_str, - const struct spdk_vhost_dev_backend *backend); + const struct spdk_vhost_dev_backend *backend, + const struct spdk_vhost_user_dev_backend *user_backend); int vhost_dev_unregister(struct spdk_vhost_dev *vdev); void vhost_dump_info_json(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w); @@ -504,7 +508,7 @@ int vhost_user_session_set_coalescing(struct spdk_vhost_dev *vdev, int vhost_user_dev_set_coalescing(struct spdk_vhost_dev *vdev, uint32_t delay_base_us, uint32_t iops_threshold); int vhost_user_dev_register(struct spdk_vhost_dev *vdev, const char *name, - struct spdk_cpuset *cpumask, const struct spdk_vhost_dev_backend *backend); + struct spdk_cpuset *cpumask, const struct spdk_vhost_user_dev_backend *user_backend); int vhost_user_dev_unregister(struct spdk_vhost_dev *vdev); int vhost_user_init(void); typedef void (*vhost_fini_cb)(void *ctx); diff --git a/lib/vhost/vhost_scsi.c b/lib/vhost/vhost_scsi.c index bd9f1c686..6432c4948 100644 --- a/lib/vhost/vhost_scsi.c +++ b/lib/vhost/vhost_scsi.c @@ -150,10 +150,13 @@ static int vhost_scsi_dev_remove(struct spdk_vhost_dev *vdev); static int vhost_scsi_dev_param_changed(struct spdk_vhost_dev *vdev, unsigned scsi_tgt_num); -static const struct spdk_vhost_dev_backend spdk_vhost_scsi_device_backend = { +static const struct spdk_vhost_user_dev_backend spdk_vhost_scsi_user_device_backend = { .session_ctx_size = sizeof(struct spdk_vhost_scsi_session) - sizeof(struct spdk_vhost_session), .start_session = vhost_scsi_start, .stop_session = vhost_scsi_stop, +}; + +static const struct spdk_vhost_dev_backend spdk_vhost_scsi_device_backend = { .dump_info_json = vhost_scsi_dump_info_json, .write_config_json = vhost_scsi_write_config_json, .remove_device = vhost_scsi_dev_remove, @@ -875,7 +878,8 @@ spdk_vhost_scsi_dev_construct(const char *name, const char *cpumask) spdk_vhost_lock(); rc = vhost_dev_register(&svdev->vdev, name, cpumask, - &spdk_vhost_scsi_device_backend); + &spdk_vhost_scsi_device_backend, + &spdk_vhost_scsi_user_device_backend); if (rc) { free(svdev); diff --git a/test/unit/lib/vhost/vhost.c/vhost_ut.c b/test/unit/lib/vhost/vhost.c/vhost_ut.c index eee563e07..71e48164c 100644 --- a/test/unit/lib/vhost/vhost.c/vhost_ut.c +++ b/test/unit/lib/vhost/vhost.c/vhost_ut.c @@ -102,6 +102,7 @@ spdk_call_unaffinitized(void *cb(void *arg), void *arg) } static struct spdk_vhost_dev_backend g_vdev_backend; +static struct spdk_vhost_user_dev_backend g_vdev_user_backend; static int test_setup(void) @@ -120,7 +121,7 @@ alloc_vdev(struct spdk_vhost_dev **vdev_p, const char *name, const char *cpumask CU_ASSERT(rc == 0); SPDK_CU_ASSERT_FATAL(vdev != NULL); memset(vdev, 0, sizeof(*vdev)); - rc = vhost_dev_register(vdev, name, cpumask, &g_vdev_backend); + rc = vhost_dev_register(vdev, name, cpumask, &g_vdev_backend, &g_vdev_user_backend); if (rc == 0) { *vdev_p = vdev; } else {