Merge remote-tracking branch 'remotes/vivier/tags/m68k-for-2.12-pull-request' into...
[qemu.git] / blockdev-nbd.c
blob65a84739edc7ec005a62b33f074e61352217a682
1 /*
2 * Serving QEMU block devices via NBD
4 * Copyright (c) 2012 Red Hat, Inc.
6 * Author: Paolo Bonzini <pbonzini@redhat.com>
8 * This work is licensed under the terms of the GNU GPL, version 2 or
9 * later. See the COPYING file in the top-level directory.
12 #include "qemu/osdep.h"
13 #include "sysemu/blockdev.h"
14 #include "sysemu/block-backend.h"
15 #include "hw/block/block.h"
16 #include "qapi/error.h"
17 #include "qapi/qapi-commands-block.h"
18 #include "sysemu/sysemu.h"
19 #include "block/nbd.h"
20 #include "io/channel-socket.h"
21 #include "io/net-listener.h"
23 typedef struct NBDServerData {
24 QIONetListener *listener;
25 QCryptoTLSCreds *tlscreds;
26 } NBDServerData;
28 static NBDServerData *nbd_server;
30 static void nbd_blockdev_client_closed(NBDClient *client, bool ignored)
32 nbd_client_put(client);
35 static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc,
36 gpointer opaque)
38 qio_channel_set_name(QIO_CHANNEL(cioc), "nbd-server");
39 nbd_client_new(NULL, cioc,
40 nbd_server->tlscreds, NULL,
41 nbd_blockdev_client_closed);
45 static void nbd_server_free(NBDServerData *server)
47 if (!server) {
48 return;
51 qio_net_listener_disconnect(server->listener);
52 object_unref(OBJECT(server->listener));
53 if (server->tlscreds) {
54 object_unref(OBJECT(server->tlscreds));
57 g_free(server);
60 static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp)
62 Object *obj;
63 QCryptoTLSCreds *creds;
65 obj = object_resolve_path_component(
66 object_get_objects_root(), id);
67 if (!obj) {
68 error_setg(errp, "No TLS credentials with id '%s'",
69 id);
70 return NULL;
72 creds = (QCryptoTLSCreds *)
73 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
74 if (!creds) {
75 error_setg(errp, "Object with id '%s' is not TLS credentials",
76 id);
77 return NULL;
80 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
81 error_setg(errp,
82 "Expecting TLS credentials with a server endpoint");
83 return NULL;
85 object_ref(obj);
86 return creds;
90 void nbd_server_start(SocketAddress *addr, const char *tls_creds,
91 Error **errp)
93 if (nbd_server) {
94 error_setg(errp, "NBD server already running");
95 return;
98 nbd_server = g_new0(NBDServerData, 1);
99 nbd_server->listener = qio_net_listener_new();
101 qio_net_listener_set_name(nbd_server->listener,
102 "nbd-listener");
104 if (qio_net_listener_open_sync(nbd_server->listener, addr, errp) < 0) {
105 goto error;
108 if (tls_creds) {
109 nbd_server->tlscreds = nbd_get_tls_creds(tls_creds, errp);
110 if (!nbd_server->tlscreds) {
111 goto error;
114 /* TODO SOCKET_ADDRESS_TYPE_FD where fd has AF_INET or AF_INET6 */
115 if (addr->type != SOCKET_ADDRESS_TYPE_INET) {
116 error_setg(errp, "TLS is only supported with IPv4/IPv6");
117 goto error;
121 qio_net_listener_set_client_func(nbd_server->listener,
122 nbd_accept,
123 NULL,
124 NULL);
126 return;
128 error:
129 nbd_server_free(nbd_server);
130 nbd_server = NULL;
133 void qmp_nbd_server_start(SocketAddressLegacy *addr,
134 bool has_tls_creds, const char *tls_creds,
135 Error **errp)
137 SocketAddress *addr_flat = socket_address_flatten(addr);
139 nbd_server_start(addr_flat, tls_creds, errp);
140 qapi_free_SocketAddress(addr_flat);
143 void qmp_nbd_server_add(const char *device, bool has_name, const char *name,
144 bool has_writable, bool writable, Error **errp)
146 BlockDriverState *bs = NULL;
147 BlockBackend *on_eject_blk;
148 NBDExport *exp;
150 if (!nbd_server) {
151 error_setg(errp, "NBD server not running");
152 return;
155 if (!has_name) {
156 name = device;
159 if (nbd_export_find(name)) {
160 error_setg(errp, "NBD server already has export named '%s'", name);
161 return;
164 on_eject_blk = blk_by_name(device);
166 bs = bdrv_lookup_bs(device, device, errp);
167 if (!bs) {
168 return;
171 if (!has_writable) {
172 writable = false;
174 if (bdrv_is_read_only(bs)) {
175 writable = false;
178 exp = nbd_export_new(bs, 0, -1, writable ? 0 : NBD_FLAG_READ_ONLY,
179 NULL, false, on_eject_blk, errp);
180 if (!exp) {
181 return;
184 nbd_export_set_name(exp, name);
186 /* The list of named exports has a strong reference to this export now and
187 * our only way of accessing it is through nbd_export_find(), so we can drop
188 * the strong reference that is @exp. */
189 nbd_export_put(exp);
192 void qmp_nbd_server_remove(const char *name,
193 bool has_mode, NbdServerRemoveMode mode,
194 Error **errp)
196 NBDExport *exp;
198 if (!nbd_server) {
199 error_setg(errp, "NBD server not running");
200 return;
203 exp = nbd_export_find(name);
204 if (exp == NULL) {
205 error_setg(errp, "Export '%s' is not found", name);
206 return;
209 if (!has_mode) {
210 mode = NBD_SERVER_REMOVE_MODE_SAFE;
213 nbd_export_remove(exp, mode, errp);
216 void qmp_nbd_server_stop(Error **errp)
218 nbd_export_close_all();
220 nbd_server_free(nbd_server);
221 nbd_server = NULL;