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"
32 #include "block/block_int.h"
33 #include "qemu/module.h"
34 #include "qapi/qmp/qdict.h"
35 #include "qapi/qmp/qjson.h"
36 #include "qapi/qmp/qint.h"
37 #include "qapi/qmp/qstring.h"
40 #define EN_OPTSTR ":exportname="
42 typedef struct BDRVNBDState
{
43 NbdClientSession client
;
46 static int nbd_parse_uri(const char *filename
, QDict
*options
)
50 QueryParams
*qp
= NULL
;
54 uri
= uri_parse(filename
);
60 if (!strcmp(uri
->scheme
, "nbd")) {
62 } else if (!strcmp(uri
->scheme
, "nbd+tcp")) {
64 } else if (!strcmp(uri
->scheme
, "nbd+unix")) {
71 p
= uri
->path
? uri
->path
: "/";
74 qdict_put(options
, "export", qstring_from_str(p
));
77 qp
= query_params_parse(uri
->query
);
78 if (qp
->n
> 1 || (is_unix
&& !qp
->n
) || (!is_unix
&& qp
->n
)) {
84 /* nbd+unix:///export?socket=path */
85 if (uri
->server
|| uri
->port
|| strcmp(qp
->p
[0].name
, "socket")) {
89 qdict_put(options
, "path", qstring_from_str(qp
->p
[0].value
));
92 /* nbd[+tcp]://host[:port]/export */
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);
103 host
= qstring_from_str(uri
->server
);
106 qdict_put(options
, "host", host
);
108 char* port_str
= g_strdup_printf("%d", uri
->port
);
109 qdict_put(options
, "port", qstring_from_str(port_str
));
116 query_params_free(qp
);
122 static void nbd_parse_filename(const char *filename
, QDict
*options
,
127 const char *host_spec
;
128 const char *unixpath
;
130 if (qdict_haskey(options
, "host")
131 || qdict_haskey(options
, "port")
132 || qdict_haskey(options
, "path"))
134 error_setg(errp
, "host/port/path and a file name may not be specified "
139 if (strstr(filename
, "://")) {
140 int ret
= nbd_parse_uri(filename
, options
);
142 error_setg(errp
, "No valid URL specified");
147 file
= g_strdup(filename
);
149 export_name
= strstr(file
, EN_OPTSTR
);
151 if (export_name
[strlen(EN_OPTSTR
)] == 0) {
154 export_name
[0] = 0; /* truncate 'file' */
155 export_name
+= strlen(EN_OPTSTR
);
157 qdict_put(options
, "export", qstring_from_str(export_name
));
160 /* extract the host_spec - fail if it's not nbd:... */
161 if (!strstart(file
, "nbd:", &host_spec
)) {
162 error_setg(errp
, "File name string for NBD must start with 'nbd:'");
170 /* are we a UNIX or TCP socket? */
171 if (strstart(host_spec
, "unix:", &unixpath
)) {
172 qdict_put(options
, "path", qstring_from_str(unixpath
));
174 InetSocketAddress
*addr
= NULL
;
176 addr
= inet_parse(host_spec
, errp
);
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
);
190 static SocketAddress
*nbd_config(BDRVNBDState
*s
, QDict
*options
, char **export
,
193 SocketAddress
*saddr
;
195 if (qdict_haskey(options
, "path") == qdict_haskey(options
, "host")) {
196 if (qdict_haskey(options
, "path")) {
197 error_setg(errp
, "path and host may not be used at the same time.");
199 error_setg(errp
, "one of path and host must be specified.");
204 saddr
= g_new0(SocketAddress
, 1);
206 if (qdict_haskey(options
, "path")) {
207 UnixSocketAddress
*q_unix
;
208 saddr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
209 q_unix
= saddr
->u
.q_unix
= g_new0(UnixSocketAddress
, 1);
210 q_unix
->path
= g_strdup(qdict_get_str(options
, "path"));
211 qdict_del(options
, "path");
213 InetSocketAddress
*inet
;
214 saddr
->type
= SOCKET_ADDRESS_KIND_INET
;
215 inet
= saddr
->u
.inet
= g_new0(InetSocketAddress
, 1);
216 inet
->host
= g_strdup(qdict_get_str(options
, "host"));
217 if (!qdict_get_try_str(options
, "port")) {
218 inet
->port
= g_strdup_printf("%d", NBD_DEFAULT_PORT
);
220 inet
->port
= g_strdup(qdict_get_str(options
, "port"));
222 qdict_del(options
, "host");
223 qdict_del(options
, "port");
226 s
->client
.is_unix
= saddr
->type
== SOCKET_ADDRESS_KIND_UNIX
;
228 *export
= g_strdup(qdict_get_try_str(options
, "export"));
230 qdict_del(options
, "export");
236 NbdClientSession
*nbd_get_client_session(BlockDriverState
*bs
)
238 BDRVNBDState
*s
= bs
->opaque
;
242 static QIOChannelSocket
*nbd_establish_connection(SocketAddress
*saddr
,
245 QIOChannelSocket
*sioc
;
246 Error
*local_err
= NULL
;
248 sioc
= qio_channel_socket_new();
250 qio_channel_socket_connect_sync(sioc
,
254 error_propagate(errp
, local_err
);
258 qio_channel_set_delay(QIO_CHANNEL(sioc
), false);
264 static QCryptoTLSCreds
*nbd_get_tls_creds(const char *id
, Error
**errp
)
267 QCryptoTLSCreds
*creds
;
269 obj
= object_resolve_path_component(
270 object_get_objects_root(), id
);
272 error_setg(errp
, "No TLS credentials with id '%s'",
276 creds
= (QCryptoTLSCreds
*)
277 object_dynamic_cast(obj
, TYPE_QCRYPTO_TLS_CREDS
);
279 error_setg(errp
, "Object with id '%s' is not TLS credentials",
284 if (creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
) {
286 "Expecting TLS credentials with a client endpoint");
294 static int nbd_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
297 BDRVNBDState
*s
= bs
->opaque
;
299 QIOChannelSocket
*sioc
= NULL
;
300 SocketAddress
*saddr
;
301 const char *tlscredsid
;
302 QCryptoTLSCreds
*tlscreds
= NULL
;
303 const char *hostname
= NULL
;
306 /* Pop the config into our state object. Exit if invalid. */
307 saddr
= nbd_config(s
, options
, &export
, errp
);
312 tlscredsid
= g_strdup(qdict_get_try_str(options
, "tls-creds"));
314 qdict_del(options
, "tls-creds");
315 tlscreds
= nbd_get_tls_creds(tlscredsid
, errp
);
320 if (saddr
->type
!= SOCKET_ADDRESS_KIND_INET
) {
321 error_setg(errp
, "TLS only supported over IP sockets");
324 hostname
= saddr
->u
.inet
->host
;
327 /* establish TCP connection, return error if it fails
328 * TODO: Configurable retry-until-timeout behaviour.
330 sioc
= nbd_establish_connection(saddr
, errp
);
337 ret
= nbd_client_init(bs
, sioc
, export
,
338 tlscreds
, hostname
, errp
);
341 object_unref(OBJECT(sioc
));
344 object_unref(OBJECT(tlscreds
));
346 qapi_free_SocketAddress(saddr
);
351 static int nbd_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
352 int nb_sectors
, QEMUIOVector
*qiov
)
354 return nbd_client_co_readv(bs
, sector_num
, nb_sectors
, qiov
);
357 static int nbd_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
358 int nb_sectors
, QEMUIOVector
*qiov
)
360 return nbd_client_co_writev(bs
, sector_num
, nb_sectors
, qiov
);
363 static int nbd_co_flush(BlockDriverState
*bs
)
365 return nbd_client_co_flush(bs
);
368 static void nbd_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
370 bs
->bl
.max_discard
= UINT32_MAX
>> BDRV_SECTOR_BITS
;
371 bs
->bl
.max_transfer_length
= UINT32_MAX
>> BDRV_SECTOR_BITS
;
374 static int nbd_co_discard(BlockDriverState
*bs
, int64_t sector_num
,
377 return nbd_client_co_discard(bs
, sector_num
, nb_sectors
);
380 static void nbd_close(BlockDriverState
*bs
)
382 nbd_client_close(bs
);
385 static int64_t nbd_getlength(BlockDriverState
*bs
)
387 BDRVNBDState
*s
= bs
->opaque
;
389 return s
->client
.size
;
392 static void nbd_detach_aio_context(BlockDriverState
*bs
)
394 nbd_client_detach_aio_context(bs
);
397 static void nbd_attach_aio_context(BlockDriverState
*bs
,
398 AioContext
*new_context
)
400 nbd_client_attach_aio_context(bs
, new_context
);
403 static void nbd_refresh_filename(BlockDriverState
*bs
, QDict
*options
)
405 QDict
*opts
= qdict_new();
406 const char *path
= qdict_get_try_str(options
, "path");
407 const char *host
= qdict_get_try_str(options
, "host");
408 const char *port
= qdict_get_try_str(options
, "port");
409 const char *export
= qdict_get_try_str(options
, "export");
410 const char *tlscreds
= qdict_get_try_str(options
, "tls-creds");
412 qdict_put_obj(opts
, "driver", QOBJECT(qstring_from_str("nbd")));
414 if (path
&& export
) {
415 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
416 "nbd+unix:///%s?socket=%s", export
, path
);
417 } else if (path
&& !export
) {
418 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
419 "nbd+unix://?socket=%s", path
);
420 } else if (!path
&& export
&& port
) {
421 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
422 "nbd://%s:%s/%s", host
, port
, export
);
423 } else if (!path
&& export
&& !port
) {
424 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
425 "nbd://%s/%s", host
, export
);
426 } else if (!path
&& !export
&& port
) {
427 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
428 "nbd://%s:%s", host
, port
);
429 } else if (!path
&& !export
&& !port
) {
430 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
435 qdict_put_obj(opts
, "path", QOBJECT(qstring_from_str(path
)));
437 qdict_put_obj(opts
, "host", QOBJECT(qstring_from_str(host
)));
438 qdict_put_obj(opts
, "port", QOBJECT(qstring_from_str(port
)));
440 qdict_put_obj(opts
, "host", QOBJECT(qstring_from_str(host
)));
443 qdict_put_obj(opts
, "export", QOBJECT(qstring_from_str(export
)));
446 qdict_put_obj(opts
, "tls-creds", QOBJECT(qstring_from_str(tlscreds
)));
449 bs
->full_open_options
= opts
;
452 static BlockDriver bdrv_nbd
= {
453 .format_name
= "nbd",
454 .protocol_name
= "nbd",
455 .instance_size
= sizeof(BDRVNBDState
),
456 .bdrv_parse_filename
= nbd_parse_filename
,
457 .bdrv_file_open
= nbd_open
,
458 .bdrv_co_readv
= nbd_co_readv
,
459 .bdrv_co_writev
= nbd_co_writev
,
460 .bdrv_close
= nbd_close
,
461 .bdrv_co_flush_to_os
= nbd_co_flush
,
462 .bdrv_co_discard
= nbd_co_discard
,
463 .bdrv_refresh_limits
= nbd_refresh_limits
,
464 .bdrv_getlength
= nbd_getlength
,
465 .bdrv_detach_aio_context
= nbd_detach_aio_context
,
466 .bdrv_attach_aio_context
= nbd_attach_aio_context
,
467 .bdrv_refresh_filename
= nbd_refresh_filename
,
470 static BlockDriver bdrv_nbd_tcp
= {
471 .format_name
= "nbd",
472 .protocol_name
= "nbd+tcp",
473 .instance_size
= sizeof(BDRVNBDState
),
474 .bdrv_parse_filename
= nbd_parse_filename
,
475 .bdrv_file_open
= nbd_open
,
476 .bdrv_co_readv
= nbd_co_readv
,
477 .bdrv_co_writev
= nbd_co_writev
,
478 .bdrv_close
= nbd_close
,
479 .bdrv_co_flush_to_os
= nbd_co_flush
,
480 .bdrv_co_discard
= nbd_co_discard
,
481 .bdrv_refresh_limits
= nbd_refresh_limits
,
482 .bdrv_getlength
= nbd_getlength
,
483 .bdrv_detach_aio_context
= nbd_detach_aio_context
,
484 .bdrv_attach_aio_context
= nbd_attach_aio_context
,
485 .bdrv_refresh_filename
= nbd_refresh_filename
,
488 static BlockDriver bdrv_nbd_unix
= {
489 .format_name
= "nbd",
490 .protocol_name
= "nbd+unix",
491 .instance_size
= sizeof(BDRVNBDState
),
492 .bdrv_parse_filename
= nbd_parse_filename
,
493 .bdrv_file_open
= nbd_open
,
494 .bdrv_co_readv
= nbd_co_readv
,
495 .bdrv_co_writev
= nbd_co_writev
,
496 .bdrv_close
= nbd_close
,
497 .bdrv_co_flush_to_os
= nbd_co_flush
,
498 .bdrv_co_discard
= nbd_co_discard
,
499 .bdrv_refresh_limits
= nbd_refresh_limits
,
500 .bdrv_getlength
= nbd_getlength
,
501 .bdrv_detach_aio_context
= nbd_detach_aio_context
,
502 .bdrv_attach_aio_context
= nbd_attach_aio_context
,
503 .bdrv_refresh_filename
= nbd_refresh_filename
,
506 static void bdrv_nbd_init(void)
508 bdrv_register(&bdrv_nbd
);
509 bdrv_register(&bdrv_nbd_tcp
);
510 bdrv_register(&bdrv_nbd_unix
);
513 block_init(bdrv_nbd_init
);