{"id":818250,"url":"http://patchwork.ozlabs.org/api/patches/818250/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20170925135801.144261-9-vsementsov@virtuozzo.com/","project":{"id":14,"url":"http://patchwork.ozlabs.org/api/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":"<20170925135801.144261-9-vsementsov@virtuozzo.com>","list_archive_url":null,"date":"2017-09-25T13:58:01","name":"[8/8] nbd: Minimal structured read for client","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"8bb986d5bb1c7dad8a4c35ea4c0000413b8b24fb","submitter":{"id":66592,"url":"http://patchwork.ozlabs.org/api/people/66592/?format=json","name":"Vladimir Sementsov-Ogievskiy","email":"vsementsov@virtuozzo.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20170925135801.144261-9-vsementsov@virtuozzo.com/mbox/","series":[{"id":4967,"url":"http://patchwork.ozlabs.org/api/series/4967/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/list/?series=4967","date":"2017-09-25T13:58:01","name":"nbd minimal structured read","version":1,"mbox":"http://patchwork.ozlabs.org/series/4967/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/818250/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/818250/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>)","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 3y15L62xLrz9tXC\n\tfor <incoming@patchwork.ozlabs.org>;\n\tMon, 25 Sep 2017 23:59:26 +1000 (AEST)","from localhost ([::1]:42569 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 1dwTuu-0006mx-Ej\n\tfor incoming@patchwork.ozlabs.org; Mon, 25 Sep 2017 09:59:24 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:39689)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <vsementsov@virtuozzo.com>) id 1dwTu0-0006Zo-Oj\n\tfor qemu-devel@nongnu.org; Mon, 25 Sep 2017 09:58:30 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <vsementsov@virtuozzo.com>) id 1dwTtv-0007Dh-TN\n\tfor qemu-devel@nongnu.org; Mon, 25 Sep 2017 09:58:28 -0400","from mailhub.sw.ru ([195.214.232.25]:8054 helo=relay.sw.ru)\n\tby eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32)\n\t(Exim 4.71) (envelope-from <vsementsov@virtuozzo.com>)\n\tid 1dwTtv-0007C9-Ak\n\tfor qemu-devel@nongnu.org; Mon, 25 Sep 2017 09:58:23 -0400","from kvm.sw.ru (msk-vpn.virtuozzo.com [195.214.232.6])\n\tby relay.sw.ru (8.13.4/8.13.4) with ESMTP id v8PDw1g0013085;\n\tMon, 25 Sep 2017 16:58:03 +0300 (MSK)"],"From":"Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>","To":"qemu-devel@nongnu.org, qemu-block@nongnu.org","Date":"Mon, 25 Sep 2017 16:58:01 +0300","Message-Id":"<20170925135801.144261-9-vsementsov@virtuozzo.com>","X-Mailer":"git-send-email 2.11.1","In-Reply-To":"<20170925135801.144261-1-vsementsov@virtuozzo.com>","References":"<20170925135801.144261-1-vsementsov@virtuozzo.com>","X-detected-operating-system":"by eggs.gnu.org: OpenBSD 3.x [fuzzy]","X-Received-From":"195.214.232.25","Subject":"[Qemu-devel] [PATCH 8/8] nbd: Minimal structured read for client","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":"kwolf@redhat.com, vsementsov@virtuozzo.com, Hmreitz@redhat.com,\n\tden@openvz.org, pbonzini@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":"Minimal implementation: drop most of additional error information.\n\nSigned-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>\n---\n block/nbd-client.h  |   2 +\n include/block/nbd.h |  15 ++++-\n block/nbd-client.c  |  97 +++++++++++++++++++++++++-----\n nbd/client.c        | 169 +++++++++++++++++++++++++++++++++++++++++++++++-----\n 4 files changed, 249 insertions(+), 34 deletions(-)","diff":"diff --git a/block/nbd-client.h b/block/nbd-client.h\nindex b435754b82..9e178de510 100644\n--- a/block/nbd-client.h\n+++ b/block/nbd-client.h\n@@ -35,6 +35,8 @@ typedef struct NBDClientSession {\n     NBDClientRequest requests[MAX_NBD_REQUESTS];\n     NBDReply reply;\n     bool quit;\n+\n+    bool structured_reply;\n } NBDClientSession;\n \n NBDClientSession *nbd_get_client_session(BlockDriverState *bs);\ndiff --git a/include/block/nbd.h b/include/block/nbd.h\nindex 314f2f9bbc..7604e80c49 100644\n--- a/include/block/nbd.h\n+++ b/include/block/nbd.h\n@@ -57,11 +57,17 @@ struct NBDRequest {\n };\n typedef struct NBDRequest NBDRequest;\n \n-struct NBDReply {\n+typedef struct NBDReply {\n+    bool simple;\n     uint64_t handle;\n     uint32_t error;\n-};\n-typedef struct NBDReply NBDReply;\n+\n+    uint16_t flags;\n+    uint16_t type;\n+    uint32_t tail_length;\n+    uint64_t offset;\n+    uint32_t hole_size;\n+} NBDReply;\n \n typedef struct NBDSimpleReply {\n     uint32_t magic;  /* NBD_SIMPLE_REPLY_MAGIC */\n@@ -178,12 +184,15 @@ enum {\n \n #define NBD_SREP_TYPE_NONE          0\n #define NBD_SREP_TYPE_OFFSET_DATA   1\n+#define NBD_SREP_TYPE_OFFSET_HOLE   2\n #define NBD_SREP_TYPE_ERROR         NBD_SREP_ERR(1)\n+#define NBD_SREP_TYPE_ERROR_OFFSET  NBD_SREP_ERR(2)\n \n /* Details collected by NBD_OPT_EXPORT_NAME and NBD_OPT_GO */\n struct NBDExportInfo {\n     /* Set by client before nbd_receive_negotiate() */\n     bool request_sizes;\n+    bool structured_reply;\n     /* Set by server results during nbd_receive_negotiate() */\n     uint64_t size;\n     uint16_t flags;\ndiff --git a/block/nbd-client.c b/block/nbd-client.c\nindex e4f0c789f4..bdf9299bb9 100644\n--- a/block/nbd-client.c\n+++ b/block/nbd-client.c\n@@ -179,9 +179,10 @@ err:\n     return rc;\n }\n \n-static int nbd_co_receive_reply(NBDClientSession *s,\n-                                uint64_t handle,\n-                                QEMUIOVector *qiov)\n+static int nbd_co_receive_1_reply_or_chunk(NBDClientSession *s,\n+                                           uint64_t handle,\n+                                           bool *cont,\n+                                           QEMUIOVector *qiov)\n {\n     int ret;\n     int i = HANDLE_TO_INDEX(s, handle);\n@@ -191,29 +192,95 @@ static int nbd_co_receive_reply(NBDClientSession *s,\n     qemu_coroutine_yield();\n     s->requests[i].receiving = false;\n     if (!s->ioc || s->quit) {\n-        ret = -EIO;\n-    } else {\n-        assert(s->reply.handle == handle);\n-        ret = -s->reply.error;\n-        if (qiov && s->reply.error == 0) {\n+        *cont = false;\n+        return -EIO;\n+    }\n+\n+    assert(s->reply.handle == handle);\n+    *cont = !(s->reply.simple || (s->reply.flags & NBD_SREP_FLAG_DONE));\n+    ret = -s->reply.error;\n+    if (ret < 0) {\n+        goto out;\n+    }\n+\n+    if (s->reply.simple) {\n+        if (qiov) {\n             if (qio_channel_readv_all(s->ioc, qiov->iov, qiov->niov,\n-                                      NULL) < 0) {\n-                ret = -EIO;\n-                s->quit = true;\n+                                      NULL) < 0)\n+            {\n+                goto fatal;\n             }\n         }\n+        goto out;\n+    }\n \n-        /* Tell the read handler to read another header.  */\n-        s->reply.handle = 0;\n+    /* here we deal with successful structured reply */\n+    switch (s->reply.type) {\n+        QEMUIOVector sub_qiov;\n+    case NBD_SREP_TYPE_OFFSET_DATA:\n+        if (!qiov || s->reply.offset + s->reply.tail_length > qiov->size) {\n+            goto fatal;\n+        }\n+        qemu_iovec_init(&sub_qiov, qiov->niov);\n+        qemu_iovec_concat(&sub_qiov, qiov, s->reply.offset,\n+                          s->reply.tail_length);\n+        ret = qio_channel_readv_all(s->ioc, sub_qiov.iov, sub_qiov.niov, NULL);\n+        qemu_iovec_destroy(&sub_qiov);\n+        if (ret < 0) {\n+            goto fatal;\n+        }\n+        assert(ret == 0);\n+        break;\n+    case NBD_SREP_TYPE_OFFSET_HOLE:\n+        if (!qiov || s->reply.offset + s->reply.hole_size > qiov->size) {\n+            goto fatal;\n+        }\n+        qemu_iovec_memset(qiov, s->reply.offset, 0, s->reply.hole_size);\n+        break;\n+    case NBD_SREP_TYPE_NONE:\n+        break;\n+    default:\n+        goto fatal;\n     }\n \n-    s->requests[i].coroutine = NULL;\n+out:\n+    /* For assert at loop start in nbd_read_reply_entry */\n+    s->reply.handle = 0;\n+\n+    if (s->read_reply_co) {\n+        aio_co_wake(s->read_reply_co);\n+    }\n+\n+    return ret;\n \n-    /* Kick the read_reply_co to get the next reply.  */\n+fatal:\n+    /* protocol or ioc failure */\n+    *cont = false;\n+    s->quit = true;\n     if (s->read_reply_co) {\n         aio_co_wake(s->read_reply_co);\n     }\n \n+    return -EIO;\n+}\n+\n+static int nbd_co_receive_reply(NBDClientSession *s,\n+                                uint64_t handle,\n+                                QEMUIOVector *qiov)\n+{\n+    int ret = 0;\n+    int i = HANDLE_TO_INDEX(s, handle);\n+    bool cont = true;\n+\n+    while (cont) {\n+        int rc = nbd_co_receive_1_reply_or_chunk(s, handle, &cont, qiov);\n+        if (rc < 0 && ret == 0) {\n+            ret = rc;\n+        }\n+    }\n+\n+    s->requests[i].coroutine = NULL;\n+\n     qemu_co_mutex_lock(&s->send_mutex);\n     s->in_flight--;\n     qemu_co_queue_next(&s->free_sema);\ndiff --git a/nbd/client.c b/nbd/client.c\nindex 51ae492e92..880eb17b85 100644\n--- a/nbd/client.c\n+++ b/nbd/client.c\n@@ -719,6 +719,13 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name,\n         if (fixedNewStyle) {\n             int result;\n \n+            result = nbd_request_simple_option(ioc, NBD_OPT_STRUCTURED_REPLY,\n+                                               errp);\n+            if (result < 0) {\n+                goto fail;\n+            }\n+            info->structured_reply = result > 0;\n+\n             /* Try NBD_OPT_GO first - if it works, we are done (it\n              * also gives us a good message if the server requires\n              * TLS).  If it is not available, fall back to\n@@ -759,6 +766,12 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name,\n             goto fail;\n         }\n         be16_to_cpus(&info->flags);\n+\n+        if (info->structured_reply && !(info->flags & NBD_CMD_FLAG_DF)) {\n+            error_setg(errp, \"Structured reply is negotiated, \"\n+                             \"but DF flag is not.\");\n+            goto fail;\n+        }\n     } else if (magic == NBD_CLIENT_MAGIC) {\n         uint32_t oldflags;\n \n@@ -942,6 +955,128 @@ 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+ */\n+static int nbd_receive_simple_reply(QIOChannel *ioc, NBDReply *reply,\n+                                    Error **errp)\n+{\n+    NBDSimpleReply simple_reply;\n+    int ret;\n+\n+    ret = nbd_read(ioc, (uint8_t *)&simple_reply + sizeof(simple_reply.magic),\n+                   sizeof(simple_reply) - sizeof(simple_reply.magic), errp);\n+    if (ret < 0) {\n+        return ret;\n+    }\n+\n+    reply->error = be32_to_cpu(simple_reply.error);\n+    reply->handle = be64_to_cpu(simple_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 read)\n+ * Data for NBD_SREP_TYPE_OFFSET_DATA is not read too.\n+ * tail_length field of reply out parameter corresponds to unread part of reply.\n+ */\n+static int nbd_receive_structured_reply_chunk(QIOChannel *ioc, NBDReply *reply,\n+                                              Error **errp)\n+{\n+    NBDStructuredReplyChunk chunk;\n+    ssize_t ret;\n+    uint16_t message_size;\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+    reply->flags = be16_to_cpu(chunk.flags);\n+    reply->type = be16_to_cpu(chunk.type);\n+    reply->handle = be64_to_cpu(chunk.handle);\n+    reply->tail_length = be32_to_cpu(chunk.length);\n+\n+    switch (reply->type) {\n+    case NBD_SREP_TYPE_NONE:\n+        break;\n+    case NBD_SREP_TYPE_OFFSET_DATA:\n+        if (reply->tail_length < sizeof(reply->offset)) {\n+            return -EIO;\n+        }\n+        ret = nbd_read(ioc, &reply->offset, sizeof(reply->offset), errp);\n+        if (ret < 0) {\n+            return ret;\n+        }\n+        be64_to_cpus(&reply->offset);\n+        reply->tail_length -= sizeof(reply->offset);\n+\n+        break;\n+    case NBD_SREP_TYPE_OFFSET_HOLE:\n+        ret = nbd_read(ioc, &reply->offset, sizeof(reply->offset), errp);\n+        if (ret < 0) {\n+            return ret;\n+        }\n+        be64_to_cpus(&reply->offset);\n+\n+        ret = nbd_read(ioc, &reply->hole_size, sizeof(reply->hole_size), errp);\n+        if (ret < 0) {\n+            return ret;\n+        }\n+        be32_to_cpus(&reply->hole_size);\n+\n+        break;\n+    case NBD_SREP_TYPE_ERROR:\n+    case NBD_SREP_TYPE_ERROR_OFFSET:\n+        ret = nbd_read(ioc, &reply->error, sizeof(reply->error), errp);\n+        if (ret < 0) {\n+            return ret;\n+        }\n+        be32_to_cpus(&reply->error);\n+\n+        ret = nbd_read(ioc, &message_size, sizeof(message_size), errp);\n+        if (ret < 0) {\n+            return ret;\n+        }\n+        be16_to_cpus(&message_size);\n+\n+        if (message_size > 0) {\n+            /* TODO: provide error message to user */\n+            ret = nbd_drop(ioc, message_size, errp);\n+            if (ret < 0) {\n+                return ret;\n+            }\n+        }\n+\n+        if (reply->type == NBD_SREP_TYPE_ERROR_OFFSET) {\n+            /* drop 64bit offset */\n+            ret = nbd_drop(ioc, 8, errp);\n+            if (ret < 0) {\n+                return ret;\n+            }\n+        }\n+        break;\n+    default:\n+        if (reply->type & (1 << 15)) {\n+            /* unknown error */\n+            ret = nbd_drop(ioc, reply->tail_length, errp);\n+            if (ret < 0) {\n+                return ret;\n+            }\n+\n+            reply->error = NBD_EINVAL;\n+            reply->tail_length = 0;\n+        } else {\n+            /* unknown non-error reply type */\n+            return -EINVAL;\n+        }\n+    }\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@@ -949,24 +1084,32 @@ 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, &magic, sizeof(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(&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 (magic) {\n+    case NBD_SIMPLE_REPLY_MAGIC:\n+        reply->simple = true;\n+        ret = nbd_receive_simple_reply(ioc, reply, errp);\n+        break;\n+    case NBD_STRUCTURED_REPLY_MAGIC:\n+        reply->simple = false;\n+        ret = nbd_receive_structured_reply_chunk(ioc, reply, errp);\n+        break;\n+    default:\n+        error_setg(errp, \"invalid magic (got 0x%\" PRIx32 \")\", magic);\n+        return -EINVAL;\n+    }\n+    if (ret < 0) {\n+        return ret;\n+    }\n \n     reply->error = nbd_errno_to_system_errno(reply->error);\n \n@@ -977,11 +1120,5 @@ int nbd_receive_reply(QIOChannel *ioc, NBDReply *reply, Error **errp)\n     }\n     trace_nbd_receive_reply(magic, reply->error, reply->handle);\n \n-    if (magic != NBD_SIMPLE_REPLY_MAGIC) {\n-        error_setg(errp, \"invalid magic (got 0x%\" PRIx32 \")\", magic);\n-        return -EINVAL;\n-    }\n-\n     return 1;\n }\n-\n","prefixes":["8/8"]}