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_str(options
, "export", 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_str(options
, "server.type", "unix");
98 qdict_put_str(options
, "server.path", qp
->p
[0].value
);
103 /* nbd[+tcp]://host[:port]/export */
109 /* strip braces from literal IPv6 address */
110 if (uri
->server
[0] == '[') {
111 host
= qstring_from_substr(uri
->server
, 1,
112 strlen(uri
->server
) - 2);
114 host
= qstring_from_str(uri
->server
);
117 qdict_put_str(options
, "server.type", "inet");
118 qdict_put(options
, "server.host", host
);
120 port_str
= g_strdup_printf("%d", uri
->port
?: NBD_DEFAULT_PORT
);
121 qdict_put_str(options
, "server.port", port_str
);
127 query_params_free(qp
);
133 static bool nbd_has_filename_options_conflict(QDict
*options
, Error
**errp
)
137 for (e
= qdict_first(options
); e
; e
= qdict_next(options
, e
)) {
138 if (!strcmp(e
->key
, "host") ||
139 !strcmp(e
->key
, "port") ||
140 !strcmp(e
->key
, "path") ||
141 !strcmp(e
->key
, "export") ||
142 strstart(e
->key
, "server.", NULL
))
144 error_setg(errp
, "Option '%s' cannot be used with a file name",
153 static void nbd_parse_filename(const char *filename
, QDict
*options
,
158 const char *host_spec
;
159 const char *unixpath
;
161 if (nbd_has_filename_options_conflict(options
, errp
)) {
165 if (strstr(filename
, "://")) {
166 int ret
= nbd_parse_uri(filename
, options
);
168 error_setg(errp
, "No valid URL specified");
173 file
= g_strdup(filename
);
175 export_name
= strstr(file
, EN_OPTSTR
);
177 if (export_name
[strlen(EN_OPTSTR
)] == 0) {
180 export_name
[0] = 0; /* truncate 'file' */
181 export_name
+= strlen(EN_OPTSTR
);
183 qdict_put_str(options
, "export", export_name
);
186 /* extract the host_spec - fail if it's not nbd:... */
187 if (!strstart(file
, "nbd:", &host_spec
)) {
188 error_setg(errp
, "File name string for NBD must start with 'nbd:'");
196 /* are we a UNIX or TCP socket? */
197 if (strstart(host_spec
, "unix:", &unixpath
)) {
198 qdict_put_str(options
, "server.type", "unix");
199 qdict_put_str(options
, "server.path", unixpath
);
201 InetSocketAddress
*addr
= g_new(InetSocketAddress
, 1);
203 if (inet_parse(addr
, host_spec
, errp
)) {
207 qdict_put_str(options
, "server.type", "inet");
208 qdict_put_str(options
, "server.host", addr
->host
);
209 qdict_put_str(options
, "server.port", addr
->port
);
211 qapi_free_InetSocketAddress(addr
);
218 static bool nbd_process_legacy_socket_options(QDict
*output_options
,
219 QemuOpts
*legacy_opts
,
222 const char *path
= qemu_opt_get(legacy_opts
, "path");
223 const char *host
= qemu_opt_get(legacy_opts
, "host");
224 const char *port
= qemu_opt_get(legacy_opts
, "port");
227 if (!path
&& !host
&& !port
) {
231 for (e
= qdict_first(output_options
); e
; e
= qdict_next(output_options
, e
))
233 if (strstart(e
->key
, "server.", NULL
)) {
234 error_setg(errp
, "Cannot use 'server' and path/host/port at the "
241 error_setg(errp
, "path and host may not be used at the same time");
245 error_setg(errp
, "port may not be used without host");
249 qdict_put_str(output_options
, "server.type", "unix");
250 qdict_put_str(output_options
, "server.path", path
);
252 qdict_put_str(output_options
, "server.type", "inet");
253 qdict_put_str(output_options
, "server.host", host
);
254 qdict_put_str(output_options
, "server.port",
255 port
?: stringify(NBD_DEFAULT_PORT
));
261 static SocketAddress
*nbd_config(BDRVNBDState
*s
, QDict
*options
,
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
) {
282 * FIXME .numeric, .to, .ipv4 or .ipv6 don't work with -drive
283 * server.type=inet. .to doesn't matter, it's ignored anyway.
284 * That's because when @options come from -blockdev or
285 * blockdev_add, members are typed according to the QAPI schema,
286 * but when they come from -drive, they're all QString. The
287 * visitor expects the former.
289 iv
= qobject_input_visitor_new(crumpled_addr
);
290 visit_type_SocketAddress(iv
, NULL
, &saddr
, &local_err
);
292 error_propagate(errp
, local_err
);
298 qobject_decref(crumpled_addr
);
303 NBDClientSession
*nbd_get_client_session(BlockDriverState
*bs
)
305 BDRVNBDState
*s
= bs
->opaque
;
309 static QIOChannelSocket
*nbd_establish_connection(SocketAddress
*saddr
,
312 QIOChannelSocket
*sioc
;
313 Error
*local_err
= NULL
;
315 sioc
= qio_channel_socket_new();
316 qio_channel_set_name(QIO_CHANNEL(sioc
), "nbd-client");
318 qio_channel_socket_connect_sync(sioc
,
322 object_unref(OBJECT(sioc
));
323 error_propagate(errp
, local_err
);
327 qio_channel_set_delay(QIO_CHANNEL(sioc
), false);
333 static QCryptoTLSCreds
*nbd_get_tls_creds(const char *id
, Error
**errp
)
336 QCryptoTLSCreds
*creds
;
338 obj
= object_resolve_path_component(
339 object_get_objects_root(), id
);
341 error_setg(errp
, "No TLS credentials with id '%s'",
345 creds
= (QCryptoTLSCreds
*)
346 object_dynamic_cast(obj
, TYPE_QCRYPTO_TLS_CREDS
);
348 error_setg(errp
, "Object with id '%s' is not TLS credentials",
353 if (creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
) {
355 "Expecting TLS credentials with a client endpoint");
363 static QemuOptsList nbd_runtime_opts
= {
365 .head
= QTAILQ_HEAD_INITIALIZER(nbd_runtime_opts
.head
),
369 .type
= QEMU_OPT_STRING
,
370 .help
= "TCP host to connect to",
374 .type
= QEMU_OPT_STRING
,
375 .help
= "TCP port to connect to",
379 .type
= QEMU_OPT_STRING
,
380 .help
= "Unix socket path to connect to",
384 .type
= QEMU_OPT_STRING
,
385 .help
= "Name of the NBD export to open",
389 .type
= QEMU_OPT_STRING
,
390 .help
= "ID of the TLS credentials to use",
395 static int nbd_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
398 BDRVNBDState
*s
= bs
->opaque
;
399 QemuOpts
*opts
= NULL
;
400 Error
*local_err
= NULL
;
401 QIOChannelSocket
*sioc
= NULL
;
402 QCryptoTLSCreds
*tlscreds
= NULL
;
403 const char *hostname
= NULL
;
406 opts
= qemu_opts_create(&nbd_runtime_opts
, NULL
, 0, &error_abort
);
407 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
409 error_propagate(errp
, local_err
);
413 /* Translate @host, @port, and @path to a SocketAddress */
414 if (!nbd_process_legacy_socket_options(options
, opts
, errp
)) {
418 /* Pop the config into our state object. Exit if invalid. */
419 s
->saddr
= nbd_config(s
, options
, errp
);
424 s
->export
= g_strdup(qemu_opt_get(opts
, "export"));
426 s
->tlscredsid
= g_strdup(qemu_opt_get(opts
, "tls-creds"));
428 tlscreds
= nbd_get_tls_creds(s
->tlscredsid
, errp
);
433 /* TODO SOCKET_ADDRESS_KIND_FD where fd has AF_INET or AF_INET6 */
434 if (s
->saddr
->type
!= SOCKET_ADDRESS_TYPE_INET
) {
435 error_setg(errp
, "TLS only supported over IP sockets");
438 hostname
= s
->saddr
->u
.inet
.host
;
441 /* establish TCP connection, return error if it fails
442 * TODO: Configurable retry-until-timeout behaviour.
444 sioc
= nbd_establish_connection(s
->saddr
, errp
);
451 ret
= nbd_client_init(bs
, sioc
, s
->export
,
452 tlscreds
, hostname
, errp
);
455 object_unref(OBJECT(sioc
));
458 object_unref(OBJECT(tlscreds
));
461 qapi_free_SocketAddress(s
->saddr
);
463 g_free(s
->tlscredsid
);
469 static int nbd_co_flush(BlockDriverState
*bs
)
471 return nbd_client_co_flush(bs
);
474 static void nbd_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
476 bs
->bl
.max_pdiscard
= NBD_MAX_BUFFER_SIZE
;
477 bs
->bl
.max_pwrite_zeroes
= NBD_MAX_BUFFER_SIZE
;
478 bs
->bl
.max_transfer
= NBD_MAX_BUFFER_SIZE
;
481 static void nbd_close(BlockDriverState
*bs
)
483 BDRVNBDState
*s
= bs
->opaque
;
485 nbd_client_close(bs
);
487 qapi_free_SocketAddress(s
->saddr
);
489 g_free(s
->tlscredsid
);
492 static int64_t nbd_getlength(BlockDriverState
*bs
)
494 BDRVNBDState
*s
= bs
->opaque
;
496 return s
->client
.size
;
499 static void nbd_detach_aio_context(BlockDriverState
*bs
)
501 nbd_client_detach_aio_context(bs
);
504 static void nbd_attach_aio_context(BlockDriverState
*bs
,
505 AioContext
*new_context
)
507 nbd_client_attach_aio_context(bs
, new_context
);
510 static void nbd_refresh_filename(BlockDriverState
*bs
, QDict
*options
)
512 BDRVNBDState
*s
= bs
->opaque
;
513 QDict
*opts
= qdict_new();
514 QObject
*saddr_qdict
;
516 const char *host
= NULL
, *port
= NULL
, *path
= NULL
;
518 if (s
->saddr
->type
== SOCKET_ADDRESS_TYPE_INET
) {
519 const InetSocketAddress
*inet
= &s
->saddr
->u
.inet
;
520 if (!inet
->has_ipv4
&& !inet
->has_ipv6
&& !inet
->has_to
) {
524 } else if (s
->saddr
->type
== SOCKET_ADDRESS_TYPE_UNIX
) {
525 path
= s
->saddr
->u
.q_unix
.path
;
526 } /* else can't represent as pseudo-filename */
528 qdict_put_str(opts
, "driver", "nbd");
530 if (path
&& s
->export
) {
531 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
532 "nbd+unix:///%s?socket=%s", s
->export
, path
);
533 } else if (path
&& !s
->export
) {
534 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
535 "nbd+unix://?socket=%s", path
);
536 } else if (host
&& s
->export
) {
537 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
538 "nbd://%s:%s/%s", host
, port
, s
->export
);
539 } else if (host
&& !s
->export
) {
540 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
541 "nbd://%s:%s", host
, port
);
544 ov
= qobject_output_visitor_new(&saddr_qdict
);
545 visit_type_SocketAddress(ov
, NULL
, &s
->saddr
, &error_abort
);
546 visit_complete(ov
, &saddr_qdict
);
548 qdict_put_obj(opts
, "server", saddr_qdict
);
551 qdict_put_str(opts
, "export", s
->export
);
554 qdict_put_str(opts
, "tls-creds", s
->tlscredsid
);
558 bs
->full_open_options
= opts
;
561 static BlockDriver bdrv_nbd
= {
562 .format_name
= "nbd",
563 .protocol_name
= "nbd",
564 .instance_size
= sizeof(BDRVNBDState
),
565 .bdrv_parse_filename
= nbd_parse_filename
,
566 .bdrv_file_open
= nbd_open
,
567 .bdrv_co_preadv
= nbd_client_co_preadv
,
568 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
569 .bdrv_co_pwrite_zeroes
= nbd_client_co_pwrite_zeroes
,
570 .bdrv_close
= nbd_close
,
571 .bdrv_co_flush_to_os
= nbd_co_flush
,
572 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
573 .bdrv_refresh_limits
= nbd_refresh_limits
,
574 .bdrv_getlength
= nbd_getlength
,
575 .bdrv_detach_aio_context
= nbd_detach_aio_context
,
576 .bdrv_attach_aio_context
= nbd_attach_aio_context
,
577 .bdrv_refresh_filename
= nbd_refresh_filename
,
580 static BlockDriver bdrv_nbd_tcp
= {
581 .format_name
= "nbd",
582 .protocol_name
= "nbd+tcp",
583 .instance_size
= sizeof(BDRVNBDState
),
584 .bdrv_parse_filename
= nbd_parse_filename
,
585 .bdrv_file_open
= nbd_open
,
586 .bdrv_co_preadv
= nbd_client_co_preadv
,
587 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
588 .bdrv_co_pwrite_zeroes
= nbd_client_co_pwrite_zeroes
,
589 .bdrv_close
= nbd_close
,
590 .bdrv_co_flush_to_os
= nbd_co_flush
,
591 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
592 .bdrv_refresh_limits
= nbd_refresh_limits
,
593 .bdrv_getlength
= nbd_getlength
,
594 .bdrv_detach_aio_context
= nbd_detach_aio_context
,
595 .bdrv_attach_aio_context
= nbd_attach_aio_context
,
596 .bdrv_refresh_filename
= nbd_refresh_filename
,
599 static BlockDriver bdrv_nbd_unix
= {
600 .format_name
= "nbd",
601 .protocol_name
= "nbd+unix",
602 .instance_size
= sizeof(BDRVNBDState
),
603 .bdrv_parse_filename
= nbd_parse_filename
,
604 .bdrv_file_open
= nbd_open
,
605 .bdrv_co_preadv
= nbd_client_co_preadv
,
606 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
607 .bdrv_co_pwrite_zeroes
= nbd_client_co_pwrite_zeroes
,
608 .bdrv_close
= nbd_close
,
609 .bdrv_co_flush_to_os
= nbd_co_flush
,
610 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
611 .bdrv_refresh_limits
= nbd_refresh_limits
,
612 .bdrv_getlength
= nbd_getlength
,
613 .bdrv_detach_aio_context
= nbd_detach_aio_context
,
614 .bdrv_attach_aio_context
= nbd_attach_aio_context
,
615 .bdrv_refresh_filename
= nbd_refresh_filename
,
618 static void bdrv_nbd_init(void)
620 bdrv_register(&bdrv_nbd
);
621 bdrv_register(&bdrv_nbd_tcp
);
622 bdrv_register(&bdrv_nbd_unix
);
625 block_init(bdrv_nbd_init
);