]> git.proxmox.com Git - mirror_qemu.git/blame - blockdev-nbd.c
Add getsockopt for settable SOL_IPV6 options
[mirror_qemu.git] / blockdev-nbd.c
CommitLineData
6dd844db
PB
1/*
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
d38ea87a 12#include "qemu/osdep.h"
9c17d615 13#include "sysemu/blockdev.h"
e140177d 14#include "sysemu/block-backend.h"
0d09e41a 15#include "hw/block/block.h"
e688df6b 16#include "qapi/error.h"
9af23989 17#include "qapi/qapi-commands-block.h"
9c17d615 18#include "sysemu/sysemu.h"
737e150e 19#include "block/nbd.h"
ae398278 20#include "io/channel-socket.h"
862172f4 21#include "io/net-listener.h"
6dd844db 22
ddffee39 23typedef struct NBDServerData {
862172f4 24 QIONetListener *listener;
ddffee39
DB
25 QCryptoTLSCreds *tlscreds;
26} NBDServerData;
27
28static NBDServerData *nbd_server;
29
0c9390d9
EB
30static void nbd_blockdev_client_closed(NBDClient *client, bool ignored)
31{
32 nbd_client_put(client);
33}
6dd844db 34
862172f4
DB
35static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc,
36 gpointer opaque)
6dd844db 37{
0d73f725 38 qio_channel_set_name(QIO_CHANNEL(cioc), "nbd-server");
7f7dfe2a 39 nbd_client_new(cioc, nbd_server->tlscreds, NULL,
0c9390d9 40 nbd_blockdev_client_closed);
6dd844db
PB
41}
42
ddffee39
DB
43
44static void nbd_server_free(NBDServerData *server)
6dd844db 45{
ddffee39 46 if (!server) {
6dd844db
PB
47 return;
48 }
49
862172f4
DB
50 qio_net_listener_disconnect(server->listener);
51 object_unref(OBJECT(server->listener));
ddffee39
DB
52 if (server->tlscreds) {
53 object_unref(OBJECT(server->tlscreds));
54 }
55
56 g_free(server);
57}
58
59static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp)
60{
61 Object *obj;
62 QCryptoTLSCreds *creds;
63
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;
70 }
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;
77 }
78
79 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
80 error_setg(errp,
81 "Expecting TLS credentials with a server endpoint");
82 return NULL;
83 }
84 object_ref(obj);
85 return creds;
86}
87
88
bd269ebc
MA
89void nbd_server_start(SocketAddress *addr, const char *tls_creds,
90 Error **errp)
ddffee39
DB
91{
92 if (nbd_server) {
93 error_setg(errp, "NBD server already running");
ae398278 94 return;
6dd844db 95 }
ae398278 96
ddffee39 97 nbd_server = g_new0(NBDServerData, 1);
862172f4
DB
98 nbd_server->listener = qio_net_listener_new();
99
100 qio_net_listener_set_name(nbd_server->listener,
101 "nbd-listener");
102
103 if (qio_net_listener_open_sync(nbd_server->listener, addr, errp) < 0) {
ddffee39
DB
104 goto error;
105 }
106
bd269ebc 107 if (tls_creds) {
ddffee39
DB
108 nbd_server->tlscreds = nbd_get_tls_creds(tls_creds, errp);
109 if (!nbd_server->tlscreds) {
110 goto error;
111 }
112
bd269ebc
MA
113 /* TODO SOCKET_ADDRESS_TYPE_FD where fd has AF_INET or AF_INET6 */
114 if (addr->type != SOCKET_ADDRESS_TYPE_INET) {
ddffee39
DB
115 error_setg(errp, "TLS is only supported with IPv4/IPv6");
116 goto error;
117 }
118 }
119
862172f4
DB
120 qio_net_listener_set_client_func(nbd_server->listener,
121 nbd_accept,
122 NULL,
123 NULL);
ddffee39
DB
124
125 return;
126
127 error:
128 nbd_server_free(nbd_server);
129 nbd_server = NULL;
6dd844db
PB
130}
131
bd269ebc
MA
132void qmp_nbd_server_start(SocketAddressLegacy *addr,
133 bool has_tls_creds, const char *tls_creds,
134 Error **errp)
135{
136 SocketAddress *addr_flat = socket_address_flatten(addr);
137
138 nbd_server_start(addr_flat, tls_creds, errp);
139 qapi_free_SocketAddress(addr_flat);
140}
141
902a1f94
VSO
142void qmp_nbd_server_add(const char *device, bool has_name, const char *name,
143 bool has_writable, bool writable, Error **errp)
6dd844db 144{
094138d0
KW
145 BlockDriverState *bs = NULL;
146 BlockBackend *on_eject_blk;
6dd844db 147 NBDExport *exp;
6dd844db 148
ddffee39 149 if (!nbd_server) {
17b6be4a
PB
150 error_setg(errp, "NBD server not running");
151 return;
152 }
153
902a1f94
VSO
154 if (!has_name) {
155 name = device;
156 }
157
158 if (nbd_export_find(name)) {
159 error_setg(errp, "NBD server already has export named '%s'", name);
6dd844db
PB
160 return;
161 }
162
094138d0
KW
163 on_eject_blk = blk_by_name(device);
164
165 bs = bdrv_lookup_bs(device, device, errp);
166 if (!bs) {
60fe4fac
HB
167 return;
168 }
6dd844db 169
e6444734 170 if (!has_writable) {
f3313d23 171 writable = false;
e6444734 172 }
094138d0 173 if (bdrv_is_read_only(bs)) {
e6444734
PB
174 writable = false;
175 }
176
094138d0
KW
177 exp = nbd_export_new(bs, 0, -1, writable ? 0 : NBD_FLAG_READ_ONLY,
178 NULL, false, on_eject_blk, errp);
98f44bbe
HR
179 if (!exp) {
180 return;
181 }
6dd844db 182
902a1f94 183 nbd_export_set_name(exp, name);
6dd844db 184
741cc431
HR
185 /* The list of named exports has a strong reference to this export now and
186 * our only way of accessing it is through nbd_export_find(), so we can drop
187 * the strong reference that is @exp. */
188 nbd_export_put(exp);
6dd844db
PB
189}
190
a3b0dc75
VSO
191void qmp_nbd_server_remove(const char *name,
192 bool has_mode, NbdServerRemoveMode mode,
193 Error **errp)
194{
195 NBDExport *exp;
196
197 if (!nbd_server) {
198 error_setg(errp, "NBD server not running");
199 return;
200 }
201
202 exp = nbd_export_find(name);
203 if (exp == NULL) {
204 error_setg(errp, "Export '%s' is not found", name);
205 return;
206 }
207
208 if (!has_mode) {
209 mode = NBD_SERVER_REMOVE_MODE_SAFE;
210 }
211
212 nbd_export_remove(exp, mode, errp);
213}
214
6dd844db
PB
215void qmp_nbd_server_stop(Error **errp)
216{
741cc431 217 nbd_export_close_all();
6dd844db 218
ddffee39
DB
219 nbd_server_free(nbd_server);
220 nbd_server = NULL;
6dd844db 221}
767f0c7d
VSO
222
223void qmp_x_nbd_server_add_bitmap(const char *name, const char *bitmap,
224 bool has_bitmap_export_name,
225 const char *bitmap_export_name,
226 Error **errp)
227{
228 NBDExport *exp;
229
230 if (!nbd_server) {
231 error_setg(errp, "NBD server not running");
232 return;
233 }
234
235 exp = nbd_export_find(name);
236 if (exp == NULL) {
237 error_setg(errp, "Export '%s' is not found", name);
238 return;
239 }
240
241 nbd_export_bitmap(exp, bitmap,
242 has_bitmap_export_name ? bitmap_export_name : bitmap,
243 errp);
244}