*/
#include "qemu/osdep.h"
-#include "block/nbd-client.h"
+#include "nbd-client.h"
+#include "block/qdict.h"
#include "qapi/error.h"
#include "qemu/uri.h"
#include "block/block_int.h"
#include "qemu/module.h"
+#include "qemu/option.h"
+#include "qapi/qapi-visit-sockets.h"
+#include "qapi/qobject-input-visitor.h"
+#include "qapi/qobject-output-visitor.h"
#include "qapi/qmp/qdict.h"
-#include "qapi/qmp/qjson.h"
-#include "qapi/qmp/qint.h"
#include "qapi/qmp/qstring.h"
#include "qemu/cutils.h"
#define EN_OPTSTR ":exportname="
typedef struct BDRVNBDState {
- NbdClientSession client;
+ NBDClientSession client;
+
+ /* For nbd_refresh_filename() */
+ SocketAddress *saddr;
+ char *export, *tlscredsid;
} BDRVNBDState;
static int nbd_parse_uri(const char *filename, QDict *options)
}
/* transport */
- if (!strcmp(uri->scheme, "nbd")) {
+ if (!g_strcmp0(uri->scheme, "nbd")) {
is_unix = false;
- } else if (!strcmp(uri->scheme, "nbd+tcp")) {
+ } else if (!g_strcmp0(uri->scheme, "nbd+tcp")) {
is_unix = false;
- } else if (!strcmp(uri->scheme, "nbd+unix")) {
+ } else if (!g_strcmp0(uri->scheme, "nbd+unix")) {
is_unix = true;
} else {
ret = -EINVAL;
p = uri->path ? uri->path : "/";
p += strspn(p, "/");
if (p[0]) {
- qdict_put(options, "export", qstring_from_str(p));
+ qdict_put_str(options, "export", p);
}
qp = query_params_parse(uri->query);
ret = -EINVAL;
goto out;
}
- qdict_put(options, "path", qstring_from_str(qp->p[0].value));
+ qdict_put_str(options, "server.type", "unix");
+ qdict_put_str(options, "server.path", qp->p[0].value);
} else {
QString *host;
+ char *port_str;
+
/* nbd[+tcp]://host[:port]/export */
if (!uri->server) {
ret = -EINVAL;
/* strip braces from literal IPv6 address */
if (uri->server[0] == '[') {
host = qstring_from_substr(uri->server, 1,
- strlen(uri->server) - 2);
+ strlen(uri->server) - 1);
} else {
host = qstring_from_str(uri->server);
}
- qdict_put(options, "host", host);
- if (uri->port) {
- char* port_str = g_strdup_printf("%d", uri->port);
- qdict_put(options, "port", qstring_from_str(port_str));
- g_free(port_str);
- }
+ qdict_put_str(options, "server.type", "inet");
+ qdict_put(options, "server.host", host);
+
+ port_str = g_strdup_printf("%d", uri->port ?: NBD_DEFAULT_PORT);
+ qdict_put_str(options, "server.port", port_str);
+ g_free(port_str);
}
out:
return ret;
}
+static bool nbd_has_filename_options_conflict(QDict *options, Error **errp)
+{
+ const QDictEntry *e;
+
+ for (e = qdict_first(options); e; e = qdict_next(options, e)) {
+ if (!strcmp(e->key, "host") ||
+ !strcmp(e->key, "port") ||
+ !strcmp(e->key, "path") ||
+ !strcmp(e->key, "export") ||
+ strstart(e->key, "server.", NULL))
+ {
+ error_setg(errp, "Option '%s' cannot be used with a file name",
+ e->key);
+ return true;
+ }
+ }
+
+ return false;
+}
+
static void nbd_parse_filename(const char *filename, QDict *options,
Error **errp)
{
const char *host_spec;
const char *unixpath;
- if (qdict_haskey(options, "host")
- || qdict_haskey(options, "port")
- || qdict_haskey(options, "path"))
- {
- error_setg(errp, "host/port/path and a file name may not be specified "
- "at the same time");
+ if (nbd_has_filename_options_conflict(options, errp)) {
return;
}
export_name[0] = 0; /* truncate 'file' */
export_name += strlen(EN_OPTSTR);
- qdict_put(options, "export", qstring_from_str(export_name));
+ qdict_put_str(options, "export", export_name);
}
/* extract the host_spec - fail if it's not nbd:... */
/* are we a UNIX or TCP socket? */
if (strstart(host_spec, "unix:", &unixpath)) {
- qdict_put(options, "path", qstring_from_str(unixpath));
+ qdict_put_str(options, "server.type", "unix");
+ qdict_put_str(options, "server.path", unixpath);
} else {
- InetSocketAddress *addr = NULL;
+ InetSocketAddress *addr = g_new(InetSocketAddress, 1);
- addr = inet_parse(host_spec, errp);
- if (!addr) {
- goto out;
+ if (inet_parse(addr, host_spec, errp)) {
+ goto out_inet;
}
- qdict_put(options, "host", qstring_from_str(addr->host));
- qdict_put(options, "port", qstring_from_str(addr->port));
+ qdict_put_str(options, "server.type", "inet");
+ qdict_put_str(options, "server.host", addr->host);
+ qdict_put_str(options, "server.port", addr->port);
+ out_inet:
qapi_free_InetSocketAddress(addr);
}
g_free(file);
}
-static SocketAddress *nbd_config(BDRVNBDState *s, QDict *options, char **export,
- Error **errp)
+static bool nbd_process_legacy_socket_options(QDict *output_options,
+ QemuOpts *legacy_opts,
+ Error **errp)
{
- SocketAddress *saddr;
+ const char *path = qemu_opt_get(legacy_opts, "path");
+ const char *host = qemu_opt_get(legacy_opts, "host");
+ const char *port = qemu_opt_get(legacy_opts, "port");
+ const QDictEntry *e;
- if (qdict_haskey(options, "path") == qdict_haskey(options, "host")) {
- if (qdict_haskey(options, "path")) {
- error_setg(errp, "path and host may not be used at the same time.");
- } else {
- error_setg(errp, "one of path and host must be specified.");
- }
- return NULL;
+ if (!path && !host && !port) {
+ return true;
}
- saddr = g_new0(SocketAddress, 1);
+ for (e = qdict_first(output_options); e; e = qdict_next(output_options, e))
+ {
+ if (strstart(e->key, "server.", NULL)) {
+ error_setg(errp, "Cannot use 'server' and path/host/port at the "
+ "same time");
+ return false;
+ }
+ }
- if (qdict_haskey(options, "path")) {
- UnixSocketAddress *q_unix;
- saddr->type = SOCKET_ADDRESS_KIND_UNIX;
- q_unix = saddr->u.q_unix.data = g_new0(UnixSocketAddress, 1);
- q_unix->path = g_strdup(qdict_get_str(options, "path"));
- qdict_del(options, "path");
- } else {
- InetSocketAddress *inet;
- saddr->type = SOCKET_ADDRESS_KIND_INET;
- inet = saddr->u.inet.data = g_new0(InetSocketAddress, 1);
- inet->host = g_strdup(qdict_get_str(options, "host"));
- if (!qdict_get_try_str(options, "port")) {
- inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
- } else {
- inet->port = g_strdup(qdict_get_str(options, "port"));
+ if (path && host) {
+ error_setg(errp, "path and host may not be used at the same time");
+ return false;
+ } else if (path) {
+ if (port) {
+ error_setg(errp, "port may not be used without host");
+ return false;
}
- qdict_del(options, "host");
- qdict_del(options, "port");
+
+ qdict_put_str(output_options, "server.type", "unix");
+ qdict_put_str(output_options, "server.path", path);
+ } else if (host) {
+ qdict_put_str(output_options, "server.type", "inet");
+ qdict_put_str(output_options, "server.host", host);
+ qdict_put_str(output_options, "server.port",
+ port ?: stringify(NBD_DEFAULT_PORT));
+ }
+
+ return true;
+}
+
+static SocketAddress *nbd_config(BDRVNBDState *s, QDict *options,
+ Error **errp)
+{
+ SocketAddress *saddr = NULL;
+ QDict *addr = NULL;
+ Visitor *iv = NULL;
+ Error *local_err = NULL;
+
+ qdict_extract_subqdict(options, &addr, "server.");
+ if (!qdict_size(addr)) {
+ error_setg(errp, "NBD server address missing");
+ goto done;
}
- s->client.is_unix = saddr->type == SOCKET_ADDRESS_KIND_UNIX;
+ iv = qobject_input_visitor_new_flat_confused(addr, errp);
+ if (!iv) {
+ goto done;
+ }
- *export = g_strdup(qdict_get_try_str(options, "export"));
- if (*export) {
- qdict_del(options, "export");
+ visit_type_SocketAddress(iv, NULL, &saddr, &local_err);
+ if (local_err) {
+ error_propagate(errp, local_err);
+ goto done;
}
+done:
+ qobject_unref(addr);
+ visit_free(iv);
return saddr;
}
-NbdClientSession *nbd_get_client_session(BlockDriverState *bs)
+NBDClientSession *nbd_get_client_session(BlockDriverState *bs)
{
BDRVNBDState *s = bs->opaque;
return &s->client;
Error *local_err = NULL;
sioc = qio_channel_socket_new();
+ qio_channel_set_name(QIO_CHANNEL(sioc), "nbd-client");
qio_channel_socket_connect_sync(sioc,
saddr,
&local_err);
if (local_err) {
+ object_unref(OBJECT(sioc));
error_propagate(errp, local_err);
return NULL;
}
}
+static QemuOptsList nbd_runtime_opts = {
+ .name = "nbd",
+ .head = QTAILQ_HEAD_INITIALIZER(nbd_runtime_opts.head),
+ .desc = {
+ {
+ .name = "host",
+ .type = QEMU_OPT_STRING,
+ .help = "TCP host to connect to",
+ },
+ {
+ .name = "port",
+ .type = QEMU_OPT_STRING,
+ .help = "TCP port to connect to",
+ },
+ {
+ .name = "path",
+ .type = QEMU_OPT_STRING,
+ .help = "Unix socket path to connect to",
+ },
+ {
+ .name = "export",
+ .type = QEMU_OPT_STRING,
+ .help = "Name of the NBD export to open",
+ },
+ {
+ .name = "tls-creds",
+ .type = QEMU_OPT_STRING,
+ .help = "ID of the TLS credentials to use",
+ },
+ {
+ .name = "x-dirty-bitmap",
+ .type = QEMU_OPT_STRING,
+ .help = "experimental: expose named dirty bitmap in place of "
+ "block status",
+ },
+ { /* end of list */ }
+ },
+};
+
static int nbd_open(BlockDriverState *bs, QDict *options, int flags,
Error **errp)
{
BDRVNBDState *s = bs->opaque;
- char *export = NULL;
+ QemuOpts *opts = NULL;
+ Error *local_err = NULL;
QIOChannelSocket *sioc = NULL;
- SocketAddress *saddr;
- const char *tlscredsid;
QCryptoTLSCreds *tlscreds = NULL;
const char *hostname = NULL;
int ret = -EINVAL;
+ opts = qemu_opts_create(&nbd_runtime_opts, NULL, 0, &error_abort);
+ qemu_opts_absorb_qdict(opts, options, &local_err);
+ if (local_err) {
+ error_propagate(errp, local_err);
+ goto error;
+ }
+
+ /* Translate @host, @port, and @path to a SocketAddress */
+ if (!nbd_process_legacy_socket_options(options, opts, errp)) {
+ goto error;
+ }
+
/* Pop the config into our state object. Exit if invalid. */
- saddr = nbd_config(s, options, &export, errp);
- if (!saddr) {
+ s->saddr = nbd_config(s, options, errp);
+ if (!s->saddr) {
goto error;
}
- tlscredsid = g_strdup(qdict_get_try_str(options, "tls-creds"));
- if (tlscredsid) {
- qdict_del(options, "tls-creds");
- tlscreds = nbd_get_tls_creds(tlscredsid, errp);
+ s->export = g_strdup(qemu_opt_get(opts, "export"));
+
+ s->tlscredsid = g_strdup(qemu_opt_get(opts, "tls-creds"));
+ if (s->tlscredsid) {
+ tlscreds = nbd_get_tls_creds(s->tlscredsid, errp);
if (!tlscreds) {
goto error;
}
- if (saddr->type != SOCKET_ADDRESS_KIND_INET) {
+ /* TODO SOCKET_ADDRESS_KIND_FD where fd has AF_INET or AF_INET6 */
+ if (s->saddr->type != SOCKET_ADDRESS_TYPE_INET) {
error_setg(errp, "TLS only supported over IP sockets");
goto error;
}
- hostname = saddr->u.inet.data->host;
+ hostname = s->saddr->u.inet.host;
}
/* establish TCP connection, return error if it fails
* TODO: Configurable retry-until-timeout behaviour.
*/
- sioc = nbd_establish_connection(saddr, errp);
+ sioc = nbd_establish_connection(s->saddr, errp);
if (!sioc) {
ret = -ECONNREFUSED;
goto error;
}
/* NBD handshake */
- ret = nbd_client_init(bs, sioc, export,
- tlscreds, hostname, errp);
+ ret = nbd_client_init(bs, sioc, s->export, tlscreds, hostname,
+ qemu_opt_get(opts, "x-dirty-bitmap"), errp);
error:
if (sioc) {
object_unref(OBJECT(sioc));
if (tlscreds) {
object_unref(OBJECT(tlscreds));
}
- qapi_free_SocketAddress(saddr);
- g_free(export);
+ if (ret < 0) {
+ qapi_free_SocketAddress(s->saddr);
+ g_free(s->export);
+ g_free(s->tlscredsid);
+ }
+ qemu_opts_del(opts);
return ret;
}
static void nbd_refresh_limits(BlockDriverState *bs, Error **errp)
{
- bs->bl.max_pdiscard = NBD_MAX_BUFFER_SIZE;
- bs->bl.max_transfer = NBD_MAX_BUFFER_SIZE;
+ NBDClientSession *s = nbd_get_client_session(bs);
+ uint32_t min = s->info.min_block;
+ uint32_t max = MIN_NON_ZERO(NBD_MAX_BUFFER_SIZE, s->info.max_block);
+
+ bs->bl.request_alignment = min ? min : BDRV_SECTOR_SIZE;
+ bs->bl.max_pdiscard = max;
+ bs->bl.max_pwrite_zeroes = max;
+ bs->bl.max_transfer = max;
+
+ if (s->info.opt_block &&
+ s->info.opt_block > bs->bl.opt_transfer) {
+ bs->bl.opt_transfer = s->info.opt_block;
+ }
}
static void nbd_close(BlockDriverState *bs)
{
+ BDRVNBDState *s = bs->opaque;
+
nbd_client_close(bs);
+
+ qapi_free_SocketAddress(s->saddr);
+ g_free(s->export);
+ g_free(s->tlscredsid);
}
static int64_t nbd_getlength(BlockDriverState *bs)
{
BDRVNBDState *s = bs->opaque;
- return s->client.size;
+ return s->client.info.size;
}
static void nbd_detach_aio_context(BlockDriverState *bs)
static void nbd_refresh_filename(BlockDriverState *bs, QDict *options)
{
+ BDRVNBDState *s = bs->opaque;
QDict *opts = qdict_new();
- const char *path = qdict_get_try_str(options, "path");
- const char *host = qdict_get_try_str(options, "host");
- const char *port = qdict_get_try_str(options, "port");
- const char *export = qdict_get_try_str(options, "export");
- const char *tlscreds = qdict_get_try_str(options, "tls-creds");
+ QObject *saddr_qdict;
+ Visitor *ov;
+ const char *host = NULL, *port = NULL, *path = NULL;
+
+ if (s->saddr->type == SOCKET_ADDRESS_TYPE_INET) {
+ const InetSocketAddress *inet = &s->saddr->u.inet;
+ if (!inet->has_ipv4 && !inet->has_ipv6 && !inet->has_to) {
+ host = inet->host;
+ port = inet->port;
+ }
+ } else if (s->saddr->type == SOCKET_ADDRESS_TYPE_UNIX) {
+ path = s->saddr->u.q_unix.path;
+ } /* else can't represent as pseudo-filename */
- qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("nbd")));
+ qdict_put_str(opts, "driver", "nbd");
- if (path && export) {
+ if (path && s->export) {
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
- "nbd+unix:///%s?socket=%s", export, path);
- } else if (path && !export) {
+ "nbd+unix:///%s?socket=%s", s->export, path);
+ } else if (path && !s->export) {
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
"nbd+unix://?socket=%s", path);
- } else if (!path && export && port) {
- snprintf(bs->exact_filename, sizeof(bs->exact_filename),
- "nbd://%s:%s/%s", host, port, export);
- } else if (!path && export && !port) {
+ } else if (host && s->export) {
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
- "nbd://%s/%s", host, export);
- } else if (!path && !export && port) {
+ "nbd://%s:%s/%s", host, port, s->export);
+ } else if (host && !s->export) {
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
"nbd://%s:%s", host, port);
- } else if (!path && !export && !port) {
- snprintf(bs->exact_filename, sizeof(bs->exact_filename),
- "nbd://%s", host);
}
- if (path) {
- qdict_put_obj(opts, "path", QOBJECT(qstring_from_str(path)));
- } else if (port) {
- qdict_put_obj(opts, "host", QOBJECT(qstring_from_str(host)));
- qdict_put_obj(opts, "port", QOBJECT(qstring_from_str(port)));
- } else {
- qdict_put_obj(opts, "host", QOBJECT(qstring_from_str(host)));
- }
- if (export) {
- qdict_put_obj(opts, "export", QOBJECT(qstring_from_str(export)));
+ ov = qobject_output_visitor_new(&saddr_qdict);
+ visit_type_SocketAddress(ov, NULL, &s->saddr, &error_abort);
+ visit_complete(ov, &saddr_qdict);
+ visit_free(ov);
+ qdict_put_obj(opts, "server", saddr_qdict);
+
+ if (s->export) {
+ qdict_put_str(opts, "export", s->export);
}
- if (tlscreds) {
- qdict_put_obj(opts, "tls-creds", QOBJECT(qstring_from_str(tlscreds)));
+ if (s->tlscredsid) {
+ qdict_put_str(opts, "tls-creds", s->tlscredsid);
}
+ qdict_flatten(opts);
bs->full_open_options = opts;
}
.bdrv_file_open = nbd_open,
.bdrv_co_preadv = nbd_client_co_preadv,
.bdrv_co_pwritev = nbd_client_co_pwritev,
+ .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes,
.bdrv_close = nbd_close,
.bdrv_co_flush_to_os = nbd_co_flush,
.bdrv_co_pdiscard = nbd_client_co_pdiscard,
.bdrv_detach_aio_context = nbd_detach_aio_context,
.bdrv_attach_aio_context = nbd_attach_aio_context,
.bdrv_refresh_filename = nbd_refresh_filename,
+ .bdrv_co_block_status = nbd_client_co_block_status,
};
static BlockDriver bdrv_nbd_tcp = {
.bdrv_file_open = nbd_open,
.bdrv_co_preadv = nbd_client_co_preadv,
.bdrv_co_pwritev = nbd_client_co_pwritev,
+ .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes,
.bdrv_close = nbd_close,
.bdrv_co_flush_to_os = nbd_co_flush,
.bdrv_co_pdiscard = nbd_client_co_pdiscard,
.bdrv_detach_aio_context = nbd_detach_aio_context,
.bdrv_attach_aio_context = nbd_attach_aio_context,
.bdrv_refresh_filename = nbd_refresh_filename,
+ .bdrv_co_block_status = nbd_client_co_block_status,
};
static BlockDriver bdrv_nbd_unix = {
.bdrv_file_open = nbd_open,
.bdrv_co_preadv = nbd_client_co_preadv,
.bdrv_co_pwritev = nbd_client_co_pwritev,
+ .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes,
.bdrv_close = nbd_close,
.bdrv_co_flush_to_os = nbd_co_flush,
.bdrv_co_pdiscard = nbd_client_co_pdiscard,
.bdrv_detach_aio_context = nbd_detach_aio_context,
.bdrv_attach_aio_context = nbd_attach_aio_context,
.bdrv_refresh_filename = nbd_refresh_filename,
+ .bdrv_co_block_status = nbd_client_co_block_status,
};
static void bdrv_nbd_init(void)