qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PULL 35/41] nbd: Refactor reply to NBD_OPT_EXPORT_NAME
Date: Thu, 13 Jul 2017 16:24:28 +0200	[thread overview]
Message-ID: <1499955874-10954-36-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1499955874-10954-1-git-send-email-pbonzini@redhat.com>

From: Eric Blake <eblake@redhat.com>

Reply directly in nbd_negotiate_handle_export_name(), rather than
waiting until nbd_negotiate_options() completes.  This will make it
easier to implement NBD_OPT_GO.  Pass additional parameters around,
rather than stashing things inside NBDClient.

Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <20170707203049.534-6-eblake@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 nbd/server.c | 50 ++++++++++++++++++++++++++++----------------------
 1 file changed, 28 insertions(+), 22 deletions(-)

diff --git a/nbd/server.c b/nbd/server.c
index 8e363d3..841986d 100644
--- a/nbd/server.c
+++ b/nbd/server.c
@@ -84,7 +84,6 @@ struct NBDClient {
     int refcount;
     void (*close_fn)(NBDClient *client, bool negotiated);
 
-    bool no_zeroes;
     NBDExport *exp;
     QCryptoTLSCreds *tlscreds;
     char *tlsaclname;
@@ -277,9 +276,13 @@ static int nbd_negotiate_handle_list(NBDClient *client, uint32_t length,
 }
 
 static int nbd_negotiate_handle_export_name(NBDClient *client, uint32_t length,
+                                            uint16_t myflags, bool no_zeroes,
                                             Error **errp)
 {
     char name[NBD_MAX_NAME_SIZE + 1];
+    char buf[8 + 4 + 124] = "";
+    size_t len;
+    int ret;
 
     /* Client sends:
         [20 ..  xx]   export name (length bytes)
@@ -303,6 +306,17 @@ static int nbd_negotiate_handle_export_name(NBDClient *client, uint32_t length,
         return -EINVAL;
     }
 
+    trace_nbd_negotiate_new_style_size_flags(client->exp->size,
+                                             client->exp->nbdflags | myflags);
+    stq_be_p(buf, client->exp->size);
+    stw_be_p(buf + 8, client->exp->nbdflags | myflags);
+    len = no_zeroes ? 10 : sizeof(buf);
+    ret = nbd_write(client->ioc, buf, len, errp);
+    if (ret < 0) {
+        error_prepend(errp, "write failed: ");
+        return ret;
+    }
+
     QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
     nbd_export_get(client->exp);
 
@@ -373,14 +387,17 @@ static QIOChannel *nbd_negotiate_handle_starttls(NBDClient *client,
  * 1       if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
  *         errp is not set
  */
-static int nbd_negotiate_options(NBDClient *client, Error **errp)
+static int nbd_negotiate_options(NBDClient *client, uint16_t myflags,
+                                 Error **errp)
 {
     uint32_t flags;
     bool fixedNewstyle = false;
+    bool no_zeroes = false;
 
     /* Client sends:
         [ 0 ..   3]   client flags
 
+       Then we loop until NBD_OPT_EXPORT_NAME:
         [ 0 ..   7]   NBD_OPTS_MAGIC
         [ 8 ..  11]   NBD option
         [12 ..  15]   Data length
@@ -403,7 +420,7 @@ static int nbd_negotiate_options(NBDClient *client, Error **errp)
         flags &= ~NBD_FLAG_C_FIXED_NEWSTYLE;
     }
     if (flags & NBD_FLAG_C_NO_ZEROES) {
-        client->no_zeroes = true;
+        no_zeroes = true;
         flags &= ~NBD_FLAG_C_NO_ZEROES;
     }
     if (flags != 0) {
@@ -503,7 +520,9 @@ static int nbd_negotiate_options(NBDClient *client, Error **errp)
                 return 1;
 
             case NBD_OPT_EXPORT_NAME:
-                return nbd_negotiate_handle_export_name(client, length, errp);
+                return nbd_negotiate_handle_export_name(client, length,
+                                                        myflags, no_zeroes,
+                                                        errp);
 
             case NBD_OPT_STARTTLS:
                 if (nbd_drop(client->ioc, length, errp) < 0) {
@@ -546,7 +565,9 @@ static int nbd_negotiate_options(NBDClient *client, Error **errp)
              */
             switch (option) {
             case NBD_OPT_EXPORT_NAME:
-                return nbd_negotiate_handle_export_name(client, length, errp);
+                return nbd_negotiate_handle_export_name(client, length,
+                                                        myflags, no_zeroes,
+                                                        errp);
 
             default:
                 error_setg(errp, "Unsupported option 0x%" PRIx32 " (%s)",
@@ -572,7 +593,6 @@ static coroutine_fn int nbd_negotiate(NBDClient *client, Error **errp)
                               NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA |
                               NBD_FLAG_SEND_WRITE_ZEROES);
     bool oldStyle;
-    size_t len;
 
     /* Old style negotiation header without options
         [ 0 ..   7]   passwd       ("NBDMAGIC")
@@ -586,10 +606,7 @@ static coroutine_fn int nbd_negotiate(NBDClient *client, Error **errp)
         [ 0 ..   7]   passwd       ("NBDMAGIC")
         [ 8 ..  15]   magic        (NBD_OPTS_MAGIC)
         [16 ..  17]   server flags (0)
-        ....options sent....
-        [18 ..  25]   size
-        [26 ..  27]   export flags
-        [28 .. 151]   reserved     (0, omit if no_zeroes)
+        ....options sent, ending in NBD_OPT_EXPORT_NAME....
      */
 
     qio_channel_set_blocking(client->ioc, false, NULL);
@@ -618,24 +635,13 @@ static coroutine_fn int nbd_negotiate(NBDClient *client, Error **errp)
             error_prepend(errp, "write failed: ");
             return -EINVAL;
         }
-        ret = nbd_negotiate_options(client, errp);
+        ret = nbd_negotiate_options(client, myflags, errp);
         if (ret != 0) {
             if (ret < 0) {
                 error_prepend(errp, "option negotiation failed: ");
             }
             return ret;
         }
-
-        trace_nbd_negotiate_new_style_size_flags(
-            client->exp->size, client->exp->nbdflags | myflags);
-        stq_be_p(buf + 18, client->exp->size);
-        stw_be_p(buf + 26, client->exp->nbdflags | myflags);
-        len = client->no_zeroes ? 10 : sizeof(buf) - 18;
-        ret = nbd_write(client->ioc, buf + 18, len, errp);
-        if (ret < 0) {
-            error_prepend(errp, "write failed: ");
-            return ret;
-        }
     }
 
     trace_nbd_negotiate_success();
-- 
1.8.3.1

  parent reply	other threads:[~2017-07-13 14:25 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-13 14:23 [Qemu-devel] [PULL 00/41] Misc patches for QEMU 2.10 soft freeze Paolo Bonzini
2017-07-13 14:23 ` [Qemu-devel] [PULL 01/41] build: add -Wexpansion-to-defined Paolo Bonzini
2017-07-13 14:23 ` [Qemu-devel] [PULL 02/41] MAINTAINERS: update TCG entries Paolo Bonzini
2017-07-13 14:23 ` [Qemu-devel] [PULL 03/41] MAINTAINERS: update KVM entries Paolo Bonzini
2017-07-13 14:23 ` [Qemu-devel] [PULL 04/41] MAINTAINERS: update Xen entries Paolo Bonzini
2017-07-13 14:23 ` [Qemu-devel] [PULL 05/41] MAINTAINERS: update TCI entry Paolo Bonzini
2017-07-13 14:23 ` [Qemu-devel] [PULL 06/41] MAINTAINERS: add entry for "Unimplemented" device Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 07/41] chardev: block during sync read Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 08/41] char: move QemuOpts->ChardevBackend translation to a separate func Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 09/41] char: add backend hotswap handler Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 10/41] char: chardevice hotswap Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 11/41] char: forbid direct chardevice access for hotswap devices Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 12/41] char: avoid chardevice direct access Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 13/41] test-char: destroy chardev-udp after test Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 14/41] test-char: split char_udp_test Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 15/41] test-char: split char_file_test Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 16/41] test-char: add hotswap test Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 17/41] hmp: add hmp analogue for qmp-chardev-change Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 18/41] virtio-console: chardev hotswap support Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 19/41] serial: move TIOCM update to a separate function Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 20/41] serial: chardev hotswap support Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 21/41] exec: use qemu_ram_ptr_length to access guest ram Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 22/41] Revert "exec.c: Fix breakpoint invalidation race" Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 23/41] mttcg/i386: Patch instruction using async_safe_* framework Paolo Bonzini
2017-07-13 15:16   ` Alex Bennée
2017-07-14  8:24     ` Thomas Huth
2017-07-13 14:24 ` [Qemu-devel] [PULL 24/41] gdbstub: modernise DEBUG_GDB Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 25/41] gdbstub: rename cpu_index -> cpu_gdb_index Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 26/41] qom/cpu: remove host_tid field Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 27/41] gdbstub: don't fail on vCont; C04:0; c packets Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 28/41] chardev: fix parallel device can't be reconnect Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 29/41] memory/iommu: QOM'fy IOMMU MemoryRegion Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 30/41] memory/iommu: introduce IOMMUMemoryRegionClass Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 31/41] nbd: Create struct for tracking export info Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 32/41] nbd: Don't bother tracing an NBD_OPT_ABORT response failure Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 33/41] nbd: Expose and debug more NBD constants Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 34/41] nbd: Simplify trace of client flags in negotiation Paolo Bonzini
2017-07-13 14:24 ` Paolo Bonzini [this message]
2017-07-13 14:24 ` [Qemu-devel] [PULL 36/41] nbd: Implement NBD_OPT_GO on server Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 37/41] nbd: Implement NBD_OPT_GO on client Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 38/41] nbd: Implement NBD_INFO_BLOCK_SIZE on server Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 39/41] nbd: Implement NBD_INFO_BLOCK_SIZE on client Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 40/41] vl: fix breakage of -tb-size Paolo Bonzini
2017-07-13 14:24 ` [Qemu-devel] [PULL 41/41] translate-all: remove redundant !tcg_enabled check in dump_exec_info Paolo Bonzini
2017-07-13 16:05 ` [Qemu-devel] [PULL 00/41] Misc patches for QEMU 2.10 soft freeze no-reply
2017-07-13 17:01   ` 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=1499955874-10954-36-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).