[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v4 08/14] nbd: Let server know when client gives up
From: |
Eric Blake |
Subject: |
[Qemu-devel] [PATCH v4 08/14] nbd: Let server know when client gives up negotiation |
Date: |
Sat, 25 Jun 2016 16:15:48 -0600 |
The NBD spec says that a client should send NBD_OPT_ABORT
rather than just dropping the connection, if the client doesn't
like something the server sent during option negotiation. This
is a best-effort attempt only, and can only be done in places
where we know the server is still in sync with what we've sent,
whether or not we've read everything the server has sent.
Technically, the server then has to reply with NBD_REP_ACK, but
it's not worth complicating the client to wait around for that
reply.
Signed-off-by: Eric Blake <address@hidden>
---
v4: new patch
---
nbd/client.c | 30 ++++++++++++++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git a/nbd/client.c b/nbd/client.c
index 6e5288a..a78e81c 100644
--- a/nbd/client.c
+++ b/nbd/client.c
@@ -106,6 +106,19 @@ static int nbd_send_option_request(QIOChannel *ioc,
uint32_t opt,
return 0;
}
+/* Send NBD_OPT_ABORT as a courtesy to let the server know that we are
+ * not going to attempt further negotiation. */
+static void nbd_send_opt_abort(QIOChannel *ioc)
+{
+ /* Technically, a compliant server is supposed to reply to us; but
+ * older servers disconnected instead. At any rate, we're allowed
+ * to disconnect without waiting for the server reply, so we don't
+ * even care if the request makes it to the server, let alone
+ * waiting around for whether the server replies. */
+ nbd_send_option_request(ioc, NBD_OPT_ABORT, 0, NULL, NULL);
+}
+
+
/* Receive the header of an option reply, which should match the given
* opt. Read through the length field, but NOT the length bytes of
* payload. Return 0 if successful, -1 with errp set if it is
@@ -116,6 +129,7 @@ static int nbd_receive_option_reply(QIOChannel *ioc,
uint32_t opt,
QEMU_BUILD_BUG_ON(sizeof(*reply) != 20);
if (read_sync(ioc, reply, sizeof(*reply)) != sizeof(*reply)) {
error_setg(errp, "failed to read option reply");
+ nbd_send_opt_abort(ioc);
return -1;
}
be64_to_cpus(&reply->magic);
@@ -128,11 +142,13 @@ static int nbd_receive_option_reply(QIOChannel *ioc,
uint32_t opt,
if (reply->magic != NBD_REP_MAGIC) {
error_setg(errp, "Unexpected option reply magic");
+ nbd_send_opt_abort(ioc);
return -1;
}
if (reply->option != opt) {
error_setg(errp, "Unexpected option type %x expected %x",
reply->option, opt);
+ nbd_send_opt_abort(ioc);
return -1;
}
return 0;
@@ -201,6 +217,9 @@ static int nbd_handle_reply_err(QIOChannel *ioc,
nbd_opt_reply *reply,
cleanup:
g_free(msg);
+ if (result < 0) {
+ nbd_send_opt_abort(ioc);
+ }
return result;
}
@@ -224,25 +243,30 @@ static int nbd_receive_list(QIOChannel *ioc, char **name,
Error **errp)
if (reply.type == NBD_REP_ACK) {
if (len != 0) {
error_setg(errp, "length too long for option end");
+ nbd_send_opt_abort(ioc);
return -1;
}
} else if (reply.type == NBD_REP_SERVER) {
if (len < sizeof(namelen) || len > NBD_MAX_BUFFER_SIZE) {
error_setg(errp, "incorrect option length %"PRIu32, len);
+ nbd_send_opt_abort(ioc);
return -1;
}
if (read_sync(ioc, &namelen, sizeof(namelen)) != sizeof(namelen)) {
error_setg(errp, "failed to read option name length");
+ nbd_send_opt_abort(ioc);
return -1;
}
namelen = be32_to_cpu(namelen);
len -= sizeof(namelen);
if (len < namelen) {
error_setg(errp, "incorrect option name length");
+ nbd_send_opt_abort(ioc);
return -1;
}
if (namelen > NBD_MAX_NAME_SIZE) {
error_setg(errp, "export name length too long %" PRIu32, namelen);
+ nbd_send_opt_abort(ioc);
return -1;
}
@@ -251,6 +275,7 @@ static int nbd_receive_list(QIOChannel *ioc, char **name,
Error **errp)
error_setg(errp, "failed to read export name");
g_free(*name);
*name = NULL;
+ nbd_send_opt_abort(ioc);
return -1;
}
(*name)[namelen] = '\0';
@@ -262,6 +287,7 @@ static int nbd_receive_list(QIOChannel *ioc, char **name,
Error **errp)
g_free(*name);
g_free(buf);
*name = NULL;
+ nbd_send_opt_abort(ioc);
return -1;
}
buf[len] = '\0';
@@ -271,6 +297,7 @@ static int nbd_receive_list(QIOChannel *ioc, char **name,
Error **errp)
} else {
error_setg(errp, "Unexpected reply type %" PRIx32 " expected %x",
reply.type, NBD_REP_SERVER);
+ nbd_send_opt_abort(ioc);
return -1;
}
return 1;
@@ -320,6 +347,7 @@ static int nbd_receive_query_exports(QIOChannel *ioc,
if (!foundExport) {
error_setg(errp, "No export with name '%s' available", wantname);
+ nbd_send_opt_abort(ioc);
return -1;
}
@@ -347,12 +375,14 @@ static QIOChannel *nbd_receive_starttls(QIOChannel *ioc,
if (reply.type != NBD_REP_ACK) {
error_setg(errp, "Server rejected request to start TLS %" PRIx32,
reply.type);
+ nbd_send_opt_abort(ioc);
return NULL;
}
if (reply.length != 0) {
error_setg(errp, "Start TLS response was not zero %" PRIu32,
reply.length);
+ nbd_send_opt_abort(ioc);
return NULL;
}
--
2.5.5
- [Qemu-devel] [PATCH v4 01/14] nbd: Fix bad flag detection on server, (continued)
- [Qemu-devel] [PATCH v4 01/14] nbd: Fix bad flag detection on server, Eric Blake, 2016/06/25
- [Qemu-devel] [PATCH v4 09/14] nbd: Let client skip portions of server reply, Eric Blake, 2016/06/25
- [Qemu-devel] [PATCH v4 06/14] nbd: Send message along with server NBD_REP_ERR errors, Eric Blake, 2016/06/25
- [Qemu-devel] [PATCH v4 11/14] nbd: Support shorter handshake, Eric Blake, 2016/06/25
- [Qemu-devel] [PATCH v4 04/14] nbd: Treat flags vs. command type as separate fields, Eric Blake, 2016/06/25
- [Qemu-devel] [PATCH v4 14/14] nbd: Implement NBD_CMD_WRITE_ZEROES on client, Eric Blake, 2016/06/25
- [Qemu-devel] [PATCH v4 07/14] nbd: Share common option-sending code in client, Eric Blake, 2016/06/25
- [Qemu-devel] [PATCH v4 05/14] nbd: Share common reply-sending code in server, Eric Blake, 2016/06/25
- [Qemu-devel] [PATCH v4 08/14] nbd: Let server know when client gives up negotiation,
Eric Blake <=
- [Qemu-devel] [PATCH v4 10/14] nbd: Less allocation during NBD_OPT_LIST, Eric Blake, 2016/06/25
- [Qemu-devel] [PATCH v4 02/14] nbd: Add qemu-nbd -D for human-readable description, Eric Blake, 2016/06/25
- [Qemu-devel] [PATCH v4 13/14] nbd: Implement NBD_CMD_WRITE_ZEROES on server, Eric Blake, 2016/06/25
- [Qemu-devel] [PATCH v4 03/14] nbd: Limit nbdflags to 16 bits, Eric Blake, 2016/06/25
- [Qemu-devel] [PATCH v4 12/14] nbd: Improve server handling of shutdown requests, Eric Blake, 2016/06/25