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-export.h"
18 #include "block/nbd.h"
19 #include "io/channel-socket.h"
20 #include "io/net-listener.h"
22 typedef struct NBDServerData
{
23 QIONetListener
*listener
;
24 QCryptoTLSCreds
*tlscreds
;
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
,
38 qio_channel_set_name(QIO_CHANNEL(cioc
), "nbd-server");
39 nbd_client_new(cioc
, nbd_server
->tlscreds
, nbd_server
->tlsauthz
,
40 nbd_blockdev_client_closed
);
44 static void nbd_server_free(NBDServerData
*server
)
50 qio_net_listener_disconnect(server
->listener
);
51 object_unref(OBJECT(server
->listener
));
52 if (server
->tlscreds
) {
53 object_unref(OBJECT(server
->tlscreds
));
55 g_free(server
->tlsauthz
);
60 static QCryptoTLSCreds
*nbd_get_tls_creds(const char *id
, Error
**errp
)
63 QCryptoTLSCreds
*creds
;
65 obj
= object_resolve_path_component(
66 object_get_objects_root(), id
);
68 error_setg(errp
, "No TLS credentials with id '%s'",
72 creds
= (QCryptoTLSCreds
*)
73 object_dynamic_cast(obj
, TYPE_QCRYPTO_TLS_CREDS
);
75 error_setg(errp
, "Object with id '%s' is not TLS credentials",
80 if (creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
82 "Expecting TLS credentials with a server endpoint");
90 void nbd_server_start(SocketAddress
*addr
, const char *tls_creds
,
91 const char *tls_authz
, Error
**errp
)
94 error_setg(errp
, "NBD server already running");
98 nbd_server
= g_new0(NBDServerData
, 1);
99 nbd_server
->listener
= qio_net_listener_new();
101 qio_net_listener_set_name(nbd_server
->listener
,
104 if (qio_net_listener_open_sync(nbd_server
->listener
, addr
, 1, errp
) < 0) {
109 nbd_server
->tlscreds
= nbd_get_tls_creds(tls_creds
, errp
);
110 if (!nbd_server
->tlscreds
) {
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");
121 nbd_server
->tlsauthz
= g_strdup(tls_authz
);
123 qio_net_listener_set_client_func(nbd_server
->listener
,
131 nbd_server_free(nbd_server
);
135 void nbd_server_start_options(NbdServerOptions
*arg
, Error
**errp
)
137 nbd_server_start(arg
->addr
, arg
->tls_creds
, arg
->tls_authz
, errp
);
140 void qmp_nbd_server_start(SocketAddressLegacy
*addr
,
141 bool has_tls_creds
, const char *tls_creds
,
142 bool has_tls_authz
, const char *tls_authz
,
145 SocketAddress
*addr_flat
= socket_address_flatten(addr
);
147 nbd_server_start(addr_flat
, tls_creds
, tls_authz
, errp
);
148 qapi_free_SocketAddress(addr_flat
);
151 BlockExport
*nbd_export_create(BlockExportOptions
*exp_args
, Error
**errp
)
153 BlockExportOptionsNbd
*arg
= &exp_args
->u
.nbd
;
154 BlockDriverState
*bs
= NULL
;
155 BlockBackend
*on_eject_blk
;
156 NBDExport
*exp
= NULL
;
157 AioContext
*aio_context
;
159 assert(exp_args
->type
== BLOCK_EXPORT_TYPE_NBD
);
162 error_setg(errp
, "NBD server not running");
166 if (!arg
->has_name
) {
167 arg
->name
= arg
->device
;
170 if (strlen(arg
->name
) > NBD_MAX_STRING_SIZE
) {
171 error_setg(errp
, "export name '%s' too long", arg
->name
);
175 if (arg
->description
&& strlen(arg
->description
) > NBD_MAX_STRING_SIZE
) {
176 error_setg(errp
, "description '%s' too long", arg
->description
);
180 if (nbd_export_find(arg
->name
)) {
181 error_setg(errp
, "NBD server already has export named '%s'", arg
->name
);
185 on_eject_blk
= blk_by_name(arg
->device
);
187 bs
= bdrv_lookup_bs(arg
->device
, arg
->device
, errp
);
192 aio_context
= bdrv_get_aio_context(bs
);
193 aio_context_acquire(aio_context
);
195 if (!arg
->has_writable
) {
196 arg
->writable
= false;
198 if (bdrv_is_read_only(bs
)) {
199 arg
->writable
= false;
202 exp
= nbd_export_new(bs
, arg
->name
, arg
->description
, arg
->bitmap
,
203 !arg
->writable
, !arg
->writable
,
204 NULL
, false, on_eject_blk
, errp
);
209 /* The list of named exports has a strong reference to this export now and
210 * our only way of accessing it is through nbd_export_find(), so we can drop
211 * the strong reference that is @exp. */
215 aio_context_release(aio_context
);
216 /* TODO Remove the cast: nbd_export_new() will return a BlockExport. */
217 return (BlockExport
*) exp
;
220 void qmp_nbd_server_add(BlockExportOptionsNbd
*arg
, Error
**errp
)
222 BlockExportOptions export
= {
223 .type
= BLOCK_EXPORT_TYPE_NBD
,
226 qmp_block_export_add(&export
, errp
);
229 void qmp_nbd_server_remove(const char *name
,
230 bool has_mode
, NbdServerRemoveMode mode
,
234 AioContext
*aio_context
;
237 error_setg(errp
, "NBD server not running");
241 exp
= nbd_export_find(name
);
243 error_setg(errp
, "Export '%s' is not found", name
);
248 mode
= NBD_SERVER_REMOVE_MODE_SAFE
;
251 aio_context
= nbd_export_aio_context(exp
);
252 aio_context_acquire(aio_context
);
253 nbd_export_remove(exp
, mode
, errp
);
254 aio_context_release(aio_context
);
257 void qmp_nbd_server_stop(Error
**errp
)
260 error_setg(errp
, "NBD server not running");
264 nbd_export_close_all();
266 nbd_server_free(nbd_server
);