Skip to content

Commit

Permalink
nbd: Expose constants and structs for structured read
Browse files Browse the repository at this point in the history
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.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.

Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20171027104037.8319-4-eblake@redhat.com>
  • Loading branch information
ebblake committed Oct 30, 2017
1 parent dd68944 commit bae245d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
41 changes: 41 additions & 0 deletions include/block/nbd.h
Expand Up @@ -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 */
Expand All @@ -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. */
Expand Down Expand Up @@ -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 {
Expand All @@ -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.
Expand All @@ -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 */
Expand Down
27 changes: 27 additions & 0 deletions nbd/common.c
Expand Up @@ -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) {
Expand All @@ -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:
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion nbd/nbd-internal.h
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions nbd/server.c
Expand Up @@ -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:
Expand Down

0 comments on commit bae245d

Please sign in to comment.