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/qmp/qerror.h"
17 #include "sysemu/sysemu.h"
18 #include "qmp-commands.h"
19 #include "block/nbd.h"
20 #include "io/channel-socket.h"
22 typedef struct NBDServerData
{
23 QIOChannelSocket
*listen_ioc
;
25 QCryptoTLSCreds
*tlscreds
;
28 static NBDServerData
*nbd_server
;
31 static gboolean
nbd_accept(QIOChannel
*ioc
, GIOCondition condition
,
34 QIOChannelSocket
*cioc
;
40 cioc
= qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc
),
46 qio_channel_set_name(QIO_CHANNEL(cioc
), "nbd-server");
47 nbd_client_new(NULL
, cioc
,
48 nbd_server
->tlscreds
, NULL
,
50 object_unref(OBJECT(cioc
));
55 static void nbd_server_free(NBDServerData
*server
)
61 if (server
->watch
!= -1) {
62 g_source_remove(server
->watch
);
64 object_unref(OBJECT(server
->listen_ioc
));
65 if (server
->tlscreds
) {
66 object_unref(OBJECT(server
->tlscreds
));
72 static QCryptoTLSCreds
*nbd_get_tls_creds(const char *id
, Error
**errp
)
75 QCryptoTLSCreds
*creds
;
77 obj
= object_resolve_path_component(
78 object_get_objects_root(), id
);
80 error_setg(errp
, "No TLS credentials with id '%s'",
84 creds
= (QCryptoTLSCreds
*)
85 object_dynamic_cast(obj
, TYPE_QCRYPTO_TLS_CREDS
);
87 error_setg(errp
, "Object with id '%s' is not TLS credentials",
92 if (creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
94 "Expecting TLS credentials with a server endpoint");
102 void nbd_server_start(SocketAddress
*addr
, const char *tls_creds
,
106 error_setg(errp
, "NBD server already running");
110 nbd_server
= g_new0(NBDServerData
, 1);
111 nbd_server
->watch
= -1;
112 nbd_server
->listen_ioc
= qio_channel_socket_new();
113 qio_channel_set_name(QIO_CHANNEL(nbd_server
->listen_ioc
),
115 if (qio_channel_socket_listen_sync(
116 nbd_server
->listen_ioc
, addr
, errp
) < 0) {
121 nbd_server
->tlscreds
= nbd_get_tls_creds(tls_creds
, errp
);
122 if (!nbd_server
->tlscreds
) {
126 /* TODO SOCKET_ADDRESS_TYPE_FD where fd has AF_INET or AF_INET6 */
127 if (addr
->type
!= SOCKET_ADDRESS_TYPE_INET
) {
128 error_setg(errp
, "TLS is only supported with IPv4/IPv6");
133 nbd_server
->watch
= qio_channel_add_watch(
134 QIO_CHANNEL(nbd_server
->listen_ioc
),
143 nbd_server_free(nbd_server
);
147 void qmp_nbd_server_start(SocketAddressLegacy
*addr
,
148 bool has_tls_creds
, const char *tls_creds
,
151 SocketAddress
*addr_flat
= socket_address_flatten(addr
);
153 nbd_server_start(addr_flat
, tls_creds
, errp
);
154 qapi_free_SocketAddress(addr_flat
);
157 void qmp_nbd_server_add(const char *device
, bool has_writable
, bool writable
,
160 BlockDriverState
*bs
= NULL
;
161 BlockBackend
*on_eject_blk
;
165 error_setg(errp
, "NBD server not running");
169 if (nbd_export_find(device
)) {
170 error_setg(errp
, "NBD server already exporting device '%s'", device
);
174 on_eject_blk
= blk_by_name(device
);
176 bs
= bdrv_lookup_bs(device
, device
, errp
);
184 if (bdrv_is_read_only(bs
)) {
188 exp
= nbd_export_new(bs
, 0, -1, writable
? 0 : NBD_FLAG_READ_ONLY
,
189 NULL
, false, on_eject_blk
, errp
);
194 nbd_export_set_name(exp
, device
);
196 /* The list of named exports has a strong reference to this export now and
197 * our only way of accessing it is through nbd_export_find(), so we can drop
198 * the strong reference that is @exp. */
202 void qmp_nbd_server_stop(Error
**errp
)
204 nbd_export_close_all();
206 nbd_server_free(nbd_server
);