2 * QEMU Block driver for NBD
4 * Copyright (C) 2008 Bull S.A.S.
5 * Author: Laurent Vivier <Laurent.Vivier@bull.net>
8 * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #include "qemu/osdep.h"
30 #include "block/nbd-client.h"
31 #include "qapi/error.h"
33 #include "block/block_int.h"
34 #include "qemu/module.h"
35 #include "qapi-visit.h"
36 #include "qapi/qobject-input-visitor.h"
37 #include "qapi/qobject-output-visitor.h"
38 #include "qapi/qmp/qdict.h"
39 #include "qapi/qmp/qjson.h"
40 #include "qapi/qmp/qint.h"
41 #include "qapi/qmp/qstring.h"
42 #include "qemu/cutils.h"
44 #define EN_OPTSTR ":exportname="
46 typedef struct BDRVNBDState
{
47 NBDClientSession client
;
49 /* For nbd_refresh_filename() */
51 char *export
, *tlscredsid
;
54 static int nbd_parse_uri(const char *filename
, QDict
*options
)
58 QueryParams
*qp
= NULL
;
62 uri
= uri_parse(filename
);
68 if (!strcmp(uri
->scheme
, "nbd")) {
70 } else if (!strcmp(uri
->scheme
, "nbd+tcp")) {
72 } else if (!strcmp(uri
->scheme
, "nbd+unix")) {
79 p
= uri
->path
? uri
->path
: "/";
82 qdict_put(options
, "export", qstring_from_str(p
));
85 qp
= query_params_parse(uri
->query
);
86 if (qp
->n
> 1 || (is_unix
&& !qp
->n
) || (!is_unix
&& qp
->n
)) {
92 /* nbd+unix:///export?socket=path */
93 if (uri
->server
|| uri
->port
|| strcmp(qp
->p
[0].name
, "socket")) {
97 qdict_put(options
, "server.type", qstring_from_str("unix"));
98 qdict_put(options
, "server.data.path",
99 qstring_from_str(qp
->p
[0].value
));
104 /* nbd[+tcp]://host[:port]/export */
110 /* strip braces from literal IPv6 address */
111 if (uri
->server
[0] == '[') {
112 host
= qstring_from_substr(uri
->server
, 1,
113 strlen(uri
->server
) - 2);
115 host
= qstring_from_str(uri
->server
);
118 qdict_put(options
, "server.type", qstring_from_str("inet"));
119 qdict_put(options
, "server.data.host", host
);
121 port_str
= g_strdup_printf("%d", uri
->port
?: NBD_DEFAULT_PORT
);
122 qdict_put(options
, "server.data.port", qstring_from_str(port_str
));
128 query_params_free(qp
);
134 static bool nbd_has_filename_options_conflict(QDict
*options
, Error
**errp
)
138 for (e
= qdict_first(options
); e
; e
= qdict_next(options
, e
)) {
139 if (!strcmp(e
->key
, "host") ||
140 !strcmp(e
->key
, "port") ||
141 !strcmp(e
->key
, "path") ||
142 !strcmp(e
->key
, "export") ||
143 strstart(e
->key
, "server.", NULL
))
145 error_setg(errp
, "Option '%s' cannot be used with a file name",
154 static void nbd_parse_filename(const char *filename
, QDict
*options
,
159 const char *host_spec
;
160 const char *unixpath
;
162 if (nbd_has_filename_options_conflict(options
, errp
)) {
166 if (strstr(filename
, "://")) {
167 int ret
= nbd_parse_uri(filename
, options
);
169 error_setg(errp
, "No valid URL specified");
174 file
= g_strdup(filename
);
176 export_name
= strstr(file
, EN_OPTSTR
);
178 if (export_name
[strlen(EN_OPTSTR
)] == 0) {
181 export_name
[0] = 0; /* truncate 'file' */
182 export_name
+= strlen(EN_OPTSTR
);
184 qdict_put(options
, "export", qstring_from_str(export_name
));
187 /* extract the host_spec - fail if it's not nbd:... */
188 if (!strstart(file
, "nbd:", &host_spec
)) {
189 error_setg(errp
, "File name string for NBD must start with 'nbd:'");
197 /* are we a UNIX or TCP socket? */
198 if (strstart(host_spec
, "unix:", &unixpath
)) {
199 qdict_put(options
, "server.type", qstring_from_str("unix"));
200 qdict_put(options
, "server.data.path", qstring_from_str(unixpath
));
202 InetSocketAddress
*addr
= NULL
;
204 addr
= inet_parse(host_spec
, errp
);
209 qdict_put(options
, "server.type", qstring_from_str("inet"));
210 qdict_put(options
, "server.data.host", qstring_from_str(addr
->host
));
211 qdict_put(options
, "server.data.port", qstring_from_str(addr
->port
));
212 qapi_free_InetSocketAddress(addr
);
219 static bool nbd_process_legacy_socket_options(QDict
*output_options
,
220 QemuOpts
*legacy_opts
,
223 const char *path
= qemu_opt_get(legacy_opts
, "path");
224 const char *host
= qemu_opt_get(legacy_opts
, "host");
225 const char *port
= qemu_opt_get(legacy_opts
, "port");
228 if (!path
&& !host
&& !port
) {
232 for (e
= qdict_first(output_options
); e
; e
= qdict_next(output_options
, e
))
234 if (strstart(e
->key
, "server.", NULL
)) {
235 error_setg(errp
, "Cannot use 'server' and path/host/port at the "
242 error_setg(errp
, "path and host may not be used at the same time");
246 error_setg(errp
, "port may not be used without host");
250 qdict_put(output_options
, "server.type", qstring_from_str("unix"));
251 qdict_put(output_options
, "server.data.path", qstring_from_str(path
));
253 qdict_put(output_options
, "server.type", qstring_from_str("inet"));
254 qdict_put(output_options
, "server.data.host", qstring_from_str(host
));
255 qdict_put(output_options
, "server.data.port",
256 qstring_from_str(port
?: stringify(NBD_DEFAULT_PORT
)));
262 static SocketAddress
*nbd_config(BDRVNBDState
*s
, QDict
*options
, Error
**errp
)
264 SocketAddress
*saddr
= NULL
;
266 QObject
*crumpled_addr
= NULL
;
268 Error
*local_err
= NULL
;
270 qdict_extract_subqdict(options
, &addr
, "server.");
271 if (!qdict_size(addr
)) {
272 error_setg(errp
, "NBD server address missing");
276 crumpled_addr
= qdict_crumple(addr
, errp
);
277 if (!crumpled_addr
) {
281 iv
= qobject_input_visitor_new(crumpled_addr
);
282 visit_type_SocketAddress(iv
, NULL
, &saddr
, &local_err
);
284 error_propagate(errp
, local_err
);
288 s
->client
.is_unix
= saddr
->type
== SOCKET_ADDRESS_KIND_UNIX
;
292 qobject_decref(crumpled_addr
);
297 NBDClientSession
*nbd_get_client_session(BlockDriverState
*bs
)
299 BDRVNBDState
*s
= bs
->opaque
;
303 static QIOChannelSocket
*nbd_establish_connection(SocketAddress
*saddr
,
306 QIOChannelSocket
*sioc
;
307 Error
*local_err
= NULL
;
309 sioc
= qio_channel_socket_new();
310 qio_channel_set_name(QIO_CHANNEL(sioc
), "nbd-client");
312 qio_channel_socket_connect_sync(sioc
,
316 error_propagate(errp
, local_err
);
320 qio_channel_set_delay(QIO_CHANNEL(sioc
), false);
326 static QCryptoTLSCreds
*nbd_get_tls_creds(const char *id
, Error
**errp
)
329 QCryptoTLSCreds
*creds
;
331 obj
= object_resolve_path_component(
332 object_get_objects_root(), id
);
334 error_setg(errp
, "No TLS credentials with id '%s'",
338 creds
= (QCryptoTLSCreds
*)
339 object_dynamic_cast(obj
, TYPE_QCRYPTO_TLS_CREDS
);
341 error_setg(errp
, "Object with id '%s' is not TLS credentials",
346 if (creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
) {
348 "Expecting TLS credentials with a client endpoint");
356 static QemuOptsList nbd_runtime_opts
= {
358 .head
= QTAILQ_HEAD_INITIALIZER(nbd_runtime_opts
.head
),
362 .type
= QEMU_OPT_STRING
,
363 .help
= "TCP host to connect to",
367 .type
= QEMU_OPT_STRING
,
368 .help
= "TCP port to connect to",
372 .type
= QEMU_OPT_STRING
,
373 .help
= "Unix socket path to connect to",
377 .type
= QEMU_OPT_STRING
,
378 .help
= "Name of the NBD export to open",
382 .type
= QEMU_OPT_STRING
,
383 .help
= "ID of the TLS credentials to use",
388 static int nbd_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
391 BDRVNBDState
*s
= bs
->opaque
;
392 QemuOpts
*opts
= NULL
;
393 Error
*local_err
= NULL
;
394 QIOChannelSocket
*sioc
= NULL
;
395 QCryptoTLSCreds
*tlscreds
= NULL
;
396 const char *hostname
= NULL
;
399 opts
= qemu_opts_create(&nbd_runtime_opts
, NULL
, 0, &error_abort
);
400 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
402 error_propagate(errp
, local_err
);
406 /* Translate @host, @port, and @path to a SocketAddress */
407 if (!nbd_process_legacy_socket_options(options
, opts
, errp
)) {
411 /* Pop the config into our state object. Exit if invalid. */
412 s
->saddr
= nbd_config(s
, options
, errp
);
417 s
->export
= g_strdup(qemu_opt_get(opts
, "export"));
419 s
->tlscredsid
= g_strdup(qemu_opt_get(opts
, "tls-creds"));
421 tlscreds
= nbd_get_tls_creds(s
->tlscredsid
, errp
);
426 if (s
->saddr
->type
!= SOCKET_ADDRESS_KIND_INET
) {
427 error_setg(errp
, "TLS only supported over IP sockets");
430 hostname
= s
->saddr
->u
.inet
.data
->host
;
433 /* establish TCP connection, return error if it fails
434 * TODO: Configurable retry-until-timeout behaviour.
436 sioc
= nbd_establish_connection(s
->saddr
, errp
);
443 ret
= nbd_client_init(bs
, sioc
, s
->export
,
444 tlscreds
, hostname
, errp
);
447 object_unref(OBJECT(sioc
));
450 object_unref(OBJECT(tlscreds
));
453 qapi_free_SocketAddress(s
->saddr
);
455 g_free(s
->tlscredsid
);
461 static int nbd_co_flush(BlockDriverState
*bs
)
463 return nbd_client_co_flush(bs
);
466 static void nbd_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
468 bs
->bl
.max_pdiscard
= NBD_MAX_BUFFER_SIZE
;
469 bs
->bl
.max_pwrite_zeroes
= NBD_MAX_BUFFER_SIZE
;
470 bs
->bl
.max_transfer
= NBD_MAX_BUFFER_SIZE
;
473 static void nbd_close(BlockDriverState
*bs
)
475 BDRVNBDState
*s
= bs
->opaque
;
477 nbd_client_close(bs
);
479 qapi_free_SocketAddress(s
->saddr
);
481 g_free(s
->tlscredsid
);
484 static int64_t nbd_getlength(BlockDriverState
*bs
)
486 BDRVNBDState
*s
= bs
->opaque
;
488 return s
->client
.size
;
491 static void nbd_detach_aio_context(BlockDriverState
*bs
)
493 nbd_client_detach_aio_context(bs
);
496 static void nbd_attach_aio_context(BlockDriverState
*bs
,
497 AioContext
*new_context
)
499 nbd_client_attach_aio_context(bs
, new_context
);
502 static void nbd_refresh_filename(BlockDriverState
*bs
, QDict
*options
)
504 BDRVNBDState
*s
= bs
->opaque
;
505 QDict
*opts
= qdict_new();
506 QObject
*saddr_qdict
;
508 const char *host
= NULL
, *port
= NULL
, *path
= NULL
;
510 if (s
->saddr
->type
== SOCKET_ADDRESS_KIND_INET
) {
511 const InetSocketAddress
*inet
= s
->saddr
->u
.inet
.data
;
512 if (!inet
->has_ipv4
&& !inet
->has_ipv6
&& !inet
->has_to
) {
516 } else if (s
->saddr
->type
== SOCKET_ADDRESS_KIND_UNIX
) {
517 path
= s
->saddr
->u
.q_unix
.data
->path
;
520 qdict_put(opts
, "driver", qstring_from_str("nbd"));
522 if (path
&& s
->export
) {
523 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
524 "nbd+unix:///%s?socket=%s", s
->export
, path
);
525 } else if (path
&& !s
->export
) {
526 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
527 "nbd+unix://?socket=%s", path
);
528 } else if (host
&& s
->export
) {
529 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
530 "nbd://%s:%s/%s", host
, port
, s
->export
);
531 } else if (host
&& !s
->export
) {
532 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
533 "nbd://%s:%s", host
, port
);
536 ov
= qobject_output_visitor_new(&saddr_qdict
);
537 visit_type_SocketAddress(ov
, NULL
, &s
->saddr
, &error_abort
);
538 visit_complete(ov
, &saddr_qdict
);
540 qdict_put_obj(opts
, "server", saddr_qdict
);
543 qdict_put(opts
, "export", qstring_from_str(s
->export
));
546 qdict_put(opts
, "tls-creds", qstring_from_str(s
->tlscredsid
));
550 bs
->full_open_options
= opts
;
553 static BlockDriver bdrv_nbd
= {
554 .format_name
= "nbd",
555 .protocol_name
= "nbd",
556 .instance_size
= sizeof(BDRVNBDState
),
557 .bdrv_parse_filename
= nbd_parse_filename
,
558 .bdrv_file_open
= nbd_open
,
559 .bdrv_co_preadv
= nbd_client_co_preadv
,
560 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
561 .bdrv_co_pwrite_zeroes
= nbd_client_co_pwrite_zeroes
,
562 .bdrv_close
= nbd_close
,
563 .bdrv_co_flush_to_os
= nbd_co_flush
,
564 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
565 .bdrv_refresh_limits
= nbd_refresh_limits
,
566 .bdrv_getlength
= nbd_getlength
,
567 .bdrv_detach_aio_context
= nbd_detach_aio_context
,
568 .bdrv_attach_aio_context
= nbd_attach_aio_context
,
569 .bdrv_refresh_filename
= nbd_refresh_filename
,
572 static BlockDriver bdrv_nbd_tcp
= {
573 .format_name
= "nbd",
574 .protocol_name
= "nbd+tcp",
575 .instance_size
= sizeof(BDRVNBDState
),
576 .bdrv_parse_filename
= nbd_parse_filename
,
577 .bdrv_file_open
= nbd_open
,
578 .bdrv_co_preadv
= nbd_client_co_preadv
,
579 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
580 .bdrv_co_pwrite_zeroes
= nbd_client_co_pwrite_zeroes
,
581 .bdrv_close
= nbd_close
,
582 .bdrv_co_flush_to_os
= nbd_co_flush
,
583 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
584 .bdrv_refresh_limits
= nbd_refresh_limits
,
585 .bdrv_getlength
= nbd_getlength
,
586 .bdrv_detach_aio_context
= nbd_detach_aio_context
,
587 .bdrv_attach_aio_context
= nbd_attach_aio_context
,
588 .bdrv_refresh_filename
= nbd_refresh_filename
,
591 static BlockDriver bdrv_nbd_unix
= {
592 .format_name
= "nbd",
593 .protocol_name
= "nbd+unix",
594 .instance_size
= sizeof(BDRVNBDState
),
595 .bdrv_parse_filename
= nbd_parse_filename
,
596 .bdrv_file_open
= nbd_open
,
597 .bdrv_co_preadv
= nbd_client_co_preadv
,
598 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
599 .bdrv_co_pwrite_zeroes
= nbd_client_co_pwrite_zeroes
,
600 .bdrv_close
= nbd_close
,
601 .bdrv_co_flush_to_os
= nbd_co_flush
,
602 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
603 .bdrv_refresh_limits
= nbd_refresh_limits
,
604 .bdrv_getlength
= nbd_getlength
,
605 .bdrv_detach_aio_context
= nbd_detach_aio_context
,
606 .bdrv_attach_aio_context
= nbd_attach_aio_context
,
607 .bdrv_refresh_filename
= nbd_refresh_filename
,
610 static void bdrv_nbd_init(void)
612 bdrv_register(&bdrv_nbd
);
613 bdrv_register(&bdrv_nbd_tcp
);
614 bdrv_register(&bdrv_nbd_unix
);
617 block_init(bdrv_nbd_init
);