diff options
author | swapnili <swapnil.ingle@nutanix.com> | 2021-02-10 17:54:51 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-10 17:54:51 +0100 |
commit | aa2157aad1876c3963efe34c694c93eadd309c97 (patch) | |
tree | a232b4ef5155eacb7a902603a20ec358e38cdee4 /lib/libvfio-user.c | |
parent | d3651f5d30a0532f39da962b9e76ba8cbada6e6c (diff) | |
download | libvfio-user-aa2157aad1876c3963efe34c694c93eadd309c97.zip libvfio-user-aa2157aad1876c3963efe34c694c93eadd309c97.tar.gz libvfio-user-aa2157aad1876c3963efe34c694c93eadd309c97.tar.bz2 |
API error return converged to one func (#325)
* API error return converged to one func
Use ERROR_INT() or ERROR_PTR() to return errors from API's.
This way if we want to change the behaviour later, we will just need
to update these funcitons.
Also fixed some error return cases and comments.
Reviewed-by: John Levon <john.levon@nutanix.com>
Diffstat (limited to 'lib/libvfio-user.c')
-rw-r--r-- | lib/libvfio-user.c | 66 |
1 files changed, 34 insertions, 32 deletions
diff --git a/lib/libvfio-user.c b/lib/libvfio-user.c index e5ba0aa..fc427f5 100644 --- a/lib/libvfio-user.c +++ b/lib/libvfio-user.c @@ -986,7 +986,7 @@ vfu_realize_ctx(vfu_ctx_t *vfu_ctx) if (vfu_ctx->pci.config_space == NULL) { vfu_ctx->pci.config_space = calloc(1, cfg_reg->size); if (vfu_ctx->pci.config_space == NULL) { - return ERROR(ENOMEM); + return ERROR_INT(ENOMEM); } } @@ -1015,7 +1015,7 @@ vfu_realize_ctx(vfu_ctx_t *vfu_ctx) vfu_ctx->irqs = calloc(1, sizeof(vfu_irqs_t) + size); if (vfu_ctx->irqs == NULL) { // vfu_ctx->pci.config_space should be free'ed by vfu_destroy_ctx(). - return ERROR(ENOMEM); + return ERROR_INT(ENOMEM); } // Set context irq information. @@ -1051,7 +1051,7 @@ vfu_run_ctx(vfu_ctx_t *vfu_ctx) assert(vfu_ctx != NULL); if (!vfu_ctx->realized) { - return ERROR(EINVAL); + return ERROR_INT(EINVAL); } blocking = !(vfu_ctx->flags & LIBVFIO_USER_FLAG_ATTACH_NB); @@ -1131,19 +1131,16 @@ vfu_create_ctx(vfu_trans_t trans, const char *path, int flags, void *pvt, //FIXME: Validate arguments. if (trans != VFU_TRANS_SOCK) { - errno = ENOTSUP; - return NULL; + return ERROR_PTR(ENOTSUP); } if (dev_type != VFU_DEV_TYPE_PCI) { - errno = EINVAL; - return NULL; + return ERROR_PTR(EINVAL); } vfu_ctx = calloc(1, sizeof(vfu_ctx_t)); if (vfu_ctx == NULL) { - errno = ENOMEM; - return NULL; + return ERROR_PTR(ENOMEM); } vfu_ctx->fd = -1; @@ -1197,9 +1194,8 @@ vfu_create_ctx(vfu_trans_t trans, const char *path, int flags, void *pvt, err_out: vfu_destroy_ctx(vfu_ctx); - errno = -err; - return NULL; + return ERROR_PTR(-err); } int @@ -1207,7 +1203,7 @@ vfu_setup_log(vfu_ctx_t *vfu_ctx, vfu_log_fn_t *log, int log_level) { if (log_level != LOG_ERR && log_level != LOG_INFO && log_level != LOG_DEBUG) { - return ERROR(EINVAL); + return ERROR_INT(EINVAL); } vfu_ctx->log = log; @@ -1285,13 +1281,13 @@ vfu_setup_region(vfu_ctx_t *vfu_ctx, int region_idx, size_t size, if ((mmap_areas == NULL) != (nr_mmap_areas == 0) || (mmap_areas != NULL && fd == -1)) { vfu_log(vfu_ctx, LOG_ERR, "invalid mappable region arguments"); - return ERROR(EINVAL); + return ERROR_INT(EINVAL); } if (region_idx < VFU_PCI_DEV_BAR0_REGION_IDX || region_idx >= VFU_PCI_DEV_NUM_REGIONS) { vfu_log(vfu_ctx, LOG_ERR, "invalid region index %d", region_idx); - return ERROR(EINVAL); + return ERROR_INT(EINVAL); } /* @@ -1299,19 +1295,19 @@ vfu_setup_region(vfu_ctx_t *vfu_ctx, int region_idx, size_t size, */ if (region_idx == VFU_PCI_DEV_CFG_REGION_IDX && flags != VFU_REGION_FLAG_RW) { - return ERROR(EINVAL); + return ERROR_INT(EINVAL); } if (region_idx == VFU_PCI_DEV_MIGR_REGION_IDX && size < vfu_get_migr_register_area_size()) { vfu_log(vfu_ctx, LOG_ERR, "invalid migration region size %d", size); - return ERROR(EINVAL); + return ERROR_INT(EINVAL); } for (i = 0; i < nr_mmap_areas; i++) { struct iovec *iov = &mmap_areas[i]; if ((size_t)iov->iov_base + iov->iov_len > size) { - return ERROR(EINVAL); + return ERROR_INT(EINVAL); } } @@ -1352,7 +1348,7 @@ out: if (ret < 0) { free(reg->mmap_areas); memset(reg, 0, sizeof (*reg)); - return ERROR(-ret); + return ERROR_INT(-ret); } return 0; } @@ -1377,7 +1373,7 @@ vfu_setup_device_dma_cb(vfu_ctx_t *vfu_ctx, vfu_map_dma_cb_t *map_dma, // Create the internal DMA controller. vfu_ctx->dma = dma_controller_create(vfu_ctx, VFU_DMA_REGIONS); if (vfu_ctx->dma == NULL) { - return ERROR(ENOMEM); + return ERROR_INT(ENOMEM); } vfu_ctx->map_dma = map_dma; @@ -1397,7 +1393,7 @@ vfu_setup_device_nr_irqs(vfu_ctx_t *vfu_ctx, enum vfu_dev_irq_type type, vfu_log(vfu_ctx, LOG_ERR, "Invalid IRQ index %d, should be between " "(%d to %d)", type, VFU_DEV_INTX_IRQ, VFU_DEV_REQ_IRQ); - return ERROR(EINVAL); + return ERROR_INT(EINVAL); } vfu_ctx->irq_count[type] = count; @@ -1417,19 +1413,19 @@ vfu_setup_device_migration_callbacks(vfu_ctx_t *vfu_ctx, if (vfu_ctx->migr_reg == NULL) { vfu_log(vfu_ctx, LOG_ERR, "no device migration region"); - return ERROR(EINVAL); + return ERROR_INT(EINVAL); } if (callbacks->version != VFU_MIGR_CALLBACKS_VERS) { vfu_log(vfu_ctx, LOG_ERR, "unsupported migration callbacks version %d", callbacks->version); - return ERROR(EINVAL); + return ERROR_INT(EINVAL); } vfu_ctx->migration = init_migration(callbacks, data_offset, &ret); if (vfu_ctx->migration == NULL) { vfu_log(vfu_ctx, LOG_ERR, "failed to initialize device migration"); - return ERROR(ret); + return ERROR_INT(ret); } return 0; @@ -1449,8 +1445,7 @@ vfu_addr_to_sg(vfu_ctx_t *vfu_ctx, dma_addr_t dma_addr, assert(vfu_ctx != NULL); if (unlikely(vfu_ctx->unmap_dma == NULL)) { - errno = EINVAL; - return -1; + return ERROR_INT(EINVAL); } return dma_addr_to_sg(vfu_ctx->dma, dma_addr, len, sg, max_sg, prot); @@ -1460,11 +1455,18 @@ inline int vfu_map_sg(vfu_ctx_t *vfu_ctx, const dma_sg_t *sg, struct iovec *iov, int cnt) { + int ret; + if (unlikely(vfu_ctx->unmap_dma == NULL)) { - errno = EINVAL; - return -1; + return ERROR_INT(EINVAL); + } + + ret = dma_map_sg(vfu_ctx->dma, sg, iov, cnt); + if (ret < 0) { + return ERROR_INT(-ret); } - return dma_map_sg(vfu_ctx->dma, sg, iov, cnt); + + return 0; } inline void @@ -1491,7 +1493,7 @@ vfu_dma_read(vfu_ctx_t *vfu_ctx, dma_sg_t *sg, void *data) dma_recv = calloc(recv_size, 1); if (dma_recv == NULL) { - return -ENOMEM; + return ERROR_INT(ENOMEM); } dma_send.addr = sg->dma_addr; @@ -1502,7 +1504,7 @@ vfu_dma_read(vfu_ctx_t *vfu_ctx, dma_sg_t *sg, void *data) memcpy(data, dma_recv->data, sg->length); /* FIXME no need for memcpy */ free(dma_recv); - return ret; + return ret < 0 ? ERROR_INT(-ret) : 0; } int @@ -1517,7 +1519,7 @@ vfu_dma_write(vfu_ctx_t *vfu_ctx, dma_sg_t *sg, void *data) dma_send = calloc(send_size, 1); if (dma_send == NULL) { - return -ENOMEM; + return ERROR_INT(ENOMEM); } dma_send->addr = sg->dma_addr; dma_send->count = sg->length; @@ -1527,7 +1529,7 @@ vfu_dma_write(vfu_ctx_t *vfu_ctx, dma_sg_t *sg, void *data) &dma_recv, sizeof(dma_recv)); free(dma_send); - return ret; + return ret < 0 ? ERROR_INT(-ret) : 0; } uint64_t |