hw/arm/iotkit: Refactor into abstract base class and subclass
[qemu/ar7.git] / blockdev-nbd.c
blobd73ac1b026af0bcd755236ff8f92514b59613576
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(cioc, nbd_server->tlscreds, NULL,
40 nbd_blockdev_client_closed);
44 static void nbd_server_free(NBDServerData *server)
46 if (!server) {
47 return;
50 qio_net_listener_disconnect(server->listener);
51 object_unref(OBJECT(server->listener));
52 if (server->tlscreds) {
53 object_unref(OBJECT(server->tlscreds));
56 g_free(server);
59 static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp)
61 Object *obj;
62 QCryptoTLSCreds *creds;
64 obj = object_resolve_path_component(
65 object_get_objects_root(), id);
66 if (!obj) {
67 error_setg(errp, "No TLS credentials with id '%s'",
68 id);
69 return NULL;
71 creds = (QCryptoTLSCreds *)
72 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
73 if (!creds) {
74 error_setg(errp, "Object with id '%s' is not TLS credentials",
75 id);
76 return NULL;
79 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
80 error_setg(errp,
81 "Expecting TLS credentials with a server endpoint");
82 return NULL;
84 object_ref(obj);
85 return creds;
89 void nbd_server_start(SocketAddress *addr, const char *tls_creds,
90 Error **errp)
92 if (nbd_server) {
93 error_setg(errp, "NBD server already running");
94 return;
97 nbd_server = g_new0(NBDServerData, 1);
98 nbd_server->listener = qio_net_listener_new();
100 qio_net_listener_set_name(nbd_server->listener,
101 "nbd-listener");
103 if (qio_net_listener_open_sync(nbd_server->listener, addr, errp) < 0) {
104 goto error;
107 if (tls_creds) {
108 nbd_server->tlscreds = nbd_get_tls_creds(tls_creds, errp);
109 if (!nbd_server->tlscreds) {
110 goto error;
113 /* TODO SOCKET_ADDRESS_TYPE_FD where fd has AF_INET or AF_INET6 */
114 if (addr->type != SOCKET_ADDRESS_TYPE_INET) {
115 error_setg(errp, "TLS is only supported with IPv4/IPv6");
116 goto error;
120 qio_net_listener_set_client_func(nbd_server->listener,
121 nbd_accept,
122 NULL,
123 NULL);
125 return;
127 error:
128 nbd_server_free(nbd_server);
129 nbd_server = NULL;
132 void qmp_nbd_server_start(SocketAddressLegacy *addr,
133 bool has_tls_creds, const char *tls_creds,
134 Error **errp)
136 SocketAddress *addr_flat = socket_address_flatten(addr);
138 nbd_server_start(addr_flat, tls_creds, errp);
139 qapi_free_SocketAddress(addr_flat);
142 void qmp_nbd_server_add(const char *device, bool has_name, const char *name,
143 bool has_writable, bool writable,
144 bool has_bitmap, const char *bitmap, Error **errp)
146 BlockDriverState *bs = NULL;
147 BlockBackend *on_eject_blk;
148 NBDExport *exp;
149 int64_t len;
151 if (!nbd_server) {
152 error_setg(errp, "NBD server not running");
153 return;
156 if (!has_name) {
157 name = device;
160 if (nbd_export_find(name)) {
161 error_setg(errp, "NBD server already has export named '%s'", name);
162 return;
165 on_eject_blk = blk_by_name(device);
167 bs = bdrv_lookup_bs(device, device, errp);
168 if (!bs) {
169 return;
172 len = bdrv_getlength(bs);
173 if (len < 0) {
174 error_setg_errno(errp, -len,
175 "Failed to determine the NBD export's length");
176 return;
179 if (!has_writable) {
180 writable = false;
182 if (bdrv_is_read_only(bs)) {
183 writable = false;
186 exp = nbd_export_new(bs, 0, len, name, NULL, bitmap,
187 writable ? 0 : NBD_FLAG_READ_ONLY,
188 NULL, false, on_eject_blk, errp);
189 if (!exp) {
190 return;
193 /* The list of named exports has a strong reference to this export now and
194 * our only way of accessing it is through nbd_export_find(), so we can drop
195 * the strong reference that is @exp. */
196 nbd_export_put(exp);
199 void qmp_nbd_server_remove(const char *name,
200 bool has_mode, NbdServerRemoveMode mode,
201 Error **errp)
203 NBDExport *exp;
205 if (!nbd_server) {
206 error_setg(errp, "NBD server not running");
207 return;
210 exp = nbd_export_find(name);
211 if (exp == NULL) {
212 error_setg(errp, "Export '%s' is not found", name);
213 return;
216 if (!has_mode) {
217 mode = NBD_SERVER_REMOVE_MODE_SAFE;
220 nbd_export_remove(exp, mode, errp);
223 void qmp_nbd_server_stop(Error **errp)
225 if (!nbd_server) {
226 error_setg(errp, "NBD server not running");
227 return;
230 nbd_export_close_all();
232 nbd_server_free(nbd_server);
233 nbd_server = NULL;