From: John Levon <john.levon@nutanix.com>
To: qemu-devel@nongnu.org
Cc: "John Levon" <john.levon@nutanix.com>,
"Thanos Makatos" <thanos.makatos@nutanix.com>,
"Cédric Le Goater" <clg@redhat.com>,
"Peter Maydell" <peter.maydell@linaro.org>
Subject: [PATCH v2 4/5] vfio-user: simplify vfio_user_recv_one()
Date: Mon, 17 Nov 2025 21:26:55 +0530 [thread overview]
Message-ID: <20251117155656.2060336-5-john.levon@nutanix.com> (raw)
In-Reply-To: <20251117155656.2060336-1-john.levon@nutanix.com>
This function was unnecessarily difficult to understand due to the
separate handling of request and reply messages. Use common code for
both where we can.
Signed-off-by: John Levon <john.levon@nutanix.com>
---
hw/vfio-user/proxy.c | 68 +++++++++++++++++++-------------------------
1 file changed, 30 insertions(+), 38 deletions(-)
diff --git a/hw/vfio-user/proxy.c b/hw/vfio-user/proxy.c
index 87e50501af..aa5b971fb6 100644
--- a/hw/vfio-user/proxy.c
+++ b/hw/vfio-user/proxy.c
@@ -281,15 +281,14 @@ static int vfio_user_recv_hdr(VFIOUserProxy *proxy, Error **errp,
*/
static int vfio_user_recv_one(VFIOUserProxy *proxy, Error **errp)
{
- VFIOUserMsg *msg = NULL;
g_autofree int *fdp = NULL;
- VFIOUserFDs *reqfds;
- VFIOUserHdr hdr;
+ VFIOUserMsg *msg = NULL;
bool isreply = false;
- int i, ret;
- size_t msgleft, numfds = 0;
+ size_t msgleft = 0;
+ size_t numfds = 0;
char *data = NULL;
- char *buf = NULL;
+ VFIOUserHdr hdr;
+ int i, ret;
/*
* Complete any partial reads
@@ -317,8 +316,8 @@ static int vfio_user_recv_one(VFIOUserProxy *proxy, Error **errp)
}
/*
- * For replies, find the matching pending request.
- * For requests, reap incoming FDs.
+ * Find the matching request if this is a reply, or initialize a new
+ * server->client request.
*/
if (isreply) {
QTAILQ_FOREACH(msg, &proxy->pending, next) {
@@ -332,51 +331,44 @@ static int vfio_user_recv_one(VFIOUserProxy *proxy, Error **errp)
}
QTAILQ_REMOVE(&proxy->pending, msg, next);
- /*
- * Process any received FDs
- */
- if (numfds != 0) {
- if (msg->fds == NULL || msg->fds->recv_fds < numfds) {
- error_setg(errp, "unexpected FDs");
- goto err;
- }
- msg->fds->recv_fds = numfds;
- memcpy(msg->fds->fds, fdp, numfds * sizeof(int));
- }
- } else {
- if (numfds != 0) {
- reqfds = vfio_user_getfds(numfds);
- memcpy(reqfds->fds, fdp, numfds * sizeof(int));
- } else {
- reqfds = NULL;
- }
- }
-
- /*
- * Put the whole message into a single buffer.
- */
- if (isreply) {
if (hdr.size > msg->rsize) {
error_setg(errp, "reply larger than recv buffer");
goto err;
}
- *msg->hdr = hdr;
- data = (char *)msg->hdr + sizeof(hdr);
} else {
+ void *buf;
+
if (hdr.size > proxy->max_xfer_size + sizeof(VFIOUserDMARW)) {
error_setg(errp, "vfio_user_recv request larger than max");
goto err;
}
+
buf = g_malloc0(hdr.size);
- memcpy(buf, &hdr, sizeof(hdr));
- data = buf + sizeof(hdr);
- msg = vfio_user_getmsg(proxy, (VFIOUserHdr *)buf, reqfds);
+ msg = vfio_user_getmsg(proxy, buf, NULL);
msg->type = VFIO_MSG_REQ;
}
+ *msg->hdr = hdr;
+ data = (char *)msg->hdr + sizeof(hdr);
+
+ if (numfds != 0) {
+ if (msg->type == VFIO_MSG_REQ) {
+ msg->fds = vfio_user_getfds(numfds);
+ } else {
+ if (msg->fds == NULL || msg->fds->recv_fds < numfds) {
+ error_setg(errp, "unexpected FDs");
+ goto err;
+ }
+ msg->fds->recv_fds = numfds;
+ }
+
+ memcpy(msg->fds->fds, fdp, numfds * sizeof(int));
+ }
+
/*
- * Read rest of message.
+ * Read rest of message into the data buffer.
*/
+
msgleft = hdr.size - sizeof(hdr);
while (msgleft > 0) {
ret = qio_channel_read(proxy->ioc, data, msgleft, errp);
--
2.43.0
next prev parent reply other threads:[~2025-11-17 15:57 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-17 15:56 [PATCH v2 0/5] vfio-user coverity fixes John Levon
2025-11-17 15:56 ` [PATCH v2 1/5] vfio-user: simplify vfio_user_process() John Levon
2025-11-18 14:15 ` Cédric Le Goater
2025-11-17 15:56 ` [PATCH v2 2/5] vfio-user: clarify partial message handling John Levon
2025-11-18 14:15 ` Cédric Le Goater
2025-11-17 15:56 ` [PATCH v2 3/5] vfio-user: refactor out header handling John Levon
2025-11-18 14:38 ` Cédric Le Goater
2025-11-17 15:56 ` John Levon [this message]
2025-11-18 14:57 ` [PATCH v2 4/5] vfio-user: simplify vfio_user_recv_one() Cédric Le Goater
2025-11-18 15:03 ` John Levon
2025-11-17 15:56 ` [PATCH v2 5/5] vfio-user: recycle msg on failure John Levon
2025-11-17 15:58 ` John Levon
2025-11-18 15:18 ` Philippe Mathieu-Daudé
2025-11-18 17:43 ` John Levon
2025-11-19 13:45 ` John Levon
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251117155656.2060336-5-john.levon@nutanix.com \
--to=john.levon@nutanix.com \
--cc=clg@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=thanos.makatos@nutanix.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).