diff options
-rw-r--r-- | lib/irq.c | 1 | ||||
-rw-r--r-- | lib/libvfio-user.c | 77 | ||||
-rw-r--r-- | lib/tran_sock.c | 112 | ||||
-rw-r--r-- | samples/client.c | 102 | ||||
-rw-r--r-- | samples/gpio-pci-idio-16.c | 1 | ||||
-rw-r--r-- | samples/null.c | 1 | ||||
-rw-r--r-- | test/mocks.c | 6 | ||||
-rw-r--r-- | test/unit-tests.c | 3 |
8 files changed, 145 insertions, 158 deletions
@@ -37,7 +37,6 @@ #include <sys/eventfd.h> #include "irq.h" -#include "tran_sock.h" #define LM2VFIO_IRQT(type) (type - 1) diff --git a/lib/libvfio-user.c b/lib/libvfio-user.c index c86713a..47d0651 100644 --- a/lib/libvfio-user.c +++ b/lib/libvfio-user.c @@ -697,8 +697,8 @@ validate_header(vfu_ctx_t *vfu_ctx, struct vfio_user_header *hdr, size_t size) * Populates @hdr to contain the header for the next command to be processed. * Stores any passed FDs into @fds and the number in @nr_fds. * - * Returns 0 if there is no command to process, -errno if an error occured, or - * the number of bytes read. + * Returns 0 if there is no command to process, -errno on error, or the number + * of bytes read. */ int MOCK_DEFINE(get_next_command)(vfu_ctx_t *vfu_ctx, struct vfio_user_header *hdr, @@ -706,10 +706,9 @@ MOCK_DEFINE(get_next_command)(vfu_ctx_t *vfu_ctx, struct vfio_user_header *hdr, { int ret; - /* FIXME get request shouldn't set errno, it should return it as -errno */ ret = vfu_ctx->tran->get_request(vfu_ctx, hdr, fds, nr_fds); if (unlikely(ret < 0)) { - switch (-ret) { + switch (errno) { case EAGAIN: return 0; @@ -722,9 +721,8 @@ MOCK_DEFINE(get_next_command)(vfu_ctx_t *vfu_ctx, struct vfio_user_header *hdr, return -ENOTCONN; default: - vfu_log(vfu_ctx, LOG_ERR, "failed to receive request: %s", - strerror(-ret)); - return ret; + vfu_log(vfu_ctx, LOG_ERR, "failed to receive request: %m"); + return -errno; } } @@ -794,14 +792,16 @@ MOCK_DEFINE(exec_command)(vfu_ctx_t *vfu_ctx, struct vfio_user_header *hdr, if (cmd_data_size > 0) { ret = vfu_ctx->tran->recv_body(vfu_ctx, hdr, &cmd_data); - if (ret == -ENOMSG) { - vfu_reset_ctx(vfu_ctx, "closed"); - return -ENOTCONN; - } else if (ret == -ECONNRESET) { - vfu_reset_ctx(vfu_ctx, "reset"); - return -ENOTCONN; - } else if (ret < 0) { - return ret; + if (ret < 0) { + if (errno == ENOMSG) { + vfu_reset_ctx(vfu_ctx, "closed"); + return -ENOTCONN; + } else if (errno == ECONNRESET) { + vfu_reset_ctx(vfu_ctx, "reset"); + return -ENOTCONN; + } else { + return -errno; + } } } @@ -981,14 +981,16 @@ MOCK_DEFINE(process_request)(vfu_ctx_t *vfu_ctx) fds_out, nr_fds_out, -ret); if (ret < 0) { - vfu_log(vfu_ctx, LOG_ERR, "failed to reply: %s", strerror(-ret)); + vfu_log(vfu_ctx, LOG_ERR, "failed to reply: %m"); - if (ret == -ECONNRESET) { + if (errno == ECONNRESET) { vfu_reset_ctx(vfu_ctx, "reset"); ret = -ENOTCONN; - } else if (ret == -ENOMSG) { + } else if (errno == ENOMSG) { vfu_reset_ctx(vfu_ctx, "closed"); ret = -ENOTCONN; + } else { + ret = -errno; } } } @@ -1239,6 +1241,7 @@ vfu_create_ctx(vfu_trans_t trans, const char *path, int flags, void *pvt, if (vfu_ctx->tran->init != NULL) { err = vfu_ctx->tran->init(vfu_ctx); if (err < 0) { + err = -errno; goto err_out; } } @@ -1564,7 +1567,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 ERROR_INT(ENOMEM); + return -1; } dma_send.addr = (uint64_t)sg->dma_addr; @@ -1573,20 +1576,22 @@ vfu_dma_read(vfu_ctx_t *vfu_ctx, dma_sg_t *sg, void *data) &dma_send, sizeof(dma_send), NULL, dma_recv, recv_size); - if (ret == -ENOMSG) { - vfu_reset_ctx(vfu_ctx, "closed"); - ret = -ENOTCONN; - } else if (ret == -ECONNRESET) { - vfu_reset_ctx(vfu_ctx, "reset"); - ret = -ENOTCONN; - } else if (ret == 0) { + if (ret < 0) { + if (errno == ENOMSG) { + vfu_reset_ctx(vfu_ctx, "closed"); + errno = ENOTCONN; + } else if (errno == ECONNRESET) { + vfu_reset_ctx(vfu_ctx, "reset"); + errno = ENOTCONN; + } + } else { /* FIXME no need for memcpy */ memcpy(data, dma_recv->data, sg->length); } free(dma_recv); - return ret < 0 ? ERROR_INT(-ret) : 0; + return ret; } int @@ -1601,7 +1606,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 ERROR_INT(ENOMEM); + return -1; } dma_send->addr = (uint64_t)sg->dma_addr; dma_send->count = sg->length; @@ -1610,17 +1615,19 @@ vfu_dma_write(vfu_ctx_t *vfu_ctx, dma_sg_t *sg, void *data) dma_send, send_size, NULL, &dma_recv, sizeof(dma_recv)); - if (ret == -ENOMSG) { - vfu_reset_ctx(vfu_ctx, "closed"); - ret = -ENOTCONN; - } else if (ret == -ECONNRESET) { - vfu_reset_ctx(vfu_ctx, "reset"); - ret = -ENOTCONN; + if (ret < 0) { + if (errno == ENOMSG) { + vfu_reset_ctx(vfu_ctx, "closed"); + errno = ENOTCONN; + } else if (errno == ECONNRESET) { + vfu_reset_ctx(vfu_ctx, "reset"); + errno = ENOTCONN; + } } free(dma_send); - return ret < 0 ? ERROR_INT(-ret) : 0; + return ret; } uint64_t diff --git a/lib/tran_sock.c b/lib/tran_sock.c index a1d6a1a..868a5be 100644 --- a/lib/tran_sock.c +++ b/lib/tran_sock.c @@ -121,11 +121,11 @@ MOCK_DEFINE(tran_sock_send_iovec)(int sock, uint16_t msg_id, bool is_reply, if (ret == -1) { /* Treat a failed write due to EPIPE the same as a short write. */ if (errno == EPIPE) { - return -ECONNRESET; + return ERROR_INT(ECONNRESET); } - return -errno; + return -1; } else if ((size_t)ret < hdr.msg_size) { - return -ECONNRESET; + return ERROR_INT(ECONNRESET); } return 0; @@ -177,15 +177,15 @@ get_msg(void *data, size_t len, int *fds, size_t *nr_fds, int sock_fd, ret = recvmsg(sock_fd, &msg, sock_flags); if (ret == -1) { - return -errno; + return -1; } else if (ret == 0) { - return -ENOMSG; + return ERROR_INT(ENOMSG); } else if ((size_t)ret < len) { - return -ECONNRESET; + return ERROR_INT(ECONNRESET); } if (msg.msg_flags & MSG_CTRUNC || msg.msg_flags & MSG_TRUNC) { - return -EFAULT; + return ERROR_INT(EFAULT); } if (nr_fds != NULL) { @@ -194,11 +194,11 @@ get_msg(void *data, size_t len, int *fds, size_t *nr_fds, int sock_fd, continue; } if (cmsg->cmsg_len < CMSG_LEN(sizeof(int))) { - return -EINVAL; + return ERROR_INT(EINVAL); } int size = cmsg->cmsg_len - CMSG_LEN(0); if (size % sizeof(int) != 0) { - return -EINVAL; + return ERROR_INT(EINVAL); } *nr_fds = (int)(size / sizeof(int)); memcpy(fds, CMSG_DATA(cmsg), *nr_fds * sizeof(int)); @@ -234,22 +234,22 @@ tran_sock_recv_fds(int sock, struct vfio_user_header *hdr, bool is_reply, if (is_reply) { if (msg_id != NULL && hdr->msg_id != *msg_id) { - return -EPROTO; + return ERROR_INT(EPROTO); } if (hdr->flags.type != VFIO_USER_F_TYPE_REPLY) { - return -EINVAL; + return ERROR_INT(EINVAL); } if (hdr->flags.error == 1U) { if (hdr->error_no <= 0) { hdr->error_no = EINVAL; } - return -hdr->error_no; + return ERROR_INT(hdr->error_no); } } else { if (hdr->flags.type != VFIO_USER_F_TYPE_COMMAND) { - return -EINVAL; + return ERROR_INT(EINVAL); } if (msg_id != NULL) { *msg_id = hdr->msg_id; @@ -260,11 +260,11 @@ tran_sock_recv_fds(int sock, struct vfio_user_header *hdr, bool is_reply, ret = recv(sock, data, MIN(hdr->msg_size - sizeof(*hdr), *len), MSG_WAITALL); if (ret < 0) { - return -errno; + return -1; } else if (ret == 0) { - return -ENOMSG; + return ERROR_INT(ENOMSG); } else if (*len != (size_t)ret) { - return -ECONNRESET; + return ERROR_INT(ECONNRESET); } *len = ret; } @@ -312,20 +312,20 @@ tran_sock_recv_alloc(int sock, struct vfio_user_header *hdr, bool is_reply, data = calloc(1, len); if (data == NULL) { - return -errno; + return -1; } ret = recv(sock, data, len, MSG_WAITALL); if (ret < 0) { - ret = -errno; + ret = errno; free(data); - return ret; + return ERROR_INT(ret); } else if (ret == 0) { free(data); - return -ENOMSG; + return ERROR_INT(ENOMSG); } else if (len != (size_t)ret) { free(data); - return -ECONNRESET; + return ERROR_INT(ECONNRESET); } *datap = data; @@ -398,7 +398,7 @@ tran_sock_init(vfu_ctx_t *vfu_ctx) ts = calloc(1, sizeof(tran_sock_t)); if (ts == NULL) { - ret = -errno; + ret = errno; goto out; } @@ -406,7 +406,7 @@ tran_sock_init(vfu_ctx_t *vfu_ctx) ts->conn_fd = -1; if ((ts->listen_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { - ret = -errno; + ret = errno; goto out; } @@ -414,29 +414,29 @@ tran_sock_init(vfu_ctx_t *vfu_ctx) ret = fcntl(ts->listen_fd, F_SETFL, fcntl(ts->listen_fd, F_GETFL, 0) | O_NONBLOCK); if (ret < 0) { - ret = -errno; + ret = errno; goto out; } } ret = snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", vfu_ctx->uuid); if (ret >= (int)sizeof(addr.sun_path)) { - ret = -ENAMETOOLONG; - } - if (ret < 0) { + ret = ENAMETOOLONG; + } else if (ret < 0) { + ret = EINVAL; goto out; } /* start listening business */ ret = bind(ts->listen_fd, (struct sockaddr *)&addr, sizeof(addr)); if (ret < 0) { - ret = -errno; + ret = errno; goto out; } ret = listen(ts->listen_fd, 0); if (ret < 0) { - ret = -errno; + ret = errno; } out: @@ -445,7 +445,7 @@ out: close(ts->listen_fd); } free(ts); - return ret; + return ERROR_INT(ret); } vfu_ctx->tran_data = ts; @@ -486,7 +486,7 @@ tran_parse_version_json(const char *json_str, struct json_object *jo_caps = NULL; struct json_object *jo_top = NULL; struct json_object *jo = NULL; - int ret = -EINVAL; + int ret = EINVAL; if ((jo_top = json_tokener_parse(json_str)) == NULL) { goto out; @@ -540,7 +540,10 @@ tran_parse_version_json(const char *json_str, out: /* We just need to put our top-level object. */ json_object_put(jo_top); - return ret; + if (ret != 0) { + return ERROR_INT(ret); + } + return 0; } static int @@ -558,29 +561,28 @@ recv_version(vfu_ctx_t *vfu_ctx, int sock, uint16_t *msg_idp, (void **)&cversion, &vlen); if (ret < 0) { - vfu_log(vfu_ctx, LOG_ERR, "failed to receive version: %s", - strerror(-ret)); + vfu_log(vfu_ctx, LOG_ERR, "failed to receive version: %m"); return ret; } if (hdr.cmd != VFIO_USER_VERSION) { vfu_log(vfu_ctx, LOG_ERR, "msg%#hx: invalid cmd %hu (expected %u)", *msg_idp, hdr.cmd, VFIO_USER_VERSION); - ret = -EINVAL; + ret = EINVAL; goto out; } if (vlen < sizeof(*cversion)) { vfu_log(vfu_ctx, LOG_ERR, "msg%#hx: VFIO_USER_VERSION: invalid size %lu", *msg_idp, vlen); - ret = -EINVAL; + ret = EINVAL; goto out; } if (cversion->major != LIB_VFIO_USER_MAJOR) { vfu_log(vfu_ctx, LOG_ERR, "unsupported client major %hu (must be %u)", cversion->major, LIB_VFIO_USER_MAJOR); - ret = -ENOTSUP; + ret = EINVAL; goto out; } @@ -593,7 +595,7 @@ recv_version(vfu_ctx_t *vfu_ctx, int sock, uint16_t *msg_idp, if (json_str[len - 1] != '\0') { vfu_log(vfu_ctx, LOG_ERR, "ignoring invalid JSON from client"); - ret = -EINVAL; + ret = EINVAL; goto out; } @@ -608,6 +610,7 @@ recv_version(vfu_ctx_t *vfu_ctx, int sock, uint16_t *msg_idp, #else vfu_log(vfu_ctx, LOG_ERR, "failed to parse client JSON"); #endif + ret = errno; goto out; } @@ -617,6 +620,7 @@ recv_version(vfu_ctx_t *vfu_ctx, int sock, uint16_t *msg_idp, if (ret != 0) { vfu_log(vfu_ctx, LOG_ERR, "refusing client page size of %zu", pgsize); + ret = -ret; goto out; } } @@ -626,7 +630,7 @@ recv_version(vfu_ctx_t *vfu_ctx, int sock, uint16_t *msg_idp, vfu_ctx->client_max_fds > VFIO_USER_CLIENT_MAX_FDS_LIMIT) { vfu_log(vfu_ctx, LOG_ERR, "refusing client max_fds of %d", vfu_ctx->client_max_fds); - ret = -EINVAL; + ret = EINVAL; goto out; } } @@ -636,11 +640,12 @@ out: // FIXME: spec, is it OK to just have the header? (void) tran_sock_send_error(sock, *msg_idp, hdr.cmd, ret); free(cversion); - cversion = NULL; + *versionp = NULL; + return ERROR_INT(ret); } *versionp = cversion; - return ret; + return 0; } static int @@ -700,7 +705,7 @@ negotiate(vfu_ctx_t *vfu_ctx, int sock) ret = recv_version(vfu_ctx, sock, &msg_id, &client_version); if (ret < 0) { - vfu_log(vfu_ctx, LOG_ERR, "failed to recv version: %s", strerror(-ret)); + vfu_log(vfu_ctx, LOG_ERR, "failed to recv version: %m"); return ret; } @@ -709,7 +714,7 @@ negotiate(vfu_ctx_t *vfu_ctx, int sock) free(client_version); if (ret < 0) { - vfu_log(vfu_ctx, LOG_ERR, "failed to send version: %s", strerror(-ret)); + vfu_log(vfu_ctx, LOG_ERR, "failed to send version: %m"); } return ret; @@ -729,8 +734,7 @@ tran_sock_attach(vfu_ctx_t *vfu_ctx) if (ts->conn_fd != -1) { vfu_log(vfu_ctx, LOG_ERR, "%s: already attached with fd=%d", __func__, ts->conn_fd); - errno = EINVAL; - return -1; + return ERROR_INT(EINVAL); } ts->conn_fd = accept(ts->listen_fd, NULL, NULL); @@ -740,10 +744,10 @@ tran_sock_attach(vfu_ctx_t *vfu_ctx) ret = negotiate(vfu_ctx, ts->conn_fd); if (ret < 0) { + ret = errno; close(ts->conn_fd); ts->conn_fd = -1; - errno = -ret; - return -1; + return ERROR_INT(ret); } return 0; @@ -763,7 +767,7 @@ tran_sock_get_request(vfu_ctx_t *vfu_ctx, struct vfio_user_header *hdr, if (ts->conn_fd == -1) { vfu_log(vfu_ctx, LOG_ERR, "%s: not connected", __func__); - return -ENOTCONN; + return ERROR_INT(ENOTCONN); } /* @@ -793,7 +797,7 @@ tran_sock_recv_body(vfu_ctx_t *vfu_ctx, const struct vfio_user_header *hdr, if (hdr->msg_size > SERVER_MAX_MSG_SIZE) { vfu_log(vfu_ctx, LOG_ERR, "msg%#hx: size of %u is too large", hdr->msg_id, hdr->msg_size); - return -EINVAL; + return ERROR_INT(EINVAL); } ts = vfu_ctx->tran_data; @@ -803,23 +807,23 @@ tran_sock_recv_body(vfu_ctx_t *vfu_ctx, const struct vfio_user_header *hdr, data = malloc(body_size); if (data == NULL) { - return -errno; + return -1; } ret = recv(ts->conn_fd, data, body_size, 0); if (ret < 0) { - ret = -errno; + ret = errno; free(data); - return ret; + return ERROR_INT(ret); } else if (ret == 0) { free(data); - return -ENOMSG; + return ERROR_INT(ENOMSG); } else if (ret != (int)body_size) { vfu_log(vfu_ctx, LOG_ERR, "msg%#hx: short read: expected=%zu, actual=%d", hdr->msg_id, body_size, ret); free(data); - return -ECONNRESET; + return ERROR_INT(ECONNRESET); } *datap = data; diff --git a/samples/client.c b/samples/client.c index b2008fd..e4e97b0 100644 --- a/samples/client.c +++ b/samples/client.c @@ -141,7 +141,7 @@ recv_version(int sock, int *server_max_fds, size_t *pgsize) (void **)&sversion, &vlen); if (ret < 0) { - errx(EXIT_FAILURE, "failed to receive version: %s", strerror(-ret)); + err(EXIT_FAILURE, "failed to receive version"); } // FIXME: are we out of spec? reply cmd's are zero @@ -183,7 +183,7 @@ recv_version(int sock, int *server_max_fds, size_t *pgsize) ret = tran_parse_version_json(json_str, server_max_fds, pgsize); if (ret < 0) { - errx(EXIT_FAILURE, "failed to parse server JSON \"%s\"", json_str); + err(EXIT_FAILURE, "failed to parse server JSON \"%s\"", json_str); } } @@ -203,7 +203,7 @@ send_device_reset(int sock) int ret = tran_sock_msg(sock, 1, VFIO_USER_DEVICE_RESET, NULL, 0, NULL, NULL, 0); if (ret < 0) { - errx(EXIT_FAILURE, "failed to reset device: %s", strerror(-ret)); + err(EXIT_FAILURE, "failed to reset device"); } } @@ -257,8 +257,7 @@ do_get_device_region_info(int sock, struct vfio_region_info *region_info, region_info, region_info->argsz, NULL, region_info, region_info->argsz, fds, nr_fds); if (ret < 0) { - errx(EXIT_FAILURE, "failed to get device region info: %s", - strerror(-ret)); + err(EXIT_FAILURE, "failed to get device region info"); } } @@ -373,7 +372,7 @@ get_device_info(int sock, struct vfio_device_info *dev_info) dev_info, sizeof(*dev_info)); if (ret < 0) { - errx(EXIT_FAILURE, "failed to get device info: %s", strerror(-ret)); + err(EXIT_FAILURE, "failed to get device info"); } if (dev_info->num_regions != 10) { @@ -405,8 +404,7 @@ configure_irqs(int sock) NULL, &vfio_irq_info, sizeof(vfio_irq_info)); if (ret < 0) { - errx(EXIT_FAILURE, "failed to get %s info: %s", irq_to_str[i], - strerror(-ret)); + err(EXIT_FAILURE, "failed to get %s info", irq_to_str[i]); } if (vfio_irq_info.count > 0) { printf("client: IRQ %s: count=%d flags=%#x\n", @@ -436,8 +434,7 @@ configure_irqs(int sock) NULL, NULL, 0, NULL, 0); if (ret < 0) { - errx(EXIT_FAILURE, "failed to send configure IRQs message: %s", - strerror(-ret)); + err(EXIT_FAILURE, "failed to send configure IRQs message"); } return irq_fd; @@ -491,9 +488,9 @@ access_region(int sock, int region, bool is_write, uint64_t offset, recv_data, recv_data_len, NULL, 0); pthread_mutex_unlock(&mutex); if (ret != 0) { - warnx("failed to %s region %d %#lx-%#lx: %s", + warn("failed to %s region %d %#lx-%#lx", is_write ? "write to" : "read from", region, offset, - offset + data_len - 1, strerror(-ret)); + offset + data_len - 1); free(recv_data); return ret; } @@ -502,7 +499,8 @@ access_region(int sock, int region, bool is_write, uint64_t offset, is_write ? "write" : "read", data_len, recv_data->count); free(recv_data); - return -EINVAL; + errno = EINVAL; + return -1; } /* @@ -525,14 +523,14 @@ access_bar0(int sock, time_t *t) ret = access_region(sock, VFU_PCI_DEV_BAR0_REGION_IDX, true, 0, t, sizeof(*t)); if (ret < 0) { - errx(EXIT_FAILURE, "failed to write to BAR0: %s", strerror(-ret)); + err(EXIT_FAILURE, "failed to write to BAR0"); } printf("client: wrote to BAR0: %ld\n", *t); ret = access_region(sock, VFU_PCI_DEV_BAR0_REGION_IDX, false, 0, t, sizeof(*t)); if (ret < 0) { - errx(EXIT_FAILURE, "failed to read from BAR0: %s", strerror(-ret)); + err(EXIT_FAILURE, "failed to read from BAR0"); } printf("client: read from BAR0: %ld\n", *t); @@ -562,7 +560,7 @@ handle_dma_write(int sock, struct vfio_user_dma_region *dma_regions, ret = tran_sock_recv(sock, &hdr, false, &msg_id, &dma_access, &size); if (ret < 0) { - errx(EXIT_FAILURE, "failed to receive DMA read: %s", strerror(-ret)); + err(EXIT_FAILURE, "failed to receive DMA read"); } data = calloc(dma_access.count, 1); @@ -571,7 +569,7 @@ handle_dma_write(int sock, struct vfio_user_dma_region *dma_regions, } if (recv(sock, data, dma_access.count, 0) == -1) { - err(EXIT_FAILURE, "failed to recieve DMA read data"); + err(EXIT_FAILURE, "failed to receive DMA read data"); } for (i = 0; i < nr_dma_regions; i++) { @@ -593,8 +591,7 @@ handle_dma_write(int sock, struct vfio_user_dma_region *dma_regions, ret = tran_sock_send(sock, msg_id, true, VFIO_USER_DMA_WRITE, &dma_access, sizeof(dma_access)); if (ret < 0) { - errx(EXIT_FAILURE, "failed to send reply of DMA write: %s", - strerror(-ret)); + err(EXIT_FAILURE, "failed to send reply of DMA write"); } free(data); } @@ -612,7 +609,7 @@ handle_dma_read(int sock, struct vfio_user_dma_region *dma_regions, ret = tran_sock_recv(sock, &hdr, false, &msg_id, &dma_access, &size); if (ret < 0) { - errx(EXIT_FAILURE, "failed to recieve DMA read"); + err(EXIT_FAILURE, "failed to receive DMA read"); } response_sz = sizeof(dma_access) + dma_access.count; @@ -637,8 +634,7 @@ handle_dma_read(int sock, struct vfio_user_dma_region *dma_regions, ret = tran_sock_send(sock, msg_id, true, VFIO_USER_DMA_READ, response, response_sz); if (ret < 0) { - errx(EXIT_FAILURE, "failed to send reply of DMA write: %s", - strerror(-ret)); + err(EXIT_FAILURE, "failed to send reply of DMA write"); } free(response); } @@ -692,8 +688,7 @@ get_dirty_bitmaps(int sock, struct vfio_user_dma_region *dma_regions, NULL, 0, &hdr, data, ARRAY_SIZE(data), NULL, 0); if (ret != 0) { - errx(EXIT_FAILURE, "failed to start dirty page logging: %s", - strerror(-ret)); + err(EXIT_FAILURE, "failed to start dirty page logging"); } for (i = 0; i < ARRAY_SIZE(bitmaps); i++) { @@ -744,8 +739,7 @@ do_migrate(int sock, int migr_reg_index, size_t nr_iters, offsetof(struct vfio_device_migration_info, pending_bytes), &pending_bytes, sizeof(pending_bytes)); if (ret < 0) { - errx(EXIT_FAILURE, "failed to read pending_bytes: %s", - strerror(-ret)); + err(EXIT_FAILURE, "failed to read pending_bytes"); } for (i = 0; i < nr_iters && pending_bytes > 0; i++) { @@ -755,16 +749,14 @@ do_migrate(int sock, int migr_reg_index, size_t nr_iters, offsetof(struct vfio_device_migration_info, data_offset), &data_offset, sizeof(data_offset)); if (ret < 0) { - errx(EXIT_FAILURE, "failed to read data_offset: %s", - strerror(-ret)); + err(EXIT_FAILURE, "failed to read data_offset"); } ret = access_region(sock, migr_reg_index, false, offsetof(struct vfio_device_migration_info, data_size), &data_size, sizeof(data_size)); if (ret < 0) { - errx(EXIT_FAILURE, "failed to read data_size: %s", - strerror(-ret)); + err(EXIT_FAILURE, "failed to read data_size"); } migr_iter[i].iov_len = data_size; @@ -777,8 +769,7 @@ do_migrate(int sock, int migr_reg_index, size_t nr_iters, ret = access_region(sock, migr_reg_index, false, data_offset, (char*)migr_iter[i].iov_base, data_size); if (ret < 0) { - errx(EXIT_FAILURE, "failed to read migration data: %s", - strerror(-ret)); + err(EXIT_FAILURE, "failed to read migration data"); } /* FIXME send migration data to the destination client process */ @@ -791,8 +782,7 @@ do_migrate(int sock, int migr_reg_index, size_t nr_iters, offsetof(struct vfio_device_migration_info, pending_bytes), &pending_bytes, sizeof(pending_bytes)); if (ret < 0) { - errx(EXIT_FAILURE, "failed to read pending_bytes: %s", - strerror(-ret)); + err(EXIT_FAILURE, "failed to read pending_bytes"); } } return i; @@ -875,8 +865,7 @@ migrate_from(int sock, int migr_reg_index, size_t *nr_iters, offsetof(struct vfio_device_migration_info, device_state), &device_state, sizeof(device_state)); if (ret < 0) { - errx(EXIT_FAILURE, "failed to write to device state: %s", - strerror(-ret)); + err(EXIT_FAILURE, "failed to write to device state"); } _nr_iters = do_migrate(sock, migr_reg_index, 1, *migr_iters); @@ -897,8 +886,7 @@ migrate_from(int sock, int migr_reg_index, size_t *nr_iters, offsetof(struct vfio_device_migration_info, device_state), &device_state, sizeof(device_state)); if (ret < 0) { - errx(EXIT_FAILURE, "failed to write to device state: %s", - strerror(-ret)); + err(EXIT_FAILURE, "failed to write to device state"); } _nr_iters += do_migrate(sock, migr_reg_index, 1, (*migr_iters) + _nr_iters); @@ -914,8 +902,7 @@ migrate_from(int sock, int migr_reg_index, size_t *nr_iters, offsetof(struct vfio_device_migration_info, device_state), &device_state, sizeof(device_state)); if (ret < 0) { - errx(EXIT_FAILURE, "failed to write to device state: %s", - strerror(-ret)); + err(EXIT_FAILURE, "failed to write to device state"); } return _nr_iters; @@ -987,8 +974,7 @@ migrate_to(char *old_sock_path, int *server_max_fds, offsetof(struct vfio_device_migration_info, device_state), &device_state, sizeof(device_state)); if (ret < 0) { - errx(EXIT_FAILURE, "failed to set device state to resuming: %s", - strerror(-ret)); + err(EXIT_FAILURE, "failed to set device state to resuming"); } for (i = 0; i < nr_iters; i++) { @@ -998,8 +984,7 @@ migrate_to(char *old_sock_path, int *server_max_fds, offsetof(struct vfio_device_migration_info, data_offset), &data_offset, sizeof(data_offset)); if (ret < 0) { - errx(EXIT_FAILURE, "failed to read migration data offset: %s", - strerror(-ret)); + err(EXIT_FAILURE, "failed to read migration data offset"); } /* XXX write migration data */ @@ -1014,8 +999,7 @@ migrate_to(char *old_sock_path, int *server_max_fds, data_offset, migr_iters[i].iov_base, migr_iters[i].iov_len); if (ret < 0) { - errx(EXIT_FAILURE, "failed to write device migration data: %s", - strerror(-ret)); + err(EXIT_FAILURE, "failed to write device migration data"); } /* XXX write data_size */ @@ -1024,8 +1008,7 @@ migrate_to(char *old_sock_path, int *server_max_fds, offsetof(struct vfio_device_migration_info, data_size), &data_len, sizeof(data_len)); if (ret < 0) { - errx(EXIT_FAILURE, "failed to write migration data size: %s", - strerror(-ret)); + err(EXIT_FAILURE, "failed to write migration data size"); } } @@ -1035,8 +1018,7 @@ migrate_to(char *old_sock_path, int *server_max_fds, offsetof(struct vfio_device_migration_info, device_state), &device_state, sizeof(device_state)); if (ret < 0) { - errx(EXIT_FAILURE, "failed to set device state to running: %s", - strerror(-ret)); + err(EXIT_FAILURE, "failed to set device state to running"); } /* validate contents of BAR1 */ @@ -1083,7 +1065,7 @@ map_dma_regions(int sock, int max_fds, struct vfio_user_dma_region *dma_regions, dma_region_fds + (i * max_fds), max_fds, NULL, NULL, 0, NULL, 0); if (ret < 0) { - errx(EXIT_FAILURE, "failed to map DMA regions: %s", strerror(-ret)); + err(EXIT_FAILURE, "failed to map DMA regions"); } } } @@ -1137,10 +1119,9 @@ int main(int argc, char *argv[]) /* try to access a bogus region, we should get an error */ ret = access_region(sock, 0xdeadbeef, false, 0, &ret, sizeof(ret)); - if (ret != -EINVAL) { + if (ret != -1 || errno != EINVAL) { errx(EXIT_FAILURE, - "expected -EINVAL accessing bogus region, got %d instead", - ret); + "expected EINVAL accessing bogus region, got %d instead", errno); } /* XXX VFIO_USER_DEVICE_GET_INFO */ @@ -1155,8 +1136,7 @@ int main(int argc, char *argv[]) ret = access_region(sock, VFU_PCI_DEV_CFG_REGION_IDX, false, 0, &config_space, sizeof(config_space)); if (ret < 0) { - errx(EXIT_FAILURE, "failed to read PCI configuration space: %s", - strerror(-ret)); + err(EXIT_FAILURE, "failed to read PCI configuration space"); } assert(config_space.id.vid == 0xdead); @@ -1211,8 +1191,7 @@ int main(int argc, char *argv[]) &dirty_bitmap, sizeof(dirty_bitmap), NULL, NULL, 0); if (ret != 0) { - errx(EXIT_FAILURE, "failed to start dirty page logging: %s", - strerror(-ret)); + err(EXIT_FAILURE, "failed to start dirty page logging"); } /* @@ -1238,8 +1217,7 @@ int main(int argc, char *argv[]) &dirty_bitmap, sizeof(dirty_bitmap), NULL, NULL, 0); if (ret != 0) { - errx(EXIT_FAILURE, "failed to stop dirty page logging: %s", - strerror(-ret)); + err(EXIT_FAILURE, "failed to stop dirty page logging"); } /* BAR1 can be memory mapped and read directly */ @@ -1253,7 +1231,7 @@ int main(int argc, char *argv[]) dma_regions, sizeof(*dma_regions) * server_max_fds, NULL, NULL, 0); if (ret < 0) { - errx(EXIT_FAILURE, "failed to unmap DMA regions: %s", strerror(-ret)); + err(EXIT_FAILURE, "failed to unmap DMA regions"); } /* @@ -1265,7 +1243,7 @@ int main(int argc, char *argv[]) t = time(NULL) + 3; ret = access_region(sock, VFU_PCI_DEV_BAR0_REGION_IDX, true, 0, &t, sizeof(t)); if (ret < 0) { - errx(EXIT_FAILURE, "failed to write to BAR0: %s", strerror(-ret)); + err(EXIT_FAILURE, "failed to write to BAR0"); } nr_iters = migrate_from(sock, migr_reg_index, &nr_iters, &migr_iters, diff --git a/samples/gpio-pci-idio-16.c b/samples/gpio-pci-idio-16.c index 86ea083..138a861 100644 --- a/samples/gpio-pci-idio-16.c +++ b/samples/gpio-pci-idio-16.c @@ -44,7 +44,6 @@ #include "common.h" #include "libvfio-user.h" -#include "tran_sock.h" static void _log(vfu_ctx_t *vfu_ctx UNUSED, UNUSED int level, char const *msg) diff --git a/samples/null.c b/samples/null.c index d05d0fc..045710b 100644 --- a/samples/null.c +++ b/samples/null.c @@ -40,7 +40,6 @@ #include "common.h" #include "libvfio-user.h" -#include "tran_sock.h" static void null_log(vfu_ctx_t *vfu_ctx UNUSED, UNUSED int level, char const *msg) diff --git a/test/mocks.c b/test/mocks.c index 7121826..3361c5a 100644 --- a/test/mocks.c +++ b/test/mocks.c @@ -39,11 +39,11 @@ #include <cmocka.h> -#include "mocks.h" #include "dma.h" #include "migration.h" -#include "../lib/private.h" -#include "../lib/tran_sock.h" +#include "mocks.h" +#include "private.h" +#include "tran_sock.h" struct function { diff --git a/test/unit-tests.c b/test/unit-tests.c index cf7c7a8..ef93ccb 100644 --- a/test/unit-tests.c +++ b/test/unit-tests.c @@ -1648,6 +1648,7 @@ recv_body(UNUSED vfu_ctx_t *vfu_ctx, UNUSED const struct vfio_user_header *hdr, UNUSED void **datap) { /* hack to avoid having to refactor the rest of exec_command */ + errno = ENOBUFS; return -1; } @@ -1685,7 +1686,7 @@ test_exec_command(UNUSED void **state) expect_value(should_exec_command, cmd, 0xbeef); r = exec_command(&vfu_ctx, &hdr, size, &fds, 0, NULL, NULL, &_iovecs, &iovecs, &nr_iovecs, &free_iovec_data); - assert_int_equal(-1, r); + assert_int_equal(-ENOBUFS, r); } static void |