提交 c203c59a 编写于 作者: E Eric Blake 提交者: Paolo Bonzini

nbd: Support shorter handshake

The NBD Protocol allows the server and client to mutually agree
on a shorter handshake (omit the 124 bytes of reserved 0), via
the server advertising NBD_FLAG_NO_ZEROES and the client
acknowledging with NBD_FLAG_C_NO_ZEROES (only possible in
newstyle, whether or not it is fixed newstyle).  It doesn't
shave much off the wire, but we might as well implement it.
Signed-off-by: NEric Blake <eblake@redhat.com>
Reviewed-by: NAlex Bligh <alex@alex.org.uk>
Message-Id: <1476469998-28592-13-git-send-email-eblake@redhat.com>
Signed-off-by: NPaolo Bonzini <pbonzini@redhat.com>
上级 75368aab
...@@ -74,11 +74,13 @@ typedef struct NBDReply NBDReply; ...@@ -74,11 +74,13 @@ typedef struct NBDReply NBDReply;
/* New-style handshake (global) flags, sent from server to client, and /* New-style handshake (global) flags, sent from server to client, and
control what will happen during handshake phase. */ control what will happen during handshake phase. */
#define NBD_FLAG_FIXED_NEWSTYLE (1 << 0) /* Fixed newstyle protocol. */ #define NBD_FLAG_FIXED_NEWSTYLE (1 << 0) /* Fixed newstyle protocol. */
#define NBD_FLAG_NO_ZEROES (1 << 1) /* End handshake without zeroes. */
/* New-style client flags, sent from client to server to control what happens /* New-style client flags, sent from client to server to control what happens
during handshake phase. */ during handshake phase. */
#define NBD_FLAG_C_FIXED_NEWSTYLE (1 << 0) /* Fixed newstyle protocol. */ #define NBD_FLAG_C_FIXED_NEWSTYLE (1 << 0) /* Fixed newstyle protocol. */
#define NBD_FLAG_C_NO_ZEROES (1 << 1) /* End handshake without zeroes. */
/* Reply types. */ /* Reply types. */
#define NBD_REP_ACK (1) /* Data sending finished. */ #define NBD_REP_ACK (1) /* Data sending finished. */
......
...@@ -440,6 +440,7 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint16_t *flags, ...@@ -440,6 +440,7 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint16_t *flags,
char buf[256]; char buf[256];
uint64_t magic, s; uint64_t magic, s;
int rc; int rc;
bool zeroes = true;
TRACE("Receiving negotiation tlscreds=%p hostname=%s.", TRACE("Receiving negotiation tlscreds=%p hostname=%s.",
tlscreds, hostname ? hostname : "<null>"); tlscreds, hostname ? hostname : "<null>");
...@@ -504,6 +505,11 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint16_t *flags, ...@@ -504,6 +505,11 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint16_t *flags,
TRACE("Server supports fixed new style"); TRACE("Server supports fixed new style");
clientflags |= NBD_FLAG_C_FIXED_NEWSTYLE; clientflags |= NBD_FLAG_C_FIXED_NEWSTYLE;
} }
if (globalflags & NBD_FLAG_NO_ZEROES) {
zeroes = false;
TRACE("Server supports no zeroes");
clientflags |= NBD_FLAG_C_NO_ZEROES;
}
/* client requested flags */ /* client requested flags */
clientflags = cpu_to_be32(clientflags); clientflags = cpu_to_be32(clientflags);
if (write_sync(ioc, &clientflags, sizeof(clientflags)) != if (write_sync(ioc, &clientflags, sizeof(clientflags)) !=
...@@ -591,7 +597,7 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint16_t *flags, ...@@ -591,7 +597,7 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint16_t *flags,
} }
TRACE("Size is %" PRIu64 ", export flags %" PRIx16, *size, *flags); TRACE("Size is %" PRIu64 ", export flags %" PRIx16, *size, *flags);
if (drop_sync(ioc, 124) != 124) { if (zeroes && drop_sync(ioc, 124) != 124) {
error_setg(errp, "Failed to read reserved block"); error_setg(errp, "Failed to read reserved block");
goto fail; goto fail;
} }
......
...@@ -81,6 +81,7 @@ struct NBDClient { ...@@ -81,6 +81,7 @@ struct NBDClient {
int refcount; int refcount;
void (*close)(NBDClient *client); void (*close)(NBDClient *client);
bool no_zeroes;
NBDExport *exp; NBDExport *exp;
QCryptoTLSCreds *tlscreds; QCryptoTLSCreds *tlscreds;
char *tlsaclname; char *tlsaclname;
...@@ -450,6 +451,11 @@ static int nbd_negotiate_options(NBDClient *client) ...@@ -450,6 +451,11 @@ static int nbd_negotiate_options(NBDClient *client)
fixedNewstyle = true; fixedNewstyle = true;
flags &= ~NBD_FLAG_C_FIXED_NEWSTYLE; flags &= ~NBD_FLAG_C_FIXED_NEWSTYLE;
} }
if (flags & NBD_FLAG_C_NO_ZEROES) {
TRACE("Client supports no zeroes at handshake end");
client->no_zeroes = true;
flags &= ~NBD_FLAG_C_NO_ZEROES;
}
if (flags != 0) { if (flags != 0) {
TRACE("Unknown client flags 0x%" PRIx32 " received", flags); TRACE("Unknown client flags 0x%" PRIx32 " received", flags);
return -EIO; return -EIO;
...@@ -602,6 +608,7 @@ static coroutine_fn int nbd_negotiate(NBDClientNewData *data) ...@@ -602,6 +608,7 @@ static coroutine_fn int nbd_negotiate(NBDClientNewData *data)
const uint16_t myflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM | const uint16_t myflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM |
NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA); NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA);
bool oldStyle; bool oldStyle;
size_t len;
/* Old style negotiation header without options /* Old style negotiation header without options
[ 0 .. 7] passwd ("NBDMAGIC") [ 0 .. 7] passwd ("NBDMAGIC")
...@@ -618,7 +625,7 @@ static coroutine_fn int nbd_negotiate(NBDClientNewData *data) ...@@ -618,7 +625,7 @@ static coroutine_fn int nbd_negotiate(NBDClientNewData *data)
....options sent.... ....options sent....
[18 .. 25] size [18 .. 25] size
[26 .. 27] export flags [26 .. 27] export flags
[28 .. 151] reserved (0) [28 .. 151] reserved (0, omit if no_zeroes)
*/ */
qio_channel_set_blocking(client->ioc, false, NULL); qio_channel_set_blocking(client->ioc, false, NULL);
...@@ -637,7 +644,7 @@ static coroutine_fn int nbd_negotiate(NBDClientNewData *data) ...@@ -637,7 +644,7 @@ static coroutine_fn int nbd_negotiate(NBDClientNewData *data)
stw_be_p(buf + 26, client->exp->nbdflags | myflags); stw_be_p(buf + 26, client->exp->nbdflags | myflags);
} else { } else {
stq_be_p(buf + 8, NBD_OPTS_MAGIC); stq_be_p(buf + 8, NBD_OPTS_MAGIC);
stw_be_p(buf + 16, NBD_FLAG_FIXED_NEWSTYLE); stw_be_p(buf + 16, NBD_FLAG_FIXED_NEWSTYLE | NBD_FLAG_NO_ZEROES);
} }
if (oldStyle) { if (oldStyle) {
...@@ -664,8 +671,8 @@ static coroutine_fn int nbd_negotiate(NBDClientNewData *data) ...@@ -664,8 +671,8 @@ static coroutine_fn int nbd_negotiate(NBDClientNewData *data)
client->exp->size, client->exp->nbdflags | myflags); client->exp->size, client->exp->nbdflags | myflags);
stq_be_p(buf + 18, client->exp->size); stq_be_p(buf + 18, client->exp->size);
stw_be_p(buf + 26, client->exp->nbdflags | myflags); stw_be_p(buf + 26, client->exp->nbdflags | myflags);
if (nbd_negotiate_write(client->ioc, buf + 18, sizeof(buf) - 18) != len = client->no_zeroes ? 10 : sizeof(buf) - 18;
sizeof(buf) - 18) { if (nbd_negotiate_write(client->ioc, buf + 18, len) != len) {
LOG("write failed"); LOG("write failed");
goto fail; goto fail;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册