nbd patches for 2017-07-10
- Eric Blake: MAINTAINERS: Promote NBD to supported, with new maintainer - Vladimir Sementsov-Ogievskiy: [00/10] nbd refactoring part 2 -----BEGIN PGP SIGNATURE----- Comment: Public key at http://people.redhat.com/eblake/eblake.gpg iQEcBAABCAAGBQJZY5ZGAAoJEKeha0olJ0Nq8w8H/23OZ/tr2Nb3qNu6mOMVSeDp gvBATbfatfaiBENDtaA76SAWbUlj262OiHGEfWt1VBnBFLIq+sTxGbUIRh34pqre AYkDyq+691YDuI54dl6m3KDLusTIZlPskEEsh/88+H1fuZP4lew4Fg5SBRQR00Uf N/s5NyS+FTy22GCS5nGWaDzcuKPb5QlVjB8D3vQ4ZWWUw1RMGHTiZ0VfnhBytg7e TFaq++Qn7uPVtodLeM5qNsc8XivzqlymBvM7Y9JC4oS0XgoYVFN1uQg47A3ZDoGz cTaTt9/RdkQnfPw9RLZBxKq/kEVF+sJIAtbGNc1oUagFOcWLgLtiWfjkA+1CT3k= =C+wT -----END PGP SIGNATURE----- Merge remote-tracking branch 'remotes/ericb/tags/pull-nbd-2017-07-10-v2' into staging nbd patches for 2017-07-10 - Eric Blake: MAINTAINERS: Promote NBD to supported, with new maintainer - Vladimir Sementsov-Ogievskiy: [00/10] nbd refactoring part 2 # gpg: Signature made Mon 10 Jul 2017 15:59:18 BST # gpg: using RSA key 0xA7A16B4A2527436A # gpg: Good signature from "Eric Blake <eblake@redhat.com>" # gpg: aka "Eric Blake (Free Software Programmer) <ebb9@byu.net>" # gpg: aka "[jpeg image of size 6874]" # Primary key fingerprint: 71C2 CC22 B1C4 6029 27D2 F3AA A7A1 6B4A 2527 436A * remotes/ericb/tags/pull-nbd-2017-07-10-v2: nbd: use generic trace subsystem instead of TRACE macro nbd: refactor tracing nbd/server: rename clientflags var in nbd_negotiate_options nbd/server: fix TRACE in nbd_negotiate_send_rep_len nbd/client: refactor TRACE of NBD_MAGIC nbd/common: nbd_tls_handshake: remove extra TRACE nbd/server: add errp to nbd_send_reply() nbd/server: use errp instead of LOG nbd/server: refactor nbd_negotiate nbd/server: nbd_negotiate: return 1 on NBD_OPT_ABORT MAINTAINERS: Promote NBD to supported, with new maintainer Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
This commit is contained in:
commit
6b06e3e49e
21
MAINTAINERS
21
MAINTAINERS
|
@ -1348,15 +1348,6 @@ W: http://info.iet.unipi.it/~luigi/netmap/
|
||||||
S: Maintained
|
S: Maintained
|
||||||
F: net/netmap.c
|
F: net/netmap.c
|
||||||
|
|
||||||
Network Block Device (NBD)
|
|
||||||
M: Paolo Bonzini <pbonzini@redhat.com>
|
|
||||||
S: Odd Fixes
|
|
||||||
F: block/nbd*
|
|
||||||
F: nbd/
|
|
||||||
F: include/block/nbd*
|
|
||||||
F: qemu-nbd.c
|
|
||||||
T: git git://github.com/bonzini/qemu.git nbd-next
|
|
||||||
|
|
||||||
NUMA
|
NUMA
|
||||||
M: Eduardo Habkost <ehabkost@redhat.com>
|
M: Eduardo Habkost <ehabkost@redhat.com>
|
||||||
S: Maintained
|
S: Maintained
|
||||||
|
@ -1710,6 +1701,18 @@ S: Supported
|
||||||
F: block/iscsi.c
|
F: block/iscsi.c
|
||||||
F: block/iscsi-opts.c
|
F: block/iscsi-opts.c
|
||||||
|
|
||||||
|
Network Block Device (NBD)
|
||||||
|
M: Eric Blake <eblake@redhat.com>
|
||||||
|
M: Paolo Bonzini <pbonzini@redhat.com>
|
||||||
|
L: qemu-block@nongnu.org
|
||||||
|
S: Maintained
|
||||||
|
F: block/nbd*
|
||||||
|
F: nbd/
|
||||||
|
F: include/block/nbd*
|
||||||
|
F: qemu-nbd.*
|
||||||
|
F: blockdev-nbd.c
|
||||||
|
T: git git://repo.or.cz/qemu/ericb.git nbd
|
||||||
|
|
||||||
NFS
|
NFS
|
||||||
M: Jeff Cody <jcody@redhat.com>
|
M: Jeff Cody <jcody@redhat.com>
|
||||||
M: Peter Lieven <pl@kamp.de>
|
M: Peter Lieven <pl@kamp.de>
|
||||||
|
|
|
@ -168,6 +168,7 @@ trace-events-subdirs += linux-user
|
||||||
trace-events-subdirs += qapi
|
trace-events-subdirs += qapi
|
||||||
trace-events-subdirs += accel/tcg
|
trace-events-subdirs += accel/tcg
|
||||||
trace-events-subdirs += accel/kvm
|
trace-events-subdirs += accel/kvm
|
||||||
|
trace-events-subdirs += nbd
|
||||||
|
|
||||||
trace-events-files = $(SRC_PATH)/trace-events $(trace-events-subdirs:%=$(SRC_PATH)/%/trace-events)
|
trace-events-files = $(SRC_PATH)/trace-events $(trace-events-subdirs:%=$(SRC_PATH)/%/trace-events)
|
||||||
|
|
||||||
|
|
81
nbd/client.c
81
nbd/client.c
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
#include "qemu/osdep.h"
|
#include "qemu/osdep.h"
|
||||||
#include "qapi/error.h"
|
#include "qapi/error.h"
|
||||||
|
#include "trace.h"
|
||||||
#include "nbd-internal.h"
|
#include "nbd-internal.h"
|
||||||
|
|
||||||
static int nbd_errno_to_system_errno(int err)
|
static int nbd_errno_to_system_errno(int err)
|
||||||
|
@ -44,7 +45,7 @@ static int nbd_errno_to_system_errno(int err)
|
||||||
ret = ESHUTDOWN;
|
ret = ESHUTDOWN;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
TRACE("Squashing unexpected error %d to EINVAL", err);
|
trace_nbd_unknown_error(err);
|
||||||
/* fallthrough */
|
/* fallthrough */
|
||||||
case NBD_EINVAL:
|
case NBD_EINVAL:
|
||||||
ret = EINVAL;
|
ret = EINVAL;
|
||||||
|
@ -103,7 +104,7 @@ static int nbd_send_option_request(QIOChannel *ioc, uint32_t opt,
|
||||||
if (len == -1) {
|
if (len == -1) {
|
||||||
req.length = len = strlen(data);
|
req.length = len = strlen(data);
|
||||||
}
|
}
|
||||||
TRACE("Sending option request %" PRIu32", len %" PRIu32, opt, len);
|
trace_nbd_send_option_request(opt, len);
|
||||||
|
|
||||||
stq_be_p(&req.magic, NBD_OPTS_MAGIC);
|
stq_be_p(&req.magic, NBD_OPTS_MAGIC);
|
||||||
stl_be_p(&req.option, opt);
|
stl_be_p(&req.option, opt);
|
||||||
|
@ -153,8 +154,7 @@ static int nbd_receive_option_reply(QIOChannel *ioc, uint32_t opt,
|
||||||
be32_to_cpus(&reply->type);
|
be32_to_cpus(&reply->type);
|
||||||
be32_to_cpus(&reply->length);
|
be32_to_cpus(&reply->length);
|
||||||
|
|
||||||
TRACE("Received option reply %" PRIx32", type %" PRIx32", len %" PRIu32,
|
trace_nbd_receive_option_reply(reply->option, reply->type, reply->length);
|
||||||
reply->option, reply->type, reply->length);
|
|
||||||
|
|
||||||
if (reply->magic != NBD_REP_MAGIC) {
|
if (reply->magic != NBD_REP_MAGIC) {
|
||||||
error_setg(errp, "Unexpected option reply magic");
|
error_setg(errp, "Unexpected option reply magic");
|
||||||
|
@ -201,8 +201,7 @@ static int nbd_handle_reply_err(QIOChannel *ioc, nbd_opt_reply *reply,
|
||||||
|
|
||||||
switch (reply->type) {
|
switch (reply->type) {
|
||||||
case NBD_REP_ERR_UNSUP:
|
case NBD_REP_ERR_UNSUP:
|
||||||
TRACE("server doesn't understand request %" PRIx32
|
trace_nbd_reply_err_unsup(reply->option);
|
||||||
", attempting fallback", reply->option);
|
|
||||||
result = 0;
|
result = 0;
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
|
@ -342,12 +341,11 @@ static int nbd_receive_query_exports(QIOChannel *ioc,
|
||||||
{
|
{
|
||||||
bool foundExport = false;
|
bool foundExport = false;
|
||||||
|
|
||||||
TRACE("Querying export list for '%s'", wantname);
|
trace_nbd_receive_query_exports_start(wantname);
|
||||||
if (nbd_send_option_request(ioc, NBD_OPT_LIST, 0, NULL, errp) < 0) {
|
if (nbd_send_option_request(ioc, NBD_OPT_LIST, 0, NULL, errp) < 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
TRACE("Reading available export names");
|
|
||||||
while (1) {
|
while (1) {
|
||||||
int ret = nbd_receive_list(ioc, wantname, &foundExport, errp);
|
int ret = nbd_receive_list(ioc, wantname, &foundExport, errp);
|
||||||
|
|
||||||
|
@ -362,7 +360,7 @@ static int nbd_receive_query_exports(QIOChannel *ioc,
|
||||||
nbd_send_opt_abort(ioc);
|
nbd_send_opt_abort(ioc);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
TRACE("Found desired export name '%s'", wantname);
|
trace_nbd_receive_query_exports_success(wantname);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -376,12 +374,12 @@ static QIOChannel *nbd_receive_starttls(QIOChannel *ioc,
|
||||||
QIOChannelTLS *tioc;
|
QIOChannelTLS *tioc;
|
||||||
struct NBDTLSHandshakeData data = { 0 };
|
struct NBDTLSHandshakeData data = { 0 };
|
||||||
|
|
||||||
TRACE("Requesting TLS from server");
|
trace_nbd_receive_starttls_request();
|
||||||
if (nbd_send_option_request(ioc, NBD_OPT_STARTTLS, 0, NULL, errp) < 0) {
|
if (nbd_send_option_request(ioc, NBD_OPT_STARTTLS, 0, NULL, errp) < 0) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
TRACE("Getting TLS reply from server");
|
trace_nbd_receive_starttls_reply();
|
||||||
if (nbd_receive_option_reply(ioc, NBD_OPT_STARTTLS, &reply, errp) < 0) {
|
if (nbd_receive_option_reply(ioc, NBD_OPT_STARTTLS, &reply, errp) < 0) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -400,14 +398,14 @@ static QIOChannel *nbd_receive_starttls(QIOChannel *ioc,
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
TRACE("TLS request approved, setting up TLS");
|
trace_nbd_receive_starttls_new_client();
|
||||||
tioc = qio_channel_tls_new_client(ioc, tlscreds, hostname, errp);
|
tioc = qio_channel_tls_new_client(ioc, tlscreds, hostname, errp);
|
||||||
if (!tioc) {
|
if (!tioc) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-client-tls");
|
qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-client-tls");
|
||||||
data.loop = g_main_loop_new(g_main_context_default(), FALSE);
|
data.loop = g_main_loop_new(g_main_context_default(), FALSE);
|
||||||
TRACE("Starting TLS handshake");
|
trace_nbd_receive_starttls_tls_handshake();
|
||||||
qio_channel_tls_handshake(tioc,
|
qio_channel_tls_handshake(tioc,
|
||||||
nbd_tls_handshake,
|
nbd_tls_handshake,
|
||||||
&data,
|
&data,
|
||||||
|
@ -437,8 +435,7 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint16_t *flags,
|
||||||
int rc;
|
int rc;
|
||||||
bool zeroes = true;
|
bool zeroes = true;
|
||||||
|
|
||||||
TRACE("Receiving negotiation tlscreds=%p hostname=%s.",
|
trace_nbd_receive_negotiate(tlscreds, hostname ? hostname : "<null>");
|
||||||
tlscreds, hostname ? hostname : "<null>");
|
|
||||||
|
|
||||||
rc = -EINVAL;
|
rc = -EINVAL;
|
||||||
|
|
||||||
|
@ -461,15 +458,8 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint16_t *flags,
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
TRACE("Magic is %c%c%c%c%c%c%c%c",
|
magic = ldq_be_p(buf);
|
||||||
qemu_isprint(buf[0]) ? buf[0] : '.',
|
trace_nbd_receive_negotiate_magic(magic);
|
||||||
qemu_isprint(buf[1]) ? buf[1] : '.',
|
|
||||||
qemu_isprint(buf[2]) ? buf[2] : '.',
|
|
||||||
qemu_isprint(buf[3]) ? buf[3] : '.',
|
|
||||||
qemu_isprint(buf[4]) ? buf[4] : '.',
|
|
||||||
qemu_isprint(buf[5]) ? buf[5] : '.',
|
|
||||||
qemu_isprint(buf[6]) ? buf[6] : '.',
|
|
||||||
qemu_isprint(buf[7]) ? buf[7] : '.');
|
|
||||||
|
|
||||||
if (memcmp(buf, "NBDMAGIC", 8) != 0) {
|
if (memcmp(buf, "NBDMAGIC", 8) != 0) {
|
||||||
error_setg(errp, "Invalid magic received");
|
error_setg(errp, "Invalid magic received");
|
||||||
|
@ -481,7 +471,7 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint16_t *flags,
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
magic = be64_to_cpu(magic);
|
magic = be64_to_cpu(magic);
|
||||||
TRACE("Magic is 0x%" PRIx64, magic);
|
trace_nbd_receive_negotiate_magic(magic);
|
||||||
|
|
||||||
if (magic == NBD_OPTS_MAGIC) {
|
if (magic == NBD_OPTS_MAGIC) {
|
||||||
uint32_t clientflags = 0;
|
uint32_t clientflags = 0;
|
||||||
|
@ -493,15 +483,13 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint16_t *flags,
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
globalflags = be16_to_cpu(globalflags);
|
globalflags = be16_to_cpu(globalflags);
|
||||||
TRACE("Global flags are %" PRIx32, globalflags);
|
trace_nbd_receive_negotiate_server_flags(globalflags);
|
||||||
if (globalflags & NBD_FLAG_FIXED_NEWSTYLE) {
|
if (globalflags & NBD_FLAG_FIXED_NEWSTYLE) {
|
||||||
fixedNewStyle = true;
|
fixedNewStyle = true;
|
||||||
TRACE("Server supports fixed new style");
|
|
||||||
clientflags |= NBD_FLAG_C_FIXED_NEWSTYLE;
|
clientflags |= NBD_FLAG_C_FIXED_NEWSTYLE;
|
||||||
}
|
}
|
||||||
if (globalflags & NBD_FLAG_NO_ZEROES) {
|
if (globalflags & NBD_FLAG_NO_ZEROES) {
|
||||||
zeroes = false;
|
zeroes = false;
|
||||||
TRACE("Server supports no zeroes");
|
|
||||||
clientflags |= NBD_FLAG_C_NO_ZEROES;
|
clientflags |= NBD_FLAG_C_NO_ZEROES;
|
||||||
}
|
}
|
||||||
/* client requested flags */
|
/* client requested flags */
|
||||||
|
@ -523,7 +511,7 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint16_t *flags,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!name) {
|
if (!name) {
|
||||||
TRACE("Using default NBD export name \"\"");
|
trace_nbd_receive_negotiate_default_name();
|
||||||
name = "";
|
name = "";
|
||||||
}
|
}
|
||||||
if (fixedNewStyle) {
|
if (fixedNewStyle) {
|
||||||
|
@ -572,7 +560,6 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint16_t *flags,
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
*size = be64_to_cpu(s);
|
*size = be64_to_cpu(s);
|
||||||
TRACE("Size is %" PRIu64, *size);
|
|
||||||
|
|
||||||
if (nbd_read(ioc, &oldflags, sizeof(oldflags), errp) < 0) {
|
if (nbd_read(ioc, &oldflags, sizeof(oldflags), errp) < 0) {
|
||||||
error_prepend(errp, "Failed to read export flags");
|
error_prepend(errp, "Failed to read export flags");
|
||||||
|
@ -589,7 +576,7 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint16_t *flags,
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
TRACE("Size is %" PRIu64 ", export flags %" PRIx16, *size, *flags);
|
trace_nbd_receive_negotiate_size_flags(*size, *flags);
|
||||||
if (zeroes && nbd_drop(ioc, 124, errp) < 0) {
|
if (zeroes && nbd_drop(ioc, 124, errp) < 0) {
|
||||||
error_prepend(errp, "Failed to read reserved block");
|
error_prepend(errp, "Failed to read reserved block");
|
||||||
goto fail;
|
goto fail;
|
||||||
|
@ -611,7 +598,7 @@ int nbd_init(int fd, QIOChannelSocket *sioc, uint16_t flags, off_t size,
|
||||||
return -E2BIG;
|
return -E2BIG;
|
||||||
}
|
}
|
||||||
|
|
||||||
TRACE("Setting NBD socket");
|
trace_nbd_init_set_socket();
|
||||||
|
|
||||||
if (ioctl(fd, NBD_SET_SOCK, (unsigned long) sioc->fd) < 0) {
|
if (ioctl(fd, NBD_SET_SOCK, (unsigned long) sioc->fd) < 0) {
|
||||||
int serrno = errno;
|
int serrno = errno;
|
||||||
|
@ -619,7 +606,7 @@ int nbd_init(int fd, QIOChannelSocket *sioc, uint16_t flags, off_t size,
|
||||||
return -serrno;
|
return -serrno;
|
||||||
}
|
}
|
||||||
|
|
||||||
TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE);
|
trace_nbd_init_set_block_size(BDRV_SECTOR_SIZE);
|
||||||
|
|
||||||
if (ioctl(fd, NBD_SET_BLKSIZE, (unsigned long)BDRV_SECTOR_SIZE) < 0) {
|
if (ioctl(fd, NBD_SET_BLKSIZE, (unsigned long)BDRV_SECTOR_SIZE) < 0) {
|
||||||
int serrno = errno;
|
int serrno = errno;
|
||||||
|
@ -627,10 +614,9 @@ int nbd_init(int fd, QIOChannelSocket *sioc, uint16_t flags, off_t size,
|
||||||
return -serrno;
|
return -serrno;
|
||||||
}
|
}
|
||||||
|
|
||||||
TRACE("Setting size to %lu block(s)", sectors);
|
trace_nbd_init_set_size(sectors);
|
||||||
if (size % BDRV_SECTOR_SIZE) {
|
if (size % BDRV_SECTOR_SIZE) {
|
||||||
TRACE("Ignoring trailing %d bytes of export",
|
trace_nbd_init_trailing_bytes(size % BDRV_SECTOR_SIZE);
|
||||||
(int) (size % BDRV_SECTOR_SIZE));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ioctl(fd, NBD_SET_SIZE_BLOCKS, sectors) < 0) {
|
if (ioctl(fd, NBD_SET_SIZE_BLOCKS, sectors) < 0) {
|
||||||
|
@ -642,7 +628,7 @@ int nbd_init(int fd, QIOChannelSocket *sioc, uint16_t flags, off_t size,
|
||||||
if (ioctl(fd, NBD_SET_FLAGS, (unsigned long) flags) < 0) {
|
if (ioctl(fd, NBD_SET_FLAGS, (unsigned long) flags) < 0) {
|
||||||
if (errno == ENOTTY) {
|
if (errno == ENOTTY) {
|
||||||
int read_only = (flags & NBD_FLAG_READ_ONLY) != 0;
|
int read_only = (flags & NBD_FLAG_READ_ONLY) != 0;
|
||||||
TRACE("Setting readonly attribute");
|
trace_nbd_init_set_readonly();
|
||||||
|
|
||||||
if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
|
if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
|
||||||
int serrno = errno;
|
int serrno = errno;
|
||||||
|
@ -656,7 +642,7 @@ int nbd_init(int fd, QIOChannelSocket *sioc, uint16_t flags, off_t size,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TRACE("Negotiation ended");
|
trace_nbd_init_finish();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -666,7 +652,7 @@ int nbd_client(int fd)
|
||||||
int ret;
|
int ret;
|
||||||
int serrno;
|
int serrno;
|
||||||
|
|
||||||
TRACE("Doing NBD loop");
|
trace_nbd_client_loop();
|
||||||
|
|
||||||
ret = ioctl(fd, NBD_DO_IT);
|
ret = ioctl(fd, NBD_DO_IT);
|
||||||
if (ret < 0 && errno == EPIPE) {
|
if (ret < 0 && errno == EPIPE) {
|
||||||
|
@ -678,12 +664,12 @@ int nbd_client(int fd)
|
||||||
}
|
}
|
||||||
serrno = errno;
|
serrno = errno;
|
||||||
|
|
||||||
TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
|
trace_nbd_client_loop_ret(ret, strerror(serrno));
|
||||||
|
|
||||||
TRACE("Clearing NBD queue");
|
trace_nbd_client_clear_queue();
|
||||||
ioctl(fd, NBD_CLEAR_QUE);
|
ioctl(fd, NBD_CLEAR_QUE);
|
||||||
|
|
||||||
TRACE("Clearing NBD socket");
|
trace_nbd_client_clear_socket();
|
||||||
ioctl(fd, NBD_CLEAR_SOCK);
|
ioctl(fd, NBD_CLEAR_SOCK);
|
||||||
|
|
||||||
errno = serrno;
|
errno = serrno;
|
||||||
|
@ -720,11 +706,8 @@ ssize_t nbd_send_request(QIOChannel *ioc, NBDRequest *request)
|
||||||
{
|
{
|
||||||
uint8_t buf[NBD_REQUEST_SIZE];
|
uint8_t buf[NBD_REQUEST_SIZE];
|
||||||
|
|
||||||
TRACE("Sending request to server: "
|
trace_nbd_send_request(request->from, request->len, request->handle,
|
||||||
"{ .from = %" PRIu64", .len = %" PRIu32 ", .handle = %" PRIu64
|
request->flags, request->type);
|
||||||
", .flags = %" PRIx16 ", .type = %" PRIu16 " }",
|
|
||||||
request->from, request->len, request->handle,
|
|
||||||
request->flags, request->type);
|
|
||||||
|
|
||||||
stl_be_p(buf, NBD_REQUEST_MAGIC);
|
stl_be_p(buf, NBD_REQUEST_MAGIC);
|
||||||
stw_be_p(buf + 4, request->flags);
|
stw_be_p(buf + 4, request->flags);
|
||||||
|
@ -769,9 +752,7 @@ ssize_t nbd_receive_reply(QIOChannel *ioc, NBDReply *reply, Error **errp)
|
||||||
error_setg(errp, "server shutting down");
|
error_setg(errp, "server shutting down");
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
TRACE("Got reply: { magic = 0x%" PRIx32 ", .error = % " PRId32
|
trace_nbd_receive_reply(magic, reply->error, reply->handle);
|
||||||
", handle = %" PRIu64" }",
|
|
||||||
magic, reply->error, reply->handle);
|
|
||||||
|
|
||||||
if (magic != NBD_REPLY_MAGIC) {
|
if (magic != NBD_REPLY_MAGIC) {
|
||||||
error_setg(errp, "invalid magic (got 0x%" PRIx32 ")", magic);
|
error_setg(errp, "invalid magic (got 0x%" PRIx32 ")", magic);
|
||||||
|
|
|
@ -97,9 +97,7 @@ void nbd_tls_handshake(QIOTask *task,
|
||||||
{
|
{
|
||||||
struct NBDTLSHandshakeData *data = opaque;
|
struct NBDTLSHandshakeData *data = opaque;
|
||||||
|
|
||||||
if (qio_task_propagate_error(task, &data->error)) {
|
qio_task_propagate_error(task, &data->error);
|
||||||
TRACE("TLS failed %s", error_get_pretty(data->error));
|
|
||||||
}
|
|
||||||
data->complete = true;
|
data->complete = true;
|
||||||
g_main_loop_quit(data->loop);
|
g_main_loop_quit(data->loop);
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,25 +31,6 @@
|
||||||
#include "qemu/queue.h"
|
#include "qemu/queue.h"
|
||||||
#include "qemu/main-loop.h"
|
#include "qemu/main-loop.h"
|
||||||
|
|
||||||
/* #define DEBUG_NBD */
|
|
||||||
|
|
||||||
#ifdef DEBUG_NBD
|
|
||||||
#define DEBUG_NBD_PRINT 1
|
|
||||||
#else
|
|
||||||
#define DEBUG_NBD_PRINT 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define TRACE(msg, ...) do { \
|
|
||||||
if (DEBUG_NBD_PRINT) { \
|
|
||||||
LOG(msg, ## __VA_ARGS__); \
|
|
||||||
} \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define LOG(msg, ...) do { \
|
|
||||||
fprintf(stderr, "%s:%s():L%d: " msg "\n", \
|
|
||||||
__FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
/* This is all part of the "official" NBD API.
|
/* This is all part of the "official" NBD API.
|
||||||
*
|
*
|
||||||
* The most up-to-date documentation is available at:
|
* The most up-to-date documentation is available at:
|
||||||
|
|
405
nbd/server.c
405
nbd/server.c
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
#include "qemu/osdep.h"
|
#include "qemu/osdep.h"
|
||||||
#include "qapi/error.h"
|
#include "qapi/error.h"
|
||||||
|
#include "trace.h"
|
||||||
#include "nbd-internal.h"
|
#include "nbd-internal.h"
|
||||||
|
|
||||||
static int system_errno_to_nbd_errno(int err)
|
static int system_errno_to_nbd_errno(int err)
|
||||||
|
@ -134,31 +135,33 @@ static void nbd_client_receive_next_request(NBDClient *client);
|
||||||
/* Send a reply header, including length, but no payload.
|
/* Send a reply header, including length, but no payload.
|
||||||
* Return -errno on error, 0 on success. */
|
* Return -errno on error, 0 on success. */
|
||||||
static int nbd_negotiate_send_rep_len(QIOChannel *ioc, uint32_t type,
|
static int nbd_negotiate_send_rep_len(QIOChannel *ioc, uint32_t type,
|
||||||
uint32_t opt, uint32_t len)
|
uint32_t opt, uint32_t len, Error **errp)
|
||||||
{
|
{
|
||||||
uint64_t magic;
|
uint64_t magic;
|
||||||
|
|
||||||
TRACE("Reply opt=%" PRIx32 " type=%" PRIx32 " len=%" PRIu32,
|
trace_nbd_negotiate_send_rep_len(opt, type, len);
|
||||||
type, opt, len);
|
|
||||||
|
|
||||||
magic = cpu_to_be64(NBD_REP_MAGIC);
|
magic = cpu_to_be64(NBD_REP_MAGIC);
|
||||||
if (nbd_write(ioc, &magic, sizeof(magic), NULL) < 0) {
|
if (nbd_write(ioc, &magic, sizeof(magic), errp) < 0) {
|
||||||
LOG("write failed (rep magic)");
|
error_prepend(errp, "write failed (rep magic): ");
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
opt = cpu_to_be32(opt);
|
opt = cpu_to_be32(opt);
|
||||||
if (nbd_write(ioc, &opt, sizeof(opt), NULL) < 0) {
|
if (nbd_write(ioc, &opt, sizeof(opt), errp) < 0) {
|
||||||
LOG("write failed (rep opt)");
|
error_prepend(errp, "write failed (rep opt): ");
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
type = cpu_to_be32(type);
|
type = cpu_to_be32(type);
|
||||||
if (nbd_write(ioc, &type, sizeof(type), NULL) < 0) {
|
if (nbd_write(ioc, &type, sizeof(type), errp) < 0) {
|
||||||
LOG("write failed (rep type)");
|
error_prepend(errp, "write failed (rep type): ");
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
len = cpu_to_be32(len);
|
len = cpu_to_be32(len);
|
||||||
if (nbd_write(ioc, &len, sizeof(len), NULL) < 0) {
|
if (nbd_write(ioc, &len, sizeof(len), errp) < 0) {
|
||||||
LOG("write failed (rep data length)");
|
error_prepend(errp, "write failed (rep data length): ");
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -166,16 +169,17 @@ static int nbd_negotiate_send_rep_len(QIOChannel *ioc, uint32_t type,
|
||||||
|
|
||||||
/* Send a reply header with default 0 length.
|
/* Send a reply header with default 0 length.
|
||||||
* Return -errno on error, 0 on success. */
|
* Return -errno on error, 0 on success. */
|
||||||
static int nbd_negotiate_send_rep(QIOChannel *ioc, uint32_t type, uint32_t opt)
|
static int nbd_negotiate_send_rep(QIOChannel *ioc, uint32_t type, uint32_t opt,
|
||||||
|
Error **errp)
|
||||||
{
|
{
|
||||||
return nbd_negotiate_send_rep_len(ioc, type, opt, 0);
|
return nbd_negotiate_send_rep_len(ioc, type, opt, 0, errp);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Send an error reply.
|
/* Send an error reply.
|
||||||
* Return -errno on error, 0 on success. */
|
* Return -errno on error, 0 on success. */
|
||||||
static int GCC_FMT_ATTR(4, 5)
|
static int GCC_FMT_ATTR(5, 6)
|
||||||
nbd_negotiate_send_rep_err(QIOChannel *ioc, uint32_t type,
|
nbd_negotiate_send_rep_err(QIOChannel *ioc, uint32_t type,
|
||||||
uint32_t opt, const char *fmt, ...)
|
uint32_t opt, Error **errp, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
va_list va;
|
va_list va;
|
||||||
char *msg;
|
char *msg;
|
||||||
|
@ -187,17 +191,18 @@ nbd_negotiate_send_rep_err(QIOChannel *ioc, uint32_t type,
|
||||||
va_end(va);
|
va_end(va);
|
||||||
len = strlen(msg);
|
len = strlen(msg);
|
||||||
assert(len < 4096);
|
assert(len < 4096);
|
||||||
TRACE("sending error message \"%s\"", msg);
|
trace_nbd_negotiate_send_rep_err(msg);
|
||||||
ret = nbd_negotiate_send_rep_len(ioc, type, opt, len);
|
ret = nbd_negotiate_send_rep_len(ioc, type, opt, len, errp);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
if (nbd_write(ioc, msg, len, NULL) < 0) {
|
if (nbd_write(ioc, msg, len, errp) < 0) {
|
||||||
LOG("write failed (error message)");
|
error_prepend(errp, "write failed (error message): ");
|
||||||
ret = -EIO;
|
ret = -EIO;
|
||||||
} else {
|
} else {
|
||||||
ret = 0;
|
ret = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
out:
|
out:
|
||||||
g_free(msg);
|
g_free(msg);
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -205,7 +210,8 @@ out:
|
||||||
|
|
||||||
/* Send a single NBD_REP_SERVER reply to NBD_OPT_LIST, including payload.
|
/* Send a single NBD_REP_SERVER reply to NBD_OPT_LIST, including payload.
|
||||||
* Return -errno on error, 0 on success. */
|
* Return -errno on error, 0 on success. */
|
||||||
static int nbd_negotiate_send_rep_list(QIOChannel *ioc, NBDExport *exp)
|
static int nbd_negotiate_send_rep_list(QIOChannel *ioc, NBDExport *exp,
|
||||||
|
Error **errp)
|
||||||
{
|
{
|
||||||
size_t name_len, desc_len;
|
size_t name_len, desc_len;
|
||||||
uint32_t len;
|
uint32_t len;
|
||||||
|
@ -213,79 +219,86 @@ static int nbd_negotiate_send_rep_list(QIOChannel *ioc, NBDExport *exp)
|
||||||
const char *desc = exp->description ? exp->description : "";
|
const char *desc = exp->description ? exp->description : "";
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
TRACE("Advertising export name '%s' description '%s'", name, desc);
|
trace_nbd_negotiate_send_rep_list(name, desc);
|
||||||
name_len = strlen(name);
|
name_len = strlen(name);
|
||||||
desc_len = strlen(desc);
|
desc_len = strlen(desc);
|
||||||
len = name_len + desc_len + sizeof(len);
|
len = name_len + desc_len + sizeof(len);
|
||||||
ret = nbd_negotiate_send_rep_len(ioc, NBD_REP_SERVER, NBD_OPT_LIST, len);
|
ret = nbd_negotiate_send_rep_len(ioc, NBD_REP_SERVER, NBD_OPT_LIST, len,
|
||||||
|
errp);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
len = cpu_to_be32(name_len);
|
len = cpu_to_be32(name_len);
|
||||||
if (nbd_write(ioc, &len, sizeof(len), NULL) < 0) {
|
if (nbd_write(ioc, &len, sizeof(len), errp) < 0) {
|
||||||
LOG("write failed (name length)");
|
error_prepend(errp, "write failed (name length): ");
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
if (nbd_write(ioc, name, name_len, NULL) < 0) {
|
|
||||||
LOG("write failed (name buffer)");
|
if (nbd_write(ioc, name, name_len, errp) < 0) {
|
||||||
|
error_prepend(errp, "write failed (name buffer): ");
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
if (nbd_write(ioc, desc, desc_len, NULL) < 0) {
|
|
||||||
LOG("write failed (description buffer)");
|
if (nbd_write(ioc, desc, desc_len, errp) < 0) {
|
||||||
|
error_prepend(errp, "write failed (description buffer): ");
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Process the NBD_OPT_LIST command, with a potential series of replies.
|
/* Process the NBD_OPT_LIST command, with a potential series of replies.
|
||||||
* Return -errno on error, 0 on success. */
|
* Return -errno on error, 0 on success. */
|
||||||
static int nbd_negotiate_handle_list(NBDClient *client, uint32_t length)
|
static int nbd_negotiate_handle_list(NBDClient *client, uint32_t length,
|
||||||
|
Error **errp)
|
||||||
{
|
{
|
||||||
NBDExport *exp;
|
NBDExport *exp;
|
||||||
|
|
||||||
if (length) {
|
if (length) {
|
||||||
if (nbd_drop(client->ioc, length, NULL) < 0) {
|
if (nbd_drop(client->ioc, length, errp) < 0) {
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
return nbd_negotiate_send_rep_err(client->ioc,
|
return nbd_negotiate_send_rep_err(client->ioc,
|
||||||
NBD_REP_ERR_INVALID, NBD_OPT_LIST,
|
NBD_REP_ERR_INVALID, NBD_OPT_LIST,
|
||||||
|
errp,
|
||||||
"OPT_LIST should not have length");
|
"OPT_LIST should not have length");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* For each export, send a NBD_REP_SERVER reply. */
|
/* For each export, send a NBD_REP_SERVER reply. */
|
||||||
QTAILQ_FOREACH(exp, &exports, next) {
|
QTAILQ_FOREACH(exp, &exports, next) {
|
||||||
if (nbd_negotiate_send_rep_list(client->ioc, exp)) {
|
if (nbd_negotiate_send_rep_list(client->ioc, exp, errp)) {
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* Finish with a NBD_REP_ACK. */
|
/* Finish with a NBD_REP_ACK. */
|
||||||
return nbd_negotiate_send_rep(client->ioc, NBD_REP_ACK, NBD_OPT_LIST);
|
return nbd_negotiate_send_rep(client->ioc, NBD_REP_ACK, NBD_OPT_LIST, errp);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int nbd_negotiate_handle_export_name(NBDClient *client, uint32_t length)
|
static int nbd_negotiate_handle_export_name(NBDClient *client, uint32_t length,
|
||||||
|
Error **errp)
|
||||||
{
|
{
|
||||||
char name[NBD_MAX_NAME_SIZE + 1];
|
char name[NBD_MAX_NAME_SIZE + 1];
|
||||||
|
|
||||||
/* Client sends:
|
/* Client sends:
|
||||||
[20 .. xx] export name (length bytes)
|
[20 .. xx] export name (length bytes)
|
||||||
*/
|
*/
|
||||||
TRACE("Checking length");
|
trace_nbd_negotiate_handle_export_name();
|
||||||
if (length >= sizeof(name)) {
|
if (length >= sizeof(name)) {
|
||||||
LOG("Bad length received");
|
error_setg(errp, "Bad length received");
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
if (nbd_read(client->ioc, name, length, NULL) < 0) {
|
if (nbd_read(client->ioc, name, length, errp) < 0) {
|
||||||
LOG("read failed");
|
error_prepend(errp, "read failed: ");
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
name[length] = '\0';
|
name[length] = '\0';
|
||||||
|
|
||||||
TRACE("Client requested export '%s'", name);
|
trace_nbd_negotiate_handle_export_name_request(name);
|
||||||
|
|
||||||
client->exp = nbd_export_find(name);
|
client->exp = nbd_export_find(name);
|
||||||
if (!client->exp) {
|
if (!client->exp) {
|
||||||
LOG("export not found");
|
error_setg(errp, "export not found");
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -298,38 +311,40 @@ static int nbd_negotiate_handle_export_name(NBDClient *client, uint32_t length)
|
||||||
/* Handle NBD_OPT_STARTTLS. Return NULL to drop connection, or else the
|
/* Handle NBD_OPT_STARTTLS. Return NULL to drop connection, or else the
|
||||||
* new channel for all further (now-encrypted) communication. */
|
* new channel for all further (now-encrypted) communication. */
|
||||||
static QIOChannel *nbd_negotiate_handle_starttls(NBDClient *client,
|
static QIOChannel *nbd_negotiate_handle_starttls(NBDClient *client,
|
||||||
uint32_t length)
|
uint32_t length,
|
||||||
|
Error **errp)
|
||||||
{
|
{
|
||||||
QIOChannel *ioc;
|
QIOChannel *ioc;
|
||||||
QIOChannelTLS *tioc;
|
QIOChannelTLS *tioc;
|
||||||
struct NBDTLSHandshakeData data = { 0 };
|
struct NBDTLSHandshakeData data = { 0 };
|
||||||
|
|
||||||
TRACE("Setting up TLS");
|
trace_nbd_negotiate_handle_starttls();
|
||||||
ioc = client->ioc;
|
ioc = client->ioc;
|
||||||
if (length) {
|
if (length) {
|
||||||
if (nbd_drop(ioc, length, NULL) < 0) {
|
if (nbd_drop(ioc, length, errp) < 0) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
nbd_negotiate_send_rep_err(ioc, NBD_REP_ERR_INVALID, NBD_OPT_STARTTLS,
|
nbd_negotiate_send_rep_err(ioc, NBD_REP_ERR_INVALID, NBD_OPT_STARTTLS,
|
||||||
|
errp,
|
||||||
"OPT_STARTTLS should not have length");
|
"OPT_STARTTLS should not have length");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nbd_negotiate_send_rep(client->ioc, NBD_REP_ACK,
|
if (nbd_negotiate_send_rep(client->ioc, NBD_REP_ACK,
|
||||||
NBD_OPT_STARTTLS) < 0) {
|
NBD_OPT_STARTTLS, errp) < 0) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
tioc = qio_channel_tls_new_server(ioc,
|
tioc = qio_channel_tls_new_server(ioc,
|
||||||
client->tlscreds,
|
client->tlscreds,
|
||||||
client->tlsaclname,
|
client->tlsaclname,
|
||||||
NULL);
|
errp);
|
||||||
if (!tioc) {
|
if (!tioc) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-server-tls");
|
qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-server-tls");
|
||||||
TRACE("Starting TLS handshake");
|
trace_nbd_negotiate_handle_starttls_handshake();
|
||||||
data.loop = g_main_loop_new(g_main_context_default(), FALSE);
|
data.loop = g_main_loop_new(g_main_context_default(), FALSE);
|
||||||
qio_channel_tls_handshake(tioc,
|
qio_channel_tls_handshake(tioc,
|
||||||
nbd_tls_handshake,
|
nbd_tls_handshake,
|
||||||
|
@ -342,20 +357,26 @@ static QIOChannel *nbd_negotiate_handle_starttls(NBDClient *client,
|
||||||
g_main_loop_unref(data.loop);
|
g_main_loop_unref(data.loop);
|
||||||
if (data.error) {
|
if (data.error) {
|
||||||
object_unref(OBJECT(tioc));
|
object_unref(OBJECT(tioc));
|
||||||
error_free(data.error);
|
error_propagate(errp, data.error);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return QIO_CHANNEL(tioc);
|
return QIO_CHANNEL(tioc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* nbd_negotiate_options
|
||||||
/* Process all NBD_OPT_* client option commands.
|
* Process all NBD_OPT_* client option commands.
|
||||||
* Return -errno on error, 0 on success. */
|
* Return:
|
||||||
static int nbd_negotiate_options(NBDClient *client)
|
* -errno on error, errp is set
|
||||||
|
* 0 on successful negotiation, errp is not set
|
||||||
|
* 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
|
||||||
|
* errp is not set
|
||||||
|
*/
|
||||||
|
static int nbd_negotiate_options(NBDClient *client, Error **errp)
|
||||||
{
|
{
|
||||||
uint32_t flags;
|
uint32_t flags;
|
||||||
bool fixedNewstyle = false;
|
bool fixedNewstyle = false;
|
||||||
|
Error *local_err = NULL;
|
||||||
|
|
||||||
/* Client sends:
|
/* Client sends:
|
||||||
[ 0 .. 3] client flags
|
[ 0 .. 3] client flags
|
||||||
|
@ -371,67 +392,67 @@ static int nbd_negotiate_options(NBDClient *client)
|
||||||
... Rest of request
|
... Rest of request
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if (nbd_read(client->ioc, &flags, sizeof(flags), NULL) < 0) {
|
if (nbd_read(client->ioc, &flags, sizeof(flags), errp) < 0) {
|
||||||
LOG("read failed");
|
error_prepend(errp, "read failed: ");
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
TRACE("Checking client flags");
|
trace_nbd_negotiate_options_flags();
|
||||||
be32_to_cpus(&flags);
|
be32_to_cpus(&flags);
|
||||||
if (flags & NBD_FLAG_C_FIXED_NEWSTYLE) {
|
if (flags & NBD_FLAG_C_FIXED_NEWSTYLE) {
|
||||||
TRACE("Client supports fixed newstyle handshake");
|
trace_nbd_negotiate_options_newstyle();
|
||||||
fixedNewstyle = true;
|
fixedNewstyle = true;
|
||||||
flags &= ~NBD_FLAG_C_FIXED_NEWSTYLE;
|
flags &= ~NBD_FLAG_C_FIXED_NEWSTYLE;
|
||||||
}
|
}
|
||||||
if (flags & NBD_FLAG_C_NO_ZEROES) {
|
if (flags & NBD_FLAG_C_NO_ZEROES) {
|
||||||
TRACE("Client supports no zeroes at handshake end");
|
trace_nbd_negotiate_options_no_zeroes();
|
||||||
client->no_zeroes = true;
|
client->no_zeroes = true;
|
||||||
flags &= ~NBD_FLAG_C_NO_ZEROES;
|
flags &= ~NBD_FLAG_C_NO_ZEROES;
|
||||||
}
|
}
|
||||||
if (flags != 0) {
|
if (flags != 0) {
|
||||||
TRACE("Unknown client flags 0x%" PRIx32 " received", flags);
|
error_setg(errp, "Unknown client flags 0x%" PRIx32 " received", flags);
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
int ret;
|
int ret;
|
||||||
uint32_t clientflags, length;
|
uint32_t option, length;
|
||||||
uint64_t magic;
|
uint64_t magic;
|
||||||
|
|
||||||
if (nbd_read(client->ioc, &magic, sizeof(magic), NULL) < 0) {
|
if (nbd_read(client->ioc, &magic, sizeof(magic), errp) < 0) {
|
||||||
LOG("read failed");
|
error_prepend(errp, "read failed: ");
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
TRACE("Checking opts magic");
|
magic = be64_to_cpu(magic);
|
||||||
if (magic != be64_to_cpu(NBD_OPTS_MAGIC)) {
|
trace_nbd_negotiate_options_check_magic(magic);
|
||||||
LOG("Bad magic received");
|
if (magic != NBD_OPTS_MAGIC) {
|
||||||
|
error_setg(errp, "Bad magic received");
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nbd_read(client->ioc, &clientflags,
|
if (nbd_read(client->ioc, &option,
|
||||||
sizeof(clientflags), NULL) < 0)
|
sizeof(option), errp) < 0) {
|
||||||
{
|
error_prepend(errp, "read failed: ");
|
||||||
LOG("read failed");
|
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
clientflags = be32_to_cpu(clientflags);
|
option = be32_to_cpu(option);
|
||||||
|
|
||||||
if (nbd_read(client->ioc, &length, sizeof(length), NULL) < 0) {
|
if (nbd_read(client->ioc, &length, sizeof(length), errp) < 0) {
|
||||||
LOG("read failed");
|
error_prepend(errp, "read failed: ");
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
length = be32_to_cpu(length);
|
length = be32_to_cpu(length);
|
||||||
|
|
||||||
TRACE("Checking option 0x%" PRIx32, clientflags);
|
trace_nbd_negotiate_options_check_option(option);
|
||||||
if (client->tlscreds &&
|
if (client->tlscreds &&
|
||||||
client->ioc == (QIOChannel *)client->sioc) {
|
client->ioc == (QIOChannel *)client->sioc) {
|
||||||
QIOChannel *tioc;
|
QIOChannel *tioc;
|
||||||
if (!fixedNewstyle) {
|
if (!fixedNewstyle) {
|
||||||
TRACE("Unsupported option 0x%" PRIx32, clientflags);
|
error_setg(errp, "Unsupported option 0x%" PRIx32, option);
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
switch (clientflags) {
|
switch (option) {
|
||||||
case NBD_OPT_STARTTLS:
|
case NBD_OPT_STARTTLS:
|
||||||
tioc = nbd_negotiate_handle_starttls(client, length);
|
tioc = nbd_negotiate_handle_starttls(client, length, errp);
|
||||||
if (!tioc) {
|
if (!tioc) {
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
@ -441,32 +462,33 @@ static int nbd_negotiate_options(NBDClient *client)
|
||||||
|
|
||||||
case NBD_OPT_EXPORT_NAME:
|
case NBD_OPT_EXPORT_NAME:
|
||||||
/* No way to return an error to client, so drop connection */
|
/* No way to return an error to client, so drop connection */
|
||||||
TRACE("Option 0x%x not permitted before TLS", clientflags);
|
error_setg(errp, "Option 0x%x not permitted before TLS",
|
||||||
|
option);
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if (nbd_drop(client->ioc, length, NULL) < 0) {
|
if (nbd_drop(client->ioc, length, errp) < 0) {
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
ret = nbd_negotiate_send_rep_err(client->ioc,
|
ret = nbd_negotiate_send_rep_err(client->ioc,
|
||||||
NBD_REP_ERR_TLS_REQD,
|
NBD_REP_ERR_TLS_REQD,
|
||||||
clientflags,
|
option, errp,
|
||||||
"Option 0x%" PRIx32
|
"Option 0x%" PRIx32
|
||||||
"not permitted before TLS",
|
"not permitted before TLS",
|
||||||
clientflags);
|
option);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
/* Let the client keep trying, unless they asked to quit */
|
/* Let the client keep trying, unless they asked to quit */
|
||||||
if (clientflags == NBD_OPT_ABORT) {
|
if (option == NBD_OPT_ABORT) {
|
||||||
return -EINVAL;
|
return 1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else if (fixedNewstyle) {
|
} else if (fixedNewstyle) {
|
||||||
switch (clientflags) {
|
switch (option) {
|
||||||
case NBD_OPT_LIST:
|
case NBD_OPT_LIST:
|
||||||
ret = nbd_negotiate_handle_list(client, length);
|
ret = nbd_negotiate_handle_list(client, length, errp);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -476,25 +498,33 @@ static int nbd_negotiate_options(NBDClient *client)
|
||||||
/* NBD spec says we must try to reply before
|
/* NBD spec says we must try to reply before
|
||||||
* disconnecting, but that we must also tolerate
|
* disconnecting, but that we must also tolerate
|
||||||
* guests that don't wait for our reply. */
|
* guests that don't wait for our reply. */
|
||||||
nbd_negotiate_send_rep(client->ioc, NBD_REP_ACK, clientflags);
|
nbd_negotiate_send_rep(client->ioc, NBD_REP_ACK, option,
|
||||||
return -EINVAL;
|
&local_err);
|
||||||
|
|
||||||
|
if (local_err != NULL) {
|
||||||
|
const char *error = error_get_pretty(local_err);
|
||||||
|
trace_nbd_opt_abort_reply_failed(error);
|
||||||
|
error_free(local_err);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
|
||||||
case NBD_OPT_EXPORT_NAME:
|
case NBD_OPT_EXPORT_NAME:
|
||||||
return nbd_negotiate_handle_export_name(client, length);
|
return nbd_negotiate_handle_export_name(client, length, errp);
|
||||||
|
|
||||||
case NBD_OPT_STARTTLS:
|
case NBD_OPT_STARTTLS:
|
||||||
if (nbd_drop(client->ioc, length, NULL) < 0) {
|
if (nbd_drop(client->ioc, length, errp) < 0) {
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
if (client->tlscreds) {
|
if (client->tlscreds) {
|
||||||
ret = nbd_negotiate_send_rep_err(client->ioc,
|
ret = nbd_negotiate_send_rep_err(client->ioc,
|
||||||
NBD_REP_ERR_INVALID,
|
NBD_REP_ERR_INVALID,
|
||||||
clientflags,
|
option, errp,
|
||||||
"TLS already enabled");
|
"TLS already enabled");
|
||||||
} else {
|
} else {
|
||||||
ret = nbd_negotiate_send_rep_err(client->ioc,
|
ret = nbd_negotiate_send_rep_err(client->ioc,
|
||||||
NBD_REP_ERR_POLICY,
|
NBD_REP_ERR_POLICY,
|
||||||
clientflags,
|
option, errp,
|
||||||
"TLS not configured");
|
"TLS not configured");
|
||||||
}
|
}
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
|
@ -502,15 +532,15 @@ static int nbd_negotiate_options(NBDClient *client)
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (nbd_drop(client->ioc, length, NULL) < 0) {
|
if (nbd_drop(client->ioc, length, errp) < 0) {
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
ret = nbd_negotiate_send_rep_err(client->ioc,
|
ret = nbd_negotiate_send_rep_err(client->ioc,
|
||||||
NBD_REP_ERR_UNSUP,
|
NBD_REP_ERR_UNSUP,
|
||||||
clientflags,
|
option, errp,
|
||||||
"Unsupported option 0x%"
|
"Unsupported option 0x%"
|
||||||
PRIx32,
|
PRIx32,
|
||||||
clientflags);
|
option);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -521,19 +551,26 @@ static int nbd_negotiate_options(NBDClient *client)
|
||||||
* If broken new-style we should drop the connection
|
* If broken new-style we should drop the connection
|
||||||
* for anything except NBD_OPT_EXPORT_NAME
|
* for anything except NBD_OPT_EXPORT_NAME
|
||||||
*/
|
*/
|
||||||
switch (clientflags) {
|
switch (option) {
|
||||||
case NBD_OPT_EXPORT_NAME:
|
case NBD_OPT_EXPORT_NAME:
|
||||||
return nbd_negotiate_handle_export_name(client, length);
|
return nbd_negotiate_handle_export_name(client, length, errp);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
TRACE("Unsupported option 0x%" PRIx32, clientflags);
|
error_setg(errp, "Unsupported option 0x%" PRIx32, option);
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static coroutine_fn int nbd_negotiate(NBDClient *client)
|
/* nbd_negotiate
|
||||||
|
* Return:
|
||||||
|
* -errno on error, errp is set
|
||||||
|
* 0 on successful negotiation, errp is not set
|
||||||
|
* 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
|
||||||
|
* errp is not set
|
||||||
|
*/
|
||||||
|
static coroutine_fn int nbd_negotiate(NBDClient *client, Error **errp)
|
||||||
{
|
{
|
||||||
char buf[8 + 8 + 8 + 128];
|
char buf[8 + 8 + 8 + 128];
|
||||||
int ret;
|
int ret;
|
||||||
|
@ -563,66 +600,63 @@ static coroutine_fn int nbd_negotiate(NBDClient *client)
|
||||||
|
|
||||||
qio_channel_set_blocking(client->ioc, false, NULL);
|
qio_channel_set_blocking(client->ioc, false, NULL);
|
||||||
|
|
||||||
TRACE("Beginning negotiation.");
|
trace_nbd_negotiate_begin();
|
||||||
memset(buf, 0, sizeof(buf));
|
memset(buf, 0, sizeof(buf));
|
||||||
memcpy(buf, "NBDMAGIC", 8);
|
memcpy(buf, "NBDMAGIC", 8);
|
||||||
|
|
||||||
oldStyle = client->exp != NULL && !client->tlscreds;
|
oldStyle = client->exp != NULL && !client->tlscreds;
|
||||||
if (oldStyle) {
|
if (oldStyle) {
|
||||||
TRACE("advertising size %" PRIu64 " and flags %x",
|
trace_nbd_negotiate_old_style(client->exp->size,
|
||||||
client->exp->size, client->exp->nbdflags | myflags);
|
client->exp->nbdflags | myflags);
|
||||||
stq_be_p(buf + 8, NBD_CLIENT_MAGIC);
|
stq_be_p(buf + 8, NBD_CLIENT_MAGIC);
|
||||||
stq_be_p(buf + 16, client->exp->size);
|
stq_be_p(buf + 16, client->exp->size);
|
||||||
stw_be_p(buf + 26, client->exp->nbdflags | myflags);
|
stw_be_p(buf + 26, client->exp->nbdflags | myflags);
|
||||||
|
|
||||||
|
if (nbd_write(client->ioc, buf, sizeof(buf), errp) < 0) {
|
||||||
|
error_prepend(errp, "write failed: ");
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
stq_be_p(buf + 8, NBD_OPTS_MAGIC);
|
stq_be_p(buf + 8, NBD_OPTS_MAGIC);
|
||||||
stw_be_p(buf + 16, NBD_FLAG_FIXED_NEWSTYLE | NBD_FLAG_NO_ZEROES);
|
stw_be_p(buf + 16, NBD_FLAG_FIXED_NEWSTYLE | NBD_FLAG_NO_ZEROES);
|
||||||
}
|
|
||||||
|
|
||||||
if (oldStyle) {
|
if (nbd_write(client->ioc, buf, 18, errp) < 0) {
|
||||||
if (client->tlscreds) {
|
error_prepend(errp, "write failed: ");
|
||||||
TRACE("TLS cannot be enabled with oldstyle protocol");
|
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
if (nbd_write(client->ioc, buf, sizeof(buf), NULL) < 0) {
|
ret = nbd_negotiate_options(client, errp);
|
||||||
LOG("write failed");
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (nbd_write(client->ioc, buf, 18, NULL) < 0) {
|
|
||||||
LOG("write failed");
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
ret = nbd_negotiate_options(client);
|
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
LOG("option negotiation failed");
|
if (ret < 0) {
|
||||||
|
error_prepend(errp, "option negotiation failed: ");
|
||||||
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
TRACE("advertising size %" PRIu64 " and flags %x",
|
trace_nbd_negotiate_new_style_size_flags(
|
||||||
client->exp->size, client->exp->nbdflags | myflags);
|
client->exp->size, client->exp->nbdflags | myflags);
|
||||||
stq_be_p(buf + 18, client->exp->size);
|
stq_be_p(buf + 18, client->exp->size);
|
||||||
stw_be_p(buf + 26, client->exp->nbdflags | myflags);
|
stw_be_p(buf + 26, client->exp->nbdflags | myflags);
|
||||||
len = client->no_zeroes ? 10 : sizeof(buf) - 18;
|
len = client->no_zeroes ? 10 : sizeof(buf) - 18;
|
||||||
ret = nbd_write(client->ioc, buf + 18, len, NULL);
|
ret = nbd_write(client->ioc, buf + 18, len, errp);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LOG("write failed");
|
error_prepend(errp, "write failed: ");
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TRACE("Negotiation succeeded.");
|
trace_nbd_negotiate_success();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int nbd_receive_request(QIOChannel *ioc, NBDRequest *request)
|
static int nbd_receive_request(QIOChannel *ioc, NBDRequest *request,
|
||||||
|
Error **errp)
|
||||||
{
|
{
|
||||||
uint8_t buf[NBD_REQUEST_SIZE];
|
uint8_t buf[NBD_REQUEST_SIZE];
|
||||||
uint32_t magic;
|
uint32_t magic;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
ret = nbd_read(ioc, buf, sizeof(buf), NULL);
|
ret = nbd_read(ioc, buf, sizeof(buf), errp);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -643,26 +677,23 @@ static int nbd_receive_request(QIOChannel *ioc, NBDRequest *request)
|
||||||
request->from = ldq_be_p(buf + 16);
|
request->from = ldq_be_p(buf + 16);
|
||||||
request->len = ldl_be_p(buf + 24);
|
request->len = ldl_be_p(buf + 24);
|
||||||
|
|
||||||
TRACE("Got request: { magic = 0x%" PRIx32 ", .flags = %" PRIx16
|
trace_nbd_receive_request(magic, request->flags, request->type,
|
||||||
", .type = %" PRIx16 ", from = %" PRIu64 ", len = %" PRIu32 " }",
|
request->from, request->len);
|
||||||
magic, request->flags, request->type, request->from, request->len);
|
|
||||||
|
|
||||||
if (magic != NBD_REQUEST_MAGIC) {
|
if (magic != NBD_REQUEST_MAGIC) {
|
||||||
LOG("invalid magic (got 0x%" PRIx32 ")", magic);
|
error_setg(errp, "invalid magic (got 0x%" PRIx32 ")", magic);
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int nbd_send_reply(QIOChannel *ioc, NBDReply *reply)
|
static int nbd_send_reply(QIOChannel *ioc, NBDReply *reply, Error **errp)
|
||||||
{
|
{
|
||||||
uint8_t buf[NBD_REPLY_SIZE];
|
uint8_t buf[NBD_REPLY_SIZE];
|
||||||
|
|
||||||
reply->error = system_errno_to_nbd_errno(reply->error);
|
reply->error = system_errno_to_nbd_errno(reply->error);
|
||||||
|
|
||||||
TRACE("Sending response to client: { .error = %" PRId32
|
trace_nbd_send_reply(reply->error, reply->handle);
|
||||||
", handle = %" PRIu64 " }",
|
|
||||||
reply->error, reply->handle);
|
|
||||||
|
|
||||||
/* Reply
|
/* Reply
|
||||||
[ 0 .. 3] magic (NBD_REPLY_MAGIC)
|
[ 0 .. 3] magic (NBD_REPLY_MAGIC)
|
||||||
|
@ -673,7 +704,7 @@ static int nbd_send_reply(QIOChannel *ioc, NBDReply *reply)
|
||||||
stl_be_p(buf + 4, reply->error);
|
stl_be_p(buf + 4, reply->error);
|
||||||
stq_be_p(buf + 8, reply->handle);
|
stq_be_p(buf + 8, reply->handle);
|
||||||
|
|
||||||
return nbd_write(ioc, buf, sizeof(buf), NULL);
|
return nbd_write(ioc, buf, sizeof(buf), errp);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define MAX_NBD_REQUESTS 16
|
#define MAX_NBD_REQUESTS 16
|
||||||
|
@ -759,7 +790,7 @@ static void blk_aio_attached(AioContext *ctx, void *opaque)
|
||||||
NBDExport *exp = opaque;
|
NBDExport *exp = opaque;
|
||||||
NBDClient *client;
|
NBDClient *client;
|
||||||
|
|
||||||
TRACE("Export %s: Attaching clients to AIO context %p\n", exp->name, ctx);
|
trace_nbd_blk_aio_attached(exp->name, ctx);
|
||||||
|
|
||||||
exp->ctx = ctx;
|
exp->ctx = ctx;
|
||||||
|
|
||||||
|
@ -779,7 +810,7 @@ static void blk_aio_detach(void *opaque)
|
||||||
NBDExport *exp = opaque;
|
NBDExport *exp = opaque;
|
||||||
NBDClient *client;
|
NBDClient *client;
|
||||||
|
|
||||||
TRACE("Export %s: Detaching clients from AIO context %p\n", exp->name, exp->ctx);
|
trace_nbd_blk_aio_detach(exp->name, exp->ctx);
|
||||||
|
|
||||||
QTAILQ_FOREACH(client, &exp->clients, next) {
|
QTAILQ_FOREACH(client, &exp->clients, next) {
|
||||||
qio_channel_detach_aio_context(client->ioc);
|
qio_channel_detach_aio_context(client->ioc);
|
||||||
|
@ -960,22 +991,26 @@ void nbd_export_close_all(void)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int nbd_co_send_reply(NBDRequestData *req, NBDReply *reply, int len)
|
static int nbd_co_send_reply(NBDRequestData *req, NBDReply *reply, int len,
|
||||||
|
Error **errp)
|
||||||
{
|
{
|
||||||
NBDClient *client = req->client;
|
NBDClient *client = req->client;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
g_assert(qemu_in_coroutine());
|
g_assert(qemu_in_coroutine());
|
||||||
|
|
||||||
|
trace_nbd_co_send_reply(reply->handle, reply->error, len);
|
||||||
|
|
||||||
qemu_co_mutex_lock(&client->send_lock);
|
qemu_co_mutex_lock(&client->send_lock);
|
||||||
client->send_coroutine = qemu_coroutine_self();
|
client->send_coroutine = qemu_coroutine_self();
|
||||||
|
|
||||||
if (!len) {
|
if (!len) {
|
||||||
ret = nbd_send_reply(client->ioc, reply);
|
ret = nbd_send_reply(client->ioc, reply, errp);
|
||||||
} else {
|
} else {
|
||||||
qio_channel_set_cork(client->ioc, true);
|
qio_channel_set_cork(client->ioc, true);
|
||||||
ret = nbd_send_reply(client->ioc, reply);
|
ret = nbd_send_reply(client->ioc, reply, errp);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
ret = nbd_write(client->ioc, req->data, len, NULL);
|
ret = nbd_write(client->ioc, req->data, len, errp);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
ret = -EIO;
|
ret = -EIO;
|
||||||
}
|
}
|
||||||
|
@ -994,17 +1029,18 @@ static int nbd_co_send_reply(NBDRequestData *req, NBDReply *reply, int len)
|
||||||
* the client (although the caller may still need to disconnect after reporting
|
* the client (although the caller may still need to disconnect after reporting
|
||||||
* the error).
|
* the error).
|
||||||
*/
|
*/
|
||||||
static int nbd_co_receive_request(NBDRequestData *req, NBDRequest *request)
|
static int nbd_co_receive_request(NBDRequestData *req, NBDRequest *request,
|
||||||
|
Error **errp)
|
||||||
{
|
{
|
||||||
NBDClient *client = req->client;
|
NBDClient *client = req->client;
|
||||||
|
|
||||||
g_assert(qemu_in_coroutine());
|
g_assert(qemu_in_coroutine());
|
||||||
assert(client->recv_coroutine == qemu_coroutine_self());
|
assert(client->recv_coroutine == qemu_coroutine_self());
|
||||||
if (nbd_receive_request(client->ioc, request) < 0) {
|
if (nbd_receive_request(client->ioc, request, errp) < 0) {
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
TRACE("Decoding type");
|
trace_nbd_co_receive_request_decode_type(request->handle, request->type);
|
||||||
|
|
||||||
if (request->type != NBD_CMD_WRITE) {
|
if (request->type != NBD_CMD_WRITE) {
|
||||||
/* No payload, we are ready to read the next request. */
|
/* No payload, we are ready to read the next request. */
|
||||||
|
@ -1014,7 +1050,6 @@ static int nbd_co_receive_request(NBDRequestData *req, NBDRequest *request)
|
||||||
if (request->type == NBD_CMD_DISC) {
|
if (request->type == NBD_CMD_DISC) {
|
||||||
/* Special case: we're going to disconnect without a reply,
|
/* Special case: we're going to disconnect without a reply,
|
||||||
* whether or not flags, from, or len are bogus */
|
* whether or not flags, from, or len are bogus */
|
||||||
TRACE("Request type is DISCONNECT");
|
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1022,46 +1057,49 @@ static int nbd_co_receive_request(NBDRequestData *req, NBDRequest *request)
|
||||||
* checks as possible until after reading any NBD_CMD_WRITE
|
* checks as possible until after reading any NBD_CMD_WRITE
|
||||||
* payload, so we can try and keep the connection alive. */
|
* payload, so we can try and keep the connection alive. */
|
||||||
if ((request->from + request->len) < request->from) {
|
if ((request->from + request->len) < request->from) {
|
||||||
LOG("integer overflow detected, you're probably being attacked");
|
error_setg(errp,
|
||||||
|
"integer overflow detected, you're probably being attacked");
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request->type == NBD_CMD_READ || request->type == NBD_CMD_WRITE) {
|
if (request->type == NBD_CMD_READ || request->type == NBD_CMD_WRITE) {
|
||||||
if (request->len > NBD_MAX_BUFFER_SIZE) {
|
if (request->len > NBD_MAX_BUFFER_SIZE) {
|
||||||
LOG("len (%" PRIu32" ) is larger than max len (%u)",
|
error_setg(errp, "len (%" PRIu32" ) is larger than max len (%u)",
|
||||||
request->len, NBD_MAX_BUFFER_SIZE);
|
request->len, NBD_MAX_BUFFER_SIZE);
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
req->data = blk_try_blockalign(client->exp->blk, request->len);
|
req->data = blk_try_blockalign(client->exp->blk, request->len);
|
||||||
if (req->data == NULL) {
|
if (req->data == NULL) {
|
||||||
|
error_setg(errp, "No memory");
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (request->type == NBD_CMD_WRITE) {
|
if (request->type == NBD_CMD_WRITE) {
|
||||||
TRACE("Reading %" PRIu32 " byte(s)", request->len);
|
if (nbd_read(client->ioc, req->data, request->len, errp) < 0) {
|
||||||
|
error_prepend(errp, "reading from socket failed: ");
|
||||||
if (nbd_read(client->ioc, req->data, request->len, NULL) < 0) {
|
|
||||||
LOG("reading from socket failed");
|
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
req->complete = true;
|
req->complete = true;
|
||||||
|
|
||||||
|
trace_nbd_co_receive_request_payload_received(request->handle,
|
||||||
|
request->len);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Sanity checks, part 2. */
|
/* Sanity checks, part 2. */
|
||||||
if (request->from + request->len > client->exp->size) {
|
if (request->from + request->len > client->exp->size) {
|
||||||
LOG("operation past EOF; From: %" PRIu64 ", Len: %" PRIu32
|
error_setg(errp, "operation past EOF; From: %" PRIu64 ", Len: %" PRIu32
|
||||||
", Size: %" PRIu64, request->from, request->len,
|
", Size: %" PRIu64, request->from, request->len,
|
||||||
(uint64_t)client->exp->size);
|
(uint64_t)client->exp->size);
|
||||||
return request->type == NBD_CMD_WRITE ? -ENOSPC : -EINVAL;
|
return request->type == NBD_CMD_WRITE ? -ENOSPC : -EINVAL;
|
||||||
}
|
}
|
||||||
if (request->flags & ~(NBD_CMD_FLAG_FUA | NBD_CMD_FLAG_NO_HOLE)) {
|
if (request->flags & ~(NBD_CMD_FLAG_FUA | NBD_CMD_FLAG_NO_HOLE)) {
|
||||||
LOG("unsupported flags (got 0x%x)", request->flags);
|
error_setg(errp, "unsupported flags (got 0x%x)", request->flags);
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
if (request->type != NBD_CMD_WRITE_ZEROES &&
|
if (request->type != NBD_CMD_WRITE_ZEROES &&
|
||||||
(request->flags & NBD_CMD_FLAG_NO_HOLE)) {
|
(request->flags & NBD_CMD_FLAG_NO_HOLE)) {
|
||||||
LOG("unexpected flags (got 0x%x)", request->flags);
|
error_setg(errp, "unexpected flags (got 0x%x)", request->flags);
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1079,15 +1117,16 @@ static coroutine_fn void nbd_trip(void *opaque)
|
||||||
int ret;
|
int ret;
|
||||||
int flags;
|
int flags;
|
||||||
int reply_data_len = 0;
|
int reply_data_len = 0;
|
||||||
|
Error *local_err = NULL;
|
||||||
|
|
||||||
TRACE("Reading request.");
|
trace_nbd_trip();
|
||||||
if (client->closing) {
|
if (client->closing) {
|
||||||
nbd_client_put(client);
|
nbd_client_put(client);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
req = nbd_request_get(client);
|
req = nbd_request_get(client);
|
||||||
ret = nbd_co_receive_request(req, &request);
|
ret = nbd_co_receive_request(req, &request, &local_err);
|
||||||
client->recv_coroutine = NULL;
|
client->recv_coroutine = NULL;
|
||||||
nbd_client_receive_next_request(client);
|
nbd_client_receive_next_request(client);
|
||||||
if (ret == -EIO) {
|
if (ret == -EIO) {
|
||||||
|
@ -1112,13 +1151,11 @@ static coroutine_fn void nbd_trip(void *opaque)
|
||||||
|
|
||||||
switch (request.type) {
|
switch (request.type) {
|
||||||
case NBD_CMD_READ:
|
case NBD_CMD_READ:
|
||||||
TRACE("Request type is READ");
|
|
||||||
|
|
||||||
/* XXX: NBD Protocol only documents use of FUA with WRITE */
|
/* XXX: NBD Protocol only documents use of FUA with WRITE */
|
||||||
if (request.flags & NBD_CMD_FLAG_FUA) {
|
if (request.flags & NBD_CMD_FLAG_FUA) {
|
||||||
ret = blk_co_flush(exp->blk);
|
ret = blk_co_flush(exp->blk);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LOG("flush failed");
|
error_setg_errno(&local_err, -ret, "flush failed");
|
||||||
reply.error = -ret;
|
reply.error = -ret;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -1127,26 +1164,20 @@ static coroutine_fn void nbd_trip(void *opaque)
|
||||||
ret = blk_pread(exp->blk, request.from + exp->dev_offset,
|
ret = blk_pread(exp->blk, request.from + exp->dev_offset,
|
||||||
req->data, request.len);
|
req->data, request.len);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LOG("reading from file failed");
|
error_setg_errno(&local_err, -ret, "reading from file failed");
|
||||||
reply.error = -ret;
|
reply.error = -ret;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
reply_data_len = request.len;
|
reply_data_len = request.len;
|
||||||
TRACE("Read %" PRIu32" byte(s)", request.len);
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case NBD_CMD_WRITE:
|
case NBD_CMD_WRITE:
|
||||||
TRACE("Request type is WRITE");
|
|
||||||
|
|
||||||
if (exp->nbdflags & NBD_FLAG_READ_ONLY) {
|
if (exp->nbdflags & NBD_FLAG_READ_ONLY) {
|
||||||
TRACE("Server is read-only, return error");
|
|
||||||
reply.error = EROFS;
|
reply.error = EROFS;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
TRACE("Writing to device");
|
|
||||||
|
|
||||||
flags = 0;
|
flags = 0;
|
||||||
if (request.flags & NBD_CMD_FLAG_FUA) {
|
if (request.flags & NBD_CMD_FLAG_FUA) {
|
||||||
flags |= BDRV_REQ_FUA;
|
flags |= BDRV_REQ_FUA;
|
||||||
|
@ -1154,22 +1185,18 @@ static coroutine_fn void nbd_trip(void *opaque)
|
||||||
ret = blk_pwrite(exp->blk, request.from + exp->dev_offset,
|
ret = blk_pwrite(exp->blk, request.from + exp->dev_offset,
|
||||||
req->data, request.len, flags);
|
req->data, request.len, flags);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LOG("writing to file failed");
|
error_setg_errno(&local_err, -ret, "writing to file failed");
|
||||||
reply.error = -ret;
|
reply.error = -ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case NBD_CMD_WRITE_ZEROES:
|
case NBD_CMD_WRITE_ZEROES:
|
||||||
TRACE("Request type is WRITE_ZEROES");
|
|
||||||
|
|
||||||
if (exp->nbdflags & NBD_FLAG_READ_ONLY) {
|
if (exp->nbdflags & NBD_FLAG_READ_ONLY) {
|
||||||
TRACE("Server is read-only, return error");
|
error_setg(&local_err, "Server is read-only, return error");
|
||||||
reply.error = EROFS;
|
reply.error = EROFS;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
TRACE("Writing to device");
|
|
||||||
|
|
||||||
flags = 0;
|
flags = 0;
|
||||||
if (request.flags & NBD_CMD_FLAG_FUA) {
|
if (request.flags & NBD_CMD_FLAG_FUA) {
|
||||||
flags |= BDRV_REQ_FUA;
|
flags |= BDRV_REQ_FUA;
|
||||||
|
@ -1180,7 +1207,7 @@ static coroutine_fn void nbd_trip(void *opaque)
|
||||||
ret = blk_pwrite_zeroes(exp->blk, request.from + exp->dev_offset,
|
ret = blk_pwrite_zeroes(exp->blk, request.from + exp->dev_offset,
|
||||||
request.len, flags);
|
request.len, flags);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LOG("writing to file failed");
|
error_setg_errno(&local_err, -ret, "writing to file failed");
|
||||||
reply.error = -ret;
|
reply.error = -ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1190,39 +1217,48 @@ static coroutine_fn void nbd_trip(void *opaque)
|
||||||
abort();
|
abort();
|
||||||
|
|
||||||
case NBD_CMD_FLUSH:
|
case NBD_CMD_FLUSH:
|
||||||
TRACE("Request type is FLUSH");
|
|
||||||
|
|
||||||
ret = blk_co_flush(exp->blk);
|
ret = blk_co_flush(exp->blk);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LOG("flush failed");
|
error_setg_errno(&local_err, -ret, "flush failed");
|
||||||
reply.error = -ret;
|
reply.error = -ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case NBD_CMD_TRIM:
|
case NBD_CMD_TRIM:
|
||||||
TRACE("Request type is TRIM");
|
|
||||||
ret = blk_co_pdiscard(exp->blk, request.from + exp->dev_offset,
|
ret = blk_co_pdiscard(exp->blk, request.from + exp->dev_offset,
|
||||||
request.len);
|
request.len);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
LOG("discard failed");
|
error_setg_errno(&local_err, -ret, "discard failed");
|
||||||
reply.error = -ret;
|
reply.error = -ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
LOG("invalid request type (%" PRIu32 ") received", request.type);
|
error_setg(&local_err, "invalid request type (%" PRIu32 ") received",
|
||||||
|
request.type);
|
||||||
reply.error = EINVAL;
|
reply.error = EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
reply:
|
reply:
|
||||||
/* We must disconnect after NBD_CMD_WRITE if we did not
|
if (local_err) {
|
||||||
* read the payload.
|
/* If we are here local_err is not fatal error, already stored in
|
||||||
*/
|
* reply.error */
|
||||||
if (nbd_co_send_reply(req, &reply, reply_data_len) < 0 || !req->complete) {
|
error_report_err(local_err);
|
||||||
|
local_err = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nbd_co_send_reply(req, &reply, reply_data_len, &local_err) < 0) {
|
||||||
|
error_prepend(&local_err, "Failed to send reply: ");
|
||||||
goto disconnect;
|
goto disconnect;
|
||||||
}
|
}
|
||||||
|
|
||||||
TRACE("Request/Reply complete");
|
/* We must disconnect after NBD_CMD_WRITE if we did not
|
||||||
|
* read the payload.
|
||||||
|
*/
|
||||||
|
if (!req->complete) {
|
||||||
|
error_setg(&local_err, "Request handling failed in intermediate state");
|
||||||
|
goto disconnect;
|
||||||
|
}
|
||||||
|
|
||||||
done:
|
done:
|
||||||
nbd_request_put(req);
|
nbd_request_put(req);
|
||||||
|
@ -1230,6 +1266,9 @@ done:
|
||||||
return;
|
return;
|
||||||
|
|
||||||
disconnect:
|
disconnect:
|
||||||
|
if (local_err) {
|
||||||
|
error_reportf_err(local_err, "Disconnect client, due to: ");
|
||||||
|
}
|
||||||
nbd_request_put(req);
|
nbd_request_put(req);
|
||||||
client_close(client, true);
|
client_close(client, true);
|
||||||
nbd_client_put(client);
|
nbd_client_put(client);
|
||||||
|
@ -1248,6 +1287,7 @@ static coroutine_fn void nbd_co_client_start(void *opaque)
|
||||||
{
|
{
|
||||||
NBDClient *client = opaque;
|
NBDClient *client = opaque;
|
||||||
NBDExport *exp = client->exp;
|
NBDExport *exp = client->exp;
|
||||||
|
Error *local_err = NULL;
|
||||||
|
|
||||||
if (exp) {
|
if (exp) {
|
||||||
nbd_export_get(exp);
|
nbd_export_get(exp);
|
||||||
|
@ -1255,7 +1295,10 @@ static coroutine_fn void nbd_co_client_start(void *opaque)
|
||||||
}
|
}
|
||||||
qemu_co_mutex_init(&client->send_lock);
|
qemu_co_mutex_init(&client->send_lock);
|
||||||
|
|
||||||
if (nbd_negotiate(client)) {
|
if (nbd_negotiate(client, &local_err)) {
|
||||||
|
if (local_err) {
|
||||||
|
error_report_err(local_err);
|
||||||
|
}
|
||||||
client_close(client, false);
|
client_close(client, false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,56 @@
|
||||||
|
# nbd/client.c
|
||||||
|
nbd_unknown_error(int err) "Squashing unexpected error %d to EINVAL"
|
||||||
|
nbd_send_option_request(uint32_t opt, uint32_t len) "Sending option request %" PRIu32", len %" PRIu32
|
||||||
|
nbd_receive_option_reply(uint32_t option, uint32_t type, uint32_t length) "Received option reply %" PRIx32", type %" PRIx32", len %" PRIu32
|
||||||
|
nbd_reply_err_unsup(uint32_t option) "server doesn't understand request %" PRIx32 ", attempting fallback"
|
||||||
|
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_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
|
||||||
|
nbd_receive_negotiate_server_flags(uint32_t globalflags) "Global flags are %" PRIx32
|
||||||
|
nbd_receive_negotiate_default_name(void) "Using default NBD export name \"\""
|
||||||
|
nbd_receive_negotiate_size_flags(uint64_t size, uint16_t flags) "Size is %" PRIu64 ", export flags %" PRIx16
|
||||||
|
nbd_init_set_socket(void) "Setting NBD socket"
|
||||||
|
nbd_init_set_block_size(unsigned long block_size) "Setting block size to %lu"
|
||||||
|
nbd_init_set_size(unsigned long sectors) "Setting size to %lu block(s)"
|
||||||
|
nbd_init_trailing_bytes(int ignored_bytes) "Ignoring trailing %d bytes of export"
|
||||||
|
nbd_init_set_readonly(void) "Setting readonly attribute"
|
||||||
|
nbd_init_finish(void) "Negotiation ended"
|
||||||
|
nbd_client_loop(void) "Doing NBD loop"
|
||||||
|
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) "Sending request to server: { .from = %" PRIu64", .len = %" PRIu32 ", .handle = %" PRIu64 ", .flags = %" PRIx16 ", .type = %" PRIu16 " }"
|
||||||
|
nbd_receive_reply(uint32_t magic, int32_t error, uint64_t handle) "Got reply: { magic = 0x%" PRIx32 ", .error = % " PRId32 ", handle = %" PRIu64" }"
|
||||||
|
|
||||||
|
# nbd/server.c
|
||||||
|
nbd_negotiate_send_rep_len(uint32_t opt, uint32_t type, uint32_t len) "Reply opt=%" PRIx32 " type=%" PRIx32 " len=%" PRIu32
|
||||||
|
nbd_negotiate_send_rep_err(const char *msg) "sending error message \"%s\""
|
||||||
|
nbd_negotiate_send_rep_list(const char *name, const char *desc) "Advertising export name '%s' description '%s'"
|
||||||
|
nbd_negotiate_handle_export_name(void) "Checking length"
|
||||||
|
nbd_negotiate_handle_export_name_request(const char *name) "Client requested export '%s'"
|
||||||
|
nbd_negotiate_handle_starttls(void) "Setting up TLS"
|
||||||
|
nbd_negotiate_handle_starttls_handshake(void) "Starting TLS handshake"
|
||||||
|
nbd_negotiate_options_flags(void) "Checking client flags"
|
||||||
|
nbd_negotiate_options_newstyle(void) "Client supports fixed newstyle handshake"
|
||||||
|
nbd_negotiate_options_no_zeroes(void) "Client supports no zeroes at handshake end"
|
||||||
|
nbd_negotiate_options_check_magic(uint64_t magic) "Checking opts magic 0x%" PRIx64
|
||||||
|
nbd_negotiate_options_check_option(uint32_t option) "Checking option 0x%" PRIx32
|
||||||
|
nbd_opt_abort_reply_failed(const char *error) "Reply to NBD_OPT_ABORT request failed: %s"
|
||||||
|
nbd_negotiate_begin(void) "Beginning negotiation"
|
||||||
|
nbd_negotiate_old_style(uint64_t size, unsigned flags) "advertising size %" PRIu64 " and flags %x"
|
||||||
|
nbd_negotiate_new_style_size_flags(uint64_t size, unsigned flags) "advertising size %" PRIu64 " and flags %x"
|
||||||
|
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 = %" PRIx16 ", .type = %" PRIx16 ", from = %" PRIu64 ", len = %" PRIu32 " }"
|
||||||
|
nbd_send_reply(int32_t error, uint64_t handle) "Sending response to client: { .error = %" PRId32 ", handle = %" PRIu64 " }"
|
||||||
|
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_reply(uint64_t handle, uint32_t error, int len) "Send reply: handle = %" PRIu64 ", error = %" PRIu32 ", len = %d"
|
||||||
|
nbd_co_receive_request_decode_type(uint64_t handle, uint16_t type) "Decoding type: handle = %" PRIu64 ", type = %" PRIu16
|
||||||
|
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)"
|
||||||
|
nbd_trip(void) "Reading request"
|
Loading…
Reference in New Issue