qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Daniel P. Berrange" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: Paolo Bonzini <pbonzini@redhat.com>, qemu-block@nongnu.org
Subject: [Qemu-devel] [PATCH v6 07/16] nbd: invert client logic for negotiating protocol version
Date: Wed, 10 Feb 2016 18:41:05 +0000	[thread overview]
Message-ID: <1455129674-17255-8-git-send-email-berrange@redhat.com> (raw)
In-Reply-To: <1455129674-17255-1-git-send-email-berrange@redhat.com>

The nbd_receive_negotiate() method takes different code
paths based on whether 'name == NULL', and then checks
the expected protocol version in each branch.

This patch inverts the logic, so that it takes different
code paths based on what protocol version it receives and
then checks if name is NULL or not as needed.

This facilitates later code which allows the client to
be capable of using the new style protocol regardless
of whether an export name is listed or not.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
---
 nbd/client.c | 60 +++++++++++++++++++++++++++++-------------------------------
 1 file changed, 29 insertions(+), 31 deletions(-)

diff --git a/nbd/client.c b/nbd/client.c
index f6dd0a3..5064788 100644
--- a/nbd/client.c
+++ b/nbd/client.c
@@ -116,20 +116,11 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint32_t *flags,
     magic = be64_to_cpu(magic);
     TRACE("Magic is 0x%" PRIx64, magic);
 
-    if (name) {
+    if (magic == NBD_OPTS_MAGIC) {
         uint32_t reserved = 0;
         uint32_t opt;
         uint32_t namesize;
 
-        TRACE("Checking magic (opts_magic)");
-        if (magic != NBD_OPTS_MAGIC) {
-            if (magic == NBD_CLIENT_MAGIC) {
-                error_setg(errp, "Server does not support export names");
-            } else {
-                error_setg(errp, "Bad magic received");
-            }
-            goto fail;
-        }
         if (read_sync(ioc, &tmp, sizeof(tmp)) != sizeof(tmp)) {
             error_setg(errp, "Failed to read server flags");
             goto fail;
@@ -142,6 +133,10 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint32_t *flags,
             goto fail;
         }
         /* write the export name */
+        if (!name) {
+            error_setg(errp, "Server requires an export name");
+            goto fail;
+        }
         magic = cpu_to_be64(magic);
         if (write_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) {
             error_setg(errp, "Failed to send export name magic");
@@ -162,39 +157,42 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint32_t *flags,
             error_setg(errp, "Failed to send export name");
             goto fail;
         }
-    } else {
-        TRACE("Checking magic (cli_magic)");
 
-        if (magic != NBD_CLIENT_MAGIC) {
-            if (magic == NBD_OPTS_MAGIC) {
-                error_setg(errp, "Server requires an export name");
-            } else {
-                error_setg(errp, "Bad magic received");
-            }
+        if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) {
+            error_setg(errp, "Failed to read export length");
             goto fail;
         }
-    }
+        *size = be64_to_cpu(s);
+        TRACE("Size is %" PRIu64, *size);
 
-    if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) {
-        error_setg(errp, "Failed to read export length");
-        goto fail;
-    }
-    *size = be64_to_cpu(s);
-    TRACE("Size is %" PRIu64, *size);
+        if (read_sync(ioc, &tmp, sizeof(tmp)) != sizeof(tmp)) {
+            error_setg(errp, "Failed to read export flags");
+            goto fail;
+        }
+        *flags |= be16_to_cpu(tmp);
+    } else if (magic == NBD_CLIENT_MAGIC) {
+        if (name) {
+            error_setg(errp, "Server does not support export names");
+            goto fail;
+        }
+
+        if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) {
+            error_setg(errp, "Failed to read export length");
+            goto fail;
+        }
+        *size = be64_to_cpu(s);
+        TRACE("Size is %" PRIu64, *size);
 
-    if (!name) {
         if (read_sync(ioc, flags, sizeof(*flags)) != sizeof(*flags)) {
             error_setg(errp, "Failed to read export flags");
             goto fail;
         }
         *flags = be32_to_cpup(flags);
     } else {
-        if (read_sync(ioc, &tmp, sizeof(tmp)) != sizeof(tmp)) {
-            error_setg(errp, "Failed to read export flags");
-            goto fail;
-        }
-        *flags |= be16_to_cpu(tmp);
+        error_setg(errp, "Bad magic received");
+        goto fail;
     }
+
     if (read_sync(ioc, &buf, 124) != 124) {
         error_setg(errp, "Failed to read reserved block");
         goto fail;
-- 
2.5.0

  parent reply	other threads:[~2016-02-10 18:41 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-10 18:40 [Qemu-devel] [PATCH v6 00/16] Implement TLS support to QEMU NBD server & client Daniel P. Berrange
2016-02-10 18:40 ` [Qemu-devel] [PATCH v6 01/16] qom: add helpers for UserCreatable object types Daniel P. Berrange
2016-02-11 23:13   ` Eric Blake
2016-02-10 18:41 ` [Qemu-devel] [PATCH v6 02/16] qemu-nbd: add support for --object command line arg Daniel P. Berrange
2016-02-10 18:41 ` [Qemu-devel] [PATCH v6 03/16] nbd: convert block client to use I/O channels for connection setup Daniel P. Berrange
2016-02-10 18:41 ` [Qemu-devel] [PATCH v6 04/16] nbd: convert qemu-nbd server " Daniel P. Berrange
2016-02-10 18:41 ` [Qemu-devel] [PATCH v6 05/16] nbd: convert blockdev NBD " Daniel P. Berrange
2016-02-10 18:41 ` [Qemu-devel] [PATCH v6 06/16] nbd: convert to using I/O channels for actual socket I/O Daniel P. Berrange
2016-02-10 18:41 ` Daniel P. Berrange [this message]
2016-02-10 18:41 ` [Qemu-devel] [PATCH v6 08/16] nbd: make server compliant with fixed newstyle spec Daniel P. Berrange
2016-02-10 18:41 ` [Qemu-devel] [PATCH v6 09/16] nbd: make client request fixed new style if advertized Daniel P. Berrange
2016-02-10 18:41 ` [Qemu-devel] [PATCH v6 10/16] nbd: allow setting of an export name for qemu-nbd server Daniel P. Berrange
2016-02-10 18:41 ` [Qemu-devel] [PATCH v6 11/16] nbd: always query export list in fixed new style protocol Daniel P. Berrange
2016-02-10 18:41 ` [Qemu-devel] [PATCH v6 12/16] nbd: use "" as a default export name if none provided Daniel P. Berrange
2016-02-10 18:41 ` [Qemu-devel] [PATCH v6 13/16] nbd: implement TLS support in the protocol negotiation Daniel P. Berrange
2016-02-10 18:41 ` [Qemu-devel] [PATCH v6 14/16] nbd: enable use of TLS with NBD block driver Daniel P. Berrange
2016-02-10 18:41 ` [Qemu-devel] [PATCH v6 15/16] nbd: enable use of TLS with qemu-nbd server Daniel P. Berrange
2016-02-10 18:41 ` [Qemu-devel] [PATCH v6 16/16] nbd: enable use of TLS with nbd-server-start command Daniel P. Berrange
2016-02-12 13:28 ` [Qemu-devel] [PATCH v6 00/16] Implement TLS support to QEMU NBD server & client Kashyap Chamarthy
2016-02-12 15:00   ` Daniel P. Berrange
2016-02-12 16:03     ` Kashyap Chamarthy
2016-02-12 18:14       ` Kashyap Chamarthy
2016-02-16 16:18 ` 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=1455129674-17255-8-git-send-email-berrange@redhat.com \
    --to=berrange@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --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).