blob: 1a95d89f0096a848eab3d2a1d13daade6b0d2227 [file] [log] [blame]
Paolo Bonzini6dd844d2012-08-22 16:43:07 +02001/*
2 * Serving QEMU block devices via NBD
3 *
4 * Copyright (c) 2012 Red Hat, Inc.
5 *
6 * Author: Paolo Bonzini <pbonzini@redhat.com>
7 *
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.
10 */
11
Peter Maydelld38ea872016-01-29 17:50:05 +000012#include "qemu/osdep.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010013#include "sysemu/blockdev.h"
Max Reitze1401772014-11-18 12:21:17 +010014#include "sysemu/block-backend.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010015#include "hw/block/block.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010016#include "qapi/error.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010017#include "qapi/qapi-commands-block.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010018#include "block/nbd.h"
Daniel P. Berrangeae398272016-02-10 18:41:03 +000019#include "io/channel-socket.h"
Daniel P. Berrange862172f2017-12-18 10:16:42 +000020#include "io/net-listener.h"
Paolo Bonzini6dd844d2012-08-22 16:43:07 +020021
Daniel P. Berrangeddffee32016-02-10 18:41:14 +000022typedef struct NBDServerData {
Daniel P. Berrange862172f2017-12-18 10:16:42 +000023 QIONetListener *listener;
Daniel P. Berrangeddffee32016-02-10 18:41:14 +000024 QCryptoTLSCreds *tlscreds;
Daniel P. Berrange00019452019-02-27 16:20:34 +000025 char *tlsauthz;
Daniel P. Berrangeddffee32016-02-10 18:41:14 +000026} NBDServerData;
27
28static NBDServerData *nbd_server;
29
Eric Blake0c9390d2017-06-08 17:26:17 -050030static void nbd_blockdev_client_closed(NBDClient *client, bool ignored)
31{
32 nbd_client_put(client);
33}
Paolo Bonzini6dd844d2012-08-22 16:43:07 +020034
Daniel P. Berrange862172f2017-12-18 10:16:42 +000035static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc,
36 gpointer opaque)
Paolo Bonzini6dd844d2012-08-22 16:43:07 +020037{
Daniel P. Berrange0d73f722016-09-30 11:57:14 +010038 qio_channel_set_name(QIO_CHANNEL(cioc), "nbd-server");
Daniel P. Berrange00019452019-02-27 16:20:34 +000039 nbd_client_new(cioc, nbd_server->tlscreds, nbd_server->tlsauthz,
Eric Blake0c9390d2017-06-08 17:26:17 -050040 nbd_blockdev_client_closed);
Paolo Bonzini6dd844d2012-08-22 16:43:07 +020041}
42
Daniel P. Berrangeddffee32016-02-10 18:41:14 +000043
44static void nbd_server_free(NBDServerData *server)
Paolo Bonzini6dd844d2012-08-22 16:43:07 +020045{
Daniel P. Berrangeddffee32016-02-10 18:41:14 +000046 if (!server) {
47 return;
48 }
49
Daniel P. Berrange862172f2017-12-18 10:16:42 +000050 qio_net_listener_disconnect(server->listener);
51 object_unref(OBJECT(server->listener));
Daniel P. Berrangeddffee32016-02-10 18:41:14 +000052 if (server->tlscreds) {
53 object_unref(OBJECT(server->tlscreds));
54 }
Daniel P. Berrange00019452019-02-27 16:20:34 +000055 g_free(server->tlsauthz);
Daniel P. Berrangeddffee32016-02-10 18:41:14 +000056
57 g_free(server);
58}
59
60static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp)
61{
62 Object *obj;
63 QCryptoTLSCreds *creds;
64
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;
71 }
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;
78 }
79
80 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
81 error_setg(errp,
82 "Expecting TLS credentials with a server endpoint");
83 return NULL;
84 }
85 object_ref(obj);
86 return creds;
87}
88
89
Markus Armbrusterbd269eb2017-04-26 09:36:41 +020090void nbd_server_start(SocketAddress *addr, const char *tls_creds,
Daniel P. Berrange00019452019-02-27 16:20:34 +000091 const char *tls_authz, Error **errp)
Daniel P. Berrangeddffee32016-02-10 18:41:14 +000092{
93 if (nbd_server) {
Paolo Bonzini6dd844d2012-08-22 16:43:07 +020094 error_setg(errp, "NBD server already running");
95 return;
96 }
97
Daniel P. Berrangeddffee32016-02-10 18:41:14 +000098 nbd_server = g_new0(NBDServerData, 1);
Daniel P. Berrange862172f2017-12-18 10:16:42 +000099 nbd_server->listener = qio_net_listener_new();
100
101 qio_net_listener_set_name(nbd_server->listener,
102 "nbd-listener");
103
Juan Quintelafc8135c2019-08-19 18:08:21 +0200104 if (qio_net_listener_open_sync(nbd_server->listener, addr, 1, errp) < 0) {
Daniel P. Berrangeddffee32016-02-10 18:41:14 +0000105 goto error;
Paolo Bonzini6dd844d2012-08-22 16:43:07 +0200106 }
Daniel P. Berrangeae398272016-02-10 18:41:03 +0000107
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200108 if (tls_creds) {
Daniel P. Berrangeddffee32016-02-10 18:41:14 +0000109 nbd_server->tlscreds = nbd_get_tls_creds(tls_creds, errp);
110 if (!nbd_server->tlscreds) {
111 goto error;
112 }
113
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200114 /* TODO SOCKET_ADDRESS_TYPE_FD where fd has AF_INET or AF_INET6 */
115 if (addr->type != SOCKET_ADDRESS_TYPE_INET) {
Daniel P. Berrangeddffee32016-02-10 18:41:14 +0000116 error_setg(errp, "TLS is only supported with IPv4/IPv6");
117 goto error;
118 }
119 }
120
Daniel P. Berrange00019452019-02-27 16:20:34 +0000121 nbd_server->tlsauthz = g_strdup(tls_authz);
122
Daniel P. Berrange862172f2017-12-18 10:16:42 +0000123 qio_net_listener_set_client_func(nbd_server->listener,
124 nbd_accept,
125 NULL,
126 NULL);
Daniel P. Berrangeddffee32016-02-10 18:41:14 +0000127
128 return;
129
130 error:
131 nbd_server_free(nbd_server);
132 nbd_server = NULL;
Paolo Bonzini6dd844d2012-08-22 16:43:07 +0200133}
134
Kevin Wolfeed8b692020-02-24 15:29:57 +0100135void nbd_server_start_options(NbdServerOptions *arg, Error **errp)
136{
137 nbd_server_start(arg->addr, arg->tls_creds, arg->tls_authz, errp);
138}
139
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200140void qmp_nbd_server_start(SocketAddressLegacy *addr,
141 bool has_tls_creds, const char *tls_creds,
Daniel P. Berrange00019452019-02-27 16:20:34 +0000142 bool has_tls_authz, const char *tls_authz,
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200143 Error **errp)
144{
145 SocketAddress *addr_flat = socket_address_flatten(addr);
146
Daniel P. Berrange00019452019-02-27 16:20:34 +0000147 nbd_server_start(addr_flat, tls_creds, tls_authz, errp);
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200148 qapi_free_SocketAddress(addr_flat);
149}
150
Kevin Wolfc62d24e2020-02-24 15:29:58 +0100151void qmp_nbd_server_add(BlockExportNbd *arg, Error **errp)
Paolo Bonzini6dd844d2012-08-22 16:43:07 +0200152{
Kevin Wolf094138d2016-07-06 14:15:51 +0200153 BlockDriverState *bs = NULL;
154 BlockBackend *on_eject_blk;
Paolo Bonzini6dd844d2012-08-22 16:43:07 +0200155 NBDExport *exp;
Eric Blake7596bbb2019-01-17 13:36:42 -0600156 int64_t len;
Eric Blake61bc8462019-09-16 21:39:17 -0500157 AioContext *aio_context;
Paolo Bonzini6dd844d2012-08-22 16:43:07 +0200158
Daniel P. Berrangeddffee32016-02-10 18:41:14 +0000159 if (!nbd_server) {
Paolo Bonzini17b6be42012-11-12 14:25:17 +0100160 error_setg(errp, "NBD server not running");
161 return;
162 }
163
Kevin Wolfc62d24e2020-02-24 15:29:58 +0100164 if (!arg->has_name) {
165 arg->name = arg->device;
Vladimir Sementsov-Ogievskiy902a1f92018-01-19 16:57:15 +0300166 }
167
Kevin Wolfc62d24e2020-02-24 15:29:58 +0100168 if (strlen(arg->name) > NBD_MAX_STRING_SIZE) {
169 error_setg(errp, "export name '%s' too long", arg->name);
Eric Blake93676c82019-11-13 20:46:34 -0600170 return;
171 }
172
Kevin Wolfc62d24e2020-02-24 15:29:58 +0100173 if (arg->description && strlen(arg->description) > NBD_MAX_STRING_SIZE) {
174 error_setg(errp, "description '%s' too long", arg->description);
Eric Blakedeb6ccb2019-11-13 20:46:35 -0600175 return;
176 }
177
Kevin Wolfc62d24e2020-02-24 15:29:58 +0100178 if (nbd_export_find(arg->name)) {
179 error_setg(errp, "NBD server already has export named '%s'", arg->name);
Paolo Bonzini6dd844d2012-08-22 16:43:07 +0200180 return;
181 }
182
Kevin Wolfc62d24e2020-02-24 15:29:58 +0100183 on_eject_blk = blk_by_name(arg->device);
Kevin Wolf094138d2016-07-06 14:15:51 +0200184
Kevin Wolfc62d24e2020-02-24 15:29:58 +0100185 bs = bdrv_lookup_bs(arg->device, arg->device, errp);
Kevin Wolf094138d2016-07-06 14:15:51 +0200186 if (!bs) {
Hani Benhabiles60fe4fa2014-05-18 11:50:04 +0100187 return;
188 }
Paolo Bonzini6dd844d2012-08-22 16:43:07 +0200189
Eric Blake61bc8462019-09-16 21:39:17 -0500190 aio_context = bdrv_get_aio_context(bs);
191 aio_context_acquire(aio_context);
Eric Blake7596bbb2019-01-17 13:36:42 -0600192 len = bdrv_getlength(bs);
193 if (len < 0) {
194 error_setg_errno(errp, -len,
195 "Failed to determine the NBD export's length");
Eric Blake61bc8462019-09-16 21:39:17 -0500196 goto out;
Eric Blake7596bbb2019-01-17 13:36:42 -0600197 }
198
Kevin Wolfc62d24e2020-02-24 15:29:58 +0100199 if (!arg->has_writable) {
200 arg->writable = false;
Paolo Bonzinie6444732012-11-04 12:43:37 +0100201 }
Kevin Wolf094138d2016-07-06 14:15:51 +0200202 if (bdrv_is_read_only(bs)) {
Kevin Wolfc62d24e2020-02-24 15:29:58 +0100203 arg->writable = false;
Paolo Bonzinie6444732012-11-04 12:43:37 +0100204 }
205
Kevin Wolfc62d24e2020-02-24 15:29:58 +0100206 exp = nbd_export_new(bs, 0, len, arg->name, arg->description, arg->bitmap,
207 !arg->writable, !arg->writable,
Kevin Wolf094138d2016-07-06 14:15:51 +0200208 NULL, false, on_eject_blk, errp);
Max Reitz98f44bb2015-02-25 13:08:21 -0500209 if (!exp) {
Eric Blake61bc8462019-09-16 21:39:17 -0500210 goto out;
Max Reitz98f44bb2015-02-25 13:08:21 -0500211 }
Paolo Bonzini6dd844d2012-08-22 16:43:07 +0200212
Max Reitz741cc432016-01-29 16:36:06 +0100213 /* The list of named exports has a strong reference to this export now and
214 * our only way of accessing it is through nbd_export_find(), so we can drop
215 * the strong reference that is @exp. */
216 nbd_export_put(exp);
Eric Blake61bc8462019-09-16 21:39:17 -0500217
218 out:
219 aio_context_release(aio_context);
Paolo Bonzini6dd844d2012-08-22 16:43:07 +0200220}
221
Vladimir Sementsov-Ogievskiya3b0dc72018-01-19 16:57:16 +0300222void qmp_nbd_server_remove(const char *name,
223 bool has_mode, NbdServerRemoveMode mode,
224 Error **errp)
225{
226 NBDExport *exp;
Eric Blake61bc8462019-09-16 21:39:17 -0500227 AioContext *aio_context;
Vladimir Sementsov-Ogievskiya3b0dc72018-01-19 16:57:16 +0300228
229 if (!nbd_server) {
230 error_setg(errp, "NBD server not running");
231 return;
232 }
233
234 exp = nbd_export_find(name);
235 if (exp == NULL) {
236 error_setg(errp, "Export '%s' is not found", name);
237 return;
238 }
239
240 if (!has_mode) {
241 mode = NBD_SERVER_REMOVE_MODE_SAFE;
242 }
243
Eric Blake61bc8462019-09-16 21:39:17 -0500244 aio_context = nbd_export_aio_context(exp);
245 aio_context_acquire(aio_context);
Vladimir Sementsov-Ogievskiya3b0dc72018-01-19 16:57:16 +0300246 nbd_export_remove(exp, mode, errp);
Eric Blake61bc8462019-09-16 21:39:17 -0500247 aio_context_release(aio_context);
Vladimir Sementsov-Ogievskiya3b0dc72018-01-19 16:57:16 +0300248}
249
Paolo Bonzini6dd844d2012-08-22 16:43:07 +0200250void qmp_nbd_server_stop(Error **errp)
251{
Eric Blake7801c3a2019-01-11 13:47:14 -0600252 if (!nbd_server) {
253 error_setg(errp, "NBD server not running");
254 return;
255 }
256
Max Reitz741cc432016-01-29 16:36:06 +0100257 nbd_export_close_all();
Paolo Bonzini6dd844d2012-08-22 16:43:07 +0200258
Daniel P. Berrangeddffee32016-02-10 18:41:14 +0000259 nbd_server_free(nbd_server);
260 nbd_server = NULL;
Paolo Bonzini6dd844d2012-08-22 16:43:07 +0200261}