qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: qemu-devel@nongnu.org
Cc: vsementsov@virtuozzo.com, pbonzini@redhat.com, qemu-block@nongnu.org
Subject: [Qemu-devel] [PATCH v5 07/11] nbd/server: Include human-readable message in structured errors
Date: Thu, 19 Oct 2017 17:26:33 -0500	[thread overview]
Message-ID: <20171019222637.17890-8-eblake@redhat.com> (raw)
In-Reply-To: <20171019222637.17890-1-eblake@redhat.com>

The NBD spec permits including a human-readable error string if
structured replies are in force, so we might as well send the
client the message that we logged on any error.

Signed-off-by: Eric Blake <eblake@redhat.com>

---
v5: allow NULL msg, cast away const for ease of use [Vladimir]; send
correct length over the wire
---
 nbd/server.c     | 20 +++++++++++++-------
 nbd/trace-events |  2 +-
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/nbd/server.c b/nbd/server.c
index 9be93c4a52..e39d83fb69 100644
--- a/nbd/server.c
+++ b/nbd/server.c
@@ -1280,24 +1280,25 @@ static int coroutine_fn nbd_co_send_structured_read(NBDClient *client,
 static int coroutine_fn nbd_co_send_structured_error(NBDClient *client,
                                                      uint64_t handle,
                                                      uint32_t error,
+                                                     const char *msg,
                                                      Error **errp)
 {
     NBDStructuredError chunk;
     int nbd_err = system_errno_to_nbd_errno(error);
     struct iovec iov[] = {
         {.iov_base = &chunk, .iov_len = sizeof(chunk)},
-        /* FIXME: Support human-readable error message */
+        {.iov_base = (char *)msg, .iov_len = msg ? strlen(msg) : 0},
     };

     assert(nbd_err);
     trace_nbd_co_send_structured_error(handle, nbd_err,
-                                       nbd_err_lookup(nbd_err));
+                                       nbd_err_lookup(nbd_err), msg ? msg : "");
     set_be_chunk(&chunk.h, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_ERROR, handle,
-                 sizeof(chunk) - sizeof(chunk.h));
+                 sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len);
     stl_be_p(&chunk.error, nbd_err);
-    stw_be_p(&chunk.message_length, 0);
+    stw_be_p(&chunk.message_length, iov[1].iov_len);

-    return nbd_co_send_iov(client, iov, 1, errp);
+    return nbd_co_send_iov(client, iov, 1 + !!iov[1].iov_len, errp);
 }

 /* nbd_co_receive_request
@@ -1398,6 +1399,7 @@ static coroutine_fn void nbd_trip(void *opaque)
     int flags;
     int reply_data_len = 0;
     Error *local_err = NULL;
+    char *msg = NULL;

     trace_nbd_trip();
     if (client->closing) {
@@ -1514,14 +1516,17 @@ reply:
     if (local_err) {
         /* If we get here, local_err was not a fatal error, and should be sent
          * to the client. */
+        assert(ret < 0);
+        msg = g_strdup(error_get_pretty(local_err));
         error_report_err(local_err);
         local_err = NULL;
     }

-    if (client->structured_reply && request.type == NBD_CMD_READ) {
+    if (client->structured_reply &&
+        (ret < 0 || request.type == NBD_CMD_READ)) {
         if (ret < 0) {
             ret = nbd_co_send_structured_error(req->client, request.handle,
-                                               -ret, &local_err);
+                                               -ret, msg, &local_err);
         } else {
             ret = nbd_co_send_structured_read(req->client, request.handle,
                                               request.from, req->data,
@@ -1532,6 +1537,7 @@ reply:
                                        ret < 0 ? -ret : 0,
                                        req->data, reply_data_len, &local_err);
     }
+    g_free(msg);
     if (ret < 0) {
         error_prepend(&local_err, "Failed to send reply: ");
         goto disconnect;
diff --git a/nbd/trace-events b/nbd/trace-events
index 6894f8bbb4..52150bd738 100644
--- a/nbd/trace-events
+++ b/nbd/trace-events
@@ -57,7 +57,7 @@ nbd_blk_aio_attached(const char *name, void *ctx) "Export %s: Attaching clients
 nbd_blk_aio_detach(const char *name, void *ctx) "Export %s: Detaching clients from AIO context %p\n"
 nbd_co_send_simple_reply(uint64_t handle, uint32_t error, const char *errname, int len) "Send simple reply: handle = %" PRIu64 ", error = %" PRIu32 " (%s), len = %d"
 nbd_co_send_structured_read(uint64_t handle, uint64_t offset, void *data, size_t size) "Send structured read data reply: handle = %" PRIu64 ", offset = %" PRIu64 ", data = %p, len = %zu"
-nbd_co_send_structured_error(uint64_t handle, int err, const char *errname) "Send structured error reply: handle = %" PRIu64 ", error = %d (%s)"
+nbd_co_send_structured_error(uint64_t handle, int err, const char *errname, const char *msg) "Send structured error reply: handle = %" PRIu64 ", error = %d (%s), msg = '%s'"
 nbd_co_receive_request_decode_type(uint64_t handle, uint16_t type, const char *name) "Decoding type: handle = %" PRIu64 ", type = %" PRIu16 " (%s)"
 nbd_co_receive_request_payload_received(uint64_t handle, uint32_t len) "Payload received: handle = %" PRIu64 ", len = %" PRIu32
 nbd_co_receive_request_cmd_write(uint32_t len) "Reading %" PRIu32 " byte(s)"
-- 
2.13.6

  parent reply	other threads:[~2017-10-19 22:27 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-19 22:26 [Qemu-devel] [PATCH v5 00/11] nbd minimal structured read Eric Blake
2017-10-19 22:26 ` [Qemu-devel] [PATCH v5 01/11] nbd: Include error names in trace messages Eric Blake
2017-10-19 22:26 ` [Qemu-devel] [PATCH v5 02/11] nbd: Move nbd_errno_to_system_errno() to public header Eric Blake
2017-10-19 22:26 ` [Qemu-devel] [PATCH v5 03/11] nbd: Expose constants and structs for structured read Eric Blake
2017-10-20  8:00   ` Vladimir Sementsov-Ogievskiy
2017-10-19 22:26 ` [Qemu-devel] [PATCH v5 04/11] nbd/server: Report error for write to read-only export Eric Blake
2017-10-20  8:06   ` Vladimir Sementsov-Ogievskiy
2017-10-19 22:26 ` [Qemu-devel] [PATCH v5 05/11] nbd/server: Refactor zero-length option check Eric Blake
2017-10-20  8:34   ` Vladimir Sementsov-Ogievskiy
2017-10-20 15:07     ` Eric Blake
2017-10-20 18:12       ` Vladimir Sementsov-Ogievskiy
2017-10-19 22:26 ` [Qemu-devel] [PATCH v5 06/11] nbd: Minimal structured read for server Eric Blake
2017-10-20 19:03   ` Vladimir Sementsov-Ogievskiy
2017-10-20 19:11     ` Eric Blake
2017-10-20 19:30       ` Vladimir Sementsov-Ogievskiy
2017-10-21 16:02         ` Eric Blake
2017-10-19 22:26 ` Eric Blake [this message]
2017-10-20 19:08   ` [Qemu-devel] [PATCH v5 07/11] nbd/server: Include human-readable message in structured errors Vladimir Sementsov-Ogievskiy
2017-10-19 22:26 ` [Qemu-devel] [PATCH v5 08/11] nbd/client: refactor nbd_receive_starttls Eric Blake
2017-10-20 19:26   ` Vladimir Sementsov-Ogievskiy
2017-10-20 19:33     ` Eric Blake
2017-10-19 22:26 ` [Qemu-devel] [PATCH v5 09/11] nbd/client: prepare nbd_receive_reply for structured reply Eric Blake
2017-10-19 22:26 ` [Qemu-devel] [PATCH v5 10/11] nbd: Move nbd_read() to common header Eric Blake
2017-10-19 22:26 ` [Qemu-devel] [PATCH v5 11/11] nbd: Minimal structured read for client Eric Blake
2017-10-20 19:58   ` Vladimir Sementsov-Ogievskiy
2017-10-20 20:46     ` Eric Blake
2017-10-23 11:57   ` Eric Blake
2017-10-23 12:24     ` Vladimir Sementsov-Ogievskiy
2017-10-24  7:31   ` Eric Blake
2017-10-19 23:07 ` [Qemu-devel] [PATCH v5 00/11] nbd minimal structured read no-reply
2017-10-20 15:09   ` Eric Blake

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=20171019222637.17890-8-eblake@redhat.com \
    --to=eblake@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=vsementsov@virtuozzo.com \
    /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).