nbd mimimal structured read
v4 cover nbd mimimal structured read
As mentioned in my review of Vladimir's v3 of this series [1], I had enough tweaks during my review that it's easier to repost things for another round of discussion, adding some of my patches in between his. I did not include his 13/13 "nbd: Minimal structured read for client", where I had a lot of comments, and suggest that Vladimir is in the best position to rebase that patch on top of this v4 series (post it as a 9/8, if desired).
[1] https://lists.gnu.org/archive/html/qemu-devel/2017-10/msg02755.html
Based-on: <[email protected]> ([PULL 0/9] NBD patches through 14 Oct)
Eric Blake (5):
nbd: Include error names in trace messages
nbd: Move nbd_errno_to_system_errno() to public header
nbd: Expose constants and structs for structured read nbd/server: Include human-readable message in structured errors
nbd: Move nbd_read() to common header
Vladimir Sementsov-Ogievskiy (3):
nbd: Minimal structured read for server nbd/client: refactor nbd_receive_starttls nbd/client: prepare nbd_receive_reply for structured reply
include/block/nbd.h | 94 ++++++++++++++++++++++-- nbd/nbd-internal.h | 24 +------ block/nbd-client.c | 8 ++- nbd/client.c | 204 +++++++++++++++++++++++++++++++++------------------- nbd/common.c | 84 ++++++++++++++++++++++ nbd/server.c | 121 ++++++++++++++++++++++++++++--- nbd/trace-events | 15 ++-- 7 files changed, 427 insertions(+), 123 deletions(-)
Signature · 1 line
2.13.6
PATCH v4 1/8 nbd: Include error names in trace messages
NBD errors were originally sent over the wire based on Linux errno values; but not all the world is Linux, and not all platforms share the same values. Since a number isn't very easy to decipher on all platforms, update the trace messages to include the name of NBD errors being sent/received over the wire. Tweak the trace messages to be at the point where we are using the NBD error, not the translation to the host errno values.
--- nbd/nbd-internal.h | 1 + nbd/client.c | 3 ++- nbd/common.c | 23 +++++++++++++++++++++++ nbd/server.c | 3 ++- nbd/trace-events | 4 ++-- 5 files changed, 30 insertions(+), 4 deletions(-)
nbd/nbd-internal.h +1 -0
@@ -126,6 +126,7 @@ const char *nbd_opt_lookup(uint32_t opt);const char *nbd_rep_lookup (uint32_t rep);const char *nbd_info_lookup (uint16_t info);const char *nbd_cmd_lookup (uint16_t info); +const char *nbd_err_lookup (int err);int nbd_drop (QIOChannel *ioc,size_t size,Error **errp);
nbd/client.c +2 -1
@@ -940,6 +940,8 @@ int nbd_receive_reply(QIOChannel *ioc, NBDReply *reply, Error **errp) reply->error =ldl_be_p (buf +4 ); reply->handle =ldq_be_p (buf +8 ); +trace_nbd_receive_reply (magic, reply->error,nbd_err_lookup (reply->error), + reply->handle); reply->error =nbd_errno_to_system_errno (reply->error);if (reply->error ==ESHUTDOWN ) {
@@ -947,7 +949,6 @@ int nbd_receive_reply(QIOChannel *ioc, NBDReply *reply, Error **errp)error_setg (errp,"server shutting down" );return -EINVAL ; } -trace_nbd_receive_reply (magic, reply->error, reply->handle);if (magic !=NBD_SIMPLE_REPLY_MAGIC ) {error_setg (errp,"invalid magic (got 0x%" PRIx32")" , magic);
nbd/common.c +23 -0
@@ -148,3 +148,26 @@ const char *nbd_cmd_lookup(uint16_t cmd)return "<unknown>" ; } } + + +const char *nbd_err_lookup (int err) +{ +switch (err) { +case NBD_SUCCESS : +return "success" ; +case NBD_EPERM : +return "EPERM" ; +case NBD_EIO : +return "EIO" ; +case NBD_ENOMEM : +return "ENOMEM" ; +case NBD_EINVAL : +return "EINVAL" ; +case NBD_ENOSPC : +return "ENOSPC" ; +case NBD_ESHUTDOWN : +return "ESHUTDOWN" ; +default : +return "<unknown>" ; + } +}
nbd/server.c +2 -1
@@ -1227,7 +1227,8 @@ static int nbd_co_send_simple_reply(NBDClient *client, {.iov_base = data, .iov_len = len} }; -trace_nbd_co_send_simple_reply (handle, nbd_err, len); +trace_nbd_co_send_simple_reply (handle, nbd_err,nbd_err_lookup (nbd_err), + len);set_be_simple_reply (&reply, nbd_err, handle);return nbd_co_send_iov (client, iov, len ?2 :1 , errp);
nbd/trace-events +2 -2
@@ -29,7 +29,7 @@ nbd_client_loop_ret(int ret, const char *error) "NBD loop returned %d: %s" nbd_client_clear_queue(void) "Clearing NBD queue" nbd_client_clear_socket(void) "Clearing NBD socket" 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) }" -nbd_receive_reply(uint32_t magic, int32_t error, uint64_t handle) "Got reply: { magic = 0x%" PRIx32 ", .error = % " PRId32 ", handle = %" PRIu64" }" +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" }" # nbd/server.c nbd_negotiate_send_rep_len(uint32_t opt, const char *optname, uint32_t type, const char *typename, uint32_t len) "Reply opt=0x%" PRIx32 " (%s), type=0x%" PRIx32 " (%s), len=%" PRIu32
@@ -53,7 +53,7 @@ nbd_negotiate_success(void) "Negotiation succeeded" nbd_receive_request(uint32_t magic, uint16_t flags, uint16_t type, uint64_t from, uint32_t len) "Got request: { magic = 0x%" PRIx32 ", .flags = 0x%" PRIx16 ", .type = 0x%" PRIx16 ", from = %" PRIu64 ", len = %" PRIu32 " }" nbd_blk_aio_attached(const char *name, void *ctx) "Export %s: Attaching clients to AIO context %p\n" 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, int len) "Send simple reply: handle = %" PRIu64 ", error = %" PRIu32 ", len = %d" +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_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)"
Signature · 1 line
2.13.6
Re: [Qemu-devel] [PATCH v4 1/8] nbd: Include error names in trace messages
Quoted 9 lines · Eric Blake
NBD errors were originally sent over the wire based on Linux errno values; but not all the world is Linux, and not all platforms share the same values. Since a number isn't very easy to decipher on all platforms, update the trace messages to include the name of NBD errors being sent/received over the wire. Tweak the trace messages to be at the point where we are using the NBD error, not the translation to the host errno values.
Signed-off-by Eric Blake <[email protected]>
Signature · 2 lines
Best regards, Vladimir
Re: [Qemu-devel] [PATCH v4 1/8] nbd: Include error names in trace messages
In reply to Eric BlakeQuoted 13 lines · Eric Blake
NBD errors were originally sent over the wire based on Linux errno values; but not all the world is Linux, and not all platforms share the same values. Since a number isn't very easy to decipher on all platforms, update the trace messages to include the name of NBD errors being sent/received over the wire. Tweak the trace messages to be at the point where we are using the NBD error, not the translation to the host errno values.
Signed-off-by Eric Blake <[email protected]>---
+++ b/nbd/trace-events
nbd/trace-events nbd_client_loop_ret(int ret, const char *error) "NBD loop returned %d: %s" · line 32
Quoted 4 lines
nbd/trace-events +0
-1@@ -29,7 +29,7 @@ nbd_client_loop_ret(int ret, const char *error) "NBD loop returned %d: %s" nbd_client_clear_queue(void) "Clearing NBD queue" nbd_client_clear_socket(void) "Clearing NBD socket" 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) }" -nbd_receive_reply(uint32_t magic, int32_t error, uint64_t handle) "Got reply: { magic = 0x%" PRIx32 ", .error = % " PRId32 ", handle = %" PRIu64" }"
Pre-existing unusual spacing in '.error = % " PRId32',
nbd/trace-events nbd_client_loop_ret(int ret, const char *error) "NBD loop returned %d: %s" · line 32
Quoted 1 line
nbd/trace-events +1
-0+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" }"
so I'll clean that up in the next revision.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH v4 2/8 nbd: Move nbd_errno_to_system_errno() to public header
In reply to Eric BlakeThis is needed in preparation for structured reply handling, as we will be performing the translation from NBD error to system errno value higher in the stack at block/nbd-client.c.
--- include/block/nbd.h | 13 +++++++++++++ nbd/nbd-internal.h | 12 ------------ nbd/client.c | 32 -------------------------------- nbd/common.c | 34 ++++++++++++++++++++++++++++++++++ nbd/trace-events | 4 +++- 5 files changed, 50 insertions(+), 45 deletions(-)
include/block/nbd.h +13 -0
@@ -149,6 +149,18 @@ enum {* aren't overflowing some other buffer. */ #define NBD_MAX_NAME_SIZE 256 +/* NBD errors are based on errno numbers, so there is a 1:1 mapping, +* but only a limited set of errno values is specified in the protocol. +* Everything else is squashed to EINVAL. +*/ +#define NBD_SUCCESS 0 +#define NBD_EPERM 1 +#define NBD_EIO 5 +#define NBD_ENOMEM 12 +#define NBD_EINVAL 22 +#define NBD_ENOSPC 28 +#define NBD_ESHUTDOWN 108 +/* Details collected by NBD_OPT_EXPORT_NAME and NBD_OPT_GO */ struct NBDExportInfo {/* Set by client before nbd_receive_negotiate() */
@@ -172,6 +184,7 @@ int nbd_send_request(QIOChannel *ioc, NBDRequest *request);int nbd_receive_reply (QIOChannel *ioc,NBDReply *reply,Error **errp);int nbd_client (int fd);int nbd_disconnect (int fd); +int nbd_errno_to_system_errno (int err);typedef struct NBDExport NBDExport ;typedef struct NBDClient NBDClient ;
nbd/nbd-internal.h +0 -12
@@ -64,18 +64,6 @@#define NBD_SET_TIMEOUT _IO(0xab, 9)#define NBD_SET_FLAGS _IO(0xab, 10) -/* NBD errors are based on errno numbers, so there is a 1:1 mapping, -* but only a limited set of errno values is specified in the protocol. -* Everything else is squashed to EINVAL. -*/ -#define NBD_SUCCESS 0 -#define NBD_EPERM 1 -#define NBD_EIO 5 -#define NBD_ENOMEM 12 -#define NBD_EINVAL 22 -#define NBD_ENOSPC 28 -#define NBD_ESHUTDOWN 108 -/* nbd_read_eof * Tries to read @size bytes from @ioc. * Returns 1 on success
nbd/client.c +0 -32
@@ -22,38 +22,6 @@#include "trace.h" #include "nbd-internal.h" -static int nbd_errno_to_system_errno (int err) -{ -int ret; -switch (err) { -case NBD_SUCCESS : - ret =0 ; -break ; -case NBD_EPERM : - ret =EPERM ; -break ; -case NBD_EIO : - ret =EIO ; -break ; -case NBD_ENOMEM : - ret =ENOMEM ; -break ; -case NBD_ENOSPC : - ret =ENOSPC ; -break ; -case NBD_ESHUTDOWN : - ret =ESHUTDOWN ; -break ; -default : -trace_nbd_unknown_error (err); -/* fallthrough */ -case NBD_EINVAL : - ret =EINVAL ; -break ; - } -return ret; -} -/* Definitions for opaque data types */ static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
nbd/common.c +34 -0
@@ -18,6 +18,7 @@#include "qemu/osdep.h" #include "qapi/error.h" +#include "trace.h" #include "nbd-internal.h" /* Discard length bytes from channel. Return -errno on failure and 0 on
@@ -171,3 +172,36 @@ const char *nbd_err_lookup(int err)return "<unknown>" ; } } + + +int nbd_errno_to_system_errno (int err) +{ +int ret; +switch (err) { +case NBD_SUCCESS : + ret =0 ; +break ; +case NBD_EPERM : + ret =EPERM ; +break ; +case NBD_EIO : + ret =EIO ; +break ; +case NBD_ENOMEM : + ret =ENOMEM ; +break ; +case NBD_ENOSPC : + ret =ENOSPC ; +break ; +case NBD_ESHUTDOWN : + ret =ESHUTDOWN ; +break ; +default : +trace_nbd_unknown_error (err); +/* fallthrough */ +case NBD_EINVAL : + ret =EINVAL ; +break ; + } +return ret; +}
nbd/trace-events +3 -1
@@ -1,5 +1,4 @@ # nbd/client.c -nbd_unknown_error(int err) "Squashing unexpected error %d to EINVAL" nbd_send_option_request(uint32_t opt, const char *name, uint32_t len) "Sending option request %" PRIu32" (%s), len %" PRIu32 nbd_receive_option_reply(uint32_t option, const char *optname, uint32_t type, const char *typename, uint32_t length) "Received option reply 0x%" PRIx32" (%s), type 0x%" PRIx32" (%s), len %" PRIu32 nbd_reply_err_unsup(uint32_t option, const char *name) "server doesn't understand request 0x%" PRIx32 " (%s), attempting fallback"
@@ -31,6 +30,9 @@ nbd_client_clear_socket(void) "Clearing NBD socket" 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) }" 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" }" +# nbd/common.c +nbd_unknown_error(int err) "Squashing unexpected error %d to EINVAL" + # nbd/server.c nbd_negotiate_send_rep_len(uint32_t opt, const char *optname, uint32_t type, const char *typename, uint32_t len) "Reply opt=0x%" PRIx32 " (%s), type=0x%" PRIx32 " (%s), len=%" PRIu32 nbd_negotiate_send_rep_err(const char *msg) "sending error message \"%s\""
Signature · 1 line
2.13.6
Re: [Qemu-devel] [PATCH v4 2/8] nbd: Move nbd_errno_to_system_errno() to public header
Quoted 3 lines · Eric Blake
This is needed in preparation for structured reply handling, as we will be performing the translation from NBD error to system errno value higher in the stack at
block/nbd-client.c.
you've forget to sign-off.
Quoted 174 lines
--- include/block/nbd.h | 13 +++++++++++++ nbd/nbd-internal.h | 12 ------------ nbd/client.c | 32 -------------------------------- nbd/common.c | 34 ++++++++++++++++++++++++++++++++++ nbd/trace-events | 4 +++- 5 files changed, 50 insertions(+), 45 deletions(-)include/block/nbd.h +13
-0@@ -149,6 +149,18 @@ enum {* aren't overflowing some other buffer. */ #define NBD_MAX_NAME_SIZE 256 +/* NBD errors are based on errno numbers, so there is a 1:1 mapping, +* but only a limited set of errno values is specified in the protocol. +* Everything else is squashed to EINVAL. +*/ +#define NBD_SUCCESS 0 +#define NBD_EPERM 1 +#define NBD_EIO 5 +#define NBD_ENOMEM 12 +#define NBD_EINVAL 22 +#define NBD_ENOSPC 28 +#define NBD_ESHUTDOWN 108 +/* Details collected by NBD_OPT_EXPORT_NAME and NBD_OPT_GO */ struct NBDExportInfo {/* Set by client before nbd_receive_negotiate() */ @@ -172,6 +184,7 @@ int nbd_send_request(QIOChannel *ioc, NBDRequest *request);int nbd_receive_reply (QIOChannel *ioc,NBDReply *reply,Error **errp);int nbd_client (int fd);int nbd_disconnect (int fd); +int nbd_errno_to_system_errno (int err);typedef struct NBDExport NBDExport ;typedef struct NBDClient NBDClient ;nbd/nbd-internal.h +0
-12@@ -64,18 +64,6 @@#define NBD_SET_TIMEOUT _IO(0xab, 9)#define NBD_SET_FLAGS _IO(0xab, 10) -/* NBD errors are based on errno numbers, so there is a 1:1 mapping, -* but only a limited set of errno values is specified in the protocol. -* Everything else is squashed to EINVAL. -*/ -#define NBD_SUCCESS 0 -#define NBD_EPERM 1 -#define NBD_EIO 5 -#define NBD_ENOMEM 12 -#define NBD_EINVAL 22 -#define NBD_ENOSPC 28 -#define NBD_ESHUTDOWN 108 -/* nbd_read_eof * Tries to read @size bytes from @ioc. * Returns 1 on success nbd/client.c +0
-32@@ -22,38 +22,6 @@#include "trace.h" #include "nbd-internal.h" -static int nbd_errno_to_system_errno (int err) -{ -int ret; -switch (err) { -case NBD_SUCCESS : - ret =0 ; -break ; -case NBD_EPERM : - ret =EPERM ; -break ; -case NBD_EIO : - ret =EIO ; -break ; -case NBD_ENOMEM : - ret =ENOMEM ; -break ; -case NBD_ENOSPC : - ret =ENOSPC ; -break ; -case NBD_ESHUTDOWN : - ret =ESHUTDOWN ; -break ; -default : -trace_nbd_unknown_error (err); -/* fallthrough */ -case NBD_EINVAL : - ret =EINVAL ; -break ; - } -return ret; -} -/* Definitions for opaque data types */ static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);nbd/common.c +34
-0@@ -18,6 +18,7 @@#include "qemu/osdep.h" #include "qapi/error.h" +#include "trace.h" #include "nbd-internal.h" /* Discard length bytes from channel. Return -errno on failure and 0 on @@ -171,3 +172,36 @@ const char *nbd_err_lookup(int err)return "<unknown>" ; } } + + +int nbd_errno_to_system_errno (int err) +{ +int ret; +switch (err) { +case NBD_SUCCESS : + ret =0 ; +break ; +case NBD_EPERM : + ret =EPERM ; +break ; +case NBD_EIO : + ret =EIO ; +break ; +case NBD_ENOMEM : + ret =ENOMEM ; +break ; +case NBD_ENOSPC : + ret =ENOSPC ; +break ; +case NBD_ESHUTDOWN : + ret =ESHUTDOWN ; +break ; +default : +trace_nbd_unknown_error (err); +/* fallthrough */ +case NBD_EINVAL : + ret =EINVAL ; +break ; + } +return ret; +}nbd/trace-events +3
-1@@ -1,5 +1,4 @@ # nbd/client.c -nbd_unknown_error(int err) "Squashing unexpected error %d to EINVAL" nbd_send_option_request(uint32_t opt, const char *name, uint32_t len) "Sending option request %" PRIu32" (%s), len %" PRIu32 nbd_receive_option_reply(uint32_t option, const char *optname, uint32_t type, const char *typename, uint32_t length) "Received option reply 0x%" PRIx32" (%s), type 0x%" PRIx32" (%s), len %" PRIu32 nbd_reply_err_unsup(uint32_t option, const char *name) "server doesn't understand request 0x%" PRIx32 " (%s), attempting fallback"@@ -31,6 +30,9 @@ nbd_client_clear_socket(void) "Clearing NBD socket" 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) }" 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" }" +# nbd/common.c +nbd_unknown_error(int err) "Squashing unexpected error %d to EINVAL" + # nbd/server.c nbd_negotiate_send_rep_len(uint32_t opt, const char *optname, uint32_t type, const char *typename, uint32_t len) "Reply opt=0x%" PRIx32 " (%s), type=0x%" PRIx32 " (%s), len=%" PRIu32 nbd_negotiate_send_rep_err(const char *msg) "sending error message \"%s\""
Signature · 2 lines
Best regards, Vladimir
Re: [Qemu-devel] [PATCH v4 2/8] nbd: Move nbd_errno_to_system_errno() to public header
Quoted 6 lines · Vladimir Sementsov-Ogievskiy
Quoted 3 lines · Eric Blake
This is needed in preparation for structured reply handling, as we will be performing the translation from NBD error to system errno value higher in the stack at
block/nbd-client.c.you've forget to sign-off.
D'oh. Bad patch split on my part.
Quoted 11 lines
Reviewed-by Vladimir Sementsov-Ogievskiy <[email protected]>Quoted 8 lines
---
include/block/nbd.h| 13 +++++++++++++ nbd/nbd-internal.h | 12 ------------ nbd/client.c | 32 -------------------------------- nbd/common.c | 34 ++++++++++++++++++++++++++++++++++ nbd/trace-events | 4 +++-5 files changed, 50 insertions(+), 45 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH v4 3/8 nbd: Expose constants and structs for structured read
In reply to Eric BlakeUpcoming patches will implement the NBD structured reply extension [1] for both client and server roles. Declare the constants, structs, and lookup routines that will be valuable whether the server or client code is backported in isolation.
This includes moving one constant from an internal header to the public header, as part of the structured read processing will be done in block/nbd-client.c rather than nbd/client.c.
[1]https://github.com/NetworkBlockDevice/nbd/blob/extension-structured-reply/doc/proto.md
Based on patches from Vladimir Sementsov-Ogievskiy.
--- include/block/nbd.h | 41 +++++++++++++++++++++++++++++++++++++++++ nbd/nbd-internal.h | 2 +- nbd/common.c | 27 +++++++++++++++++++++++++++ nbd/server.c | 2 ++ 4 files changed, 71 insertions(+), 1 deletion(-)
include/block/nbd.h +41 -0
@@ -69,6 +69,28 @@ typedef struct NBDSimpleReply {uint64_t handle; }QEMU_PACKED NBDSimpleReply; +/* Header of all structured replies */ +typedef struct NBDStructuredReplyChunk { +uint32_t magic;/* NBD_STRUCTURED_REPLY_MAGIC */ +uint16_t flags;/* combination of NBD_REPLY_FLAG_* */ +uint16_t type;/* NBD_REPLY_TYPE_* */ +uint64_t handle;/* request handle */ +uint32_t length;/* length of payload */ +} QEMU_PACKED NBDStructuredReplyChunk; + +/* Header of NBD_REPLY_TYPE_OFFSET_DATA, complete NBD_REPLY_TYPE_OFFSET_HOLE */ +typedef struct NBDStructuredRead { +NBDStructuredReplyChunk h; +uint64_t offset; +} QEMU_PACKED NBDStructuredRead; + +/* Header of all NBD_REPLY_TYPE_ERROR* errors */ +typedef struct NBDStructuredError { +NBDStructuredReplyChunk h; +uint32_t error; +uint16_t message_length; +} QEMU_PACKED NBDStructuredError; +/* Transmission (export) flags: sent from server to client during handshake, but describe what will happen during transmission */ #define NBD_FLAG_HAS_FLAGS (1 << 0)/* Flags are there */
@@ -79,6 +101,7 @@ typedef struct NBDSimpleReply {rotational media */ #define NBD_FLAG_SEND_TRIM (1 << 5)/* Send TRIM (discard) */ #define NBD_FLAG_SEND_WRITE_ZEROES (1 << 6)/* Send WRITE_ZEROES */ +#define NBD_FLAG_SEND_DF (1 << 7)/* Send DF (Do not Fragment) */ /* New-style handshake (global) flags, sent from server to client, and control what will happen during handshake phase. */
@@ -125,6 +148,7 @@ typedef struct NBDSimpleReply {/* Request flags, sent from client to server during transmission phase */ #define NBD_CMD_FLAG_FUA (1 << 0)/* 'force unit access' during write */ #define NBD_CMD_FLAG_NO_HOLE (1 << 1)/* don't punch hole on zero run */ +#define NBD_CMD_FLAG_DF (1 << 2)/* don't fragment structured read */ /* Supported request types */ enum {
@@ -149,6 +173,22 @@ enum {* aren't overflowing some other buffer. */ #define NBD_MAX_NAME_SIZE 256 +/* Two types of reply structures */ +#define NBD_SIMPLE_REPLY_MAGIC 0x67446698 +#define NBD_STRUCTURED_REPLY_MAGIC 0x668e33ef + +/* Structured reply flags */ +#define NBD_REPLY_FLAG_DONE (1 << 0)/* This reply-chunk is last */ + +/* Structured reply types */ +#define NBD_REPLY_ERR (value) ((1 << 15) | (value)) + +#define NBD_REPLY_TYPE_NONE 0 +#define NBD_REPLY_TYPE_OFFSET_DATA 1 +#define NBD_REPLY_TYPE_OFFSET_HOLE 2 +#define NBD_REPLY_TYPE_ERROR NBD_REPLY_ERR(1) +#define NBD_REPLY_TYPE_ERROR_OFFSET NBD_REPLY_ERR(2) +/* NBD errors are based on errno numbers, so there is a 1:1 mapping, * but only a limited set of errno values is specified in the protocol. * Everything else is squashed to EINVAL.
@@ -159,6 +199,7 @@ enum {#define NBD_ENOMEM 12#define NBD_EINVAL 22#define NBD_ENOSPC 28 +#define NBD_EOVERFLOW 75#define NBD_ESHUTDOWN 108/* Details collected by NBD_OPT_EXPORT_NAME and NBD_OPT_GO */
nbd/nbd-internal.h +1 -1
@@ -47,7 +47,6 @@#define NBD_OLDSTYLE_NEGOTIATE_SIZE (8 + 8 + 8 + 4 + 124)#define NBD_REQUEST_MAGIC 0x25609513 -#define NBD_SIMPLE_REPLY_MAGIC 0x67446698#define NBD_OPTS_MAGIC 0x49484156454F5054LL#define NBD_CLIENT_MAGIC 0x0000420281861253LL#define NBD_REP_MAGIC 0x0003e889045565a9LL
@@ -114,6 +113,7 @@ const char *nbd_opt_lookup(uint32_t opt);const char *nbd_rep_lookup (uint32_t rep);const char *nbd_info_lookup (uint16_t info);const char *nbd_cmd_lookup (uint16_t info); +const char *nbd_reply_type_lookup (uint16_t type);const char *nbd_err_lookup (int err);int nbd_drop (QIOChannel *ioc,size_t size,Error **errp);
nbd/common.c +27 -0
@@ -151,6 +151,28 @@ const char *nbd_cmd_lookup(uint16_t cmd) } +const char *nbd_reply_type_lookup (uint16_t type) +{ +switch (type) { +case NBD_REPLY_TYPE_NONE : +return "none" ; +case NBD_REPLY_TYPE_OFFSET_DATA : +return "data" ; +case NBD_REPLY_TYPE_OFFSET_HOLE : +return "hole" ; +case NBD_REPLY_TYPE_ERROR : +return "generic error" ; +case NBD_REPLY_TYPE_ERROR_OFFSET : +return "error at offset" ; +default : +if (type & (1 <<15 )) { +return "<unknown error>" ; + } +return "<unknown>" ; + } +} + +const char *nbd_err_lookup (int err) {switch (err) {
@@ -166,6 +188,8 @@ const char *nbd_err_lookup(int err)return "EINVAL" ;case NBD_ENOSPC :return "ENOSPC" ; +case NBD_EOVERFLOW : +return "EOVERFLOW" ;case NBD_ESHUTDOWN :return "ESHUTDOWN" ;default :
@@ -193,6 +217,9 @@ int nbd_errno_to_system_errno(int err)case NBD_ENOSPC : ret =ENOSPC ;break ; +case NBD_EOVERFLOW : + ret =EOVERFLOW ; +break ;case NBD_ESHUTDOWN : ret =ESHUTDOWN ;break ;
nbd/server.c +2 -0
@@ -40,6 +40,8 @@ static int system_errno_to_nbd_errno(int err)case EFBIG :case ENOSPC :return NBD_ENOSPC ; +case EOVERFLOW : +return NBD_EOVERFLOW ;case ESHUTDOWN :return NBD_ESHUTDOWN ;case EINVAL :
Signature · 1 line
2.13.6
Re: [Qemu-devel] [PATCH v4 3/8] nbd: Expose constants and structs for structured read
Quoted 10 lines · Eric Blake
Upcoming patches will implement the NBD structured reply extension [1] for both client and server roles. Declare the constants, structs, and lookup routines that will be valuable whether the server or client code is backported in isolation.
This includes moving one constant from an internal header to the public header, as part of the structured read processing will be done in
block/nbd-client.crather thannbd/client.c.[1]https://github.com/NetworkBlockDevice/nbd/blob/extension-structured-reply/doc/proto.md
this link may become dead in future, after merging extension spec into master...
Quoted 4 lines
Based on patches from Vladimir Sementsov-Ogievskiy.
Signed-off-by Eric Blake <[email protected]>
Signature · 2 lines
Best regards, Vladimir
Re: [Qemu-devel] [PATCH v4 3/8] nbd: Expose constants and structs for structured read
Quoted 10 lines · Vladimir Sementsov-Ogievskiy
Quoted 9 lines · Eric Blake
Upcoming patches will implement the NBD structured reply extension [1] for both client and server roles. Declare the constants, structs, and lookup routines that will be valuable whether the server or client code is backported in isolation.
This includes moving one constant from an internal header to the public header, as part of the structured read processing will be done in
block/nbd-client.crather thannbd/client.c.
Quoted 5 lines
Quoted 2 lines
[1]https://github.com/NetworkBlockDevice/nbd/blob/extension-structured-reply/doc/proto.md
this link may become dead in future, after merging extension spec into master...
Perhaps; but it's a git branch, so it's not going to disappear, even when it is no longer actively maintained. For comparison, here's the (now-stale) link for NBD_CMD_WRITE_ZEROES:
https://github.com/NetworkBlockDevice/nbd/blob/extension-write-zeroes/doc/proto.md
So I'm not too worried about having the URL in the commit message.
Quoted 10 lines
Quoted 5 lines
Based on patches from Vladimir Sementsov-Ogievskiy.
Signed-off-by Eric Blake <[email protected]>Reviewed-by Vladimir Sementsov-Ogievskiy <[email protected]>
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH v4 4/8 nbd: Minimal structured read for server
In reply to Eric BlakeFrom: Vladimir Sementsov-Ogievskiy <[email protected]>
Minimal implementation of structured read: one structured reply chunk, no segmentation.
Minimal structured error implementation: no text message. Support DF flag, but just ignore it, as there is no segmentation any way.
---
v4: better _DF flag handling, convert errno to wire format, add comments and tracing, rework structured error for less churn when adding text message later, don't kill connection on redundant client option
--- nbd/server.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++++------ nbd/trace-events | 2 ++ 2 files changed, 98 insertions(+), 10 deletions(-)
nbd/server.c +96 -10
@@ -100,6 +100,8 @@ struct NBDClient {QTAILQ_ENTRY (NBDClient) next;int nb_requests;bool closing; + +bool structured_reply; };/* That's all folks */
@@ -762,6 +764,23 @@ static int nbd_negotiate_options(NBDClient *client, uint16_t myflags,return ret; }break ; + +case NBD_OPT_STRUCTURED_REPLY : +if (client->structured_reply) { + ret =nbd_negotiate_send_rep_err ( + client->ioc,NBD_REP_ERR_INVALID , option, errp, +"structured reply already negotiated" ); + }else { + ret =nbd_negotiate_send_rep (client->ioc,NBD_REP_ACK , + option, errp); + } +if (ret <0 ) { +return ret; + } + client->structured_reply = true; + myflags |=NBD_CMD_FLAG_DF ; +break ; +default :if (nbd_drop (client->ioc, length, errp) <0 ) {return -EIO ;
@@ -1236,6 +1255,59 @@ static int nbd_co_send_simple_reply(NBDClient *client,return nbd_co_send_iov (client, iov, len ?2 :1 , errp); } +static inline void set_be_chunk (NBDStructuredReplyChunk *chunk,uint16_t flags, +uint16_t type,uint64_t handle,uint32_t length) +{ +stl_be_p (&chunk->magic,NBD_STRUCTURED_REPLY_MAGIC ); +stw_be_p (&chunk->flags, flags); +stw_be_p (&chunk->type, type); +stq_be_p (&chunk->handle, handle); +stl_be_p (&chunk->length, length); +} + +static int coroutine_fn nbd_co_send_structured_read(NBDClient *client, +uint64_t handle, +uint64_t offset, +void *data, +size_t size, +Error **errp) +{ +NBDStructuredRead chunk; +struct iovec iov[] = { + {.iov_base = &chunk, .iov_len =sizeof (chunk)}, + {.iov_base = data, .iov_len = size} + }; + +trace_nbd_co_send_structured_read (handle, offset, data, size); +set_be_chunk (&chunk.h,NBD_REPLY_FLAG_DONE ,NBD_REPLY_TYPE_OFFSET_DATA , + handle,sizeof (chunk) -sizeof (chunk.h) + size); +stq_be_p (&chunk.offset, offset); + +return nbd_co_send_iov (client, iov,2 , errp); +} + +static int coroutine_fn nbd_co_send_structured_error(NBDClient *client, +uint64_t handle, +uint32_t error, +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 */ + }; + +assert (nbd_err); +trace_nbd_co_send_structured_error (handle, nbd_err); +set_be_chunk (&chunk.h,NBD_REPLY_FLAG_DONE ,NBD_REPLY_TYPE_ERROR , handle, +sizeof (chunk) -sizeof (chunk.h)); +stl_be_p (&chunk.error, nbd_err); +stw_be_p (&chunk.message_length,0 ); + +return nbd_co_send_iov (client, iov,1 , errp); +} +/* nbd_co_receive_request * Collect a client request. Return 0 if request looks valid, -EIO to drop * connection right away, and any other negative value to report an error to
@@ -1246,6 +1318,7 @@ static int nbd_co_receive_request(NBDRequestData *req, NBDRequest *request, Error **errp) {NBDClient *client = req->client; +int valid_flags;g_assert (qemu_in_coroutine ());assert (client->recv_coroutine ==qemu_coroutine_self ());
@@ -1307,13 +1380,15 @@ static int nbd_co_receive_request(NBDRequestData *req, NBDRequest *request, (uint64_t)client->exp->size);return request->type ==NBD_CMD_WRITE ? -ENOSPC : -EINVAL ; } -if (request->flags & ~(NBD_CMD_FLAG_FUA |NBD_CMD_FLAG_NO_HOLE )) { -error_setg (errp,"unsupported flags (got 0x%x)" , request->flags); -return -EINVAL ; + valid_flags =NBD_CMD_FLAG_FUA ; +if (request->type ==NBD_CMD_READ && client->structured_reply) { + valid_flags |=NBD_CMD_FLAG_DF ; + }else if (request->type ==NBD_CMD_WRITE_ZEROES ) { + valid_flags |=NBD_CMD_FLAG_NO_HOLE ; } -if (request->type !=NBD_CMD_WRITE_ZEROES && - (request->flags &NBD_CMD_FLAG_NO_HOLE )) { -error_setg (errp,"unexpected flags (got 0x%x)" , request->flags); +if (request->flags & ~valid_flags) { +error_setg (errp,"unsupported flags for command %s (got 0x%x)" , +nbd_cmd_lookup (request->type), request->flags);return -EINVAL ; }
@@ -1450,10 +1525,21 @@ reply: local_err =NULL ; } -if (nbd_co_send_simple_reply (req->client, request.handle, - ret <0 ? -ret :0 , - req->data, reply_data_len, &local_err) <0 ) - { +if (client->structured_reply && request.type ==NBD_CMD_READ ) { +if (ret <0 ) { + ret =nbd_co_send_structured_error (req->client, request.handle, + -ret, &local_err); + }else { + ret =nbd_co_send_structured_read (req->client, request.handle, + request.from, req->data, + reply_data_len, &local_err); + } + }else { + ret =nbd_co_send_simple_reply (req->client, request.handle, + ret <0 ? -ret :0 , + req->data, reply_data_len, &local_err); + } +if (ret <0 ) {error_prepend (&local_err,"Failed to send reply: " ); goto disconnect; }
nbd/trace-events +2 -0
@@ -56,6 +56,8 @@ nbd_receive_request(uint32_t magic, uint16_t flags, uint16_t type, uint64_t from nbd_blk_aio_attached(const char *name, void *ctx) "Export %s: Attaching clients to AIO context %p\n" 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) "Send structured error reply: handle = %" PRIu64 ", error = %d" 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)"
Signature · 1 line
2.13.6
Re: [Qemu-devel] [PATCH v4 4/8] nbd: Minimal structured read for server
Quoted 34 lines · Eric Blake
From: Vladimir Sementsov-Ogievskiy <[email protected]>
Minimal implementation of structured read: one structured reply chunk, no segmentation.
Minimal structured error implementation: no text message. Support DF flag, but just ignore it, as there is no segmentation any way.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>Signed-off-by Eric Blake <[email protected]>---
v4: better_DFflag handling, convert errno to wire format, add comments and tracing, rework structured error for less churn when adding text message later, don't kill connection on redundant client option--- nbd/server.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++++------ nbd/trace-events | 2 ++ 2 files changed, 98 insertions(+), 10 deletions(-)nbd/server.c +2
-0@@ -100,6 +100,8 @@ struct NBDClient {QTAILQ_ENTRY (NBDClient) next;int nb_requests;bool closing; + +bool structured_reply; };/* That's all folks */
nbd/server.c static int nbd_negotiate_options(NBDClient *client, uint16_t myflags, · line 772
Quoted 9 lines
nbd/server.c +6
-0@@ -762,6 +764,23 @@ static int nbd_negotiate_options(NBDClient *client, uint16_t myflags,return ret; }break ; + +case NBD_OPT_STRUCTURED_REPLY : +if (client->structured_reply) { + ret =nbd_negotiate_send_rep_err ( + client->ioc,NBD_REP_ERR_INVALID , option, errp, +"structured reply already negotiated" );
You were going to send a patch to spec for this..
nbd/server.c static int nbd_negotiate_options(NBDClient *client, uint16_t myflags, · line 781
Quoted 9 lines
nbd/server.c +9
-0+ }else { + ret =nbd_negotiate_send_rep (client->ioc,NBD_REP_ACK , + option, errp); + } +if (ret <0 ) { +return ret; + } + client->structured_reply = true; + myflags |=NBD_CMD_FLAG_DF ;
it should be NBD_FLAG_SEND_DF
[...]
the following looks ok.
Signature · 2 lines
Best regards, Vladimir
Re: [Qemu-devel] [PATCH v4 4/8] nbd: Minimal structured read for server
Quoted 13 lines · Vladimir Sementsov-Ogievskiy
Quoted 11 lines · Eric Blake
From: Vladimir Sementsov-Ogievskiy <[email protected]>
Minimal implementation of structured read: one structured reply chunk, no segmentation.
Minimal structured error implementation: no text message. Support DF flag, but just ignore it, as there is no segmentation any way.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>Signed-off-by Eric Blake <[email protected]>
Quoted 8 lines
Quoted 6 lines
+
+ caseNBD_OPT_STRUCTURED_REPLY:
+ if (client->structured_reply) {
+ ret = nbd_negotiate_send_rep_err(
+client->ioc,NBD_REP_ERR_INVALID, option, errp, + "structured reply already negotiated");You were going to send a patch to spec for this..
Still am; it's on my queue for tasks to do today. :)
Quoted 12 lines
Quoted 11 lines
+ } else {
+ ret = nbd_negotiate_send_rep(client->ioc,NBD_REP_ACK,
+ option, errp); + }
+ if (ret < 0) {
+ return ret;
+ }
+client->structured_reply= true;
+ myflags |=NBD_CMD_FLAG_DF;
Quoted 1 line
it should be
NBD_FLAG_SEND_DF
Ouch, you're right. NBD_CMD_FLAG_DF has the same value as NBD_FLAG_SEND_FLUSH (which we always advertise). If nothing else, you proved that my weekend hacking was shoved out the door quickly, rather than fully tested. :)
Quoted 5 lines
[...]
the following looks ok.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [Qemu-devel] [PATCH v4 4/8] nbd: Minimal structured read for server
Quoted 21 lines · Eric Blake
Quoted 20 lines · Vladimir Sementsov-Ogievskiy
Quoted 18 lines · Eric Blake
From: Vladimir Sementsov-Ogievskiy <[email protected]>
Minimal implementation of structured read: one structured reply chunk, no segmentation.
Minimal structured error implementation: no text message. Support DF flag, but just ignore it, as there is no segmentation any way.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>Signed-off-by Eric Blake <[email protected]>+
+ caseNBD_OPT_STRUCTURED_REPLY:
+ if (client->structured_reply) {
+ ret = nbd_negotiate_send_rep_err(
+client->ioc,NBD_REP_ERR_INVALID, option, errp, + "structured reply already negotiated");
Quoted 3 lines
Quoted 1 line
You were going to send a patch to spec for this..
Still am; it's on my queue for tasks to do today. :)
https://lists.debian.org/nbd/2017/nbd-201710/msg00013.html
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [Qemu-devel] [PATCH v4 4/8] nbd: Minimal structured read for server
In reply to Eric BlakeQuoted 12 lines · Eric Blake
From: Vladimir Sementsov-Ogievskiy <[email protected]>
Minimal implementation of structured read: one structured reply chunk, no segmentation.
Minimal structured error implementation: no text message. Support DF flag, but just ignore it, as there is no segmentation any way.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>Signed-off-by Eric Blake <[email protected]>
nbd/server.c static int nbd_negotiate_options(NBDClient *client, uint16_t myflags, · line 776
Quoted 10 lines
nbd/server.c +10
-0+ +case NBD_OPT_STRUCTURED_REPLY : +if (client->structured_reply) { + ret =nbd_negotiate_send_rep_err ( + client->ioc,NBD_REP_ERR_INVALID , option, errp, +"structured reply already negotiated" ); + }else { + ret =nbd_negotiate_send_rep (client->ioc,NBD_REP_ACK , + option, errp); + }
Fails spectacularly if the client sent a non-zero length payload along with the option, because we forgot to nbd_drop the payload, getting the server out-of-sync with the client's next option request. I'm requesting clarification from the NBD list on how to best handle that (presumably with NBD_REP_ERR_INVALID), and in the meantime will copy what we do for NBD_OPT_LIST with unexpected payload. It also made me realize that we don't reject unexpected payload for NBD_OPT_STARTTLS; a common helper function would make that easier, so guess what I'm adding in v5 :)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH v4 5/8 nbd/server: Include human-readable message in structured errors
In reply to Eric BlakeThe 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.
--- nbd/server.c | 22 ++++++++++++++++------ nbd/trace-events | 2 +- 2 files changed, 17 insertions(+), 7 deletions(-)
nbd/server.c +16 -6
@@ -1289,23 +1289,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, +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 = msg, .iov_len =strlen (msg)}, };assert (nbd_err); -trace_nbd_co_send_structured_error (handle, nbd_err); +trace_nbd_co_send_structured_error (handle, nbd_err, +nbd_err_lookup (nbd_err), msg);set_be_chunk (&chunk.h,NBD_REPLY_FLAG_DONE ,NBD_REPLY_TYPE_ERROR , handle,sizeof (chunk) -sizeof (chunk.h));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
@@ -1406,6 +1408,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) {
@@ -1521,14 +1524,20 @@ 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 ) { +if (!msg) { + msg =g_strdup ("" ); + } 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,
@@ -1539,6 +1548,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;
nbd/trace-events +1 -1
@@ -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) "Send structured error reply: handle = %" PRIu64 ", error = %d" +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)"
Signature · 1 line
2.13.6
Re: [Qemu-devel] [PATCH v4 5/8] nbd/server: Include human-readable message in structured errors
Quoted 15 lines · Eric Blake
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 <[email protected]>--- nbd/server.c | 22 ++++++++++++++++------ nbd/trace-events | 2 +- 2 files changed, 17 insertions(+), 7 deletions(-)nbd/server.c +0
-0
nbd/server.c static int coroutine_fn nbd_co_send_structured_read(NBDClient *client, · line 1292
Quoted 4 lines
nbd/server.c +1
-0@@ -1289,23 +1289,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, + char *msg,
it's not const because we want to put it into iov..
may be it is better to make it const and convert to (char *) like in qio_channel_write_all, to 1: more native message parameter type
2: allow constat strings like nbd_co_send_structured_error(..., "some error")
nbd/server.c static int coroutine_fn nbd_co_send_structured_read(NBDClient *client, · line 1300
Quoted 9 lines
nbd/server.c +1
-1Error **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 = msg, .iov_len =strlen (msg)}, };
msg is always non-zero? so we don't want to send errors without message.. (1)
nbd/server.c static int coroutine_fn nbd_co_send_structured_read(NBDClient *client, · line 1304
Quoted 5 lines
nbd/server.c +2
-1assert (nbd_err); -trace_nbd_co_send_structured_error (handle, nbd_err); +trace_nbd_co_send_structured_error (handle, nbd_err, +nbd_err_lookup (nbd_err), msg);
it doesn't really matter, but "nbd_err_lookup(nbd_err)" is a bit unrelated and looks like part of previous patch
nbd/server.c static int coroutine_fn nbd_co_send_structured_read(NBDClient *client, · line 1310
Quoted 8 lines
nbd/server.c +2
-2set_be_chunk (&chunk.h,NBD_REPLY_FLAG_DONE ,NBD_REPLY_TYPE_ERROR , handle,sizeof (chunk) -sizeof (chunk.h));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);
but you allow messages of zero length..
it's ok, but looks a bit strange in connection with (1)
Quoted 17 lines
nbd/server.c +3
-0}/* nbd_co_receive_request @@ -1406,6 +1408,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) {@@ -1521,14 +1524,20 @@ 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));
nbd/server.c reply: · line 1537
Quoted 10 lines
nbd/server.c +4
-1error_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 ) { +if (!msg) { + msg =g_strdup ("" );
I'd prefer to handle msg=NULL in nbd_co_send_structured_error instead of this.
Quoted 20 lines
nbd/server.c +3
-1+ } 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,@@ -1539,6 +1548,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;nbd/trace-events +0
-0
nbd/trace-events nbd_blk_aio_attached(const char *name, void *ctx) "Export %s: Attaching clients · line 63
Quoted 8 lines
nbd/trace-events +1
-1@@ -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) "Send structured error reply: handle = %" PRIu64 ", error = %d" +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)"
anyway it should work, so, with or without my suggestions:
Signature · 2 lines
Best regards, Vladimir
Re: [Qemu-devel] [PATCH v4 5/8] nbd/server: Include human-readable message in structured errors
Quoted 20 lines · Vladimir Sementsov-Ogievskiy
Quoted 19 lines · Eric Blake
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 <[email protected]>---
nbd/server.c | 22 ++++++++++++++++------
nbd/trace-events | 2 +-2 files changed, 17 insertions(+), 7 deletions(-)nbd/server.c +0
-0@@ -1289,23 +1289,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,
Quoted 8 lines
nbd/server.c static int coroutine_fn nbd_co_send_structured_read(NBDClient *client, · line 1292
Quoted 1 line
nbd/server.c +1
-0+ char *msg,it's not const because we want to put it into iov..
may be it is better to make it const and convert to (char *) like inqio_channel_write_all, to
1: more native message parameter type
2: allow constat strings likenbd_co_send_structured_error(..., "some error")
Yes, casting away const would be a more convenient interface (it's a bummer that iov can't be const-correct for write, because it is also used by read which cannot be const).
Quoted 11 lines
nbd/server.c static int coroutine_fn nbd_co_send_structured_read(NBDClient *client, · line 1300
Quoted 10 lines
nbd/server.c +1
-1Error **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 = msg, .iov_len = strlen(msg)}, };
Quoted 2 lines
msg is always non-zero? so we don't want to send errors without message.. (1)
I played with the idea of allowing NULL, and don't know whether it was easier to require non-NULL message (which may require NULL check at all callers) or to allow NULL (requiring more code here).
Quoted 9 lines
nbd/server.c static int coroutine_fn nbd_co_send_structured_read(NBDClient *client, · line 1304
Quoted 6 lines
nbd/server.c +2
-1assert(nbd_err); - trace_nbd_co_send_structured_error(handle, nbd_err); + trace_nbd_co_send_structured_error(handle, nbd_err, + nbd_err_lookup(nbd_err), msg);it doesn't really matter, but "
nbd_err_lookup(nbd_err)" is a bit unrelated and looks like part of previous patch
The previous patch was yours; I tried to minimize the changes I made to your patches, so I stuck this one in mine instead. But if you think we should trace accurately from the get-go, I'm just fine rebasing the series to make your patch trace the error name at its introduction, rather than in my followup that supports error messages.
Quoted 8 lines
Quoted 8 lines
nbd/server.c +1
-1set_be_chunk(&chunk.h, NBD_REPLY_FLAG_DONE,NBD_REPLY_TYPE_ERROR , handle, sizeof(chunk) - sizeof(chunk.h)); stl_be_p(&chunk.error, nbd_err); - stw_be_p(&chunk.message_length, 0); + stw_be_p(&chunk.message_length, iov[1].iov_len);
Quoted 5 lines
nbd/server.c static int coroutine_fn nbd_co_send_structured_read(NBDClient *client, · line 1310
Quoted 2 lines
nbd/server.c +1
-1- return nbd_co_send_iov(client, iov, 1, errp); + return nbd_co_send_iov(client, iov, 1 + !!iov[1].iov_len, errp);but you allow messages of zero length..
it's ok, but looks a bit strange in connection with (1)
Passing "" is the easiest thing to do for callers that can't pass NULL. My first attempt had:
.iov_len = msg ? strlen(msg) : 0
and
trace_...(msg ? msg : "")
For this patch, there is only a single caller, so it's really hard to judge which style (required non-NULL parameter, vs. doing more work in the common function) will be more useful until we add further callers.
Quoted 9 lines
Quoted 6 lines
nbd/server.c +2
-1- if (client->structured_reply && request.type == NBD_CMD_READ) { + if (client->structured_reply && + (ret < 0 || request.type == NBD_CMD_READ)) {if (ret < 0) {
+ if (!msg) {
+ msg =g_strdup("");I'd prefer to handle msg=NULL in
nbd_co_send_structured_errorinstead of this.
Okay, then it sounds like accepting a NULL parameter is the way to go!
It's also worth considering that casting away const would mean I don't have to g_strdup("").
Quoted 3 lines
anyway it should work, so, with or without my suggestions:
Reviewed-by Vladimir Sementsov-Ogievskiy <[email protected]>
Thanks for your reviews!
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [Qemu-devel] [PATCH v4 5/8] nbd/server: Include human-readable message in structured errors
In reply to Eric BlakeQuoted 12 lines · Eric Blake
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 <[email protected]>--- nbd/server.c | 22 ++++++++++++++++------ nbd/trace-events | 2 +- 2 files changed, 17 insertions(+), 7 deletions(-)assert(nbd_err);
nbd/server.c static int coroutine_fn nbd_co_send_structured_read(NBDClient *client, · line 1306
Quoted 5 lines
nbd/server.c +2
-1-trace_nbd_co_send_structured_error (handle, nbd_err); +trace_nbd_co_send_structured_error (handle, nbd_err, +nbd_err_lookup (nbd_err), msg);set_be_chunk (&chunk.h,NBD_REPLY_FLAG_DONE ,NBD_REPLY_TYPE_ERROR , handle,sizeof (chunk) -sizeof (chunk.h));
Bug - it's a bad idea to not include the message length in the overall length, because the client then gets out of sync with the server (it reads only 6 bytes instead of 6+strlen(msg) bytes, and expects the message to start with the magic number for the next reply).
nbd/server.c static int coroutine_fn nbd_co_send_structured_read(NBDClient *client, · line 1308
Quoted 3 lines
stl_be_p(&chunk.error, nbd_err);nbd/server.c +1
-1-stw_be_p (&chunk.message_length,0 ); +stw_be_p (&chunk.message_length, iov[1 ].iov_len);
But this also highlights a bug in 9/8, where we have:
Quoted 15 lines
(quoted diff) +15
-0+static int nbd_parse_error_payload(NBDStructuredReplyChunk *chunk, + uint8_t *payload, int *request_ret, + Error **errp) +{ + uint32_t error; + uint16_t message_size; + + assert(chunk->type & (1 << 15)); + + if (chunk->length < sizeof(error) + sizeof(message_size)) { + error_setg(errp, + "Protocol error: invalid payload for structured error"); + return -EINVAL; + } +
Review
Quoted 10 lines
(quoted diff) +10
-0+ error = nbd_errno_to_system_errno(payload_advance32(&payload)); + if (error == 0) { + error_setg(errp, "Protocol error: server sent structured error chunk" + "with error = 0"); + return -EINVAL; + } + + *request_ret = error; + message_size = payload_advance16(&payload); + error_setg_errno(errp, error, "%.*s", message_size, payload);
Whoops - no sanity check that message_size fits within chunk->length. So when we read message_length 33 (when the server sends a message 33 bytes long), we are then dereferencing up to 33 bytes of garbage beyond the end of payload.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH v4 6/8 nbd/client: refactor nbd_receive_starttls
In reply to Eric BlakeFrom: Vladimir Sementsov-Ogievskiy <[email protected]>
Split out nbd_request_simple_option to be reused for structured reply option.
---
v4: reduce redundant traces, typo fix in commit message
--- nbd/client.c | 69 ++++++++++++++++++++++++++++++++++++++------------------ nbd/trace-events | 4 +--- 2 files changed, 48 insertions(+), 25 deletions(-)
nbd/client.c +47 -22
@@ -508,35 +508,60 @@ static int nbd_receive_query_exports(QIOChannel *ioc, } } +/* nbd_request_simple_option: Send an option request, and parse the reply +* return 1 for successful negotiation, +* 0 if operation is unsupported, +* -1 with errp set for any other error +*/ +static int nbd_request_simple_option (QIOChannel *ioc,int opt,Error **errp) +{ +nbd_opt_reply reply; + +if (nbd_send_option_request (ioc, opt,0 ,NULL , errp) <0 ) { +return -1 ; + } + +if (nbd_receive_option_reply (ioc, opt, &reply, errp) <0 ) { +return -1 ; + } + +if (reply.length !=0 ) { +error_setg (errp,"Option %d ('%s') response length is %" PRIu32 +" (it should be zero)" , opt,nbd_opt_lookup (opt), + reply.length); +nbd_send_opt_abort (ioc); +return -1 ; + } + +if (reply.type ==NBD_REP_ERR_UNSUP ) { +return 0 ; + } + +if (reply.type !=NBD_REP_ACK ) { +error_setg (errp,"Server rejected request for option %d (%s) " +"with reply %" PRIx32" (%s)" , opt,nbd_opt_lookup (opt), + reply.type,nbd_rep_lookup (reply.type)); +nbd_send_opt_abort (ioc); +return -1 ; + } + +return 1 ; +} +static QIOChannel *nbd_receive_starttls (QIOChannel *ioc,QCryptoTLSCreds *tlscreds,const char *hostname,Error **errp) { -nbd_opt_reply reply; +int ret;QIOChannelTLS *tioc;struct NBDTLSHandshakeData data = {0 }; -trace_nbd_receive_starttls_request (); -if (nbd_send_option_request (ioc,NBD_OPT_STARTTLS ,0 ,NULL , errp) <0 ) { -return NULL ; - } - -trace_nbd_receive_starttls_reply (); -if (nbd_receive_option_reply (ioc,NBD_OPT_STARTTLS , &reply, errp) <0 ) { -return NULL ; - } - -if (reply.type !=NBD_REP_ACK ) { -error_setg (errp,"Server rejected request to start TLS %" PRIx32, - reply.type); -nbd_send_opt_abort (ioc); -return NULL ; - } - -if (reply.length !=0 ) { -error_setg (errp,"Start TLS response was not zero %" PRIu32, - reply.length); -nbd_send_opt_abort (ioc); + ret =nbd_request_simple_option (ioc,NBD_OPT_STARTTLS , errp); +if (ret <=0 ) { +if (ret ==0 ) { +error_setg (errp,"Server don't support STARTTLS option" ); +nbd_send_opt_abort (ioc); + }return NULL ; }
nbd/trace-events +1 -3
@@ -8,9 +8,7 @@ nbd_opt_go_info_unknown(int info, const char *name) "Ignoring unknown info %d (% nbd_opt_go_info_block_size(uint32_t minimum, uint32_t preferred, uint32_t maximum) "Block sizes are 0x%" PRIx32 ", 0x%" PRIx32 ", 0x%" PRIx32 nbd_receive_query_exports_start(const char *wantname) "Querying export list for '%s'" nbd_receive_query_exports_success(const char *wantname) "Found desired export name '%s'" -nbd_receive_starttls_request(void) "Requesting TLS from server" -nbd_receive_starttls_reply(void) "Getting TLS reply from server" -nbd_receive_starttls_new_client(void) "TLS request approved, setting up TLS" +nbd_receive_starttls_new_client(void) "Setting up TLS" nbd_receive_starttls_tls_handshake(void) "Starting TLS handshake" nbd_receive_negotiate(void *tlscreds, const char *hostname) "Receiving negotiation tlscreds=%p hostname=%s" nbd_receive_negotiate_magic(uint64_t magic) "Magic is 0x%" PRIx64
Signature · 1 line
2.13.6
Re: [Qemu-devel] [PATCH v4 6/8] nbd/client: refactor nbd_receive_starttls
Quoted 8 lines · Eric Blake
From: Vladimir Sementsov-Ogievskiy <[email protected]>
Split out
nbd_request_simple_optionto be reused for structured reply option.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>Signed-off-by Eric Blake <[email protected]>
ok for me.
Signature · 2 lines
Best regards, Vladimir
Re: [Qemu-devel] [PATCH v4 6/8] nbd/client: refactor nbd_receive_starttls
In reply to Eric BlakeQuoted 22 lines · Eric Blake
From: Vladimir Sementsov-Ogievskiy <[email protected]>
Split out
nbd_request_simple_optionto be reused for structured reply option.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>Signed-off-by Eric Blake <[email protected]>nbd/client.c +13
-0+static int nbd_request_simple_option (QIOChannel *ioc,int opt,Error **errp) +{ +nbd_opt_reply reply; + +if (nbd_send_option_request (ioc, opt,0 ,NULL , errp) <0 ) { +return -1 ; + } + +if (nbd_receive_option_reply (ioc, opt, &reply, errp) <0 ) { +return -1 ; + } + +if (reply.length !=0 ) {
nbd/client.c static int nbd_receive_query_exports(QIOChannel *ioc, · line 538
Quoted 10 lines
nbd/client.c +10
-0+error_setg (errp,"Option %d ('%s') response length is %" PRIu32 +" (it should be zero)" , opt,nbd_opt_lookup (opt), + reply.length); +nbd_send_opt_abort (ioc); +return -1 ; + } + +if (reply.type ==NBD_REP_ERR_UNSUP ) { +return 0 ; + }
Oops, these two conditions are swapped. A non-zero reply length is perfectly acceptable if the server is sending NBD_REP_ERR_UNSUP with an error message (as is the case with old qemu server, new qemu client). We can only enforce non-zero length...
nbd/client.c static int nbd_receive_query_exports(QIOChannel *ioc, · line 547
Quoted 9 lines
nbd/client.c +9
-0+ +if (reply.type !=NBD_REP_ACK ) { +error_setg (errp,"Server rejected request for option %d (%s) " +"with reply %" PRIx32" (%s)" , opt,nbd_opt_lookup (opt), + reply.type,nbd_rep_lookup (reply.type)); +nbd_send_opt_abort (ioc); +return -1 ; + } +
...here, after we know we got an ACK.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH v4 7/8 nbd/client: prepare nbd_receive_reply for structured reply
In reply to Eric BlakeFrom: Vladimir Sementsov-Ogievskiy <[email protected]>
In following patch nbd_receive_reply will be used both for simple and structured reply header receiving.
NBDReply is altered into union of simple reply header and structured reply chunk header, simple error translation moved to block/nbd-client to be consistent with further structured reply error translation.
--- include/block/nbd.h | 30 ++++++++++++--- block/nbd-client.c | 8 ++-- nbd/client.c | 104 +++++++++++++++++++++++++++++++++++++++++----------- nbd/trace-events | 3 +- 4 files changed, 113 insertions(+), 32 deletions(-)
include/block/nbd.h +24 -6
@@ -57,12 +57,6 @@ struct NBDRequest { };typedef struct NBDRequest NBDRequest ; -struct NBDReply { -uint64_t handle; -uint32_t error; -}; -typedef struct NBDReply NBDReply ; - typedef struct NBDSimpleReply { uint32_t magic; /* NBD_SIMPLE_REPLY_MAGIC */ uint32_t error;
@@ -78,6 +72,20 @@ typedef struct NBDStructuredReplyChunk {uint32_t length;/* length of payload */ }QEMU_PACKED NBDStructuredReplyChunk; +typedef union NBDReply { +NBDSimpleReply simple; +NBDStructuredReplyChunk structured; +struct { +/* @magic and @handle fields have the same offset and size both in +* simple reply and structured reply chunk, so let them be accessible +* without ".simple." or ".structured." specification +*/ +uint32_t magic; +uint32_t _skip; +uint64_t handle; + } QEMU_PACKED; +}NBDReply ; +/* Header of NBD_REPLY_TYPE_OFFSET_DATA, complete NBD_REPLY_TYPE_OFFSET_HOLE */ typedef struct NBDStructuredRead { NBDStructuredReplyChunk h;
@@ -256,4 +264,14 @@ void nbd_client_put(NBDClient *client);void nbd_server_start (SocketAddress *addr,const char *tls_creds,Error **errp); +static inline bool nbd_reply_is_simple (NBDReply *reply) +{ +return reply->magic ==NBD_SIMPLE_REPLY_MAGIC ; +} + +static inline bool nbd_reply_is_structured (NBDReply *reply) +{ +return reply->magic ==NBD_STRUCTURED_REPLY_MAGIC ; +} +#endif
block/nbd-client.c +5 -3
@@ -92,7 +92,9 @@ static coroutine_fn void nbd_read_reply_entry(void *opaque) i =HANDLE_TO_INDEX (s, s->reply.handle);if (i >=MAX_NBD_REQUESTS || !s->requests[i].coroutine || - !s->requests[i].receiving) { + !s->requests[i].receiving || +nbd_reply_is_structured (&s->reply)) + {break ; }
@@ -194,8 +196,8 @@ static int nbd_co_receive_reply(NBDClientSession *s, ret = -EIO ; }else {assert (s->reply.handle == handle); - ret = -s->reply.error; -if (qiov && s->reply.error ==0 ) { + ret = -nbd_errno_to_system_errno (s->reply.simple.error); +if (qiov && ret ==0 ) {if (qio_channel_readv_all (s->ioc, qiov->iov, qiov->niov,NULL ) <0 ) { ret = -EIO ;
nbd/client.c +82 -22
126 lines not on this page · raw message
nbd/trace-events +2 -1
@@ -26,7 +26,8 @@ nbd_client_loop_ret(int ret, const char *error) "NBD loop returned %d: %s" nbd_client_clear_queue(void) "Clearing NBD queue" nbd_client_clear_socket(void) "Clearing NBD socket" 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) }" -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" }" +nbd_receive_simple_reply(int32_t error, const char *errname, uint64_t handle) "Got simple reply: { .error = % " PRId32 " (%s), handle = %" PRIu64" }" +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 " }" # nbd/common.c nbd_unknown_error(int err) "Squashing unexpected error %d to EINVAL"
Signature · 1 line
2.13.6
Re: [Qemu-devel] [PATCH v4 7/8] nbd/client: prepare nbd_receive_reply for structured reply
Quoted 10 lines · Eric Blake
From: Vladimir Sementsov-Ogievskiy <[email protected]>
In following patch
nbd_receive_replywill be used both for simple and structured reply header receiving.
NBDReply is altered into union of simple reply header and structured reply chunk header, simple error translation moved toblock/nbd-clientto be consistent with further structured reply error translation.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>Signed-off-by Eric Blake <[email protected]>
looks ok.
Signature · 2 lines
Best regards, Vladimir
PATCH v4 8/8 nbd: Move nbd_read() to common header
In reply to Eric BlakeAn upcoming change to block/nbd-client.c will want to read the tail of a structured reply chunk directly from the wire. Move this function to make it easier.
Based on a patch from Vladimir Sementsov-Ogievskiy.
--- include/block/nbd.h | 10 ++++++++++ nbd/nbd-internal.h | 9 --------- 2 files changed, 10 insertions(+), 9 deletions(-)
include/block/nbd.h +10 -0
@@ -264,6 +264,16 @@ void nbd_client_put(NBDClient *client);void nbd_server_start (SocketAddress *addr,const char *tls_creds,Error **errp); + +/* nbd_read +* Reads @size bytes from @ioc. Returns 0 on success. +*/ +static inline int nbd_read (QIOChannel *ioc,void *buffer,size_t size, +Error **errp) +{ +return qio_channel_read_all (ioc, buffer, size, errp) <0 ? -EIO :0 ; +} +static inline bool nbd_reply_is_simple (NBDReply *reply) {return reply->magic ==NBD_SIMPLE_REPLY_MAGIC ;
nbd/nbd-internal.h +0 -9
@@ -82,15 +82,6 @@ static inline int nbd_read_eof(QIOChannel *ioc, void *buffer, size_t size,return ret; } -/* nbd_read -* Reads @size bytes from @ioc. Returns 0 on success. -*/ -static inline int nbd_read (QIOChannel *ioc,void *buffer,size_t size, -Error **errp) -{ -return qio_channel_read_all (ioc, buffer, size, errp) <0 ? -EIO :0 ; -} -/* nbd_write * Writes @size bytes to @ioc. Returns 0 on success. */
Signature · 1 line
2.13.6
Re: [Qemu-devel] [PATCH v4 8/8] nbd: Move nbd_read() to common header
Quoted 7 lines · Eric Blake
An upcoming change to
block/nbd-client.cwill want to read the tail of a structured reply chunk directly from the wire. Move this function to make it easier.Based on a patch from Vladimir Sementsov-Ogievskiy.
Signed-off-by Eric Blake <[email protected]>
Signature · 2 lines
Best regards, Vladimir
[Qemu-devel] [PATCH v4 RFC 9/8] nbd: Minimal structured read for client
In reply to Eric BlakeMinimal implementation: for structured error only error_report error message.
---
it's an updated
[PATCH v3 13/13] nbd: Minimal structured read for client
https://lists.gnu.org/archive/html/qemu-devel/2017-10/msg02760.html
from
[PATCH v3 00/13] nbd minimal structured read
https://lists.gnu.org/archive/html/qemu-devel/2017-10/msg02755.html
and now it is based on
[PATCH v4 8/8] nbd: Move nbd_read() to common header
https://lists.gnu.org/archive/html/qemu-devel/2017-10/msg03489.html
from
[PATCH v4 0/8] nbd mimimal structured read
https://lists.gnu.org/archive/html/qemu-devel/2017-10/msg03305.html
v4:
- add Error messages and errp to most of new functions
(I try to return save first occurred error from structured reply,
but rewrite it if fatal error occurred)
- add comment to NBDExportInfo
- move nbd_reply_type_lookup prot. to public .h (I think it should be merged into some previous patch of v4)
- remove ''#include "qemu/error-report.h"'
- rewrite payload_advance* helpers
- add comments about qemu_vfree for *payload
- _SREP_ -> _REPLY_
- move checking NBD_REPLY_FLAG_DONE for NBD_REPLY_TYPE_NONE
- do not negotiate structured reply from qemu-nbd client thread (make client->info.structured_reply in-out)
RFC: I don't like resulting error handling: it is rather confusing,
requires a lot of definitions of "local_err" which looks bad. Propagating, setting error and then drop it for errp=NULL case - all it looks not very effective. Any ideas?
include/block/nbd.h | 12 ++ nbd/nbd-internal.h | 1 - block/nbd-client.c | 483 ++++++++++++++++++++++++++++++++++++++++++++++++---- nbd/client.c | 10 ++ 4 files changed, 474 insertions(+), 32 deletions(-)
include/block/nbd.h +12 -0
@@ -197,6 +197,11 @@ enum {#define NBD_REPLY_TYPE_ERROR NBD_REPLY_ERR(1)#define NBD_REPLY_TYPE_ERROR_OFFSET NBD_REPLY_ERR(2) +static inline bool nbd_reply_type_is_error (int type) +{ +return type & (1 <<15 ); +} +/* NBD errors are based on errno numbers, so there is a 1:1 mapping, * but only a limited set of errno values is specified in the protocol. * Everything else is squashed to EINVAL.
@@ -214,6 +219,11 @@ enum {struct NBDExportInfo {/* Set by client before nbd_receive_negotiate() */ bool request_sizes; + +/* In-out fields, set by client before nbd_receive_negotiate() and +* updated by server results during nbd_receive_negotiate() */ +bool structured_reply; +/* Set by server results during nbd_receive_negotiate() */ uint64_t size;uint16_t flags;
@@ -284,4 +294,6 @@ static inline bool nbd_reply_is_structured(NBDReply *reply) return reply->magic == NBD_STRUCTURED_REPLY_MAGIC; } +const char *nbd_reply_type_lookup (uint16_t type); +#endif
nbd/nbd-internal.h +0 -1
@@ -104,7 +104,6 @@ const char *nbd_opt_lookup(uint32_t opt);const char *nbd_rep_lookup (uint32_t rep);const char *nbd_info_lookup (uint16_t info);const char *nbd_cmd_lookup (uint16_t info); -const char *nbd_reply_type_lookup (uint16_t type);const char *nbd_err_lookup (int err);int nbd_drop (QIOChannel *ioc,size_t size,Error **errp);
block/nbd-client.c +452 -31
547 lines not on this page · raw message
nbd/client.c +10 -0
@@ -684,6 +684,16 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name,if (fixedNewStyle) {int result; +if (info->structured_reply) { + result =nbd_request_simple_option (ioc, +NBD_OPT_STRUCTURED_REPLY , + errp); +if (result <0 ) { + goto fail; + } + info->structured_reply = result ==1 ; + } +/* Try NBD_OPT_GO first - if it works, we are done (it * also gives us a good message if the server requires * TLS). If it is not available, fall back to
Signature · 1 line
2.11.1
Re: [Qemu-devel] [PATCH v4 RFC 9/8] nbd: Minimal structured read for client
Quoted 4 lines · Vladimir Sementsov-Ogievskiy
Minimal implementation: for structured error only
error_reporterror message.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>
Quoted 8 lines
---
RFC: I don't like resulting error handling: it is rather confusing,
requires a lot of definitions of "local_err" which looks bad. Propagating, setting error and then drop it for errp=NULL case - all it looks not very effective. Any ideas?
As long as the function you call has a sane return value that can easily be used to tell if you had an error or not, you can often use that to pass errp through the call, rather than needing a local_err and error_propagate(). I'll see if I can spot such places during my review.
Quoted 5 lines
include/block/nbd.h | 12 ++ nbd/nbd-internal.h | 1 - block/nbd-client.c | 483 ++++++++++++++++++++++++++++++++++++++++++++++++---- nbd/client.c | 10 ++ 4 files changed, 474 insertions(+), 32 deletions(-)
Feels like a big patch. Of course, we can't enable the client requesting structured replies until all the pieces are in place (otherwise, it becomes difficult to bisect a broken client against a working server); but it may be possible to split this patch into smaller pieces if it would aid review. That said, I'm fine keeping it as one big patch, if I don't see too much that needs tweaking.
First, before I review anything, a quick check, with this patch applied:
Server:
$ ./qemu-nbd -x foo -f qcow2 --trace='nbd_*' file -r
Client:
$ ./qemu-io -f raw nbd://localhost:10809/foo --trace='nbd_*'
Looking at the trace, the negotiation succeeds (yay!).
But in the client, I then perform 'w 0 0' (a zero-byte write, which should fail because the server is read-only). I see:
C: [email protected]:nbd_send_request Sending request to server: { .from = 0, .len = 0, .handle = 93997172956880, .flags = 0x1, .type = 1 (write) }
S: [email protected]:nbd_receive_request Got request: { magic = 0x25609513, .flags = 0x1, .type = 0x1, from = 0, len = 0 }
S: [email protected]:nbd_co_receive_request_decode_type Decoding
type: handle = 93997172956880, type = 1 (write)
S: [email protected]:nbd_co_receive_request_payload_received Payload received: handle = 93997172956880, len = 0
S: [email protected]:nbd_co_send_structured_error Send structured error reply: handle = 93997172956880, error = 1 (EPERM), msg = ''
C: [email protected]:nbd_receive_structured_reply_chunk Got structured reply chunk: { flags = 0x1, type = 32769, handle = 93997172956880, length = 6 }
C: wrote 0/0 bytes at offset 0
C: 0 bytes, 1 ops; 0.0002 sec (0 bytes/sec and 4291.8455 ops/sec)
Oops - the client claimed success, even though the server replied with EPERM. And the server didn't do a good job of including details on the error message. So there's still some tweaks needed.
block/nbd-client.c err: · line 187
Quoted 11 lines
block/nbd-client.c +4
-3@@ -181,75 +181,486 @@ err:return rc; } -static int nbd_co_receive_reply (NBDClientSession *s, -uint64_t handle, -QEMUIOVector *qiov) +static inline uint16_t payload_advance16 (uint8_t **payload) +{ + *payload +=2 ; +return lduw_be_p (*payload -2 );
Correct - loads a 16-bit unsigned quantity.
block/nbd-client.c err: · line 193
Quoted 6 lines
block/nbd-client.c +6
-0+} + +static inline uint32_t payload_advance32 (uint8_t **payload) +{ + *payload +=4 ; +return lduw_be_p (*payload -4 );
Oops, you want ldl_be_p(), to load a 32-bit quantity (sign not relevant).
block/nbd-client.c err: · line 199
Quoted 6 lines
block/nbd-client.c +6
-0+} + +static inline uint64_t payload_advance64 (uint8_t **payload) +{ + *payload +=8 ; +return lduw_be_p (*payload -8 );
Oops, you want ldq_be_p(), to load a 64-bit quantity. Peter's pending patch "[PATCH v4] docs/devel/loads-stores.rst: Document our various load and store APIs" goes into more details on which load/store routines to use when.
Quoted 15 lines
block/nbd-client.c +15
-0+ +static int nbd_co_receive_offset_data_payload (NBDClientSession *s, +QEMUIOVector *qiov,Error **errp) +{ +QEMUIOVector sub_qiov; +uint64_t offset; +size_t data_size; +int ret; +NBDStructuredReplyChunk *chunk = &s->reply.structured; + +assert (nbd_reply_is_structured (&s->reply)); + +if (chunk->length <sizeof (offset)) { +error_setg (errp,"Protocol error: invalid payload for " +"NBD_REPLY_TYPE_OFFSET_DATA" );
block/nbd-client.c err: · line 286
Quoted 10 lines
block/nbd-client.c +10
-0+return -EINVAL ; + } + +if (nbd_read (s->ioc, &offset,sizeof (offset), errp) <0 ) { +return -EIO ; + } +be64_to_cpus (&offset); + + data_size = chunk->length -sizeof (offset); +if (offset + data_size > qiov->size) {
Arithmetic overflow is possible here; it's safer to write:
if (offset > qiov->size - data_size)
Similar for hole_size earlier.
Quoted 8 lines
block/nbd-client.c +8
-0+ +#define NBD_MAX_MALLOC_PAYLOAD 1000 +/* nbd_co_receive_structured_payload +* The resulting pointer stored in @payload requires qemu_vfree() to free it. +*/ +static coroutine_fn int nbd_co_receive_structured_payload( +NBDClientSession *s,void **payload,Error **errp) +{
block/nbd-client.c err: · line 316
Quoted 10 lines
block/nbd-client.c +10
-0+int ret; +uint32_t len; + +assert (nbd_reply_is_structured (&s->reply)); + + len = s->reply.structured.length; + +if (len ==0 ) { +return 0 ; + }
Should we assign *payload to NULL even on this short-circuit (if payload itself is non-NULL)? Oh, you do this in the caller [1]
block/nbd-client.c err: · line 324
Quoted 8 lines
block/nbd-client.c +8
-0+ +if (payload ==NULL ) { +error_setg (errp,"Unexpected structured payload" ); +return -EINVAL ; + } + +if (len >NBD_MAX_MALLOC_PAYLOAD ) { +error_setg (errp,"Too large payload" );
Grammar: Payload too large
block/nbd-client.c err: · line 328
Quoted 4 lines
block/nbd-client.c +4
-0+return -EINVAL ; + } + + *payload =qemu_memalign (8 , len);
Why are we bothering to read this into memalign'ed memory, seeing as how it is packed data, and more likely than not, we will NOT be benefitting from the alignment? Can we get by with simpler g_new()/g_free()?
Quoted 11 lines
block/nbd-client.c +11
-0+ ret =nbd_read (s->ioc, *payload, len, errp); +if (ret <0 ) { +qemu_vfree (*payload); + *payload =NULL ; +return ret; + } + +return 0 ; +} + +/* nbd_co_do_receive_one_chunk
block/nbd-client.c err: · line 349
Quoted 10 lines
block/nbd-client.c +10
-0+* for simple reply: +* set request_ret to received reply error +* if qiov is not NULL: read payload to @qiov +* for structured reply chunk: +* if error chunk: read payload, set @request_ret, do not set @payload +* else if offset_data chunk: read payload data to @qiov, do not set @payload +* else: read payload to @payload +* +* The pointer stored in @payload requires qemu_vfree() to free it. +* If function fails @errp containts corresponding error message. If function
s/containts/contains/
block/nbd-client.c err: · line 351
Quoted 2 lines
block/nbd-client.c +2
-0+* doesn't fail but reply contains error @request_ret is set to corresponding +* return code and errp is set to corresponding error message.
Hmm, that feels awkward - if errp can be set even when the function does not return a negative value, then it's tougher to tell failures apart.
block/nbd-client.c err: · line 364
Quoted 13 lines
block/nbd-client.c +10
-0+*/ +static coroutine_fn int nbd_co_do_receive_one_chunk( +NBDClientSession *s,uint64_t handle,bool only_structured, +int *request_ret,QEMUIOVector *qiov,void **payload,Error **errp) {int ret;int i =HANDLE_TO_INDEX (s, handle); +void *local_payload =NULL ; +NBDStructuredReplyChunk *chunk; + +if (payload) { + *payload =NULL ; + }
[1] answer to my question above.
Quoted 20 lines
block/nbd-client.c +20
-0+ +/* nbd_co_receive_one_chunk +* Read reply, wake up read_reply_co and set s->quit if needed. +* Return value is a fatal error code or normal nbd reply error code +* +* The pointer stored in @payload requires qemu_vfree() to free it. +*/ +static coroutine_fn int nbd_co_receive_one_chunk( +NBDClientSession *s,uint64_t handle,bool only_structured, +QEMUIOVector *qiov,NBDReply *reply,void **payload,Error **errp) +{ +int request_ret; +int ret =nbd_co_do_receive_one_chunk (s, handle, only_structured, + &request_ret, qiov, payload, errp); + +if (ret <0 ) { + s->quit = true; + }else { +/* For assert at loop start in nbd_read_reply_entry */ +if (reply) {
block/nbd-client.c err: · line 464
Quoted 10 lines
block/nbd-client.c +5
-1+ *reply = s->reply; + } + s->reply.handle =0 ; + ret = request_ret; + } -/* Kick the read_reply_co to get the next reply. */ if (s->read_reply_co) {aio_co_wake (s->read_reply_co); }
Some things are a bit harder to test as long as our only working server is currently hardcoded to send exactly one chunk per message; but that's part of the testing I plan to do in the next couple of days (even if I end up using gdb to fake packet sequences rather than actually coding a more-complete server).
Quoted 12 lines
block/nbd-client.c +11
-0+return ret; +} + +typedef struct NBDReplyChunkIter { +int ret; +Error *err; +bool done, only_structured; +}NBDReplyChunkIter ; + +static void nbd_iter_error (NBDReplyChunkIter *iter,bool fatal, +int ret,Error **local_err)
block/nbd-client.c err: · line 486
Quoted 10 lines
block/nbd-client.c +10
-0+{ +assert (ret <0 ); + +if (fatal || iter->ret ==0 ) { +if (iter->ret !=0 ) { +error_free (iter->err); + iter->err =NULL ; + } + iter->ret = ret; +error_propagate (&iter->err, *local_err);
error_propagate() can be safely called more than once (only the first call sets an error). I guess what you're trying to do here is that if the server sends more than one error chunk, you favor that message; but if we fail to communicate with the server, that takes even higher priority.
Quoted 8 lines
block/nbd-client.c +8
-0+ }else { +error_free (*local_err); + } + + *local_err =NULL ; +} + +/* NBD_FOREACH_REPLY_CHUNK
block/nbd-client.c err: · line 504
Quoted 10 lines
block/nbd-client.c +10
-0+* The pointer stored in @payload requires qemu_vfree() to free it. +*/ +#define NBD_FOREACH_REPLY_CHUNK (s, iter, handle, structured, \ + qiov, reply, payload) \ + for (iter = (NBDReplyChunkIter) { .only_structured = structured }; \ + nbd_reply_chunk_iter_receive(s, &iter, handle, qiov, reply, payload);) + +/* nbd_reply_chunk_iter_receive +* The pointer stored in @payload requires qemu_vfree() to free it. +*/
Maybe worth a comment documenting the return value? Although it is somewhat mentioned below [2]
Quoted 44 lines
block/nbd-client.c +44
-0+static bool nbd_reply_chunk_iter_receive (NBDClientSession *s, +NBDReplyChunkIter *iter, +uint64_t handle, +QEMUIOVector *qiov,NBDReply *reply, +void **payload) +{ +int ret; +NBDReply local_reply; +NBDStructuredReplyChunk *chunk; +Error *local_err =NULL ; +if (s->quit) { +error_setg (&local_err,"Connection closed" ); +nbd_iter_error (iter, true, -EIO , &local_err); + goto break_loop; + } + +if (iter->done) { +/* Previous iteration was last. */ + goto break_loop; + } + +if (reply ==NULL ) { + reply = &local_reply; + } + + ret =nbd_co_receive_one_chunk (s, handle, iter->only_structured, + qiov, reply, payload, &local_err); +if (ret <0 ) { +/* If it is a fatal error s->quit is set by nbd_co_receive_one_chunk */ +nbd_iter_error (iter, s->quit, ret, &local_err); + } + +/* Do not execute the body of NBD_FOREACH_REPLY_CHUNK for simple reply. */ +if (nbd_reply_is_simple (&s->reply) || s->quit) { + goto break_loop; + } + + chunk = &reply->structured; + iter->only_structured = true; + +if (chunk->type ==NBD_REPLY_TYPE_NONE ) { +/* NBD_REPLY_FLAG_DONE is already checked in nbd_co_receive_one_chunk */ +assert (chunk->flags &NBD_REPLY_FLAG_DONE ); + goto break_loop;
block/nbd-client.c err: · line 558
Quoted 10 lines
block/nbd-client.c +10
-0+ } + +if (chunk->flags &NBD_REPLY_FLAG_DONE ) { +/* This iteration is last. */ + iter->done = true; + } + +/* Execute the loop body */ +return true; +
[2]
Quoted 13 lines
block/nbd-client.c +4
-2+break_loop: + s->requests[HANDLE_TO_INDEX (s, handle)].coroutine =NULL ; +qemu_co_mutex_lock (&s->send_mutex); s->in_flight--;qemu_co_queue_next (&s->free_sema);qemu_co_mutex_unlock (&s->send_mutex); -return ret; +return false; } -static int nbd_co_request(BlockDriverState *bs,
block/nbd-client.c err: · line 577
Quoted 10 lines
block/nbd-client.c +8
-2- NBDRequest *request, - QEMUIOVector *qiov) +static int nbd_co_receive_return_code (NBDClientSession *s,uint64_t handle, +Error **errp) +{ +NBDReplyChunkIter iter; + + NBD_FOREACH_REPLY_CHUNK(s, iter, handle, false, NULL, NULL, NULL) { +/* nbd_reply_chunk_iter_receive does all the work */ + ;
Do we need the empty ';'? {} is a valid loop body.
Quoted 33 lines
block/nbd-client.c +33
-0+ } + +error_propagate (errp, iter.err); +return iter.ret; +} + +static int nbd_co_receive_cmdread_reply (NBDClientSession *s,uint64_t handle, +QEMUIOVector *qiov,Error **errp) +{ +NBDReplyChunkIter iter; +NBDReply reply; +void *payload =NULL ; +Error *local_err =NULL ; + +NBD_FOREACH_REPLY_CHUNK (s, iter, handle, s->info.structured_reply, + qiov, &reply, &payload) + { +int ret; +NBDStructuredReplyChunk *chunk = &reply.structured; + +assert (nbd_reply_is_structured (&reply)); + +switch (chunk->type) { +case NBD_REPLY_TYPE_OFFSET_DATA : +/* special cased in nbd_co_receive_one_chunk, data is already +* in qiov */ +break ; +case NBD_REPLY_TYPE_OFFSET_HOLE : + ret =nbd_parse_offset_hole_payload (&reply.structured, payload, + qiov, &local_err); +if (ret <0 ) { + s->quit = true; +nbd_iter_error (&iter, true, ret, &local_err);
block/nbd-client.c err: · line 620
Quoted 10 lines
block/nbd-client.c +10
-0+ } +break ; +default : +if (!nbd_reply_type_is_error (chunk->type)) { +/* not allowed reply type */ + s->quit = true; +error_setg (&local_err, +"Unexpected reply type: %d (%s) for CMD_READ" , + chunk->type,nbd_reply_type_lookup (chunk->type)); +nbd_iter_error (&iter, true, -EINVAL , &local_err);
This feels a bit like action at a difference - breaking out of the loop but making sure the loop still tracks the right error. But I'm not immediately seeing if it is safe to directly use 'return' here instead.
Quoted 23 lines
block/nbd-client.c +16
-4+ } + } + +qemu_vfree (payload); + payload =NULL ; + } + +error_propagate (errp, iter.err); +return iter.ret; +} + +static int nbd_co_request (BlockDriverState *bs,NBDRequest *request, +QEMUIOVector *write_qiov) { -NBDClientSession *client =nbd_get_client_session (bs);int ret; +Error *local_err =NULL ; +NBDClientSession *client =nbd_get_client_session (bs); -if (qiov) { -assert (request->type ==NBD_CMD_WRITE || request->type ==NBD_CMD_READ ); -assert (request->len ==iov_size (qiov->iov, qiov->niov)); +assert (request->type !=NBD_CMD_READ );
block/nbd-client.c err: · line 646
Quoted 10 lines
block/nbd-client.c +5
-3+if (write_qiov) { +assert (request->type ==NBD_CMD_WRITE ); +assert (request->len ==iov_size (write_qiov->iov, write_qiov->niov)); }else { -assert (request->type !=NBD_CMD_WRITE && request->type !=NBD_CMD_READ ); +assert (request->type !=NBD_CMD_WRITE ); } - ret =nbd_co_send_request (bs, request, - request->type ==NBD_CMD_WRITE ? qiov :NULL ); + ret =nbd_co_send_request (bs, request, write_qiov);
If we have to respin things, wiring up errp handling to existing calls prior to adding structured reply handling would probably be worth splitting the patch.
Quoted 55 lines
block/nbd-client.c +19
-3if (ret <0 ) {return ret; } -return nbd_co_receive_reply (client, request->handle, - request->type ==NBD_CMD_READ ? qiov :NULL ); + ret =nbd_co_receive_return_code (client, request->handle, &local_err); +if (ret <0 ) { +error_report_err (local_err); + } +return ret; }int nbd_client_co_preadv (BlockDriverState *bs,uint64_t offset,uint64_t bytes,QEMUIOVector *qiov,int flags) { +int ret; +Error *local_err =NULL ; +NBDClientSession *client =nbd_get_client_session (bs);NBDRequest request = { .type =NBD_CMD_READ , .from = offset,@@ -259,7 +670,16 @@ int nbd_client_co_preadv(BlockDriverState *bs, uint64_t offset,assert (bytes <=NBD_MAX_BUFFER_SIZE );assert (!flags); -return nbd_co_request (bs, &request, qiov); + ret =nbd_co_send_request (bs, &request,NULL ); +if (ret <0 ) { +return ret; + } + + ret =nbd_co_receive_cmdread_reply (client, request.handle, qiov, &local_err); +if (ret <0 ) { +error_report_err (local_err); + } +return ret; } int nbd_client_co_pwritev(BlockDriverState *bs, uint64_t offset,@@ -381,6 +801,7 @@ int nbd_client_init(BlockDriverState *bs,qio_channel_set_blocking (QIO_CHANNEL (sioc), true,NULL ); client->info.request_sizes = true; + client->info.structured_reply = true; ret =nbd_receive_negotiate (QIO_CHANNEL (sioc), export, tlscreds, hostname, &client->ioc, &client->info, errp);nbd/client.c +0
-0@@ -684,6 +684,16 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name,if (fixedNewStyle) {int result;
nbd/client.c int nbd_receive_negotiate(QIOChannel *ioc, const char *name, · line 695
Quoted 10 lines
nbd/client.c +9
-0+if (info->structured_reply) { + result =nbd_request_simple_option (ioc, +NBD_OPT_STRUCTURED_REPLY , + errp); +if (result <0 ) { + goto fail; + } + info->structured_reply = result ==1 ; + }
Looks like the handshake is handled correctly.
Quoted 5 lines
nbd/client.c +1
-0+/* Try NBD_OPT_GO first - if it works, we are done (it * also gives us a good message if the server requires * TLS). If it is not available, fall back to
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [Qemu-devel] [PATCH v4 RFC 9/8] nbd: Minimal structured read for client
Quoted 27 lines · Eric Blake
Quoted 5 lines · Vladimir Sementsov-Ogievskiy
Minimal implementation: for structured error only
error_reporterror message.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>---
But in the client, I then perform 'w 0 0' (a zero-byte write, which should fail because the server is read-only). I see:
C: [email protected]:nbd_send_request Sending request to server: { .from = 0, .len = 0, .handle = 93997172956880, .flags =
0x1, .type = 1 (write) }
S: [email protected]:nbd_receive_request Got request: { magic =0x25609513, .flags =0x1, .type =0x1, from = 0, len = 0 }
S: [email protected]:nbd_co_receive_request_decode_type Decoding
type: handle = 93997172956880, type = 1 (write)
S: [email protected]:nbd_co_receive_request_payload_received Payload received: handle = 93997172956880, len = 0
S: [email protected]:nbd_co_send_structured_error Send structured error reply: handle = 93997172956880, error = 1 (EPERM), msg = ''
C: [email protected]:nbd_receive_structured_reply_chunk Got structured reply chunk: { flags =0x1, type = 32769, handle = 93997172956880, length = 6 }
C: wrote 0/0 bytes at offset 0
C: 0 bytes, 1 ops; 0.0002 sec (0 bytes/sec and 4291.8455 ops/sec)
Quoted 3 lines
Oops - the client claimed success, even though the server replied with EPERM. And the server didn't do a good job of including details on the error message. So there's still some tweaks needed.
The server not sending details is a separate pre-existing issue; in server.c, we set errp for NBD_CMD_WRITE_ZEROES but not for NBD_CMD_WRITE. You can get the server to cough up a message by using 'w -z 0 1' instead.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [Qemu-devel] [PATCH v4 RFC 9/8] nbd: Minimal structured read for client
In reply to Vladimir Sementsov-OgievskiyQuoted 5 lines · Vladimir Sementsov-Ogievskiy
Minimal implementation: for structured error only
error_reporterror message.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>---
and I replied:
Quoted 21 lines
But in the client, I then perform 'w 0 0' (a zero-byte write, which should fail because the server is read-only). I see:
C: [email protected]:nbd_send_request Sending request to server: { .from = 0, .len = 0, .handle = 93997172956880, .flags =
0x1, .type = 1 (write) }
S: [email protected]:nbd_receive_request Got request: { magic =0x25609513, .flags =0x1, .type =0x1, from = 0, len = 0 }
S: [email protected]:nbd_co_receive_request_decode_type Decoding
type: handle = 93997172956880, type = 1 (write)
S: [email protected]:nbd_co_receive_request_payload_received Payload received: handle = 93997172956880, len = 0
S: [email protected]:nbd_co_send_structured_error Send structured error reply: handle = 93997172956880, error = 1 (EPERM), msg = ''
C: [email protected]:nbd_receive_structured_reply_chunk Got structured reply chunk: { flags =0x1, type = 32769, handle = 93997172956880, length = 6 }
C: wrote 0/0 bytes at offset 0
C: 0 bytes, 1 ops; 0.0002 sec (0 bytes/sec and 4291.8455 ops/sec)
Quoted 3 lines
Oops - the client claimed success, even though the server replied with EPERM. And the server didn't do a good job of including details on the error message. So there's still some tweaks needed.
Okay, I found that issue:
Quoted 13 lines
block/nbd-client.c +13
-0+static int nbd_parse_error_payload (NBDStructuredReplyChunk *chunk, +uint8_t *payload,int *request_ret, +Error **errp) +{ +uint32_t error; +uint16_t message_size; + +assert (chunk->type & (1 <<15 )); + +if (chunk->length <sizeof (error) +sizeof (message_size)) { +error_setg (errp, +"Protocol error: invalid payload for structured error" ); +return -EINVAL ;
block/nbd-client.c err: · line 254
Quoted 10 lines
block/nbd-client.c +10
-0+ } + + error =nbd_errno_to_system_errno (payload_advance32 (&payload)); +if (error ==0 ) { +error_setg (errp,"Protocol error: server sent structured error chunk" +"with error = 0" ); +return -EINVAL ; + } + + *request_ret = error;
Here, you set *request_ret to a positive value when the server gives an error,
Quoted 17 lines
block/nbd-client.c +7
-8+static coroutine_fn int nbd_co_do_receive_one_chunk( +NBDClientSession *s,uint64_t handle,bool only_structured, +int *request_ret,QEMUIOVector *qiov,void **payload,Error **errp) { - } else { -assert (s->reply.handle == handle); - ret = -nbd_errno_to_system_errno (s->reply.simple.error); -if (qiov && ret ==0 ) { -if (qio_channel_readv_all (s->ioc, qiov->iov, qiov->niov, -NULL ) <0 ) { - ret = -EIO ; - s->quit = true; +error_setg (errp,"Connection closed" ); +return -EIO ; + } +
block/nbd-client.c err: · line 385
Quoted 10 lines
block/nbd-client.c +10
-0+assert (s->reply.handle == handle); + +if (nbd_reply_is_simple (&s->reply)) { +if (only_structured) { +error_setg (errp,"Protocol error: simple reply when structured" +"reply chunk was expected" ); +return -EINVAL ; + } + + *request_ret = -nbd_errno_to_system_errno (s->reply.simple.error);
But here, you set it to a negative value,
Quoted 24 lines
block/nbd-client.c +24
-0+/* nbd_reply_chunk_iter_receive +* The pointer stored in @payload requires qemu_vfree() to free it. +*/ +static bool nbd_reply_chunk_iter_receive (NBDClientSession *s, +NBDReplyChunkIter *iter, +uint64_t handle, +QEMUIOVector *qiov,NBDReply *reply, +void **payload) +{ +int ret; +NBDReply local_reply; +NBDStructuredReplyChunk *chunk; +Error *local_err =NULL ; +if (s->quit) { +error_setg (&local_err,"Connection closed" ); +nbd_iter_error (iter, true, -EIO , &local_err); + goto break_loop; + } + +if (iter->done) { +/* Previous iteration was last. */ + goto break_loop; + } +
block/nbd-client.c err: · line 535
Quoted 10 lines
block/nbd-client.c +10
-0+if (reply ==NULL ) { + reply = &local_reply; + } + + ret =nbd_co_receive_one_chunk (s, handle, iter->only_structured, + qiov, reply, payload, &local_err); +if (ret <0 ) { +/* If it is a fatal error s->quit is set by nbd_co_receive_one_chunk */ +nbd_iter_error (iter, s->quit, ret, &local_err); + }
and you only ever set iter.ret to non-zero if the value was negative (so you were missing all errors sent through a structured reply).
There was a lot of back-and-forth hunting through the code to see where errors flow. I wonder if our intermediate processing should try harder to distinguish between NBD errors sent by the server (but the connection is still good - aka not a fatal error for the receive loop but does affect the end client) vs. those detected locally (server sent garbage or our connection failed, either way, the error is fatal and we won't ever talk to the server again).
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [Qemu-devel] [PATCH v4 RFC 9/8] nbd: Minimal structured read for client
In reply to Vladimir Sementsov-OgievskiyQuoted 7 lines · Vladimir Sementsov-Ogievskiy
Minimal implementation: for structured error only
error_reporterror message.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>---
block/nbd-client.c err: · line 654
Quoted 10 lines
block/nbd-client.c +6
-2+static int nbd_co_request (BlockDriverState *bs,NBDRequest *request, +QEMUIOVector *write_qiov) { -return nbd_co_receive_reply (client, request->handle, - request->type ==NBD_CMD_READ ? qiov :NULL ); + ret =nbd_co_receive_return_code (client, request->handle, &local_err); +if (ret <0 ) { +error_report_err (local_err); + }
I think this new error_report_err() is a regression in behavior. Running the old server:
$ qemu-nbd -x foo -f qcow2 --trace='nbd_*' file -r
and an old client:
$ qemu-io -f raw nbd://localhost:10809/foo qemu-io> w 0 0 write failed: Operation not permitted qemu-io> q
but with the new client (once I fix the bug about being able to ignore the NBD_REP_ERR_UNSUP with non-zero length in the earlier patch):
$ ./qemu-io -f raw nbd://localhost:10809/foo qemu-io> w 0 0 Request failed: Operation not permitted write failed: Operation not permitted qemu-io>
and worse, new server with new client:
$ ./qemu-io -f raw nbd://localhost:10809/foo qemu-io> w 0 0 : Operation not permitted write failed: Operation not permitted qemu-io>
we don't even manage to post a sane message.
Reporting fatal errors where we lose connection with the server (or forcefully give up on the server because it violated protocol) may be okay, but reporting common errors where the server reported a problem but we are still connected is too verbose.
I know I asked about errp plumbing on v3, but now I'm thinking that it was a premature request; we either plumb in errp handling without any new features, or we do the new features in isolation and only later see if adding errp plumbing makes sense. Yes, that means undoing some of the changes you made between v3 and v4, so sorry for the churn it has caused.
I hope to post a v5 soon with the tweaks I've made after playing with this version.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org