All of lore.kernel.org
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PULL 20/27] nbd: Support shorter handshake
Date: Mon, 31 Oct 2016 15:37:36 +0100	[thread overview]
Message-ID: <1477924663-30950-21-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1477924663-30950-1-git-send-email-pbonzini@redhat.com>

From: Eric Blake <eblake@redhat.com>

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: Eric Blake <eblake@redhat.com>
Reviewed-by: Alex Bligh <alex@alex.org.uk>
Message-Id: <1476469998-28592-13-git-send-email-eblake@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 include/block/nbd.h |  6 ++++--
 nbd/client.c        |  8 +++++++-
 nbd/server.c        | 15 +++++++++++----
 3 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/include/block/nbd.h b/include/block/nbd.h
index b69bf1d..d326308 100644
--- a/include/block/nbd.h
+++ b/include/block/nbd.h
@@ -74,11 +74,13 @@ typedef struct NBDReply NBDReply;
 
 /* New-style handshake (global) flags, sent from server to client, and
    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
    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. */
 #define NBD_REP_ACK             (1)             /* Data sending finished. */
diff --git a/nbd/client.c b/nbd/client.c
index 0afb8be..b29963b 100644
--- a/nbd/client.c
+++ b/nbd/client.c
@@ -440,6 +440,7 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint16_t *flags,
     char buf[256];
     uint64_t magic, s;
     int rc;
+    bool zeroes = true;
 
     TRACE("Receiving negotiation tlscreds=%p hostname=%s.",
           tlscreds, hostname ? hostname : "<null>");
@@ -504,6 +505,11 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint16_t *flags,
             TRACE("Server supports fixed new style");
             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 */
         clientflags = cpu_to_be32(clientflags);
         if (write_sync(ioc, &clientflags, sizeof(clientflags)) !=
@@ -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);
-    if (drop_sync(ioc, 124) != 124) {
+    if (zeroes && drop_sync(ioc, 124) != 124) {
         error_setg(errp, "Failed to read reserved block");
         goto fail;
     }
diff --git a/nbd/server.c b/nbd/server.c
index fa01e49..afc1ec4 100644
--- a/nbd/server.c
+++ b/nbd/server.c
@@ -81,6 +81,7 @@ struct NBDClient {
     int refcount;
     void (*close)(NBDClient *client);
 
+    bool no_zeroes;
     NBDExport *exp;
     QCryptoTLSCreds *tlscreds;
     char *tlsaclname;
@@ -450,6 +451,11 @@ static int nbd_negotiate_options(NBDClient *client)
         fixedNewstyle = true;
         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) {
         TRACE("Unknown client flags 0x%" PRIx32 " received", flags);
         return -EIO;
@@ -602,6 +608,7 @@ static coroutine_fn int nbd_negotiate(NBDClientNewData *data)
     const uint16_t myflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM |
                               NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA);
     bool oldStyle;
+    size_t len;
 
     /* Old style negotiation header without options
         [ 0 ..   7]   passwd       ("NBDMAGIC")
@@ -618,7 +625,7 @@ static coroutine_fn int nbd_negotiate(NBDClientNewData *data)
         ....options sent....
         [18 ..  25]   size
         [26 ..  27]   export flags
-        [28 .. 151]   reserved     (0)
+        [28 .. 151]   reserved     (0, omit if no_zeroes)
      */
 
     qio_channel_set_blocking(client->ioc, false, NULL);
@@ -637,7 +644,7 @@ static coroutine_fn int nbd_negotiate(NBDClientNewData *data)
         stw_be_p(buf + 26, client->exp->nbdflags | myflags);
     } else {
         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) {
@@ -664,8 +671,8 @@ static coroutine_fn int nbd_negotiate(NBDClientNewData *data)
               client->exp->size, client->exp->nbdflags | myflags);
         stq_be_p(buf + 18, client->exp->size);
         stw_be_p(buf + 26, client->exp->nbdflags | myflags);
-        if (nbd_negotiate_write(client->ioc, buf + 18, sizeof(buf) - 18) !=
-            sizeof(buf) - 18) {
+        len = client->no_zeroes ? 10 : sizeof(buf) - 18;
+        if (nbd_negotiate_write(client->ioc, buf + 18, len) != len) {
             LOG("write failed");
             goto fail;
         }
-- 
2.7.4

  parent reply	other threads:[~2016-10-31 14:38 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-31 14:37 [Qemu-devel] [PULL 00/27] Misc patches for 2016-10-31 Paolo Bonzini
2016-10-31 14:37 ` [Qemu-devel] [PULL 01/27] checkpatch: tweak "struct should normally be const" warning Paolo Bonzini
2016-10-31 14:37 ` [Qemu-devel] [PULL 02/27] nbd: Use CoQueue for free_sema instead of CoMutex Paolo Bonzini
2016-10-31 14:37 ` [Qemu-devel] [PULL 03/27] qemu-error: remove dependency of stubs on monitor Paolo Bonzini
2016-10-31 14:37 ` [Qemu-devel] [PULL 04/27] tests: send error_report to test log Paolo Bonzini
2016-10-31 14:37 ` [Qemu-devel] [PULL 05/27] exec.c: ensure all AddressSpaceDispatch updates under RCU Paolo Bonzini
2016-10-31 14:37 ` [Qemu-devel] [PULL 06/27] exec.c: do not truncate non-empty memory backend file Paolo Bonzini
2016-10-31 14:37 ` [Qemu-devel] [PULL 07/27] exec.c: check memory backend file size with 'size' option Paolo Bonzini
2016-10-31 14:37 ` [Qemu-devel] [PULL 08/27] hostmem-file: make option 'size' optional Paolo Bonzini
2016-10-31 18:20   ` Eduardo Habkost
2016-10-31 19:47     ` Paolo Bonzini
2016-10-31 22:22       ` Eduardo Habkost
2016-11-01  9:32         ` Haozhong Zhang
2016-11-01 14:16           ` Eduardo Habkost
2016-11-02  1:27             ` Haozhong Zhang
2016-10-31 14:37 ` [Qemu-devel] [PULL 09/27] nbd: Add qemu-nbd -D for human-readable description Paolo Bonzini
2016-10-31 14:37 ` [Qemu-devel] [PULL 10/27] nbd: Treat flags vs. command type as separate fields Paolo Bonzini
2016-10-31 14:37 ` [Qemu-devel] [PULL 11/27] nbd: Rename NBDRequest to NBDRequestData Paolo Bonzini
2016-10-31 14:37 ` [Qemu-devel] [PULL 12/27] nbd: Rename NbdClientSession to NBDClientSession Paolo Bonzini
2016-10-31 14:37 ` [Qemu-devel] [PULL 13/27] nbd: Rename struct nbd_request and nbd_reply Paolo Bonzini
2016-10-31 14:37 ` [Qemu-devel] [PULL 14/27] nbd: Share common reply-sending code in server Paolo Bonzini
2016-10-31 14:37 ` [Qemu-devel] [PULL 15/27] nbd: Send message along with server NBD_REP_ERR errors Paolo Bonzini
2016-10-31 14:37 ` [Qemu-devel] [PULL 16/27] nbd: Share common option-sending code in client Paolo Bonzini
2016-10-31 14:37 ` [Qemu-devel] [PULL 17/27] nbd: Let server know when client gives up negotiation Paolo Bonzini
2016-10-31 14:37 ` [Qemu-devel] [PULL 18/27] nbd: Let client skip portions of server reply Paolo Bonzini
2016-10-31 14:37 ` [Qemu-devel] [PULL 19/27] nbd: Less allocation during NBD_OPT_LIST Paolo Bonzini
2016-10-31 14:37 ` Paolo Bonzini [this message]
2016-10-31 14:37 ` [Qemu-devel] [PULL 21/27] nbd: Refactor conversion to errno to silence checkpatch Paolo Bonzini
2016-10-31 14:37 ` [Qemu-devel] [PULL 22/27] nbd: Improve server handling of shutdown requests Paolo Bonzini
2016-10-31 18:05   ` Eric Blake
2016-10-31 14:37 ` [Qemu-devel] [PULL 23/27] nbd: Implement NBD_CMD_WRITE_ZEROES on server Paolo Bonzini
2016-10-31 14:37 ` [Qemu-devel] [PULL 24/27] nbd: Implement NBD_CMD_WRITE_ZEROES on client Paolo Bonzini
2016-11-15 22:59   ` Eric Blake
2016-10-31 14:37 ` [Qemu-devel] [PULL 25/27] qemu-char: do not forward events through the mux until QEMU has started Paolo Bonzini
2016-10-31 14:37 ` [Qemu-devel] [PULL 26/27] slirp: fix CharDriver breakage Paolo Bonzini
2016-10-31 14:37 ` [Qemu-devel] [PULL 27/27] x86: add AVX512_4VNNIW and AVX512_4FMAPS features Paolo Bonzini
2016-10-31 16:21 ` [Qemu-devel] [PULL 00/27] Misc patches for 2016-10-31 Peter Maydell
2016-10-31 17:18   ` Alex Bennée
2016-10-31 17:20     ` Peter Maydell
2016-10-31 17:57       ` Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1477924663-30950-21-git-send-email-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.