On Mon, 27 Aug 2012 17:00:22 +0200 Paolo Bonzini <pbonz...@redhat.com> wrote:
> Adding an NBD server inside QEMU is trivial, since all the logic is > in nbd.c and can be shared easily between qemu-nbd and QEMU itself. > The main difference is that qemu-nbd serves a single unnamed export, > while QEMU serves named exports. Quickly reviewed the qmp part and it looks good to me (apart from TODOs of course, specially on error handling). I've only a few minor comments to the schema. > > Signed-off-by: Paolo Bonzini <pbonz...@redhat.com> > --- > Makefile.objs | 2 +- > blockdev-nbd.c | 93 > ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > qapi-schema.json | 69 +++++++++++++++++++++++++++++++++++++++++ > qmp-commands.hx | 16 ++++++++++ > 4 file modificati, 179 inserzioni(+). 1 rimozione(-) > create mode 100644 blockdev-nbd.c > > diff --git a/Makefile.objs b/Makefile.objs > index 4412757..c42affc 100644 > --- a/Makefile.objs > +++ b/Makefile.objs > @@ -59,7 +59,7 @@ endif > # suppress *all* target specific code in case of system emulation, i.e. a > # single QEMU executable should support all CPUs and machines. > > -common-obj-y = $(block-obj-y) blockdev.o > +common-obj-y = $(block-obj-y) blockdev.o blockdev-nbd.o > common-obj-y += net.o net/ > common-obj-y += qom/ > common-obj-y += readline.o console.o cursor.o > diff --git a/blockdev-nbd.c b/blockdev-nbd.c > new file mode 100644 > index 0000000..5a415be > --- /dev/null > +++ b/blockdev-nbd.c > @@ -0,0 +1,93 @@ > +/* > + * QEMU host block devices > + * > + * Copyright (c) 2003-2008 Fabrice Bellard > + * > + * This work is licensed under the terms of the GNU GPL, version 2 or > + * later. See the COPYING file in the top-level directory. > + */ > + > +#include "blockdev.h" > +#include "hw/block-common.h" > +#include "monitor.h" > +#include "qerror.h" > +#include "sysemu.h" > +#include "qmp-commands.h" > +#include "trace.h" > +#include "nbd.h" > +#include "qemu_socket.h" > + > +static int server_fd = -1; > + > +static void nbd_accept(void *opaque) > +{ > + struct sockaddr_in addr; > + socklen_t addr_len = sizeof(addr); > + > + int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len); > + if (fd >= 0) { > + nbd_client_new(NULL, fd, NULL); > + } > +} > + > +static void nbd_server_start(QemuOpts *opts, Error **errp) > +{ > + if (server_fd != -1) { > + /* TODO: error */ > + return; > + } > + > + server_fd = inet_listen_opts(opts, 0, errp); > + if (server_fd != -1) { > + qemu_set_fd_handler2(server_fd, NULL, nbd_accept, NULL, NULL); > + } > +} > + > +void qmp_nbd_server_start(IPSocketAddress *addr, Error **errp) > +{ > + QemuOpts *opts; > + > + opts = qemu_opts_create(&socket_opts, NULL, 0, NULL); > + qemu_opt_set(opts, "host", addr->host); > + qemu_opt_set(opts, "port", addr->port); > + > + addr->ipv4 |= !addr->has_ipv4; > + addr->ipv6 |= !addr->has_ipv6; > + if (!addr->ipv4 || !addr->ipv6) { > + qemu_opt_set_bool(opts, "ipv4", addr->ipv4); > + qemu_opt_set_bool(opts, "ipv6", addr->ipv6); > + } > + > + nbd_server_start(opts, errp); > + qemu_opts_del(opts); > +} > + > + > +void qmp_nbd_server_add(const char *device, bool has_writable, bool writable, > + Error **errp) > +{ > + BlockDriverState *bs; > + NBDExport *exp; > + > + bs = bdrv_find(device); > + if (!bs) { > + error_set(errp, QERR_DEVICE_NOT_FOUND, device); > + return; > + } > + > + if (nbd_export_find(bdrv_get_device_name(bs))) { > + /* TODO: error */ > + return; > + } > + > + exp = nbd_export_new(bs, 0, -1, writable ? 0 : NBD_FLAG_READ_ONLY); > + nbd_export_set_name(exp, device); > +} > + > +void qmp_nbd_server_stop(Error **errp) > +{ > + nbd_export_close_all(); > + qemu_set_fd_handler2(server_fd, NULL, NULL, NULL, NULL); > + close(server_fd); > + server_fd = -1; > +} > diff --git a/qapi-schema.json b/qapi-schema.json > index 3d2b2d1..d792d2c 100644 > --- a/qapi-schema.json > +++ b/qapi-schema.json > @@ -2275,6 +2275,30 @@ > 'opts': 'NetClientOptions' } } > > ## > +# @IPSocketAddress > +# > +# Captures the destination address of an IP socket > +# > +# @host: host part of the address > +# > +# @port: port part of the address, or lowest port if @to is present > +# > +# @to: highest port to try Doesn't exist in data. If you add, please use a more descriptive name. > +# > +# @ipv4: whether to accept IPv4 addresses, default try both IPv4 and IPv6 > +# > +# @ipv6: whether to accept IPv6 addresses, default try both IPv4 and IPv6 Both are missing #optional. > +# > +# Since 1.3 > +## > +{ 'type': 'IPSocketAddress', > + 'data': { > + 'host': 'str', > + 'port': 'str', > + '*ipv4': 'bool', > + '*ipv6': 'bool' } } > + > +## > # @getfd: > # > # Receive a file descriptor via SCM rights and assign it a name > @@ -2454,3 +2478,46 @@ > # > ## > { 'command': 'query-fdsets', 'returns': ['FdsetInfo'] } > + > +## > +# @nbd-server-start: > +# > +# Start an NBD server listening on the given host and port. > +# > +# @addr: Interface on which to listen, nothing for all interfaces. > +# > +# Since: 1.3.0 > +# > +## > +{ 'command': 'nbd-server-start', > + 'data': { 'addr': 'IPSocketAddress' } } > + > +## > +# @nbd-server-add: > +# > +# Export a device to QEMU's embedded NBD server. > +# > +# @device: Block device to be exported > +# > +# @writable: Whether clients should be able to write to the device via the > +# NBD connection (default false). Missing #optional. > +# > +# Returns: error if the device is already marked for export. > +# > +# Since: 1.3.0 > +# > +## > +{ 'command': 'nbd-server-add', 'data': {'device': 'str', '*writable': > 'bool'} } > + > +## > +# @nbd-server-stop: > +# > +# Stop QEMU's embedded NBD server, and unregister all devices previously > +# added via nbd-server-add > +# > +# Returns: error if the server is already running. That error pertains to nbd-start-start. > +# > +# Since: 1.3.0 > +# > +## > +{ 'command': 'nbd-server-stop' } > diff --git a/qmp-commands.hx b/qmp-commands.hx > index 2ce4ce6..1f7d6fe 100644 > --- a/qmp-commands.hx > +++ b/qmp-commands.hx > @@ -2481,6 +2481,22 @@ EQMP > }, > > { > + .name = "nbd-server-start", > + .args_type = "addr:q", > + .mhandler.cmd_new = qmp_marshal_input_nbd_server_start, > + }, > + { > + .name = "nbd-server-add", > + .args_type = "device:B,writable:b?", > + .mhandler.cmd_new = qmp_marshal_input_nbd_server_add, > + }, > + { > + .name = "nbd-server-stop", > + .args_type = "", > + .mhandler.cmd_new = qmp_marshal_input_nbd_server_stop, > + }, > + > + { > .name = "change-vnc-password", > .args_type = "password:s", > .mhandler.cmd_new = qmp_marshal_input_change_vnc_password,