[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 14/30] nbd: Send message along with server NBD_REP_ER
From: |
Paolo Bonzini |
Subject: |
[Qemu-devel] [PULL 14/30] nbd: Send message along with server NBD_REP_ERR errors |
Date: |
Tue, 1 Nov 2016 17:29:27 +0100 |
From: Eric Blake <address@hidden>
The NBD Protocol allows us to send human-readable messages
along with any NBD_REP_ERR error during option negotiation;
make use of this fact for clients that know what to do with
our message.
Signed-off-by: Eric Blake <address@hidden>
Message-Id: <address@hidden>
Signed-off-by: Paolo Bonzini <address@hidden>
---
nbd/server.c | 78 +++++++++++++++++++++++++++++++++++++++++++++---------------
1 file changed, 59 insertions(+), 19 deletions(-)
diff --git a/nbd/server.c b/nbd/server.c
index 0f0c68c..fa01e49 100644
--- a/nbd/server.c
+++ b/nbd/server.c
@@ -236,6 +236,38 @@ 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 an error reply.
+ * Return -errno on error, 0 on success. */
+static int GCC_FMT_ATTR(4, 5)
+nbd_negotiate_send_rep_err(QIOChannel *ioc, uint32_t type,
+ uint32_t opt, const char *fmt, ...)
+{
+ va_list va;
+ char *msg;
+ int ret;
+ size_t len;
+
+ va_start(va, fmt);
+ msg = g_strdup_vprintf(fmt, va);
+ va_end(va);
+ len = strlen(msg);
+ assert(len < 4096);
+ TRACE("sending error message \"%s\"", msg);
+ ret = nbd_negotiate_send_rep_len(ioc, type, opt, len);
+ if (ret < 0) {
+ goto out;
+ }
+ if (nbd_negotiate_write(ioc, msg, len) != len) {
+ LOG("write failed (error message)");
+ ret = -EIO;
+ } else {
+ ret = 0;
+ }
+out:
+ g_free(msg);
+ return ret;
+}
+
/* 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)
@@ -281,8 +313,9 @@ static int nbd_negotiate_handle_list(NBDClient *client,
uint32_t length)
if (nbd_negotiate_drop_sync(client->ioc, length) != length) {
return -EIO;
}
- return nbd_negotiate_send_rep(client->ioc,
- NBD_REP_ERR_INVALID, NBD_OPT_LIST);
+ return nbd_negotiate_send_rep_err(client->ioc,
+ NBD_REP_ERR_INVALID, NBD_OPT_LIST,
+ "OPT_LIST should not have length");
}
/* For each export, send a NBD_REP_SERVER reply. */
@@ -329,7 +362,8 @@ fail:
return rc;
}
-
+/* Handle NBD_OPT_STARTTLS. Return NULL to drop connection, or else the
+ * new channel for all further (now-encrypted) communication. */
static QIOChannel *nbd_negotiate_handle_starttls(NBDClient *client,
uint32_t length)
{
@@ -343,7 +377,8 @@ static QIOChannel *nbd_negotiate_handle_starttls(NBDClient
*client,
if (nbd_negotiate_drop_sync(ioc, length) != length) {
return NULL;
}
- nbd_negotiate_send_rep(ioc, NBD_REP_ERR_INVALID, NBD_OPT_STARTTLS);
+ nbd_negotiate_send_rep_err(ioc, NBD_REP_ERR_INVALID, NBD_OPT_STARTTLS,
+ "OPT_STARTTLS should not have length");
return NULL;
}
@@ -474,13 +509,15 @@ static int nbd_negotiate_options(NBDClient *client)
return -EINVAL;
default:
- TRACE("Option 0x%" PRIx32 " not permitted before TLS",
- clientflags);
if (nbd_negotiate_drop_sync(client->ioc, length) != length) {
return -EIO;
}
- ret = nbd_negotiate_send_rep(client->ioc, NBD_REP_ERR_TLS_REQD,
- clientflags);
+ ret = nbd_negotiate_send_rep_err(client->ioc,
+ NBD_REP_ERR_TLS_REQD,
+ clientflags,
+ "Option 0x%" PRIx32
+ "not permitted before TLS",
+ clientflags);
if (ret < 0) {
return ret;
}
@@ -506,27 +543,30 @@ static int nbd_negotiate_options(NBDClient *client)
return -EIO;
}
if (client->tlscreds) {
- TRACE("TLS already enabled");
- ret = nbd_negotiate_send_rep(client->ioc,
- NBD_REP_ERR_INVALID,
- clientflags);
+ ret = nbd_negotiate_send_rep_err(client->ioc,
+ NBD_REP_ERR_INVALID,
+ clientflags,
+ "TLS already enabled");
} else {
- TRACE("TLS not configured");
- ret = nbd_negotiate_send_rep(client->ioc,
- NBD_REP_ERR_POLICY,
- clientflags);
+ ret = nbd_negotiate_send_rep_err(client->ioc,
+ NBD_REP_ERR_POLICY,
+ clientflags,
+ "TLS not configured");
}
if (ret < 0) {
return ret;
}
break;
default:
- TRACE("Unsupported option 0x%" PRIx32, clientflags);
if (nbd_negotiate_drop_sync(client->ioc, length) != length) {
return -EIO;
}
- ret = nbd_negotiate_send_rep(client->ioc, NBD_REP_ERR_UNSUP,
- clientflags);
+ ret = nbd_negotiate_send_rep_err(client->ioc,
+ NBD_REP_ERR_UNSUP,
+ clientflags,
+ "Unsupported option 0x%"
+ PRIx32,
+ clientflags);
if (ret < 0) {
return ret;
}
--
2.7.4
- [Qemu-devel] [PULL 04/30] tests: send error_report to test log, (continued)
- [Qemu-devel] [PULL 04/30] tests: send error_report to test log, Paolo Bonzini, 2016/11/01
- [Qemu-devel] [PULL 03/30] qemu-error: remove dependency of stubs on monitor, Paolo Bonzini, 2016/11/01
- [Qemu-devel] [PULL 05/30] exec.c: ensure all AddressSpaceDispatch updates under RCU, Paolo Bonzini, 2016/11/01
- [Qemu-devel] [PULL 07/30] exec.c: check memory backend file size with 'size' option, Paolo Bonzini, 2016/11/01
- [Qemu-devel] [PULL 06/30] exec.c: do not truncate non-empty memory backend file, Paolo Bonzini, 2016/11/01
- [Qemu-devel] [PULL 08/30] nbd: Add qemu-nbd -D for human-readable description, Paolo Bonzini, 2016/11/01
- [Qemu-devel] [PULL 10/30] nbd: Rename NBDRequest to NBDRequestData, Paolo Bonzini, 2016/11/01
- [Qemu-devel] [PULL 09/30] nbd: Treat flags vs. command type as separate fields, Paolo Bonzini, 2016/11/01
- [Qemu-devel] [PULL 13/30] nbd: Share common reply-sending code in server, Paolo Bonzini, 2016/11/01
- [Qemu-devel] [PULL 11/30] nbd: Rename NbdClientSession to NBDClientSession, Paolo Bonzini, 2016/11/01
- [Qemu-devel] [PULL 14/30] nbd: Send message along with server NBD_REP_ERR errors,
Paolo Bonzini <=
- [Qemu-devel] [PULL 15/30] nbd: Share common option-sending code in client, Paolo Bonzini, 2016/11/01
- [Qemu-devel] [PULL 18/30] nbd: Less allocation during NBD_OPT_LIST, Paolo Bonzini, 2016/11/01
- [Qemu-devel] [PULL 17/30] nbd: Let client skip portions of server reply, Paolo Bonzini, 2016/11/01
- [Qemu-devel] [PULL 16/30] nbd: Let server know when client gives up negotiation, Paolo Bonzini, 2016/11/01
- [Qemu-devel] [PULL 12/30] nbd: Rename struct nbd_request and nbd_reply, Paolo Bonzini, 2016/11/01
- [Qemu-devel] [PULL 19/30] nbd: Support shorter handshake, Paolo Bonzini, 2016/11/01
- [Qemu-devel] [PULL 20/30] nbd: Refactor conversion to errno to silence checkpatch, Paolo Bonzini, 2016/11/01
- [Qemu-devel] [PULL 21/30] nbd: Improve server handling of shutdown requests, Paolo Bonzini, 2016/11/01
- [Qemu-devel] [PULL 22/30] nbd: Implement NBD_CMD_WRITE_ZEROES on server, Paolo Bonzini, 2016/11/01
- [Qemu-devel] [PULL 23/30] nbd: Implement NBD_CMD_WRITE_ZEROES on client, Paolo Bonzini, 2016/11/01