From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, stefanha@gmail.com
Subject: [Qemu-devel] [RFC PATCH 09/13] qmp: add NBD server commands
Date: Mon, 27 Aug 2012 17:00:22 +0200 [thread overview]
Message-ID: <1346079626-16386-10-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1346079626-16386-1-git-send-email-pbonzini@redhat.com>
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.
Signed-off-by: Paolo Bonzini <pbonzini@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
+#
+# @ipv4: whether to accept IPv4 addresses, default try both IPv4 and IPv6
+#
+# @ipv6: whether to accept IPv6 addresses, default try both IPv4 and IPv6
+#
+# 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).
+#
+# 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.
+#
+# 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,
--
1.7.11.2
next prev parent reply other threads:[~2012-08-27 15:01 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-08-27 15:00 [Qemu-devel] [RFC PATCH 00/13] Embedded NBD server Paolo Bonzini
2012-08-27 15:00 ` [Qemu-devel] [RFC PATCH 01/13] nbd: add more constants Paolo Bonzini
2012-08-27 15:00 ` [Qemu-devel] [RFC PATCH 02/13] nbd: pass NBDClient to nbd_send_negotiate Paolo Bonzini
2012-08-27 15:00 ` [Qemu-devel] [RFC PATCH 03/13] nbd: do not leak nbd_trip coroutines when a connection is torn down Paolo Bonzini
2012-08-27 15:00 ` [Qemu-devel] [RFC PATCH 04/13] nbd: close all clients on deleting export Paolo Bonzini
2012-08-27 15:00 ` [Qemu-devel] [RFC PATCH 05/13] nbd: register named exports Paolo Bonzini
2012-08-27 15:00 ` [Qemu-devel] [RFC PATCH 06/13] nbd: negotiate with " Paolo Bonzini
2012-08-27 15:00 ` [Qemu-devel] [RFC PATCH 07/13] nbd: do not close BlockDriverState in nbd_export_close Paolo Bonzini
2012-08-27 15:00 ` [Qemu-devel] [RFC PATCH 08/13] qemu-sockets: publish dummy_opts Paolo Bonzini
2012-08-27 15:00 ` Paolo Bonzini [this message]
2012-09-18 20:11 ` [Qemu-devel] [RFC PATCH 09/13] qmp: add NBD server commands Luiz Capitulino
2012-09-19 8:16 ` Paolo Bonzini
2012-08-27 15:00 ` [Qemu-devel] [RFC PATCH 10/13] qemu-sockets: make inet_parse public Paolo Bonzini
2012-08-27 15:00 ` [Qemu-devel] [RFC PATCH 11/13] hmp: add NBD server commands Paolo Bonzini
2012-09-18 20:22 ` Luiz Capitulino
2012-09-19 8:00 ` Paolo Bonzini
2012-08-27 15:00 ` [Qemu-devel] [RFC PATCH 12/13] block: add close notifiers Paolo Bonzini
2012-08-27 15:00 ` [Qemu-devel] [RFC PATCH 13/13] nbd: add notifier to close exports when the image is closed Paolo Bonzini
2012-09-07 15:50 ` [Qemu-devel] ping Re: [RFC PATCH 00/13] Embedded NBD server Paolo Bonzini
2012-09-07 16:11 ` Kevin Wolf
2012-09-17 16:43 ` Paolo Bonzini
2012-09-18 8:45 ` Kevin Wolf
2012-09-18 9:09 ` Paolo Bonzini
2012-09-18 9:40 ` Kevin Wolf
2012-09-18 9:48 ` Paolo Bonzini
2012-09-18 9:55 ` Kevin Wolf
2012-09-19 10:16 ` [Qemu-devel] " Daniel P. Berrange
2012-09-19 10:22 ` Paolo Bonzini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1346079626-16386-10-git-send-email-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=kwolf@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).