{"id":831197,"url":"http://patchwork.ozlabs.org/api/1.2/patches/831197/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20171027104037.8319-11-eblake@redhat.com/","project":{"id":14,"url":"http://patchwork.ozlabs.org/api/1.2/projects/14/?format=json","name":"QEMU Development","link_name":"qemu-devel","list_id":"qemu-devel.nongnu.org","list_email":"qemu-devel@nongnu.org","web_url":"","scm_url":"","webscm_url":"","list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<20171027104037.8319-11-eblake@redhat.com>","list_archive_url":null,"date":"2017-10-27T10:40:35","name":"[v6,10/12] nbd/client: prepare nbd_receive_reply for structured reply","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"5e4c4fbabf218de6186423a3db271c352ab3b1e0","submitter":{"id":6591,"url":"http://patchwork.ozlabs.org/api/1.2/people/6591/?format=json","name":"Eric Blake","email":"eblake@redhat.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20171027104037.8319-11-eblake@redhat.com/mbox/","series":[{"id":10552,"url":"http://patchwork.ozlabs.org/api/1.2/series/10552/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/list/?series=10552","date":"2017-10-27T10:40:27","name":"nbd minimal structured read","version":6,"mbox":"http://patchwork.ozlabs.org/series/10552/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/831197/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/831197/checks/","tags":{},"related":[],"headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)","ext-mx03.extmail.prod.ext.phx2.redhat.com;\n\tdmarc=none (p=none dis=none) header.from=redhat.com","ext-mx03.extmail.prod.ext.phx2.redhat.com;\n\tspf=fail smtp.mailfrom=eblake@redhat.com"],"Received":["from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3yNgfB6bCMz9t30\n\tfor <incoming@patchwork.ozlabs.org>;\n\tFri, 27 Oct 2017 21:51:14 +1100 (AEDT)","from localhost ([::1]:56591 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1e82EL-0000GJ-3l\n\tfor incoming@patchwork.ozlabs.org; Fri, 27 Oct 2017 06:51:13 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:57494)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <eblake@redhat.com>) id 1e824c-0001Tf-6d\n\tfor qemu-devel@nongnu.org; Fri, 27 Oct 2017 06:41:13 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <eblake@redhat.com>) id 1e824a-0005mx-N6\n\tfor qemu-devel@nongnu.org; Fri, 27 Oct 2017 06:41:10 -0400","from mx1.redhat.com ([209.132.183.28]:60456)\n\tby eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32)\n\t(Exim 4.71) (envelope-from <eblake@redhat.com>)\n\tid 1e824X-0005kr-75; Fri, 27 Oct 2017 06:41:05 -0400","from smtp.corp.redhat.com\n\t(int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16])\n\t(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby mx1.redhat.com (Postfix) with ESMTPS id 46C0E7E42F;\n\tFri, 27 Oct 2017 10:41:04 +0000 (UTC)","from red.redhat.com (ovpn-120-166.rdu2.redhat.com [10.10.120.166])\n\tby smtp.corp.redhat.com (Postfix) with ESMTP id D77B05C549;\n\tFri, 27 Oct 2017 10:41:00 +0000 (UTC)"],"DMARC-Filter":"OpenDMARC Filter v1.3.2 mx1.redhat.com 46C0E7E42F","From":"Eric Blake <eblake@redhat.com>","To":"qemu-devel@nongnu.org","Date":"Fri, 27 Oct 2017 12:40:35 +0200","Message-Id":"<20171027104037.8319-11-eblake@redhat.com>","In-Reply-To":"<20171027104037.8319-1-eblake@redhat.com>","References":"<20171027104037.8319-1-eblake@redhat.com>","X-Scanned-By":"MIMEDefang 2.79 on 10.5.11.16","X-Greylist":"Sender IP whitelisted, not delayed by milter-greylist-4.5.16\n\t(mx1.redhat.com [10.5.110.27]);\n\tFri, 27 Oct 2017 10:41:04 +0000 (UTC)","X-detected-operating-system":"by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]\n\t[fuzzy]","X-Received-From":"209.132.183.28","Subject":"[Qemu-devel] [PATCH v6 10/12] nbd/client: prepare nbd_receive_reply\n\tfor structured reply","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.21","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<http://lists.nongnu.org/archive/html/qemu-devel/>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"Kevin Wolf <kwolf@redhat.com>, pbonzini@redhat.com,\n\tvsementsov@virtuozzo.com, qemu-block@nongnu.org,\n\tMax Reitz <mreitz@redhat.com>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"},"content":"From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>\n\nIn following patch nbd_receive_reply will be used both for simple\nand structured reply header receiving.\nNBDReply is altered into union of simple reply header and structured\nreply chunk header, simple error translation moved to block/nbd-client\nto be consistent with further structured reply error translation.\n\nSigned-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>\nSigned-off-by: Eric Blake <eblake@redhat.com>\n\n---\nv5: minor whitespace tweak\n---\n include/block/nbd.h |  30 ++++++++++++---\n block/nbd-client.c  |   8 ++--\n nbd/client.c        | 104 +++++++++++++++++++++++++++++++++++++++++-----------\n nbd/trace-events    |   3 +-\n 4 files changed, 113 insertions(+), 32 deletions(-)","diff":"diff --git a/include/block/nbd.h b/include/block/nbd.h\nindex 225e9575e4..2ee1578420 100644\n--- a/include/block/nbd.h\n+++ b/include/block/nbd.h\n@@ -57,12 +57,6 @@ struct NBDRequest {\n };\n typedef struct NBDRequest NBDRequest;\n\n-struct NBDReply {\n-    uint64_t handle;\n-    uint32_t error;\n-};\n-typedef struct NBDReply NBDReply;\n-\n typedef struct NBDSimpleReply {\n     uint32_t magic;  /* NBD_SIMPLE_REPLY_MAGIC */\n     uint32_t error;\n@@ -78,6 +72,20 @@ typedef struct NBDStructuredReplyChunk {\n     uint32_t length; /* length of payload */\n } QEMU_PACKED NBDStructuredReplyChunk;\n\n+typedef union NBDReply {\n+    NBDSimpleReply simple;\n+    NBDStructuredReplyChunk structured;\n+    struct {\n+        /* @magic and @handle fields have the same offset and size both in\n+         * simple reply and structured reply chunk, so let them be accessible\n+         * without \".simple.\" or \".structured.\" specification\n+         */\n+        uint32_t magic;\n+        uint32_t _skip;\n+        uint64_t handle;\n+    } QEMU_PACKED;\n+} NBDReply;\n+\n /* Header of NBD_REPLY_TYPE_OFFSET_DATA, complete NBD_REPLY_TYPE_OFFSET_HOLE */\n typedef struct NBDStructuredRead {\n     NBDStructuredReplyChunk h;\n@@ -256,4 +264,14 @@ void nbd_client_put(NBDClient *client);\n void nbd_server_start(SocketAddress *addr, const char *tls_creds,\n                       Error **errp);\n\n+static inline bool nbd_reply_is_simple(NBDReply *reply)\n+{\n+    return reply->magic == NBD_SIMPLE_REPLY_MAGIC;\n+}\n+\n+static inline bool nbd_reply_is_structured(NBDReply *reply)\n+{\n+    return reply->magic == NBD_STRUCTURED_REPLY_MAGIC;\n+}\n+\n #endif\ndiff --git a/block/nbd-client.c b/block/nbd-client.c\nindex c0683c3c83..58493b7ac4 100644\n--- a/block/nbd-client.c\n+++ b/block/nbd-client.c\n@@ -92,7 +92,9 @@ static coroutine_fn void nbd_read_reply_entry(void *opaque)\n         i = HANDLE_TO_INDEX(s, s->reply.handle);\n         if (i >= MAX_NBD_REQUESTS ||\n             !s->requests[i].coroutine ||\n-            !s->requests[i].receiving) {\n+            !s->requests[i].receiving ||\n+            nbd_reply_is_structured(&s->reply))\n+        {\n             break;\n         }\n\n@@ -194,8 +196,8 @@ static int nbd_co_receive_reply(NBDClientSession *s,\n         ret = -EIO;\n     } else {\n         assert(s->reply.handle == handle);\n-        ret = -s->reply.error;\n-        if (qiov && s->reply.error == 0) {\n+        ret = -nbd_errno_to_system_errno(s->reply.simple.error);\n+        if (qiov && ret == 0) {\n             if (qio_channel_readv_all(s->ioc, qiov->iov, qiov->niov,\n                                       NULL) < 0) {\n                 ret = -EIO;\ndiff --git a/nbd/client.c b/nbd/client.c\nindex 9acf745b79..4f0745f601 100644\n--- a/nbd/client.c\n+++ b/nbd/client.c\n@@ -908,6 +908,57 @@ int nbd_send_request(QIOChannel *ioc, NBDRequest *request)\n     return nbd_write(ioc, buf, sizeof(buf), NULL);\n }\n\n+/* nbd_receive_simple_reply\n+ * Read simple reply except magic field (which should be already read).\n+ * Payload is not read (payload is possible for CMD_READ, but here we even\n+ * don't know whether it take place or not).\n+ */\n+static int nbd_receive_simple_reply(QIOChannel *ioc, NBDSimpleReply *reply,\n+                                    Error **errp)\n+{\n+    int ret;\n+\n+    assert(reply->magic == NBD_SIMPLE_REPLY_MAGIC);\n+\n+    ret = nbd_read(ioc, (uint8_t *)reply + sizeof(reply->magic),\n+                   sizeof(*reply) - sizeof(reply->magic), errp);\n+    if (ret < 0) {\n+        return ret;\n+    }\n+\n+    be32_to_cpus(&reply->error);\n+    be64_to_cpus(&reply->handle);\n+\n+    return 0;\n+}\n+\n+/* nbd_receive_structured_reply_chunk\n+ * Read structured reply chunk except magic field (which should be already\n+ * read).\n+ * Payload is not read.\n+ */\n+static int nbd_receive_structured_reply_chunk(QIOChannel *ioc,\n+                                              NBDStructuredReplyChunk *chunk,\n+                                              Error **errp)\n+{\n+    int ret;\n+\n+    assert(chunk->magic == NBD_STRUCTURED_REPLY_MAGIC);\n+\n+    ret = nbd_read(ioc, (uint8_t *)chunk + sizeof(chunk->magic),\n+                   sizeof(*chunk) - sizeof(chunk->magic), errp);\n+    if (ret < 0) {\n+        return ret;\n+    }\n+\n+    be16_to_cpus(&chunk->flags);\n+    be16_to_cpus(&chunk->type);\n+    be64_to_cpus(&chunk->handle);\n+    be32_to_cpus(&chunk->length);\n+\n+    return 0;\n+}\n+\n /* nbd_receive_reply\n  * Returns 1 on success\n  *         0 on eof, when no data was read (errp is not set)\n@@ -915,38 +966,47 @@ int nbd_send_request(QIOChannel *ioc, NBDRequest *request)\n  */\n int nbd_receive_reply(QIOChannel *ioc, NBDReply *reply, Error **errp)\n {\n-    uint8_t buf[NBD_REPLY_SIZE];\n-    uint32_t magic;\n     int ret;\n\n-    ret = nbd_read_eof(ioc, buf, sizeof(buf), errp);\n+    ret = nbd_read_eof(ioc, &reply->magic, sizeof(reply->magic), errp);\n     if (ret <= 0) {\n         return ret;\n     }\n\n-    /* Reply\n-       [ 0 ..  3]    magic   (NBD_SIMPLE_REPLY_MAGIC)\n-       [ 4 ..  7]    error   (0 == no error)\n-       [ 7 .. 15]    handle\n-     */\n+    be32_to_cpus(&reply->magic);\n\n-    magic = ldl_be_p(buf);\n-    reply->error  = ldl_be_p(buf + 4);\n-    reply->handle = ldq_be_p(buf + 8);\n+    switch (reply->magic) {\n+    case NBD_SIMPLE_REPLY_MAGIC:\n+        ret = nbd_receive_simple_reply(ioc, &reply->simple, errp);\n+        if (ret < 0) {\n+            break;\n+        }\n\n-    trace_nbd_receive_reply(magic, reply->error, nbd_err_lookup(reply->error),\n-                            reply->handle);\n-    reply->error = nbd_errno_to_system_errno(reply->error);\n-\n-    if (reply->error == ESHUTDOWN) {\n-        /* This works even on mingw which lacks a native ESHUTDOWN */\n-        error_setg(errp, \"server shutting down\");\n+        trace_nbd_receive_simple_reply(reply->simple.error,\n+                                       nbd_err_lookup(reply->simple.error),\n+                                       reply->handle);\n+        if (reply->simple.error == NBD_ESHUTDOWN) {\n+            /* This works even on mingw which lacks a native ESHUTDOWN */\n+            error_setg(errp, \"server shutting down\");\n+            return -EINVAL;\n+        }\n+        break;\n+    case NBD_STRUCTURED_REPLY_MAGIC:\n+        ret = nbd_receive_structured_reply_chunk(ioc, &reply->structured, errp);\n+        if (ret < 0) {\n+            break;\n+        }\n+        trace_nbd_receive_structured_reply_chunk(reply->structured.flags,\n+                                                 reply->structured.type,\n+                                                 reply->structured.handle,\n+                                                 reply->structured.length);\n+        break;\n+    default:\n+        error_setg(errp, \"invalid magic (got 0x%\" PRIx32 \")\", reply->magic);\n         return -EINVAL;\n     }\n-\n-    if (magic != NBD_SIMPLE_REPLY_MAGIC) {\n-        error_setg(errp, \"invalid magic (got 0x%\" PRIx32 \")\", magic);\n-        return -EINVAL;\n+    if (ret < 0) {\n+        return ret;\n     }\n\n     return 1;\ndiff --git a/nbd/trace-events b/nbd/trace-events\nindex 596df96575..4a13757524 100644\n--- a/nbd/trace-events\n+++ b/nbd/trace-events\n@@ -26,7 +26,8 @@ nbd_client_loop_ret(int ret, const char *error) \"NBD loop returned %d: %s\"\n nbd_client_clear_queue(void) \"Clearing NBD queue\"\n nbd_client_clear_socket(void) \"Clearing NBD socket\"\n nbd_send_request(uint64_t from, uint32_t len, uint64_t handle, uint16_t flags, uint16_t type, const char *name) \"Sending request to server: { .from = %\" PRIu64\", .len = %\" PRIu32 \", .handle = %\" PRIu64 \", .flags = 0x%\" PRIx16 \", .type = %\" PRIu16 \" (%s) }\"\n-nbd_receive_reply(uint32_t magic, int32_t error, const char *errname, uint64_t handle) \"Got reply: { magic = 0x%\" PRIx32 \", .error = %\" PRId32 \" (%s), handle = %\" PRIu64\" }\"\n+nbd_receive_simple_reply(int32_t error, const char *errname, uint64_t handle) \"Got simple reply: { .error = %\" PRId32 \" (%s), handle = %\" PRIu64\" }\"\n+nbd_receive_structured_reply_chunk(uint16_t flags, uint16_t type, uint64_t handle, uint32_t length) \"Got structured reply chunk: { flags = 0x%\" PRIx16 \", type = %d, handle = %\" PRIu64 \", length = %\" PRIu32 \" }\"\n\n # nbd/common.c\n nbd_unknown_error(int err) \"Squashing unexpected error %d to EINVAL\"\n","prefixes":["v6","10/12"]}