tools/virtiofsd: xattr name mapping examples
[qemu/ar7.git] / blockdev-nbd.c
blob8174023e5c4741d588d7be1153b751ccdc19d4fd
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-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;
25 char *tlsauthz;
26 uint32_t max_connections;
27 uint32_t connections;
28 } NBDServerData;
30 static NBDServerData *nbd_server;
31 static bool is_qemu_nbd;
33 static void nbd_update_server_watch(NBDServerData *s);
35 void nbd_server_is_qemu_nbd(bool value)
37 is_qemu_nbd = value;
40 bool nbd_server_is_running(void)
42 return nbd_server || is_qemu_nbd;
45 static void nbd_blockdev_client_closed(NBDClient *client, bool ignored)
47 nbd_client_put(client);
48 assert(nbd_server->connections > 0);
49 nbd_server->connections--;
50 nbd_update_server_watch(nbd_server);
53 static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc,
54 gpointer opaque)
56 nbd_server->connections++;
57 nbd_update_server_watch(nbd_server);
59 qio_channel_set_name(QIO_CHANNEL(cioc), "nbd-server");
60 nbd_client_new(cioc, nbd_server->tlscreds, nbd_server->tlsauthz,
61 nbd_blockdev_client_closed);
64 static void nbd_update_server_watch(NBDServerData *s)
66 if (!s->max_connections || s->connections < s->max_connections) {
67 qio_net_listener_set_client_func(s->listener, nbd_accept, NULL, NULL);
68 } else {
69 qio_net_listener_set_client_func(s->listener, NULL, NULL, NULL);
73 static void nbd_server_free(NBDServerData *server)
75 if (!server) {
76 return;
79 qio_net_listener_disconnect(server->listener);
80 object_unref(OBJECT(server->listener));
81 if (server->tlscreds) {
82 object_unref(OBJECT(server->tlscreds));
84 g_free(server->tlsauthz);
86 g_free(server);
89 static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp)
91 Object *obj;
92 QCryptoTLSCreds *creds;
94 obj = object_resolve_path_component(
95 object_get_objects_root(), id);
96 if (!obj) {
97 error_setg(errp, "No TLS credentials with id '%s'",
98 id);
99 return NULL;
101 creds = (QCryptoTLSCreds *)
102 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
103 if (!creds) {
104 error_setg(errp, "Object with id '%s' is not TLS credentials",
105 id);
106 return NULL;
109 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
110 error_setg(errp,
111 "Expecting TLS credentials with a server endpoint");
112 return NULL;
114 object_ref(obj);
115 return creds;
119 void nbd_server_start(SocketAddress *addr, const char *tls_creds,
120 const char *tls_authz, uint32_t max_connections,
121 Error **errp)
123 if (nbd_server) {
124 error_setg(errp, "NBD server already running");
125 return;
128 nbd_server = g_new0(NBDServerData, 1);
129 nbd_server->max_connections = max_connections;
130 nbd_server->listener = qio_net_listener_new();
132 qio_net_listener_set_name(nbd_server->listener,
133 "nbd-listener");
135 if (qio_net_listener_open_sync(nbd_server->listener, addr, 1, errp) < 0) {
136 goto error;
139 if (tls_creds) {
140 nbd_server->tlscreds = nbd_get_tls_creds(tls_creds, errp);
141 if (!nbd_server->tlscreds) {
142 goto error;
145 /* TODO SOCKET_ADDRESS_TYPE_FD where fd has AF_INET or AF_INET6 */
146 if (addr->type != SOCKET_ADDRESS_TYPE_INET) {
147 error_setg(errp, "TLS is only supported with IPv4/IPv6");
148 goto error;
152 nbd_server->tlsauthz = g_strdup(tls_authz);
154 nbd_update_server_watch(nbd_server);
156 return;
158 error:
159 nbd_server_free(nbd_server);
160 nbd_server = NULL;
163 void nbd_server_start_options(NbdServerOptions *arg, Error **errp)
165 nbd_server_start(arg->addr, arg->tls_creds, arg->tls_authz,
166 arg->max_connections, errp);
169 void qmp_nbd_server_start(SocketAddressLegacy *addr,
170 bool has_tls_creds, const char *tls_creds,
171 bool has_tls_authz, const char *tls_authz,
172 bool has_max_connections, uint32_t max_connections,
173 Error **errp)
175 SocketAddress *addr_flat = socket_address_flatten(addr);
177 nbd_server_start(addr_flat, tls_creds, tls_authz, max_connections, errp);
178 qapi_free_SocketAddress(addr_flat);
181 void qmp_nbd_server_add(NbdServerAddOptions *arg, Error **errp)
183 BlockExport *export;
184 BlockDriverState *bs;
185 BlockBackend *on_eject_blk;
186 BlockExportOptions *export_opts;
188 bs = bdrv_lookup_bs(arg->device, arg->device, errp);
189 if (!bs) {
190 return;
194 * block-export-add would default to the node-name, but we may have to use
195 * the device name as a default here for compatibility.
197 if (!arg->has_name) {
198 arg->name = arg->device;
201 export_opts = g_new(BlockExportOptions, 1);
202 *export_opts = (BlockExportOptions) {
203 .type = BLOCK_EXPORT_TYPE_NBD,
204 .id = g_strdup(arg->name),
205 .node_name = g_strdup(bdrv_get_node_name(bs)),
206 .has_writable = arg->has_writable,
207 .writable = arg->writable,
208 .u.nbd = {
209 .has_name = true,
210 .name = g_strdup(arg->name),
211 .has_description = arg->has_description,
212 .description = g_strdup(arg->description),
213 .has_bitmap = arg->has_bitmap,
214 .bitmap = g_strdup(arg->bitmap),
219 * nbd-server-add doesn't complain when a read-only device should be
220 * exported as writable, but simply downgrades it. This is an error with
221 * block-export-add.
223 if (bdrv_is_read_only(bs)) {
224 export_opts->has_writable = true;
225 export_opts->writable = false;
228 export = blk_exp_add(export_opts, errp);
229 if (!export) {
230 goto fail;
234 * nbd-server-add removes the export when the named BlockBackend used for
235 * @device goes away.
237 on_eject_blk = blk_by_name(arg->device);
238 if (on_eject_blk) {
239 nbd_export_set_on_eject_blk(export, on_eject_blk);
242 fail:
243 qapi_free_BlockExportOptions(export_opts);
246 void qmp_nbd_server_remove(const char *name,
247 bool has_mode, BlockExportRemoveMode mode,
248 Error **errp)
250 BlockExport *exp;
252 exp = blk_exp_find(name);
253 if (exp && exp->drv->type != BLOCK_EXPORT_TYPE_NBD) {
254 error_setg(errp, "Block export '%s' is not an NBD export", name);
255 return;
258 qmp_block_export_del(name, has_mode, mode, errp);
261 void qmp_nbd_server_stop(Error **errp)
263 if (!nbd_server) {
264 error_setg(errp, "NBD server not running");
265 return;
268 blk_exp_close_all_type(BLOCK_EXPORT_TYPE_NBD);
270 nbd_server_free(nbd_server);
271 nbd_server = NULL;