From 5ccd7974cc01658a8d95a59cb5e8e96bcf3cdfc5 Mon Sep 17 00:00:00 2001 From: Dariusz Stojaczyk Date: Thu, 3 Aug 2017 11:24:00 +0200 Subject: [PATCH] vhost: simplified timed_event API Public API has been made smaller before refactoring timed_events entirely. Change-Id: Ie03bf5066582b6fd243229a9c8b1ceb77658dc2f Signed-off-by: Dariusz Stojaczyk Reviewed-on: https://review.gerrithub.io/372686 Tested-by: SPDK Automated Test System Reviewed-by: Pawel Wodkowski Reviewed-by: Daniel Verkamp Reviewed-by: Jim Harris --- lib/vhost/vhost.c | 53 ++++++++++++++++---------------------- lib/vhost/vhost_blk.c | 13 +++++++--- lib/vhost/vhost_internal.h | 18 ------------- lib/vhost/vhost_scsi.c | 18 +++++++------ 4 files changed, 41 insertions(+), 61 deletions(-) diff --git a/lib/vhost/vhost.c b/lib/vhost/vhost.c index f03e2de9d..13833dfa7 100644 --- a/lib/vhost/vhost.c +++ b/lib/vhost/vhost.c @@ -49,6 +49,20 @@ static char dev_dirname[PATH_MAX] = ""; #define MAX_VHOST_DEVICES 64 +struct spdk_vhost_timed_event { + /** User callback function to be executed on given lcore. */ + spdk_vhost_timed_event_fn cb_fn; + + /** Semaphore used to signal that event is done. */ + sem_t sem; + + /** Timout specified during initialization. */ + struct timespec timeout; + + /** Event object that can be passed to *spdk_event_call()*. */ + struct spdk_event *spdk_event; +}; + static int new_connection(int vid); static int new_device(int vid); static void destroy_device(int vid); @@ -707,12 +721,13 @@ vhost_timed_event_fn(void *arg1, void *arg2) sem_post(&ev->sem); } -static void -vhost_timed_event_init(struct spdk_vhost_timed_event *ev, int32_t lcore, - spdk_vhost_timed_event_fn cb_fn, void *arg, unsigned timeout_sec) +void +spdk_vhost_timed_event_send(int32_t lcore, spdk_vhost_timed_event_fn cb_fn, void *arg, + unsigned timeout_sec, const char *errmsg) { - /* No way to free spdk event so don't allow to use it again without calling, waiting. */ - assert(ev->spdk_event == NULL); + struct spdk_vhost_timed_event _ev = {0}; + struct spdk_vhost_timed_event *ev = &_ev; + int rc; if (sem_init(&ev->sem, 0, 0) < 0) SPDK_ERRLOG("Failed to initialize semaphore for vhost timed event\n"); @@ -721,32 +736,8 @@ vhost_timed_event_init(struct spdk_vhost_timed_event *ev, int32_t lcore, clock_gettime(CLOCK_REALTIME, &ev->timeout); ev->timeout.tv_sec += timeout_sec; ev->spdk_event = spdk_event_allocate(lcore, vhost_timed_event_fn, ev, arg); -} - -void -spdk_vhost_timed_event_init(struct spdk_vhost_timed_event *ev, int32_t lcore, - spdk_vhost_timed_event_fn cb_fn, void *arg, unsigned timeout_sec) -{ - vhost_timed_event_init(ev, lcore, cb_fn, arg, timeout_sec); -} - -void -spdk_vhost_timed_event_send(int32_t lcore, spdk_vhost_timed_event_fn cb_fn, void *arg, - unsigned timeout_sec, const char *errmsg) -{ - struct spdk_vhost_timed_event ev = {0}; - - vhost_timed_event_init(&ev, lcore, cb_fn, arg, timeout_sec); - spdk_event_call(ev.spdk_event); - spdk_vhost_timed_event_wait(&ev, errmsg); -} - -void -spdk_vhost_timed_event_wait(struct spdk_vhost_timed_event *ev, const char *errmsg) -{ - int rc; - - assert(ev->spdk_event != NULL); + assert(ev->spdk_event); + spdk_event_call(ev->spdk_event); rc = sem_timedwait(&ev->sem, &ev->timeout); if (rc != 0) { diff --git a/lib/vhost/vhost_blk.c b/lib/vhost/vhost_blk.c index 3c73d2547..fcc6b4949 100644 --- a/lib/vhost/vhost_blk.c +++ b/lib/vhost/vhost_blk.c @@ -395,6 +395,14 @@ remove_vdev_cb(void *arg) spdk_vhost_dev_mem_unregister(&bvdev->vdev); } +static void +unregister_vdev_cb(void *arg) +{ + struct spdk_vhost_blk_dev *bvdev = arg; + + spdk_poller_unregister(&bvdev->requestq_poller, NULL); +} + static struct spdk_vhost_blk_dev * to_blk_dev(struct spdk_vhost_dev *vdev) { @@ -546,7 +554,6 @@ static int destroy_device(struct spdk_vhost_dev *vdev) { struct spdk_vhost_blk_dev *bvdev; - struct spdk_vhost_timed_event event = {0}; uint32_t i; bvdev = to_blk_dev(vdev); @@ -555,9 +562,7 @@ destroy_device(struct spdk_vhost_dev *vdev) return -1; } - spdk_vhost_timed_event_init(&event, vdev->lcore, NULL, NULL, 1); - spdk_poller_unregister(&bvdev->requestq_poller, event.spdk_event); - spdk_vhost_timed_event_wait(&event, "unregister poller"); + spdk_vhost_timed_event_send(vdev->lcore, unregister_vdev_cb, bvdev, 1, "unregister vdev"); /* Wait for all tasks to finish */ for (i = 1000; i && vdev->task_cnt > 0; i--) { diff --git a/lib/vhost/vhost_internal.h b/lib/vhost/vhost_internal.h index 8202cc5c2..4d737a106 100644 --- a/lib/vhost/vhost_internal.h +++ b/lib/vhost/vhost_internal.h @@ -137,26 +137,8 @@ int spdk_vhost_dev_remove(struct spdk_vhost_dev *vdev); typedef void (*spdk_vhost_timed_event_fn)(void *); -struct spdk_vhost_timed_event { - /** User callback function to be executed on given lcore. */ - spdk_vhost_timed_event_fn cb_fn; - - /** Semaphore used to signal that event is done. */ - sem_t sem; - - /** Timout specified during initialization. */ - struct timespec timeout; - - /** Event object that can be passed to *spdk_event_call()*. */ - struct spdk_event *spdk_event; -}; - -void spdk_vhost_timed_event_init(struct spdk_vhost_timed_event *ev, int32_t lcore, - spdk_vhost_timed_event_fn cb_fn, void *arg, unsigned timeout_sec); - void spdk_vhost_timed_event_send(int32_t lcore, spdk_vhost_timed_event_fn cn_fn, void *arg, unsigned timeout_sec, const char *errmsg); -void spdk_vhost_timed_event_wait(struct spdk_vhost_timed_event *event, const char *errmsg); int spdk_vhost_blk_controller_construct(void); void spdk_vhost_dump_config_json(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w); diff --git a/lib/vhost/vhost_scsi.c b/lib/vhost/vhost_scsi.c index d74b1b069..32c7e9a9a 100644 --- a/lib/vhost/vhost_scsi.c +++ b/lib/vhost/vhost_scsi.c @@ -727,6 +727,15 @@ remove_vdev_cb(void *arg) spdk_vhost_dev_mem_unregister(&svdev->vdev); } +static void +unregister_vdev_cb(void *arg) +{ + struct spdk_vhost_scsi_dev *svdev = arg; + + spdk_poller_unregister(&svdev->requestq_poller, NULL); + spdk_poller_unregister(&svdev->mgmt_poller, NULL); +} + static struct spdk_vhost_scsi_dev * to_scsi_dev(struct spdk_vhost_dev *ctrlr) { @@ -1125,7 +1134,6 @@ destroy_device(struct spdk_vhost_dev *vdev) { struct spdk_vhost_scsi_dev *svdev; void *ev; - struct spdk_vhost_timed_event event = {0}; uint32_t i; svdev = to_scsi_dev(vdev); @@ -1134,13 +1142,7 @@ destroy_device(struct spdk_vhost_dev *vdev) return -1; } - spdk_vhost_timed_event_init(&event, vdev->lcore, NULL, NULL, 1); - spdk_poller_unregister(&svdev->requestq_poller, event.spdk_event); - spdk_vhost_timed_event_wait(&event, "unregister request queue poller"); - - spdk_vhost_timed_event_init(&event, vdev->lcore, NULL, NULL, 1); - spdk_poller_unregister(&svdev->mgmt_poller, event.spdk_event); - spdk_vhost_timed_event_wait(&event, "unregister management poller"); + spdk_vhost_timed_event_send(vdev->lcore, unregister_vdev_cb, svdev, 1, "unregister scsi vdev"); /* Wait for all tasks to finish */ for (i = 1000; i && vdev->task_cnt > 0; i--) {