[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 14/27] nbd: Share common reply-sending code in server
From: |
Paolo Bonzini |
Subject: |
[Qemu-devel] [PULL 14/27] nbd: Share common reply-sending code in server |
Date: |
Mon, 31 Oct 2016 15:37:30 +0100 |
From: Eric Blake <address@hidden>
Rather than open-coding NBD_REP_SERVER, reuse the code we
already have by adding a length parameter. Additionally,
the refactoring will make adding NBD_OPT_GO in a later patch
easier.
Signed-off-by: Eric Blake <address@hidden>
Message-Id: <address@hidden>
Signed-off-by: Paolo Bonzini <address@hidden>
---
nbd/server.c | 52 +++++++++++++++++++++++++++-------------------------
1 file changed, 27 insertions(+), 25 deletions(-)
diff --git a/nbd/server.c b/nbd/server.c
index badc2d1..0f0c68c 100644
--- a/nbd/server.c
+++ b/nbd/server.c
@@ -196,12 +196,15 @@ static ssize_t nbd_negotiate_drop_sync(QIOChannel *ioc,
size_t size)
*/
-static int nbd_negotiate_send_rep(QIOChannel *ioc, uint32_t type, uint32_t opt)
+/* Send a reply header, including length, but no payload.
+ * Return -errno on error, 0 on success. */
+static int nbd_negotiate_send_rep_len(QIOChannel *ioc, uint32_t type,
+ uint32_t opt, uint32_t len)
{
uint64_t magic;
- uint32_t len;
- TRACE("Reply opt=%" PRIx32 " type=%" PRIx32, type, opt);
+ TRACE("Reply opt=%" PRIx32 " type=%" PRIx32 " len=%" PRIu32,
+ type, opt, len);
magic = cpu_to_be64(NBD_REP_MAGIC);
if (nbd_negotiate_write(ioc, &magic, sizeof(magic)) != sizeof(magic)) {
@@ -218,7 +221,7 @@ static int nbd_negotiate_send_rep(QIOChannel *ioc, uint32_t
type, uint32_t opt)
LOG("write failed (rep type)");
return -EINVAL;
}
- len = cpu_to_be32(0);
+ len = cpu_to_be32(len);
if (nbd_negotiate_write(ioc, &len, sizeof(len)) != sizeof(len)) {
LOG("write failed (rep data length)");
return -EINVAL;
@@ -226,37 +229,32 @@ static int nbd_negotiate_send_rep(QIOChannel *ioc,
uint32_t type, uint32_t opt)
return 0;
}
+/* Send a reply header with default 0 length.
+ * Return -errno on error, 0 on success. */
+static int nbd_negotiate_send_rep(QIOChannel *ioc, uint32_t type, uint32_t opt)
+{
+ return nbd_negotiate_send_rep_len(ioc, type, opt, 0);
+}
+
+/* Send a single NBD_REP_SERVER reply to NBD_OPT_LIST, including payload.
+ * Return -errno on error, 0 on success. */
static int nbd_negotiate_send_rep_list(QIOChannel *ioc, NBDExport *exp)
{
- uint64_t magic;
size_t name_len, desc_len;
- uint32_t opt, type, len;
+ uint32_t len;
const char *name = exp->name ? exp->name : "";
const char *desc = exp->description ? exp->description : "";
+ int rc;
TRACE("Advertising export name '%s' description '%s'", name, desc);
name_len = strlen(name);
desc_len = strlen(desc);
- magic = cpu_to_be64(NBD_REP_MAGIC);
- if (nbd_negotiate_write(ioc, &magic, sizeof(magic)) != sizeof(magic)) {
- LOG("write failed (magic)");
- return -EINVAL;
- }
- opt = cpu_to_be32(NBD_OPT_LIST);
- if (nbd_negotiate_write(ioc, &opt, sizeof(opt)) != sizeof(opt)) {
- LOG("write failed (opt)");
- return -EINVAL;
- }
- type = cpu_to_be32(NBD_REP_SERVER);
- if (nbd_negotiate_write(ioc, &type, sizeof(type)) != sizeof(type)) {
- LOG("write failed (reply type)");
- return -EINVAL;
- }
- len = cpu_to_be32(name_len + desc_len + sizeof(len));
- if (nbd_negotiate_write(ioc, &len, sizeof(len)) != sizeof(len)) {
- LOG("write failed (length)");
- return -EINVAL;
+ len = name_len + desc_len + sizeof(len);
+ rc = nbd_negotiate_send_rep_len(ioc, NBD_REP_SERVER, NBD_OPT_LIST, len);
+ if (rc < 0) {
+ return rc;
}
+
len = cpu_to_be32(name_len);
if (nbd_negotiate_write(ioc, &len, sizeof(len)) != sizeof(len)) {
LOG("write failed (name length)");
@@ -273,6 +271,8 @@ static int nbd_negotiate_send_rep_list(QIOChannel *ioc,
NBDExport *exp)
return 0;
}
+/* Process the NBD_OPT_LIST command, with a potential series of replies.
+ * Return -errno on error, 0 on success. */
static int nbd_negotiate_handle_list(NBDClient *client, uint32_t length)
{
NBDExport *exp;
@@ -382,6 +382,8 @@ static QIOChannel *nbd_negotiate_handle_starttls(NBDClient
*client,
}
+/* Process all NBD_OPT_* client option commands.
+ * Return -errno on error, 0 on success. */
static int nbd_negotiate_options(NBDClient *client)
{
uint32_t flags;
--
2.7.4
- [Qemu-devel] [PULL 02/27] nbd: Use CoQueue for free_sema instead of CoMutex, (continued)
- [Qemu-devel] [PULL 02/27] nbd: Use CoQueue for free_sema instead of CoMutex, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 04/27] tests: send error_report to test log, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 06/27] exec.c: do not truncate non-empty memory backend file, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 05/27] exec.c: ensure all AddressSpaceDispatch updates under RCU, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 08/27] hostmem-file: make option 'size' optional, Paolo Bonzini, 2016/10/31
- [Qemu-devel] [PULL 09/27] nbd: Add qemu-nbd -D for human-readable description, Paolo Bonzini, 2016/10/31
- [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 <=
- [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, 2016/10/31
- [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