diff --git a/lib/vhost/vhost.c b/lib/vhost/vhost.c index 4d1c3496e..0187758b8 100644 --- a/lib/vhost/vhost.c +++ b/lib/vhost/vhost.c @@ -61,6 +61,9 @@ struct spdk_vhost_dev_event_ctx { /** Semaphore used to signal that event is done. */ sem_t sem; + + /** Response to be written by enqueued event. */ + int response; }; static int new_connection(int vid); @@ -536,12 +539,21 @@ spdk_vhost_allocate_reactor(uint64_t cpumask) return selected_core; } +void +spdk_vhost_dev_backend_event_done(void *event_ctx, int response) +{ + struct spdk_vhost_dev_event_ctx *ctx = event_ctx; + + ctx->response = response; + sem_post(&ctx->sem); +} + static void spdk_vhost_event_cb(void *arg1, void *arg2) { struct spdk_vhost_dev_event_ctx *ctx = arg1; - ctx->cb_fn(ctx->vdev, &ctx->sem); + ctx->cb_fn(ctx->vdev, ctx); } static void @@ -599,7 +611,7 @@ spdk_vhost_event_send(struct spdk_vhost_dev *vdev, spdk_vhost_event_fn cb_fn, sem_destroy(&ev_ctx.sem); free(ev_ctx.ctrlr_name); - return rc; + return ev_ctx.response; } static int diff --git a/lib/vhost/vhost_blk.c b/lib/vhost/vhost_blk.c index e48a7a095..e31e503f4 100644 --- a/lib/vhost/vhost_blk.c +++ b/lib/vhost/vhost_blk.c @@ -484,10 +484,9 @@ alloc_task_pool(struct spdk_vhost_blk_dev *bvdev) * */ static int -new_device(struct spdk_vhost_dev *vdev, void *arg) +new_device(struct spdk_vhost_dev *vdev, void *event_ctx) { struct spdk_vhost_blk_dev *bvdev; - sem_t *sem = arg; int rc = 0; bvdev = to_blk_dev(vdev); @@ -522,14 +521,14 @@ new_device(struct spdk_vhost_dev *vdev, void *arg) bvdev, vdev->lcore, 0); SPDK_NOTICELOG("Started poller for vhost controller %s on lcore %d\n", vdev->name, vdev->lcore); out: - sem_post(sem); + spdk_vhost_dev_backend_event_done(event_ctx, rc); return rc; } struct spdk_vhost_dev_destroy_ctx { struct spdk_vhost_blk_dev *bvdev; struct spdk_poller *poller; - sem_t *sem; + void *event_ctx; }; static void @@ -553,15 +552,14 @@ destroy_device_poller_cb(void *arg) spdk_vhost_dev_mem_unregister(&bvdev->vdev); spdk_poller_unregister(&ctx->poller, NULL); - sem_post(ctx->sem); + spdk_vhost_dev_backend_event_done(ctx->event_ctx, 0); } static int -destroy_device(struct spdk_vhost_dev *vdev, void *arg) +destroy_device(struct spdk_vhost_dev *vdev, void *event_ctx) { struct spdk_vhost_blk_dev *bvdev; struct spdk_vhost_dev_destroy_ctx *destroy_ctx; - sem_t *sem = arg; bvdev = to_blk_dev(vdev); if (bvdev == NULL) { @@ -576,7 +574,7 @@ destroy_device(struct spdk_vhost_dev *vdev, void *arg) } destroy_ctx->bvdev = bvdev; - destroy_ctx->sem = arg; + destroy_ctx->event_ctx = event_ctx; spdk_poller_unregister(&bvdev->requestq_poller, NULL); spdk_poller_register(&destroy_ctx->poller, destroy_device_poller_cb, destroy_ctx, vdev->lcore, @@ -584,7 +582,7 @@ destroy_device(struct spdk_vhost_dev *vdev, void *arg) return 0; err: - sem_post(sem); + spdk_vhost_dev_backend_event_done(event_ctx, -1); return -1; } diff --git a/lib/vhost/vhost_internal.h b/lib/vhost/vhost_internal.h index ed8df9f6f..a93e0a90f 100644 --- a/lib/vhost/vhost_internal.h +++ b/lib/vhost/vhost_internal.h @@ -88,11 +88,12 @@ struct spdk_vhost_dev_backend { /** * Callbacks for starting and pausing the device. - * The first param is struct spdk_vhost_dev *, - * The second one is sem_t* passed as a void*. - * The callback must call sem_post with given sem. - * If sem_post won't be called within an arbitrary - * limit of 3 seconds, this will time out. + * The first param is struct spdk_vhost_dev *. + * The second one is event context that has to be + * passed to spdk_vhost_dev_backend_event_done(). + * If spdk_vhost_dev_backend_event_done isn't called + * within an arbitrary limit of 3 seconds, these + * callbacks will time out. */ spdk_vhost_event_fn new_device; spdk_vhost_event_fn destroy_device; @@ -147,5 +148,6 @@ int spdk_vhost_dev_remove(struct spdk_vhost_dev *vdev); int spdk_vhost_blk_controller_construct(void); void spdk_vhost_dump_config_json(struct spdk_vhost_dev *vdev, struct spdk_json_write_ctx *w); +void spdk_vhost_dev_backend_event_done(void *event_ctx, int response); #endif /* SPDK_VHOST_INTERNAL_H */ diff --git a/lib/vhost/vhost_scsi.c b/lib/vhost/vhost_scsi.c index ca508eb35..1df6551e8 100644 --- a/lib/vhost/vhost_scsi.c +++ b/lib/vhost/vhost_scsi.c @@ -1052,10 +1052,9 @@ alloc_task_pool(struct spdk_vhost_scsi_dev *svdev) * and then allocated to a specific data core. */ static int -new_device(struct spdk_vhost_dev *vdev, void *arg) +new_device(struct spdk_vhost_dev *vdev, void *event_ctx) { struct spdk_vhost_scsi_dev *svdev; - sem_t *sem = arg; uint32_t i; int rc = 0; @@ -1094,14 +1093,14 @@ new_device(struct spdk_vhost_dev *vdev, void *arg) spdk_poller_register(&svdev->mgmt_poller, vdev_mgmt_worker, svdev, vdev->lcore, MGMT_POLL_PERIOD_US); out: - sem_post(sem); + spdk_vhost_dev_backend_event_done(event_ctx, rc); return rc; } struct spdk_vhost_dev_destroy_ctx { struct spdk_vhost_scsi_dev *svdev; struct spdk_poller *poller; - sem_t *sem; + void *event_ctx; }; static void @@ -1138,15 +1137,14 @@ destroy_device_poller_cb(void *arg) free_task_pool(svdev); spdk_poller_unregister(&ctx->poller, NULL); - sem_post(ctx->sem); + spdk_vhost_dev_backend_event_done(ctx->event_ctx, 0); } static int -destroy_device(struct spdk_vhost_dev *vdev, void *arg) +destroy_device(struct spdk_vhost_dev *vdev, void *event_ctx) { struct spdk_vhost_scsi_dev *svdev; struct spdk_vhost_dev_destroy_ctx *destroy_ctx; - sem_t *sem = arg; svdev = to_scsi_dev(vdev); if (svdev == NULL) { @@ -1161,7 +1159,7 @@ destroy_device(struct spdk_vhost_dev *vdev, void *arg) } destroy_ctx->svdev = svdev; - destroy_ctx->sem = arg; + destroy_ctx->event_ctx = event_ctx; spdk_poller_unregister(&svdev->requestq_poller, NULL); spdk_poller_unregister(&svdev->mgmt_poller, NULL); @@ -1171,7 +1169,7 @@ destroy_device(struct spdk_vhost_dev *vdev, void *arg) return 0; err: - sem_post(sem); + spdk_vhost_dev_backend_event_done(event_ctx, -1); return -1; } 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 4743baba1..49f567ff8 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 @@ -121,6 +121,7 @@ DEFINE_STUB(spdk_json_write_array_end, int, (struct spdk_json_write_ctx *w), 0); DEFINE_STUB_P(spdk_bdev_get_io_channel, struct spdk_io_channel, (struct spdk_bdev_desc *desc), {0}); DEFINE_STUB_V(spdk_vhost_call_external_event, (const char *ctrlr_name, spdk_vhost_event_fn fn, void *arg)); +DEFINE_STUB_V(spdk_vhost_dev_backend_event_done, (void *event_ctx, int response)); /* This sets spdk_vhost_dev_remove to either to fail or success */ DEFINE_STUB(spdk_vhost_dev_remove_fail, bool, (void), false); 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 157c9aa04..a669e81bd 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 @@ -117,6 +117,7 @@ DEFINE_STUB(spdk_json_write_string, int, (struct spdk_json_write_ctx *w, const c DEFINE_STUB(spdk_json_write_array_begin, int, (struct spdk_json_write_ctx *w), 0); DEFINE_STUB(spdk_json_write_object_end, int, (struct spdk_json_write_ctx *w), 0); DEFINE_STUB(spdk_json_write_array_end, int, (struct spdk_json_write_ctx *w), 0); +DEFINE_STUB_V(spdk_vhost_dev_backend_event_done, (void *event_ctx, int response)); /* This sets spdk_vhost_dev_remove to either to fail or success */ DEFINE_STUB(spdk_vhost_dev_remove_fail, bool, (void), false);