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/qmp/qdict.h"
36 #include "qapi/qmp/qjson.h"
37 #include "qapi/qmp/qint.h"
38 #include "qapi/qmp/qstring.h"
39 #include "qemu/cutils.h"
41 #define EN_OPTSTR ":exportname="
43 typedef struct BDRVNBDState
{
44 NbdClientSession client
;
47 static int nbd_parse_uri(const char *filename
, QDict
*options
)
51 QueryParams
*qp
= NULL
;
55 uri
= uri_parse(filename
);
61 if (!strcmp(uri
->scheme
, "nbd")) {
63 } else if (!strcmp(uri
->scheme
, "nbd+tcp")) {
65 } else if (!strcmp(uri
->scheme
, "nbd+unix")) {
72 p
= uri
->path
? uri
->path
: "/";
75 qdict_put(options
, "export", qstring_from_str(p
));
78 qp
= query_params_parse(uri
->query
);
79 if (qp
->n
> 1 || (is_unix
&& !qp
->n
) || (!is_unix
&& qp
->n
)) {
85 /* nbd+unix:///export?socket=path */
86 if (uri
->server
|| uri
->port
|| strcmp(qp
->p
[0].name
, "socket")) {
90 qdict_put(options
, "path", qstring_from_str(qp
->p
[0].value
));
93 /* nbd[+tcp]://host[:port]/export */
99 /* strip braces from literal IPv6 address */
100 if (uri
->server
[0] == '[') {
101 host
= qstring_from_substr(uri
->server
, 1,
102 strlen(uri
->server
) - 2);
104 host
= qstring_from_str(uri
->server
);
107 qdict_put(options
, "host", host
);
109 char* port_str
= g_strdup_printf("%d", uri
->port
);
110 qdict_put(options
, "port", qstring_from_str(port_str
));
117 query_params_free(qp
);
123 static void nbd_parse_filename(const char *filename
, QDict
*options
,
128 const char *host_spec
;
129 const char *unixpath
;
131 if (qdict_haskey(options
, "host")
132 || qdict_haskey(options
, "port")
133 || qdict_haskey(options
, "path"))
135 error_setg(errp
, "host/port/path and a file name may not be specified "
140 if (strstr(filename
, "://")) {
141 int ret
= nbd_parse_uri(filename
, options
);
143 error_setg(errp
, "No valid URL specified");
148 file
= g_strdup(filename
);
150 export_name
= strstr(file
, EN_OPTSTR
);
152 if (export_name
[strlen(EN_OPTSTR
)] == 0) {
155 export_name
[0] = 0; /* truncate 'file' */
156 export_name
+= strlen(EN_OPTSTR
);
158 qdict_put(options
, "export", qstring_from_str(export_name
));
161 /* extract the host_spec - fail if it's not nbd:... */
162 if (!strstart(file
, "nbd:", &host_spec
)) {
163 error_setg(errp
, "File name string for NBD must start with 'nbd:'");
171 /* are we a UNIX or TCP socket? */
172 if (strstart(host_spec
, "unix:", &unixpath
)) {
173 qdict_put(options
, "path", qstring_from_str(unixpath
));
175 InetSocketAddress
*addr
= NULL
;
177 addr
= inet_parse(host_spec
, errp
);
182 qdict_put(options
, "host", qstring_from_str(addr
->host
));
183 qdict_put(options
, "port", qstring_from_str(addr
->port
));
184 qapi_free_InetSocketAddress(addr
);
191 static SocketAddress
*nbd_config(BDRVNBDState
*s
, QDict
*options
, char **export
,
194 SocketAddress
*saddr
;
196 if (qdict_haskey(options
, "path") == qdict_haskey(options
, "host")) {
197 if (qdict_haskey(options
, "path")) {
198 error_setg(errp
, "path and host may not be used at the same time.");
200 error_setg(errp
, "one of path and host must be specified.");
205 saddr
= g_new0(SocketAddress
, 1);
207 if (qdict_haskey(options
, "path")) {
208 UnixSocketAddress
*q_unix
;
209 saddr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
210 q_unix
= saddr
->u
.q_unix
.data
= g_new0(UnixSocketAddress
, 1);
211 q_unix
->path
= g_strdup(qdict_get_str(options
, "path"));
212 qdict_del(options
, "path");
214 InetSocketAddress
*inet
;
215 saddr
->type
= SOCKET_ADDRESS_KIND_INET
;
216 inet
= saddr
->u
.inet
.data
= g_new0(InetSocketAddress
, 1);
217 inet
->host
= g_strdup(qdict_get_str(options
, "host"));
218 if (!qdict_get_try_str(options
, "port")) {
219 inet
->port
= g_strdup_printf("%d", NBD_DEFAULT_PORT
);
221 inet
->port
= g_strdup(qdict_get_str(options
, "port"));
223 qdict_del(options
, "host");
224 qdict_del(options
, "port");
227 s
->client
.is_unix
= saddr
->type
== SOCKET_ADDRESS_KIND_UNIX
;
229 *export
= g_strdup(qdict_get_try_str(options
, "export"));
231 qdict_del(options
, "export");
237 NbdClientSession
*nbd_get_client_session(BlockDriverState
*bs
)
239 BDRVNBDState
*s
= bs
->opaque
;
243 static QIOChannelSocket
*nbd_establish_connection(SocketAddress
*saddr
,
246 QIOChannelSocket
*sioc
;
247 Error
*local_err
= NULL
;
249 sioc
= qio_channel_socket_new();
251 qio_channel_socket_connect_sync(sioc
,
255 error_propagate(errp
, local_err
);
259 qio_channel_set_delay(QIO_CHANNEL(sioc
), false);
265 static QCryptoTLSCreds
*nbd_get_tls_creds(const char *id
, Error
**errp
)
268 QCryptoTLSCreds
*creds
;
270 obj
= object_resolve_path_component(
271 object_get_objects_root(), id
);
273 error_setg(errp
, "No TLS credentials with id '%s'",
277 creds
= (QCryptoTLSCreds
*)
278 object_dynamic_cast(obj
, TYPE_QCRYPTO_TLS_CREDS
);
280 error_setg(errp
, "Object with id '%s' is not TLS credentials",
285 if (creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
) {
287 "Expecting TLS credentials with a client endpoint");
295 static int nbd_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
298 BDRVNBDState
*s
= bs
->opaque
;
300 QIOChannelSocket
*sioc
= NULL
;
301 SocketAddress
*saddr
;
302 const char *tlscredsid
;
303 QCryptoTLSCreds
*tlscreds
= NULL
;
304 const char *hostname
= NULL
;
307 /* Pop the config into our state object. Exit if invalid. */
308 saddr
= nbd_config(s
, options
, &export
, errp
);
313 tlscredsid
= g_strdup(qdict_get_try_str(options
, "tls-creds"));
315 qdict_del(options
, "tls-creds");
316 tlscreds
= nbd_get_tls_creds(tlscredsid
, errp
);
321 if (saddr
->type
!= SOCKET_ADDRESS_KIND_INET
) {
322 error_setg(errp
, "TLS only supported over IP sockets");
325 hostname
= saddr
->u
.inet
.data
->host
;
328 /* establish TCP connection, return error if it fails
329 * TODO: Configurable retry-until-timeout behaviour.
331 sioc
= nbd_establish_connection(saddr
, errp
);
338 ret
= nbd_client_init(bs
, sioc
, export
,
339 tlscreds
, hostname
, errp
);
342 object_unref(OBJECT(sioc
));
345 object_unref(OBJECT(tlscreds
));
347 qapi_free_SocketAddress(saddr
);
352 static int nbd_co_flush(BlockDriverState
*bs
)
354 return nbd_client_co_flush(bs
);
357 static void nbd_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
359 bs
->bl
.max_pdiscard
= NBD_MAX_BUFFER_SIZE
;
360 bs
->bl
.max_transfer
= NBD_MAX_BUFFER_SIZE
;
363 static void nbd_close(BlockDriverState
*bs
)
365 nbd_client_close(bs
);
368 static int64_t nbd_getlength(BlockDriverState
*bs
)
370 BDRVNBDState
*s
= bs
->opaque
;
372 return s
->client
.size
;
375 static void nbd_detach_aio_context(BlockDriverState
*bs
)
377 nbd_client_detach_aio_context(bs
);
380 static void nbd_attach_aio_context(BlockDriverState
*bs
,
381 AioContext
*new_context
)
383 nbd_client_attach_aio_context(bs
, new_context
);
386 static void nbd_refresh_filename(BlockDriverState
*bs
, QDict
*options
)
388 QDict
*opts
= qdict_new();
389 const char *path
= qdict_get_try_str(options
, "path");
390 const char *host
= qdict_get_try_str(options
, "host");
391 const char *port
= qdict_get_try_str(options
, "port");
392 const char *export
= qdict_get_try_str(options
, "export");
393 const char *tlscreds
= qdict_get_try_str(options
, "tls-creds");
395 qdict_put_obj(opts
, "driver", QOBJECT(qstring_from_str("nbd")));
397 if (path
&& export
) {
398 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
399 "nbd+unix:///%s?socket=%s", export
, path
);
400 } else if (path
&& !export
) {
401 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
402 "nbd+unix://?socket=%s", path
);
403 } else if (!path
&& export
&& port
) {
404 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
405 "nbd://%s:%s/%s", host
, port
, export
);
406 } else if (!path
&& export
&& !port
) {
407 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
408 "nbd://%s/%s", host
, export
);
409 } else if (!path
&& !export
&& port
) {
410 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
411 "nbd://%s:%s", host
, port
);
412 } else if (!path
&& !export
&& !port
) {
413 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
418 qdict_put_obj(opts
, "path", QOBJECT(qstring_from_str(path
)));
420 qdict_put_obj(opts
, "host", QOBJECT(qstring_from_str(host
)));
421 qdict_put_obj(opts
, "port", QOBJECT(qstring_from_str(port
)));
423 qdict_put_obj(opts
, "host", QOBJECT(qstring_from_str(host
)));
426 qdict_put_obj(opts
, "export", QOBJECT(qstring_from_str(export
)));
429 qdict_put_obj(opts
, "tls-creds", QOBJECT(qstring_from_str(tlscreds
)));
432 bs
->full_open_options
= opts
;
435 static BlockDriver bdrv_nbd
= {
436 .format_name
= "nbd",
437 .protocol_name
= "nbd",
438 .instance_size
= sizeof(BDRVNBDState
),
439 .bdrv_parse_filename
= nbd_parse_filename
,
440 .bdrv_file_open
= nbd_open
,
441 .bdrv_co_preadv
= nbd_client_co_preadv
,
442 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
443 .bdrv_close
= nbd_close
,
444 .bdrv_co_flush_to_os
= nbd_co_flush
,
445 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
446 .bdrv_refresh_limits
= nbd_refresh_limits
,
447 .bdrv_getlength
= nbd_getlength
,
448 .bdrv_detach_aio_context
= nbd_detach_aio_context
,
449 .bdrv_attach_aio_context
= nbd_attach_aio_context
,
450 .bdrv_refresh_filename
= nbd_refresh_filename
,
453 static BlockDriver bdrv_nbd_tcp
= {
454 .format_name
= "nbd",
455 .protocol_name
= "nbd+tcp",
456 .instance_size
= sizeof(BDRVNBDState
),
457 .bdrv_parse_filename
= nbd_parse_filename
,
458 .bdrv_file_open
= nbd_open
,
459 .bdrv_co_preadv
= nbd_client_co_preadv
,
460 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
461 .bdrv_close
= nbd_close
,
462 .bdrv_co_flush_to_os
= nbd_co_flush
,
463 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
464 .bdrv_refresh_limits
= nbd_refresh_limits
,
465 .bdrv_getlength
= nbd_getlength
,
466 .bdrv_detach_aio_context
= nbd_detach_aio_context
,
467 .bdrv_attach_aio_context
= nbd_attach_aio_context
,
468 .bdrv_refresh_filename
= nbd_refresh_filename
,
471 static BlockDriver bdrv_nbd_unix
= {
472 .format_name
= "nbd",
473 .protocol_name
= "nbd+unix",
474 .instance_size
= sizeof(BDRVNBDState
),
475 .bdrv_parse_filename
= nbd_parse_filename
,
476 .bdrv_file_open
= nbd_open
,
477 .bdrv_co_preadv
= nbd_client_co_preadv
,
478 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
479 .bdrv_close
= nbd_close
,
480 .bdrv_co_flush_to_os
= nbd_co_flush
,
481 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
482 .bdrv_refresh_limits
= nbd_refresh_limits
,
483 .bdrv_getlength
= nbd_getlength
,
484 .bdrv_detach_aio_context
= nbd_detach_aio_context
,
485 .bdrv_attach_aio_context
= nbd_attach_aio_context
,
486 .bdrv_refresh_filename
= nbd_refresh_filename
,
489 static void bdrv_nbd_init(void)
491 bdrv_register(&bdrv_nbd
);
492 bdrv_register(&bdrv_nbd_tcp
);
493 bdrv_register(&bdrv_nbd_unix
);
496 block_init(bdrv_nbd_init
);