]>
Commit | Line | Data |
---|---|---|
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" |
8675cbd6 EB |
17 | #include "qapi/clone-visitor.h" |
18 | #include "qapi/qapi-visit-block-export.h" | |
5daa6bfd | 19 | #include "qapi/qapi-commands-block-export.h" |
737e150e | 20 | #include "block/nbd.h" |
ae398278 | 21 | #include "io/channel-socket.h" |
862172f4 | 22 | #include "io/net-listener.h" |
6dd844db | 23 | |
ddffee39 | 24 | typedef struct NBDServerData { |
862172f4 | 25 | QIONetListener *listener; |
ddffee39 | 26 | QCryptoTLSCreds *tlscreds; |
00019455 | 27 | char *tlsauthz; |
1c8222b0 KW |
28 | uint32_t max_connections; |
29 | uint32_t connections; | |
ddffee39 DB |
30 | } NBDServerData; |
31 | ||
32 | static NBDServerData *nbd_server; | |
00917172 | 33 | static bool is_qemu_nbd; |
ddffee39 | 34 | |
1c8222b0 KW |
35 | static void nbd_update_server_watch(NBDServerData *s); |
36 | ||
00917172 KW |
37 | void nbd_server_is_qemu_nbd(bool value) |
38 | { | |
39 | is_qemu_nbd = value; | |
40 | } | |
41 | ||
5b1cb497 KW |
42 | bool nbd_server_is_running(void) |
43 | { | |
44 | return nbd_server || is_qemu_nbd; | |
45 | } | |
46 | ||
0c9390d9 EB |
47 | static void nbd_blockdev_client_closed(NBDClient *client, bool ignored) |
48 | { | |
49 | nbd_client_put(client); | |
1c8222b0 KW |
50 | assert(nbd_server->connections > 0); |
51 | nbd_server->connections--; | |
52 | nbd_update_server_watch(nbd_server); | |
0c9390d9 | 53 | } |
6dd844db | 54 | |
862172f4 DB |
55 | static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc, |
56 | gpointer opaque) | |
6dd844db | 57 | { |
1c8222b0 KW |
58 | nbd_server->connections++; |
59 | nbd_update_server_watch(nbd_server); | |
60 | ||
0d73f725 | 61 | qio_channel_set_name(QIO_CHANNEL(cioc), "nbd-server"); |
00019455 | 62 | nbd_client_new(cioc, nbd_server->tlscreds, nbd_server->tlsauthz, |
0c9390d9 | 63 | nbd_blockdev_client_closed); |
6dd844db PB |
64 | } |
65 | ||
1c8222b0 KW |
66 | static void nbd_update_server_watch(NBDServerData *s) |
67 | { | |
68 | if (!s->max_connections || s->connections < s->max_connections) { | |
69 | qio_net_listener_set_client_func(s->listener, nbd_accept, NULL, NULL); | |
70 | } else { | |
71 | qio_net_listener_set_client_func(s->listener, NULL, NULL, NULL); | |
72 | } | |
73 | } | |
ddffee39 DB |
74 | |
75 | static void nbd_server_free(NBDServerData *server) | |
6dd844db | 76 | { |
ddffee39 | 77 | if (!server) { |
6dd844db PB |
78 | return; |
79 | } | |
80 | ||
862172f4 DB |
81 | qio_net_listener_disconnect(server->listener); |
82 | object_unref(OBJECT(server->listener)); | |
ddffee39 DB |
83 | if (server->tlscreds) { |
84 | object_unref(OBJECT(server->tlscreds)); | |
85 | } | |
00019455 | 86 | g_free(server->tlsauthz); |
ddffee39 DB |
87 | |
88 | g_free(server); | |
89 | } | |
90 | ||
91 | static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp) | |
92 | { | |
93 | Object *obj; | |
94 | QCryptoTLSCreds *creds; | |
95 | ||
96 | obj = object_resolve_path_component( | |
97 | object_get_objects_root(), id); | |
98 | if (!obj) { | |
99 | error_setg(errp, "No TLS credentials with id '%s'", | |
100 | id); | |
101 | return NULL; | |
102 | } | |
103 | creds = (QCryptoTLSCreds *) | |
104 | object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS); | |
105 | if (!creds) { | |
106 | error_setg(errp, "Object with id '%s' is not TLS credentials", | |
107 | id); | |
108 | return NULL; | |
109 | } | |
110 | ||
7b3b6168 PMD |
111 | if (!qcrypto_tls_creds_check_endpoint(creds, |
112 | QCRYPTO_TLS_CREDS_ENDPOINT_SERVER, | |
113 | errp)) { | |
ddffee39 DB |
114 | return NULL; |
115 | } | |
116 | object_ref(obj); | |
117 | return creds; | |
118 | } | |
119 | ||
120 | ||
bd269ebc | 121 | void nbd_server_start(SocketAddress *addr, const char *tls_creds, |
1c8222b0 KW |
122 | const char *tls_authz, uint32_t max_connections, |
123 | Error **errp) | |
ddffee39 DB |
124 | { |
125 | if (nbd_server) { | |
126 | error_setg(errp, "NBD server already running"); | |
ae398278 | 127 | return; |
6dd844db | 128 | } |
ae398278 | 129 | |
ddffee39 | 130 | nbd_server = g_new0(NBDServerData, 1); |
1c8222b0 | 131 | nbd_server->max_connections = max_connections; |
862172f4 DB |
132 | nbd_server->listener = qio_net_listener_new(); |
133 | ||
134 | qio_net_listener_set_name(nbd_server->listener, | |
135 | "nbd-listener"); | |
136 | ||
582d4210 EB |
137 | /* |
138 | * Because this server is persistent, a backlog of SOMAXCONN is | |
139 | * better than trying to size it to max_connections. | |
140 | */ | |
141 | if (qio_net_listener_open_sync(nbd_server->listener, addr, SOMAXCONN, | |
142 | errp) < 0) { | |
ddffee39 DB |
143 | goto error; |
144 | } | |
145 | ||
bd269ebc | 146 | if (tls_creds) { |
ddffee39 DB |
147 | nbd_server->tlscreds = nbd_get_tls_creds(tls_creds, errp); |
148 | if (!nbd_server->tlscreds) { | |
149 | goto error; | |
150 | } | |
151 | ||
bd269ebc MA |
152 | /* TODO SOCKET_ADDRESS_TYPE_FD where fd has AF_INET or AF_INET6 */ |
153 | if (addr->type != SOCKET_ADDRESS_TYPE_INET) { | |
ddffee39 DB |
154 | error_setg(errp, "TLS is only supported with IPv4/IPv6"); |
155 | goto error; | |
156 | } | |
157 | } | |
158 | ||
00019455 DB |
159 | nbd_server->tlsauthz = g_strdup(tls_authz); |
160 | ||
1c8222b0 | 161 | nbd_update_server_watch(nbd_server); |
ddffee39 DB |
162 | |
163 | return; | |
164 | ||
165 | error: | |
166 | nbd_server_free(nbd_server); | |
167 | nbd_server = NULL; | |
6dd844db PB |
168 | } |
169 | ||
eed8b691 KW |
170 | void nbd_server_start_options(NbdServerOptions *arg, Error **errp) |
171 | { | |
1c8222b0 KW |
172 | nbd_server_start(arg->addr, arg->tls_creds, arg->tls_authz, |
173 | arg->max_connections, errp); | |
eed8b691 KW |
174 | } |
175 | ||
bd269ebc MA |
176 | void qmp_nbd_server_start(SocketAddressLegacy *addr, |
177 | bool has_tls_creds, const char *tls_creds, | |
00019455 | 178 | bool has_tls_authz, const char *tls_authz, |
1c8222b0 | 179 | bool has_max_connections, uint32_t max_connections, |
bd269ebc MA |
180 | Error **errp) |
181 | { | |
182 | SocketAddress *addr_flat = socket_address_flatten(addr); | |
183 | ||
1c8222b0 | 184 | nbd_server_start(addr_flat, tls_creds, tls_authz, max_connections, errp); |
bd269ebc MA |
185 | qapi_free_SocketAddress(addr_flat); |
186 | } | |
187 | ||
b6076afc | 188 | void qmp_nbd_server_add(NbdServerAddOptions *arg, Error **errp) |
56ee8626 | 189 | { |
9b562c64 KW |
190 | BlockExport *export; |
191 | BlockDriverState *bs; | |
192 | BlockBackend *on_eject_blk; | |
b6076afc | 193 | BlockExportOptions *export_opts; |
9b562c64 KW |
194 | |
195 | bs = bdrv_lookup_bs(arg->device, arg->device, errp); | |
196 | if (!bs) { | |
197 | return; | |
198 | } | |
199 | ||
b6076afc KW |
200 | /* |
201 | * block-export-add would default to the node-name, but we may have to use | |
202 | * the device name as a default here for compatibility. | |
203 | */ | |
204 | if (!arg->has_name) { | |
8675cbd6 EB |
205 | arg->has_name = true; |
206 | arg->name = g_strdup(arg->device); | |
b6076afc KW |
207 | } |
208 | ||
209 | export_opts = g_new(BlockExportOptions, 1); | |
210 | *export_opts = (BlockExportOptions) { | |
211 | .type = BLOCK_EXPORT_TYPE_NBD, | |
d53be9ce | 212 | .id = g_strdup(arg->name), |
b6076afc | 213 | .node_name = g_strdup(bdrv_get_node_name(bs)), |
30dbc81d KW |
214 | .has_writable = arg->has_writable, |
215 | .writable = arg->writable, | |
56ee8626 | 216 | }; |
cbad81ce | 217 | QAPI_CLONE_MEMBERS(BlockExportOptionsNbdBase, &export_opts->u.nbd, |
8675cbd6 | 218 | qapi_NbdServerAddOptions_base(arg)); |
cbad81ce EB |
219 | if (arg->has_bitmap) { |
220 | export_opts->u.nbd.has_bitmaps = true; | |
221 | QAPI_LIST_PREPEND(export_opts->u.nbd.bitmaps, g_strdup(arg->bitmap)); | |
222 | } | |
9b562c64 KW |
223 | |
224 | /* | |
225 | * nbd-server-add doesn't complain when a read-only device should be | |
226 | * exported as writable, but simply downgrades it. This is an error with | |
227 | * block-export-add. | |
228 | */ | |
229 | if (bdrv_is_read_only(bs)) { | |
30dbc81d KW |
230 | export_opts->has_writable = true; |
231 | export_opts->writable = false; | |
9b562c64 KW |
232 | } |
233 | ||
b6076afc | 234 | export = blk_exp_add(export_opts, errp); |
9b562c64 | 235 | if (!export) { |
b6076afc | 236 | goto fail; |
9b562c64 KW |
237 | } |
238 | ||
239 | /* | |
240 | * nbd-server-add removes the export when the named BlockBackend used for | |
241 | * @device goes away. | |
242 | */ | |
243 | on_eject_blk = blk_by_name(arg->device); | |
244 | if (on_eject_blk) { | |
245 | nbd_export_set_on_eject_blk(export, on_eject_blk); | |
246 | } | |
b6076afc KW |
247 | |
248 | fail: | |
249 | qapi_free_BlockExportOptions(export_opts); | |
6dd844db PB |
250 | } |
251 | ||
a3b0dc75 | 252 | void qmp_nbd_server_remove(const char *name, |
3c3bc462 | 253 | bool has_mode, BlockExportRemoveMode mode, |
a3b0dc75 VSO |
254 | Error **errp) |
255 | { | |
3c3bc462 | 256 | BlockExport *exp; |
a3b0dc75 | 257 | |
3c3bc462 KW |
258 | exp = blk_exp_find(name); |
259 | if (exp && exp->drv->type != BLOCK_EXPORT_TYPE_NBD) { | |
260 | error_setg(errp, "Block export '%s' is not an NBD export", name); | |
a3b0dc75 VSO |
261 | return; |
262 | } | |
263 | ||
3c3bc462 | 264 | qmp_block_export_del(name, has_mode, mode, errp); |
a3b0dc75 VSO |
265 | } |
266 | ||
6dd844db PB |
267 | void qmp_nbd_server_stop(Error **errp) |
268 | { | |
7801c3a7 EB |
269 | if (!nbd_server) { |
270 | error_setg(errp, "NBD server not running"); | |
271 | return; | |
272 | } | |
273 | ||
bc4ee65b | 274 | blk_exp_close_all_type(BLOCK_EXPORT_TYPE_NBD); |
6dd844db | 275 | |
ddffee39 DB |
276 | nbd_server_free(nbd_server); |
277 | nbd_server = NULL; | |
6dd844db | 278 | } |