Blame SOURCES/kvm-nbd-client-Refactor-nbd_receive_list.patch

383d26
From ff100c505e2a1cdc0a405008513948caa935b6cb Mon Sep 17 00:00:00 2001
383d26
From: John Snow <jsnow@redhat.com>
383d26
Date: Wed, 27 Mar 2019 17:22:42 +0100
383d26
Subject: [PATCH 104/163] nbd/client: Refactor nbd_receive_list()
383d26
383d26
RH-Author: John Snow <jsnow@redhat.com>
383d26
Message-id: <20190327172308.31077-30-jsnow@redhat.com>
383d26
Patchwork-id: 85210
383d26
O-Subject: [RHEL-7.7 qemu-kvm-rhev PATCH 29/55] nbd/client: Refactor nbd_receive_list()
383d26
Bugzilla: 1691009
383d26
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
383d26
RH-Acked-by: Max Reitz <mreitz@redhat.com>
383d26
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
383d26
383d26
From: Eric Blake <eblake@redhat.com>
383d26
383d26
Right now, nbd_receive_list() is only called by
383d26
nbd_receive_query_exports(), which in turn is only called if the
383d26
server lacks NBD_OPT_GO but has working option negotiation, and is
383d26
merely used as a quality-of-implementation trick since servers
383d26
can't give decent errors for NBD_OPT_EXPORT_NAME.  However, servers
383d26
that lack NBD_OPT_GO are becoming increasingly rare (nbdkit was a
383d26
latecomer, in Aug 2018, but qemu has been such a server since commit
383d26
f37708f6 in July 2017 and released in 2.10), so it no longer makes
383d26
sense to micro-optimize that function for performance.
383d26
383d26
Furthermore, when debugging a server's implementation, tracing the
383d26
full reply (both names and descriptions) is useful, not to mention
383d26
that upcoming patches adding 'qemu-nbd --list' will want to collect
383d26
that data.  And when you consider that a server can send an export
383d26
name up to the NBD protocol length limit of 4k; but our current
383d26
NBD_MAX_NAME_SIZE is only 256, we can't trace all valid server
383d26
names without more storage, but 4k is large enough that the heap
383d26
is better than the stack for long names.
383d26
383d26
Thus, I'm changing the division of labor, with nbd_receive_list()
383d26
now always malloc'ing a result on success (the malloc is bounded
383d26
by the fact that we reject servers with a reply length larger
383d26
than 32M), and moving the comparison to 'wantname' to the caller.
383d26
383d26
There is a minor change in behavior where a server with 0 exports
383d26
(an immediate NBD_REP_ACK reply) is now no longer distinguished
383d26
from a server without LIST support (NBD_REP_ERR_UNSUP); this
383d26
information could be preserved with a complication to the calling
383d26
contract to provide a bit more information, but I didn't see the
383d26
point.  After all, the worst that can happen if our guess at a
383d26
match is wrong is that the caller will get a cryptic disconnect
383d26
when NBD_OPT_EXPORT_NAME fails (which is no different from what
383d26
would happen if we had not tried LIST), while treating an empty
383d26
list as immediate failure would prevent connecting to really old
383d26
servers that really did lack LIST.  Besides, NBD servers with 0
383d26
exports are rare (qemu can do it when using QMP nbd-server-start
383d26
without nbd-server-add - but qemu understands NBD_OPT_GO and
383d26
thus won't tickle this change in behavior).
383d26
383d26
Fix the spelling of foundExport to match coding standards while
383d26
in the area.
383d26
383d26
Signed-off-by: Eric Blake <eblake@redhat.com>
383d26
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
383d26
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
383d26
Message-Id: <20190117193658.16413-9-eblake@redhat.com>
383d26
(cherry picked from commit 091d0bf3c94737fc451a9b3f4eddf3a4d74c90b8)
383d26
Signed-off-by: John Snow <jsnow@redhat.com>
383d26
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
383d26
---
383d26
 nbd/client.c     | 91 ++++++++++++++++++++++++++++++++++++--------------------
383d26
 nbd/trace-events |  1 +
383d26
 2 files changed, 59 insertions(+), 33 deletions(-)
383d26
383d26
diff --git a/nbd/client.c b/nbd/client.c
383d26
index f625c20..fd4ba8d 100644
383d26
--- a/nbd/client.c
383d26
+++ b/nbd/client.c
383d26
@@ -234,18 +234,24 @@ static int nbd_handle_reply_err(QIOChannel *ioc, NBDOptionReply *reply,
383d26
     return result;
383d26
 }
383d26
 
383d26
-/* Process another portion of the NBD_OPT_LIST reply.  Set *@match if
383d26
- * the current reply matches @want or if the server does not support
383d26
- * NBD_OPT_LIST, otherwise leave @match alone.  Return 0 if iteration
383d26
- * is complete, positive if more replies are expected, or negative
383d26
- * with @errp set if an unrecoverable error occurred. */
383d26
-static int nbd_receive_list(QIOChannel *ioc, const char *want, bool *match,
383d26
+/* nbd_receive_list:
383d26
+ * Process another portion of the NBD_OPT_LIST reply, populating any
383d26
+ * name received into *@name. If @description is non-NULL, and the
383d26
+ * server provided a description, that is also populated. The caller
383d26
+ * must eventually call g_free() on success.
383d26
+ * Returns 1 if name and description were set and iteration must continue,
383d26
+ *         0 if iteration is complete (including if OPT_LIST unsupported),
383d26
+ *         -1 with @errp set if an unrecoverable error occurred.
383d26
+ */
383d26
+static int nbd_receive_list(QIOChannel *ioc, char **name, char **description,
383d26
                             Error **errp)
383d26
 {
383d26
+    int ret = -1;
383d26
     NBDOptionReply reply;
383d26
     uint32_t len;
383d26
     uint32_t namelen;
383d26
-    char name[NBD_MAX_NAME_SIZE + 1];
383d26
+    char *local_name = NULL;
383d26
+    char *local_desc = NULL;
383d26
     int error;
383d26
 
383d26
     if (nbd_receive_option_reply(ioc, NBD_OPT_LIST, &reply, errp) < 0) {
383d26
@@ -253,9 +259,6 @@ static int nbd_receive_list(QIOChannel *ioc, const char *want, bool *match,
383d26
     }
383d26
     error = nbd_handle_reply_err(ioc, &reply, errp);
383d26
     if (error <= 0) {
383d26
-        /* The server did not support NBD_OPT_LIST, so set *match on
383d26
-         * the assumption that any name will be accepted.  */
383d26
-        *match = true;
383d26
         return error;
383d26
     }
383d26
     len = reply.length;
383d26
@@ -292,33 +295,38 @@ static int nbd_receive_list(QIOChannel *ioc, const char *want, bool *match,
383d26
         nbd_send_opt_abort(ioc);
383d26
         return -1;
383d26
     }
383d26
-    if (namelen != strlen(want)) {
383d26
-        if (nbd_drop(ioc, len, errp) < 0) {
383d26
-            error_prepend(errp,
383d26
-                          "failed to skip export name with wrong length: ");
383d26
-            nbd_send_opt_abort(ioc);
383d26
-            return -1;
383d26
-        }
383d26
-        return 1;
383d26
-    }
383d26
 
383d26
-    assert(namelen < sizeof(name));
383d26
-    if (nbd_read(ioc, name, namelen, errp) < 0) {
383d26
+    local_name = g_malloc(namelen + 1);
383d26
+    if (nbd_read(ioc, local_name, namelen, errp) < 0) {
383d26
         error_prepend(errp, "failed to read export name: ");
383d26
         nbd_send_opt_abort(ioc);
383d26
-        return -1;
383d26
+        goto out;
383d26
     }
383d26
-    name[namelen] = '\0';
383d26
+    local_name[namelen] = '\0';
383d26
     len -= namelen;
383d26
-    if (nbd_drop(ioc, len, errp) < 0) {
383d26
-        error_prepend(errp, "failed to read export description: ");
383d26
-        nbd_send_opt_abort(ioc);
383d26
-        return -1;
383d26
+    if (len) {
383d26
+        local_desc = g_malloc(len + 1);
383d26
+        if (nbd_read(ioc, local_desc, len, errp) < 0) {
383d26
+            error_prepend(errp, "failed to read export description: ");
383d26
+            nbd_send_opt_abort(ioc);
383d26
+            goto out;
383d26
+        }
383d26
+        local_desc[len] = '\0';
383d26
     }
383d26
-    if (!strcmp(name, want)) {
383d26
-        *match = true;
383d26
+
383d26
+    trace_nbd_receive_list(local_name, local_desc ?: "");
383d26
+    *name = local_name;
383d26
+    local_name = NULL;
383d26
+    if (description) {
383d26
+        *description = local_desc;
383d26
+        local_desc = NULL;
383d26
     }
383d26
-    return 1;
383d26
+    ret = 1;
383d26
+
383d26
+ out:
383d26
+    g_free(local_name);
383d26
+    g_free(local_desc);
383d26
+    return ret;
383d26
 }
383d26
 
383d26
 
383d26
@@ -493,7 +501,8 @@ static int nbd_receive_query_exports(QIOChannel *ioc,
383d26
                                      const char *wantname,
383d26
                                      Error **errp)
383d26
 {
383d26
-    bool foundExport = false;
383d26
+    bool list_empty = true;
383d26
+    bool found_export = false;
383d26
 
383d26
     trace_nbd_receive_query_exports_start(wantname);
383d26
     if (nbd_send_option_request(ioc, NBD_OPT_LIST, 0, NULL, errp) < 0) {
383d26
@@ -501,14 +510,25 @@ static int nbd_receive_query_exports(QIOChannel *ioc,
383d26
     }
383d26
 
383d26
     while (1) {
383d26
-        int ret = nbd_receive_list(ioc, wantname, &foundExport, errp);
383d26
+        char *name;
383d26
+        int ret = nbd_receive_list(ioc, &name, NULL, errp);
383d26
 
383d26
         if (ret < 0) {
383d26
             /* Server gave unexpected reply */
383d26
             return -1;
383d26
         } else if (ret == 0) {
383d26
             /* Done iterating. */
383d26
-            if (!foundExport) {
383d26
+            if (list_empty) {
383d26
+                /*
383d26
+                 * We don't have enough context to tell a server that
383d26
+                 * sent an empty list apart from a server that does
383d26
+                 * not support the list command; but as this function
383d26
+                 * is just used to trigger a nicer error message
383d26
+                 * before trying NBD_OPT_EXPORT_NAME, assume the
383d26
+                 * export is available.
383d26
+                 */
383d26
+                return 0;
383d26
+            } else if (!found_export) {
383d26
                 error_setg(errp, "No export with name '%s' available",
383d26
                            wantname);
383d26
                 nbd_send_opt_abort(ioc);
383d26
@@ -517,6 +537,11 @@ static int nbd_receive_query_exports(QIOChannel *ioc,
383d26
             trace_nbd_receive_query_exports_success(wantname);
383d26
             return 0;
383d26
         }
383d26
+        list_empty = false;
383d26
+        if (!strcmp(name, wantname)) {
383d26
+            found_export = true;
383d26
+        }
383d26
+        g_free(name);
383d26
     }
383d26
 }
383d26
 
383d26
diff --git a/nbd/trace-events b/nbd/trace-events
383d26
index 5492042..d1e1ca6 100644
383d26
--- a/nbd/trace-events
383d26
+++ b/nbd/trace-events
383d26
@@ -3,6 +3,7 @@ nbd_send_option_request(uint32_t opt, const char *name, uint32_t len) "Sending o
383d26
 nbd_receive_option_reply(uint32_t option, const char *optname, uint32_t type, const char *typename, uint32_t length) "Received option reply %" PRIu32" (%s), type %" PRIu32" (%s), len %" PRIu32
383d26
 nbd_server_error_msg(uint32_t err, const char *type, const char *msg) "server reported error 0x%" PRIx32 " (%s) with additional message: %s"
383d26
 nbd_reply_err_unsup(uint32_t option, const char *name) "server doesn't understand request %" PRIu32 " (%s), attempting fallback"
383d26
+nbd_receive_list(const char *name, const char *desc) "export list includes '%s', description '%s'"
383d26
 nbd_opt_go_start(const char *name) "Attempting NBD_OPT_GO for export '%s'"
383d26
 nbd_opt_go_success(void) "Export is good to go"
383d26
 nbd_opt_go_info_unknown(int info, const char *name) "Ignoring unknown info %d (%s)"
383d26
-- 
383d26
1.8.3.1
383d26