[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 18/27] nbd: Let client skip portions of server reply
From: |
Paolo Bonzini |
Subject: |
[Qemu-devel] [PULL 18/27] nbd: Let client skip portions of server reply |
Date: |
Mon, 31 Oct 2016 15:37:34 +0100 |
From: Eric Blake <address@hidden>
The server has a nice helper function nbd_negotiate_drop_sync()
which lets it easily ignore fluff from the client (such as the
payload to an unknown option request). We can't quite make it
common, since it depends on nbd_negotiate_read() which handles
coroutine magic, but we can copy the idea into the client where
we have places where we want to ignore data (such as the
description tacked on the end of NBD_REP_SERVER).
Signed-off-by: Eric Blake <address@hidden>
Message-Id: <address@hidden>
Signed-off-by: Paolo Bonzini <address@hidden>
---
nbd/client.c | 47 +++++++++++++++++++++++++++++++++--------------
1 file changed, 33 insertions(+), 14 deletions(-)
diff --git a/nbd/client.c b/nbd/client.c
index 651e08c..5d94e34 100644
--- a/nbd/client.c
+++ b/nbd/client.c
@@ -75,6 +75,32 @@ static QTAILQ_HEAD(, NBDExport) exports =
QTAILQ_HEAD_INITIALIZER(exports);
*/
+/* Discard length bytes from channel. Return -errno on failure, or
+ * the amount of bytes consumed. */
+static ssize_t drop_sync(QIOChannel *ioc, size_t size)
+{
+ ssize_t ret, dropped = size;
+ char small[1024];
+ char *buffer;
+
+ buffer = sizeof(small) < size ? small : g_malloc(MIN(65536, size));
+ while (size > 0) {
+ ret = read_sync(ioc, buffer, MIN(65536, size));
+ if (ret < 0) {
+ goto cleanup;
+ }
+ assert(ret <= size);
+ size -= ret;
+ }
+ ret = dropped;
+
+ cleanup:
+ if (buffer != small) {
+ g_free(buffer);
+ }
+ return ret;
+}
+
/* Send an option request.
*
* The request is for option @opt, with @data containing @len bytes of
@@ -285,19 +311,12 @@ static int nbd_receive_list(QIOChannel *ioc, char **name,
Error **errp)
}
(*name)[namelen] = '\0';
len -= namelen;
- if (len) {
- char *buf = g_malloc(len + 1);
- if (read_sync(ioc, buf, len) != len) {
- error_setg(errp, "failed to read export description");
- g_free(*name);
- g_free(buf);
- *name = NULL;
- nbd_send_opt_abort(ioc);
- return -1;
- }
- buf[len] = '\0';
- TRACE("Ignoring export description: %s", buf);
- g_free(buf);
+ if (drop_sync(ioc, len) != len) {
+ error_setg(errp, "failed to read export description");
+ g_free(*name);
+ *name = NULL;
+ nbd_send_opt_abort(ioc);
+ return -1;
}
} else {
error_setg(errp, "Unexpected reply type %" PRIx32 " expected %x",
@@ -577,7 +596,7 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char
*name, uint16_t *flags,
}
TRACE("Size is %" PRIu64 ", export flags %" PRIx16, *size, *flags);
- if (read_sync(ioc, &buf, 124) != 124) {
+ if (drop_sync(ioc, 124) != 124) {
error_setg(errp, "Failed to read reserved block");
goto fail;
}
--
2.7.4
- [Qemu-devel] [PULL 07/27] exec.c: check memory backend file size with 'size' option, (continued)
- [Qemu-devel] [PULL 07/27] exec.c: check memory backend file size with 'size' option, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 11/27] nbd: Rename NBDRequest to NBDRequestData, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 10/27] nbd: Treat flags vs. command type as separate fields, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 12/27] nbd: Rename NbdClientSession to NBDClientSession, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 14/27] nbd: Share common reply-sending code in server, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 13/27] nbd: Rename struct nbd_request and nbd_reply, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 15/27] nbd: Send message along with server NBD_REP_ERR errors, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 17/27] nbd: Let server know when client gives up negotiation, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 19/27] nbd: Less allocation during NBD_OPT_LIST, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 21/27] nbd: Refactor conversion to errno to silence checkpatch, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 18/27] nbd: Let client skip portions of server reply,
Paolo Bonzini <=
- [Qemu-devel] [PULL 20/27] nbd: Support shorter handshake, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 22/27] nbd: Improve server handling of shutdown requests, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 23/27] nbd: Implement NBD_CMD_WRITE_ZEROES on server, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 16/27] nbd: Share common option-sending code in client, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 24/27] nbd: Implement NBD_CMD_WRITE_ZEROES on client, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 25/27] qemu-char: do not forward events through the mux until QEMU has started, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 26/27] slirp: fix CharDriver breakage, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 27/27] x86: add AVX512_4VNNIW and AVX512_4FMAPS features, Paolo Bonzini, 2016/10/31
- Re: [Qemu-devel] [PULL 00/27] Misc patches for 2016-10-31, Peter Maydell, 2016/10/31