]> git.proxmox.com Git - mirror_qemu.git/blame - block/nbd.c
nbd: convert block client to use I/O channels for connection setup
[mirror_qemu.git] / block / nbd.c
CommitLineData
75818250
TS
1/*
2 * QEMU Block driver for NBD
3 *
4 * Copyright (C) 2008 Bull S.A.S.
bd5921b4 5 * Author: Laurent Vivier <Laurent.Vivier@bull.net>
75818250
TS
6 *
7 * Some parts:
8 * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * THE SOFTWARE.
27 */
28
80c71a24 29#include "qemu/osdep.h"
2302c1ca 30#include "block/nbd-client.h"
1de7afc9 31#include "qemu/uri.h"
737e150e 32#include "block/block_int.h"
1de7afc9 33#include "qemu/module.h"
2019d68b 34#include "qapi/qmp/qdict.h"
f53a1feb
KW
35#include "qapi/qmp/qjson.h"
36#include "qapi/qmp/qint.h"
2019d68b 37#include "qapi/qmp/qstring.h"
75818250 38
75818250 39
1d45f8b5
LV
40#define EN_OPTSTR ":exportname="
41
75818250 42typedef struct BDRVNBDState {
2302c1ca 43 NbdClientSession client;
75818250
TS
44} BDRVNBDState;
45
f53a1feb 46static int nbd_parse_uri(const char *filename, QDict *options)
1d7d2a9d
PB
47{
48 URI *uri;
49 const char *p;
50 QueryParams *qp = NULL;
51 int ret = 0;
f53a1feb 52 bool is_unix;
1d7d2a9d
PB
53
54 uri = uri_parse(filename);
55 if (!uri) {
56 return -EINVAL;
57 }
58
59 /* transport */
60 if (!strcmp(uri->scheme, "nbd")) {
f53a1feb 61 is_unix = false;
1d7d2a9d 62 } else if (!strcmp(uri->scheme, "nbd+tcp")) {
f53a1feb 63 is_unix = false;
1d7d2a9d 64 } else if (!strcmp(uri->scheme, "nbd+unix")) {
f53a1feb 65 is_unix = true;
1d7d2a9d
PB
66 } else {
67 ret = -EINVAL;
68 goto out;
69 }
70
71 p = uri->path ? uri->path : "/";
72 p += strspn(p, "/");
73 if (p[0]) {
f53a1feb 74 qdict_put(options, "export", qstring_from_str(p));
1d7d2a9d
PB
75 }
76
77 qp = query_params_parse(uri->query);
f53a1feb 78 if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) {
1d7d2a9d
PB
79 ret = -EINVAL;
80 goto out;
81 }
82
f53a1feb 83 if (is_unix) {
1d7d2a9d
PB
84 /* nbd+unix:///export?socket=path */
85 if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
86 ret = -EINVAL;
87 goto out;
88 }
f53a1feb 89 qdict_put(options, "path", qstring_from_str(qp->p[0].value));
1d7d2a9d 90 } else {
23307908 91 QString *host;
bebbf7fa 92 /* nbd[+tcp]://host[:port]/export */
1d7d2a9d
PB
93 if (!uri->server) {
94 ret = -EINVAL;
95 goto out;
96 }
f17c90be 97
23307908
JT
98 /* strip braces from literal IPv6 address */
99 if (uri->server[0] == '[') {
100 host = qstring_from_substr(uri->server, 1,
101 strlen(uri->server) - 2);
102 } else {
103 host = qstring_from_str(uri->server);
104 }
105
106 qdict_put(options, "host", host);
bebbf7fa
KW
107 if (uri->port) {
108 char* port_str = g_strdup_printf("%d", uri->port);
109 qdict_put(options, "port", qstring_from_str(port_str));
110 g_free(port_str);
111 }
1d7d2a9d
PB
112 }
113
114out:
115 if (qp) {
116 query_params_free(qp);
117 }
118 uri_free(uri);
119 return ret;
120}
121
6963a30d
KW
122static void nbd_parse_filename(const char *filename, QDict *options,
123 Error **errp)
75818250 124{
1d45f8b5 125 char *file;
33897dc7
NT
126 char *export_name;
127 const char *host_spec;
75818250 128 const char *unixpath;
75818250 129
681e7ad0
KW
130 if (qdict_haskey(options, "host")
131 || qdict_haskey(options, "port")
132 || qdict_haskey(options, "path"))
133 {
134 error_setg(errp, "host/port/path and a file name may not be specified "
135 "at the same time");
136 return;
137 }
138
1d7d2a9d 139 if (strstr(filename, "://")) {
6963a30d
KW
140 int ret = nbd_parse_uri(filename, options);
141 if (ret < 0) {
142 error_setg(errp, "No valid URL specified");
143 }
144 return;
1d7d2a9d
PB
145 }
146
7267c094 147 file = g_strdup(filename);
1d45f8b5 148
33897dc7
NT
149 export_name = strstr(file, EN_OPTSTR);
150 if (export_name) {
151 if (export_name[strlen(EN_OPTSTR)] == 0) {
1d45f8b5
LV
152 goto out;
153 }
33897dc7
NT
154 export_name[0] = 0; /* truncate 'file' */
155 export_name += strlen(EN_OPTSTR);
f53a1feb
KW
156
157 qdict_put(options, "export", qstring_from_str(export_name));
1d45f8b5
LV
158 }
159
33897dc7
NT
160 /* extract the host_spec - fail if it's not nbd:... */
161 if (!strstart(file, "nbd:", &host_spec)) {
6963a30d 162 error_setg(errp, "File name string for NBD must start with 'nbd:'");
1d45f8b5
LV
163 goto out;
164 }
75818250 165
f53a1feb 166 if (!*host_spec) {
f53a1feb
KW
167 goto out;
168 }
169
33897dc7
NT
170 /* are we a UNIX or TCP socket? */
171 if (strstart(host_spec, "unix:", &unixpath)) {
f53a1feb 172 qdict_put(options, "path", qstring_from_str(unixpath));
75818250 173 } else {
f53a1feb
KW
174 InetSocketAddress *addr = NULL;
175
6963a30d 176 addr = inet_parse(host_spec, errp);
92de9012 177 if (!addr) {
f17c90be
KW
178 goto out;
179 }
75818250 180
f53a1feb
KW
181 qdict_put(options, "host", qstring_from_str(addr->host));
182 qdict_put(options, "port", qstring_from_str(addr->port));
183 qapi_free_InetSocketAddress(addr);
184 }
75818250 185
33897dc7 186out:
7267c094 187 g_free(file);
f53a1feb
KW
188}
189
7a5ed437
DB
190static SocketAddress *nbd_config(BDRVNBDState *s, QDict *options, char **export,
191 Error **errp)
f53a1feb 192{
7a5ed437 193 SocketAddress *saddr;
f53a1feb 194
a69d9af4
PB
195 if (qdict_haskey(options, "path") == qdict_haskey(options, "host")) {
196 if (qdict_haskey(options, "path")) {
77e8b9ca 197 error_setg(errp, "path and host may not be used at the same time.");
a69d9af4 198 } else {
77e8b9ca 199 error_setg(errp, "one of path and host must be specified.");
681e7ad0 200 }
7a5ed437 201 return NULL;
33897dc7 202 }
f53a1feb 203
7a5ed437 204 saddr = g_new0(SocketAddress, 1);
f53a1feb 205
7a5ed437 206 if (qdict_haskey(options, "path")) {
6a8f9661
EB
207 saddr->type = SOCKET_ADDRESS_KIND_UNIX;
208 saddr->u.q_unix = g_new0(UnixSocketAddress, 1);
209 saddr->u.q_unix->path = g_strdup(qdict_get_str(options, "path"));
7a5ed437
DB
210 qdict_del(options, "path");
211 } else {
6a8f9661
EB
212 saddr->type = SOCKET_ADDRESS_KIND_INET;
213 saddr->u.inet = g_new0(InetSocketAddress, 1);
214 saddr->u.inet->host = g_strdup(qdict_get_str(options, "host"));
7a5ed437 215 if (!qdict_get_try_str(options, "port")) {
6a8f9661 216 saddr->u.inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
7a5ed437 217 } else {
6a8f9661 218 saddr->u.inet->port = g_strdup(qdict_get_str(options, "port"));
7a5ed437
DB
219 }
220 qdict_del(options, "host");
221 qdict_del(options, "port");
f53a1feb
KW
222 }
223
6a8f9661 224 s->client.is_unix = saddr->type == SOCKET_ADDRESS_KIND_UNIX;
bebbf7fa 225
e2bc625f
MAL
226 *export = g_strdup(qdict_get_try_str(options, "export"));
227 if (*export) {
f53a1feb
KW
228 qdict_del(options, "export");
229 }
7a5ed437
DB
230
231 return saddr;
33897dc7 232}
1d45f8b5 233
f53a829b
HR
234NbdClientSession *nbd_get_client_session(BlockDriverState *bs)
235{
236 BDRVNBDState *s = bs->opaque;
237 return &s->client;
238}
239
064097d9
DB
240static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr,
241 Error **errp)
33897dc7 242{
064097d9
DB
243 QIOChannelSocket *sioc;
244 Error *local_err = NULL;
75818250 245
064097d9 246 sioc = qio_channel_socket_new();
75818250 247
064097d9
DB
248 qio_channel_socket_connect_sync(sioc,
249 saddr,
250 &local_err);
251 if (local_err) {
252 error_propagate(errp, local_err);
253 return NULL;
1d45f8b5 254 }
75818250 255
064097d9 256 qio_channel_set_delay(QIO_CHANNEL(sioc), false);
7a5ed437 257
064097d9 258 return sioc;
33897dc7
NT
259}
260
015a1036
HR
261static int nbd_open(BlockDriverState *bs, QDict *options, int flags,
262 Error **errp)
33897dc7
NT
263{
264 BDRVNBDState *s = bs->opaque;
e2bc625f 265 char *export = NULL;
064097d9
DB
266 int result;
267 QIOChannelSocket *sioc;
7a5ed437 268 SocketAddress *saddr;
ae255e52 269
33897dc7 270 /* Pop the config into our state object. Exit if invalid. */
7a5ed437
DB
271 saddr = nbd_config(s, options, &export, errp);
272 if (!saddr) {
77e8b9ca 273 return -EINVAL;
33897dc7
NT
274 }
275
276 /* establish TCP connection, return error if it fails
277 * TODO: Configurable retry-until-timeout behaviour.
278 */
064097d9 279 sioc = nbd_establish_connection(saddr, errp);
7a5ed437 280 qapi_free_SocketAddress(saddr);
064097d9 281 if (!sioc) {
9d0b65e6 282 g_free(export);
064097d9 283 return -ECONNREFUSED;
2c7989a9
PB
284 }
285
2302c1ca 286 /* NBD handshake */
064097d9
DB
287 result = nbd_client_init(bs, sioc, export, errp);
288 object_unref(OBJECT(sioc));
e2bc625f
MAL
289 g_free(export);
290 return result;
e183ef75
PB
291}
292
d9b09f13
PB
293static int nbd_co_readv(BlockDriverState *bs, int64_t sector_num,
294 int nb_sectors, QEMUIOVector *qiov)
295{
f53a829b 296 return nbd_client_co_readv(bs, sector_num, nb_sectors, qiov);
d9b09f13
PB
297}
298
299static int nbd_co_writev(BlockDriverState *bs, int64_t sector_num,
300 int nb_sectors, QEMUIOVector *qiov)
301{
f53a829b 302 return nbd_client_co_writev(bs, sector_num, nb_sectors, qiov);
d9b09f13
PB
303}
304
1486d04a
PB
305static int nbd_co_flush(BlockDriverState *bs)
306{
f53a829b 307 return nbd_client_co_flush(bs);
1486d04a
PB
308}
309
fa21e6fa
DL
310static void nbd_refresh_limits(BlockDriverState *bs, Error **errp)
311{
312 bs->bl.max_discard = UINT32_MAX >> BDRV_SECTOR_BITS;
313 bs->bl.max_transfer_length = UINT32_MAX >> BDRV_SECTOR_BITS;
314}
315
7a706633
PB
316static int nbd_co_discard(BlockDriverState *bs, int64_t sector_num,
317 int nb_sectors)
318{
f53a829b 319 return nbd_client_co_discard(bs, sector_num, nb_sectors);
7a706633
PB
320}
321
75818250
TS
322static void nbd_close(BlockDriverState *bs)
323{
f53a829b 324 nbd_client_close(bs);
75818250
TS
325}
326
327static int64_t nbd_getlength(BlockDriverState *bs)
328{
329 BDRVNBDState *s = bs->opaque;
330
2302c1ca 331 return s->client.size;
75818250
TS
332}
333
69447cd8
SH
334static void nbd_detach_aio_context(BlockDriverState *bs)
335{
f53a829b 336 nbd_client_detach_aio_context(bs);
69447cd8
SH
337}
338
339static void nbd_attach_aio_context(BlockDriverState *bs,
340 AioContext *new_context)
341{
f53a829b 342 nbd_client_attach_aio_context(bs, new_context);
69447cd8
SH
343}
344
4cdd01d3 345static void nbd_refresh_filename(BlockDriverState *bs, QDict *options)
2019d68b 346{
2019d68b 347 QDict *opts = qdict_new();
4cdd01d3
KW
348 const char *path = qdict_get_try_str(options, "path");
349 const char *host = qdict_get_try_str(options, "host");
350 const char *port = qdict_get_try_str(options, "port");
351 const char *export = qdict_get_try_str(options, "export");
2019d68b
HR
352
353 qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("nbd")));
354
ec0de768 355 if (path && export) {
2019d68b 356 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
ec0de768
HR
357 "nbd+unix:///%s?socket=%s", export, path);
358 } else if (path && !export) {
2019d68b 359 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
ec0de768
HR
360 "nbd+unix://?socket=%s", path);
361 } else if (!path && export && port) {
362 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
363 "nbd://%s:%s/%s", host, port, export);
364 } else if (!path && export && !port) {
365 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
366 "nbd://%s/%s", host, export);
367 } else if (!path && !export && port) {
368 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
369 "nbd://%s:%s", host, port);
370 } else if (!path && !export && !port) {
2019d68b 371 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
ec0de768
HR
372 "nbd://%s", host);
373 }
374
375 if (path) {
376 qdict_put_obj(opts, "path", QOBJECT(qstring_from_str(path)));
377 } else if (port) {
2019d68b
HR
378 qdict_put_obj(opts, "host", QOBJECT(qstring_from_str(host)));
379 qdict_put_obj(opts, "port", QOBJECT(qstring_from_str(port)));
ec0de768
HR
380 } else {
381 qdict_put_obj(opts, "host", QOBJECT(qstring_from_str(host)));
382 }
383 if (export) {
384 qdict_put_obj(opts, "export", QOBJECT(qstring_from_str(export)));
2019d68b
HR
385 }
386
387 bs->full_open_options = opts;
388}
389
5efa9d5a 390static BlockDriver bdrv_nbd = {
69447cd8
SH
391 .format_name = "nbd",
392 .protocol_name = "nbd",
393 .instance_size = sizeof(BDRVNBDState),
394 .bdrv_parse_filename = nbd_parse_filename,
395 .bdrv_file_open = nbd_open,
396 .bdrv_co_readv = nbd_co_readv,
397 .bdrv_co_writev = nbd_co_writev,
398 .bdrv_close = nbd_close,
399 .bdrv_co_flush_to_os = nbd_co_flush,
400 .bdrv_co_discard = nbd_co_discard,
fa21e6fa 401 .bdrv_refresh_limits = nbd_refresh_limits,
69447cd8
SH
402 .bdrv_getlength = nbd_getlength,
403 .bdrv_detach_aio_context = nbd_detach_aio_context,
404 .bdrv_attach_aio_context = nbd_attach_aio_context,
2019d68b 405 .bdrv_refresh_filename = nbd_refresh_filename,
1d7d2a9d
PB
406};
407
408static BlockDriver bdrv_nbd_tcp = {
69447cd8
SH
409 .format_name = "nbd",
410 .protocol_name = "nbd+tcp",
411 .instance_size = sizeof(BDRVNBDState),
412 .bdrv_parse_filename = nbd_parse_filename,
413 .bdrv_file_open = nbd_open,
414 .bdrv_co_readv = nbd_co_readv,
415 .bdrv_co_writev = nbd_co_writev,
416 .bdrv_close = nbd_close,
417 .bdrv_co_flush_to_os = nbd_co_flush,
418 .bdrv_co_discard = nbd_co_discard,
fa21e6fa 419 .bdrv_refresh_limits = nbd_refresh_limits,
69447cd8
SH
420 .bdrv_getlength = nbd_getlength,
421 .bdrv_detach_aio_context = nbd_detach_aio_context,
422 .bdrv_attach_aio_context = nbd_attach_aio_context,
2019d68b 423 .bdrv_refresh_filename = nbd_refresh_filename,
1d7d2a9d
PB
424};
425
426static BlockDriver bdrv_nbd_unix = {
69447cd8
SH
427 .format_name = "nbd",
428 .protocol_name = "nbd+unix",
429 .instance_size = sizeof(BDRVNBDState),
430 .bdrv_parse_filename = nbd_parse_filename,
431 .bdrv_file_open = nbd_open,
432 .bdrv_co_readv = nbd_co_readv,
433 .bdrv_co_writev = nbd_co_writev,
434 .bdrv_close = nbd_close,
435 .bdrv_co_flush_to_os = nbd_co_flush,
436 .bdrv_co_discard = nbd_co_discard,
fa21e6fa 437 .bdrv_refresh_limits = nbd_refresh_limits,
69447cd8
SH
438 .bdrv_getlength = nbd_getlength,
439 .bdrv_detach_aio_context = nbd_detach_aio_context,
440 .bdrv_attach_aio_context = nbd_attach_aio_context,
2019d68b 441 .bdrv_refresh_filename = nbd_refresh_filename,
75818250 442};
5efa9d5a
AL
443
444static void bdrv_nbd_init(void)
445{
446 bdrv_register(&bdrv_nbd);
1d7d2a9d
PB
447 bdrv_register(&bdrv_nbd_tcp);
448 bdrv_register(&bdrv_nbd_unix);
5efa9d5a
AL
449}
450
451block_init(bdrv_nbd_init);