diff options
-rw-r--r-- | include/libvfio-user.h | 4 | ||||
-rw-r--r-- | lib/libvfio-user.c | 43 | ||||
-rw-r--r-- | test/unit-tests.c | 12 |
3 files changed, 33 insertions, 26 deletions
diff --git a/include/libvfio-user.h b/include/libvfio-user.h index 1a336374..89e1c85 100644 --- a/include/libvfio-user.h +++ b/include/libvfio-user.h @@ -287,7 +287,9 @@ typedef void (vfu_map_dma_cb_t) (void *pvt, uint64_t iova, uint64_t len); typedef int (vfu_unmap_dma_cb_t) (void *pvt, uint64_t iova, uint64_t len); /** - * Setup device DMA map/unmap callbacks. + * Setup device DMA map/unmap callbacks. This will also enable bookkeeping of + * DMA regions received from client, otherwise they will be just acknowledged. + * * @vfu_ctx: the libvfio-user context * @map_dma: DMA region map callback (optional) * @unmap_dma: DMA region unmap callback (optional) diff --git a/lib/libvfio-user.c b/lib/libvfio-user.c index 87ecbfa..e2af26f 100644 --- a/lib/libvfio-user.c +++ b/lib/libvfio-user.c @@ -1320,6 +1320,7 @@ vfu_create_ctx(vfu_trans_t trans, const char *path, int flags, void *pvt, vfu_ctx = calloc(1, sizeof(vfu_ctx_t)); if (vfu_ctx == NULL) { + errno = ENOMEM; return NULL; } vfu_ctx->dev_type = dev_type; @@ -1333,8 +1334,8 @@ vfu_create_ctx(vfu_trans_t trans, const char *path, int flags, void *pvt, vfu_ctx->uuid = strdup(path); if (vfu_ctx->uuid == NULL) { - err = errno; - goto out; + err = -errno; + goto err_out; } /* @@ -1346,37 +1347,33 @@ vfu_create_ctx(vfu_trans_t trans, const char *path, int flags, void *pvt, vfu_ctx->reg_info = calloc(vfu_ctx->nr_regions, sizeof *vfu_ctx->reg_info); if (vfu_ctx->reg_info == NULL) { err = -ENOMEM; - goto out; + goto err_out; } if (vfu_setup_device_nr_irqs(vfu_ctx, VFU_DEV_ERR_IRQ, 1) == -1) { err = -errno; - goto out; + goto err_out; } if (vfu_setup_device_nr_irqs(vfu_ctx, VFU_DEV_REQ_IRQ, 1) == -1) { err = -errno; - goto out; + goto err_out; } if (vfu_ctx->trans->init != NULL) { err = vfu_ctx->trans->init(vfu_ctx); if (err < 0) { - goto out; + goto err_out; } vfu_ctx->fd = err; } - err = 0; - -out: - if (err != 0) { - if (vfu_ctx != NULL) { - vfu_destroy_ctx(vfu_ctx); - vfu_ctx = NULL; - } - errno = -err; - } return vfu_ctx; + +err_out: + vfu_destroy_ctx(vfu_ctx); + errno = -err; + + return NULL; } int @@ -1576,17 +1573,15 @@ vfu_setup_device_dma_cb(vfu_ctx_t *vfu_ctx, vfu_map_dma_cb_t *map_dma, assert(vfu_ctx != NULL); - vfu_ctx->map_dma = map_dma; - vfu_ctx->unmap_dma = unmap_dma; - // Create the internal DMA controller. - if (vfu_ctx->unmap_dma != NULL) { - vfu_ctx->dma = dma_controller_create(vfu_ctx, VFU_DMA_REGIONS); - if (vfu_ctx->dma == NULL) { - return ERROR(ENOMEM); - } + vfu_ctx->dma = dma_controller_create(vfu_ctx, VFU_DMA_REGIONS); + if (vfu_ctx->dma == NULL) { + return ERROR(ENOMEM); } + vfu_ctx->map_dma = map_dma; + vfu_ctx->unmap_dma = unmap_dma; + return 0; } diff --git a/test/unit-tests.c b/test/unit-tests.c index 59b24dc..6355843 100644 --- a/test/unit-tests.c +++ b/test/unit-tests.c @@ -680,6 +680,15 @@ test_dma_map_sg(void **state __attribute__((unused))) } +static void +test_vfu_setup_device_dma_cb(void **state __attribute__((unused))) +{ + vfu_ctx_t vfu_ctx = { 0 }; + + assert_int_equal(0, vfu_setup_device_dma_cb(&vfu_ctx, NULL, NULL)); + assert_non_null(vfu_ctx.dma); +} + /* * FIXME we shouldn't have to specify a setup function explicitly for each unit * test, cmocka should provide that. E.g. cmocka_run_group_tests enables us to @@ -712,7 +721,8 @@ int main(void) cmocka_unit_test_setup(test_get_region_info, setup), cmocka_unit_test_setup(test_setup_sparse_region, setup), cmocka_unit_test_setup(test_dma_map_return_value, setup), - cmocka_unit_test_setup(test_dma_map_sg, setup) + cmocka_unit_test_setup(test_dma_map_sg, setup), + cmocka_unit_test_setup(test_vfu_setup_device_dma_cb, setup) }; return cmocka_run_group_tests(tests, NULL, NULL); |