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/qstring.h"
41 #include "qemu/cutils.h"
43 #define EN_OPTSTR ":exportname="
45 typedef struct BDRVNBDState
{
46 NBDClientSession client
;
48 /* For nbd_refresh_filename() */
50 char *export
, *tlscredsid
;
53 static int nbd_parse_uri(const char *filename
, QDict
*options
)
57 QueryParams
*qp
= NULL
;
61 uri
= uri_parse(filename
);
67 if (!g_strcmp0(uri
->scheme
, "nbd")) {
69 } else if (!g_strcmp0(uri
->scheme
, "nbd+tcp")) {
71 } else if (!g_strcmp0(uri
->scheme
, "nbd+unix")) {
78 p
= uri
->path
? uri
->path
: "/";
81 qdict_put_str(options
, "export", p
);
84 qp
= query_params_parse(uri
->query
);
85 if (qp
->n
> 1 || (is_unix
&& !qp
->n
) || (!is_unix
&& qp
->n
)) {
91 /* nbd+unix:///export?socket=path */
92 if (uri
->server
|| uri
->port
|| strcmp(qp
->p
[0].name
, "socket")) {
96 qdict_put_str(options
, "server.type", "unix");
97 qdict_put_str(options
, "server.path", qp
->p
[0].value
);
102 /* nbd[+tcp]://host[:port]/export */
108 /* strip braces from literal IPv6 address */
109 if (uri
->server
[0] == '[') {
110 host
= qstring_from_substr(uri
->server
, 1,
111 strlen(uri
->server
) - 2);
113 host
= qstring_from_str(uri
->server
);
116 qdict_put_str(options
, "server.type", "inet");
117 qdict_put(options
, "server.host", host
);
119 port_str
= g_strdup_printf("%d", uri
->port
?: NBD_DEFAULT_PORT
);
120 qdict_put_str(options
, "server.port", port_str
);
126 query_params_free(qp
);
132 static bool nbd_has_filename_options_conflict(QDict
*options
, Error
**errp
)
136 for (e
= qdict_first(options
); e
; e
= qdict_next(options
, e
)) {
137 if (!strcmp(e
->key
, "host") ||
138 !strcmp(e
->key
, "port") ||
139 !strcmp(e
->key
, "path") ||
140 !strcmp(e
->key
, "export") ||
141 strstart(e
->key
, "server.", NULL
))
143 error_setg(errp
, "Option '%s' cannot be used with a file name",
152 static void nbd_parse_filename(const char *filename
, QDict
*options
,
157 const char *host_spec
;
158 const char *unixpath
;
160 if (nbd_has_filename_options_conflict(options
, errp
)) {
164 if (strstr(filename
, "://")) {
165 int ret
= nbd_parse_uri(filename
, options
);
167 error_setg(errp
, "No valid URL specified");
172 file
= g_strdup(filename
);
174 export_name
= strstr(file
, EN_OPTSTR
);
176 if (export_name
[strlen(EN_OPTSTR
)] == 0) {
179 export_name
[0] = 0; /* truncate 'file' */
180 export_name
+= strlen(EN_OPTSTR
);
182 qdict_put_str(options
, "export", export_name
);
185 /* extract the host_spec - fail if it's not nbd:... */
186 if (!strstart(file
, "nbd:", &host_spec
)) {
187 error_setg(errp
, "File name string for NBD must start with 'nbd:'");
195 /* are we a UNIX or TCP socket? */
196 if (strstart(host_spec
, "unix:", &unixpath
)) {
197 qdict_put_str(options
, "server.type", "unix");
198 qdict_put_str(options
, "server.path", unixpath
);
200 InetSocketAddress
*addr
= g_new(InetSocketAddress
, 1);
202 if (inet_parse(addr
, host_spec
, errp
)) {
206 qdict_put_str(options
, "server.type", "inet");
207 qdict_put_str(options
, "server.host", addr
->host
);
208 qdict_put_str(options
, "server.port", addr
->port
);
210 qapi_free_InetSocketAddress(addr
);
217 static bool nbd_process_legacy_socket_options(QDict
*output_options
,
218 QemuOpts
*legacy_opts
,
221 const char *path
= qemu_opt_get(legacy_opts
, "path");
222 const char *host
= qemu_opt_get(legacy_opts
, "host");
223 const char *port
= qemu_opt_get(legacy_opts
, "port");
226 if (!path
&& !host
&& !port
) {
230 for (e
= qdict_first(output_options
); e
; e
= qdict_next(output_options
, e
))
232 if (strstart(e
->key
, "server.", NULL
)) {
233 error_setg(errp
, "Cannot use 'server' and path/host/port at the "
240 error_setg(errp
, "path and host may not be used at the same time");
244 error_setg(errp
, "port may not be used without host");
248 qdict_put_str(output_options
, "server.type", "unix");
249 qdict_put_str(output_options
, "server.path", path
);
251 qdict_put_str(output_options
, "server.type", "inet");
252 qdict_put_str(output_options
, "server.host", host
);
253 qdict_put_str(output_options
, "server.port",
254 port
?: stringify(NBD_DEFAULT_PORT
));
260 static SocketAddress
*nbd_config(BDRVNBDState
*s
, QDict
*options
,
263 SocketAddress
*saddr
= NULL
;
265 QObject
*crumpled_addr
= NULL
;
267 Error
*local_err
= NULL
;
269 qdict_extract_subqdict(options
, &addr
, "server.");
270 if (!qdict_size(addr
)) {
271 error_setg(errp
, "NBD server address missing");
275 crumpled_addr
= qdict_crumple(addr
, errp
);
276 if (!crumpled_addr
) {
281 * FIXME .numeric, .to, .ipv4 or .ipv6 don't work with -drive
282 * server.type=inet. .to doesn't matter, it's ignored anyway.
283 * That's because when @options come from -blockdev or
284 * blockdev_add, members are typed according to the QAPI schema,
285 * but when they come from -drive, they're all QString. The
286 * visitor expects the former.
288 iv
= qobject_input_visitor_new(crumpled_addr
);
289 visit_type_SocketAddress(iv
, NULL
, &saddr
, &local_err
);
291 error_propagate(errp
, local_err
);
297 qobject_decref(crumpled_addr
);
302 NBDClientSession
*nbd_get_client_session(BlockDriverState
*bs
)
304 BDRVNBDState
*s
= bs
->opaque
;
308 static QIOChannelSocket
*nbd_establish_connection(SocketAddress
*saddr
,
311 QIOChannelSocket
*sioc
;
312 Error
*local_err
= NULL
;
314 sioc
= qio_channel_socket_new();
315 qio_channel_set_name(QIO_CHANNEL(sioc
), "nbd-client");
317 qio_channel_socket_connect_sync(sioc
,
321 object_unref(OBJECT(sioc
));
322 error_propagate(errp
, local_err
);
326 qio_channel_set_delay(QIO_CHANNEL(sioc
), false);
332 static QCryptoTLSCreds
*nbd_get_tls_creds(const char *id
, Error
**errp
)
335 QCryptoTLSCreds
*creds
;
337 obj
= object_resolve_path_component(
338 object_get_objects_root(), id
);
340 error_setg(errp
, "No TLS credentials with id '%s'",
344 creds
= (QCryptoTLSCreds
*)
345 object_dynamic_cast(obj
, TYPE_QCRYPTO_TLS_CREDS
);
347 error_setg(errp
, "Object with id '%s' is not TLS credentials",
352 if (creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
) {
354 "Expecting TLS credentials with a client endpoint");
362 static QemuOptsList nbd_runtime_opts
= {
364 .head
= QTAILQ_HEAD_INITIALIZER(nbd_runtime_opts
.head
),
368 .type
= QEMU_OPT_STRING
,
369 .help
= "TCP host to connect to",
373 .type
= QEMU_OPT_STRING
,
374 .help
= "TCP port to connect to",
378 .type
= QEMU_OPT_STRING
,
379 .help
= "Unix socket path to connect to",
383 .type
= QEMU_OPT_STRING
,
384 .help
= "Name of the NBD export to open",
388 .type
= QEMU_OPT_STRING
,
389 .help
= "ID of the TLS credentials to use",
394 static int nbd_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
397 BDRVNBDState
*s
= bs
->opaque
;
398 QemuOpts
*opts
= NULL
;
399 Error
*local_err
= NULL
;
400 QIOChannelSocket
*sioc
= NULL
;
401 QCryptoTLSCreds
*tlscreds
= NULL
;
402 const char *hostname
= NULL
;
405 opts
= qemu_opts_create(&nbd_runtime_opts
, NULL
, 0, &error_abort
);
406 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
408 error_propagate(errp
, local_err
);
412 /* Translate @host, @port, and @path to a SocketAddress */
413 if (!nbd_process_legacy_socket_options(options
, opts
, errp
)) {
417 /* Pop the config into our state object. Exit if invalid. */
418 s
->saddr
= nbd_config(s
, options
, errp
);
423 s
->export
= g_strdup(qemu_opt_get(opts
, "export"));
425 s
->tlscredsid
= g_strdup(qemu_opt_get(opts
, "tls-creds"));
427 tlscreds
= nbd_get_tls_creds(s
->tlscredsid
, errp
);
432 /* TODO SOCKET_ADDRESS_KIND_FD where fd has AF_INET or AF_INET6 */
433 if (s
->saddr
->type
!= SOCKET_ADDRESS_TYPE_INET
) {
434 error_setg(errp
, "TLS only supported over IP sockets");
437 hostname
= s
->saddr
->u
.inet
.host
;
440 /* establish TCP connection, return error if it fails
441 * TODO: Configurable retry-until-timeout behaviour.
443 sioc
= nbd_establish_connection(s
->saddr
, errp
);
450 ret
= nbd_client_init(bs
, sioc
, s
->export
,
451 tlscreds
, hostname
, errp
);
454 object_unref(OBJECT(sioc
));
457 object_unref(OBJECT(tlscreds
));
460 qapi_free_SocketAddress(s
->saddr
);
462 g_free(s
->tlscredsid
);
468 static int nbd_co_flush(BlockDriverState
*bs
)
470 return nbd_client_co_flush(bs
);
473 static void nbd_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
475 NBDClientSession
*s
= nbd_get_client_session(bs
);
476 uint32_t max
= MIN_NON_ZERO(NBD_MAX_BUFFER_SIZE
, s
->info
.max_block
);
478 bs
->bl
.max_pdiscard
= max
;
479 bs
->bl
.max_pwrite_zeroes
= max
;
480 bs
->bl
.max_transfer
= max
;
482 if (s
->info
.opt_block
&&
483 s
->info
.opt_block
> bs
->bl
.opt_transfer
) {
484 bs
->bl
.opt_transfer
= s
->info
.opt_block
;
488 static void nbd_close(BlockDriverState
*bs
)
490 BDRVNBDState
*s
= bs
->opaque
;
492 nbd_client_close(bs
);
494 qapi_free_SocketAddress(s
->saddr
);
496 g_free(s
->tlscredsid
);
499 static int64_t nbd_getlength(BlockDriverState
*bs
)
501 BDRVNBDState
*s
= bs
->opaque
;
503 return s
->client
.info
.size
;
506 static void nbd_detach_aio_context(BlockDriverState
*bs
)
508 nbd_client_detach_aio_context(bs
);
511 static void nbd_attach_aio_context(BlockDriverState
*bs
,
512 AioContext
*new_context
)
514 nbd_client_attach_aio_context(bs
, new_context
);
517 static void nbd_refresh_filename(BlockDriverState
*bs
, QDict
*options
)
519 BDRVNBDState
*s
= bs
->opaque
;
520 QDict
*opts
= qdict_new();
521 QObject
*saddr_qdict
;
523 const char *host
= NULL
, *port
= NULL
, *path
= NULL
;
525 if (s
->saddr
->type
== SOCKET_ADDRESS_TYPE_INET
) {
526 const InetSocketAddress
*inet
= &s
->saddr
->u
.inet
;
527 if (!inet
->has_ipv4
&& !inet
->has_ipv6
&& !inet
->has_to
) {
531 } else if (s
->saddr
->type
== SOCKET_ADDRESS_TYPE_UNIX
) {
532 path
= s
->saddr
->u
.q_unix
.path
;
533 } /* else can't represent as pseudo-filename */
535 qdict_put_str(opts
, "driver", "nbd");
537 if (path
&& s
->export
) {
538 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
539 "nbd+unix:///%s?socket=%s", s
->export
, path
);
540 } else if (path
&& !s
->export
) {
541 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
542 "nbd+unix://?socket=%s", path
);
543 } else if (host
&& s
->export
) {
544 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
545 "nbd://%s:%s/%s", host
, port
, s
->export
);
546 } else if (host
&& !s
->export
) {
547 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
548 "nbd://%s:%s", host
, port
);
551 ov
= qobject_output_visitor_new(&saddr_qdict
);
552 visit_type_SocketAddress(ov
, NULL
, &s
->saddr
, &error_abort
);
553 visit_complete(ov
, &saddr_qdict
);
555 qdict_put_obj(opts
, "server", saddr_qdict
);
558 qdict_put_str(opts
, "export", s
->export
);
561 qdict_put_str(opts
, "tls-creds", s
->tlscredsid
);
565 bs
->full_open_options
= opts
;
568 static BlockDriver bdrv_nbd
= {
569 .format_name
= "nbd",
570 .protocol_name
= "nbd",
571 .instance_size
= sizeof(BDRVNBDState
),
572 .bdrv_parse_filename
= nbd_parse_filename
,
573 .bdrv_file_open
= nbd_open
,
574 .bdrv_co_preadv
= nbd_client_co_preadv
,
575 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
576 .bdrv_co_pwrite_zeroes
= nbd_client_co_pwrite_zeroes
,
577 .bdrv_close
= nbd_close
,
578 .bdrv_co_flush_to_os
= nbd_co_flush
,
579 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
580 .bdrv_refresh_limits
= nbd_refresh_limits
,
581 .bdrv_getlength
= nbd_getlength
,
582 .bdrv_detach_aio_context
= nbd_detach_aio_context
,
583 .bdrv_attach_aio_context
= nbd_attach_aio_context
,
584 .bdrv_refresh_filename
= nbd_refresh_filename
,
587 static BlockDriver bdrv_nbd_tcp
= {
588 .format_name
= "nbd",
589 .protocol_name
= "nbd+tcp",
590 .instance_size
= sizeof(BDRVNBDState
),
591 .bdrv_parse_filename
= nbd_parse_filename
,
592 .bdrv_file_open
= nbd_open
,
593 .bdrv_co_preadv
= nbd_client_co_preadv
,
594 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
595 .bdrv_co_pwrite_zeroes
= nbd_client_co_pwrite_zeroes
,
596 .bdrv_close
= nbd_close
,
597 .bdrv_co_flush_to_os
= nbd_co_flush
,
598 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
599 .bdrv_refresh_limits
= nbd_refresh_limits
,
600 .bdrv_getlength
= nbd_getlength
,
601 .bdrv_detach_aio_context
= nbd_detach_aio_context
,
602 .bdrv_attach_aio_context
= nbd_attach_aio_context
,
603 .bdrv_refresh_filename
= nbd_refresh_filename
,
606 static BlockDriver bdrv_nbd_unix
= {
607 .format_name
= "nbd",
608 .protocol_name
= "nbd+unix",
609 .instance_size
= sizeof(BDRVNBDState
),
610 .bdrv_parse_filename
= nbd_parse_filename
,
611 .bdrv_file_open
= nbd_open
,
612 .bdrv_co_preadv
= nbd_client_co_preadv
,
613 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
614 .bdrv_co_pwrite_zeroes
= nbd_client_co_pwrite_zeroes
,
615 .bdrv_close
= nbd_close
,
616 .bdrv_co_flush_to_os
= nbd_co_flush
,
617 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
618 .bdrv_refresh_limits
= nbd_refresh_limits
,
619 .bdrv_getlength
= nbd_getlength
,
620 .bdrv_detach_aio_context
= nbd_detach_aio_context
,
621 .bdrv_attach_aio_context
= nbd_attach_aio_context
,
622 .bdrv_refresh_filename
= nbd_refresh_filename
,
625 static void bdrv_nbd_init(void)
627 bdrv_register(&bdrv_nbd
);
628 bdrv_register(&bdrv_nbd_tcp
);
629 bdrv_register(&bdrv_nbd_unix
);
632 block_init(bdrv_nbd_init
);