nbd: set name for all I/O channels created
[qemu/kevin.git] / block / nbd.c
blob1ec64ab95e08973eaab49c605cab61993163ca8b
1 /*
2 * QEMU Block driver for NBD
4 * Copyright (C) 2008 Bull S.A.S.
5 * Author: Laurent Vivier <Laurent.Vivier@bull.net>
7 * Some parts:
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
26 * THE SOFTWARE.
29 #include "qemu/osdep.h"
30 #include "block/nbd-client.h"
31 #include "qapi/error.h"
32 #include "qemu/uri.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;
46 /* For nbd_refresh_filename() */
47 char *path, *host, *port, *export, *tlscredsid;
48 } BDRVNBDState;
50 static int nbd_parse_uri(const char *filename, QDict *options)
52 URI *uri;
53 const char *p;
54 QueryParams *qp = NULL;
55 int ret = 0;
56 bool is_unix;
58 uri = uri_parse(filename);
59 if (!uri) {
60 return -EINVAL;
63 /* transport */
64 if (!strcmp(uri->scheme, "nbd")) {
65 is_unix = false;
66 } else if (!strcmp(uri->scheme, "nbd+tcp")) {
67 is_unix = false;
68 } else if (!strcmp(uri->scheme, "nbd+unix")) {
69 is_unix = true;
70 } else {
71 ret = -EINVAL;
72 goto out;
75 p = uri->path ? uri->path : "/";
76 p += strspn(p, "/");
77 if (p[0]) {
78 qdict_put(options, "export", qstring_from_str(p));
81 qp = query_params_parse(uri->query);
82 if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) {
83 ret = -EINVAL;
84 goto out;
87 if (is_unix) {
88 /* nbd+unix:///export?socket=path */
89 if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
90 ret = -EINVAL;
91 goto out;
93 qdict_put(options, "path", qstring_from_str(qp->p[0].value));
94 } else {
95 QString *host;
96 /* nbd[+tcp]://host[:port]/export */
97 if (!uri->server) {
98 ret = -EINVAL;
99 goto out;
102 /* strip braces from literal IPv6 address */
103 if (uri->server[0] == '[') {
104 host = qstring_from_substr(uri->server, 1,
105 strlen(uri->server) - 2);
106 } else {
107 host = qstring_from_str(uri->server);
110 qdict_put(options, "host", host);
111 if (uri->port) {
112 char* port_str = g_strdup_printf("%d", uri->port);
113 qdict_put(options, "port", qstring_from_str(port_str));
114 g_free(port_str);
118 out:
119 if (qp) {
120 query_params_free(qp);
122 uri_free(uri);
123 return ret;
126 static void nbd_parse_filename(const char *filename, QDict *options,
127 Error **errp)
129 char *file;
130 char *export_name;
131 const char *host_spec;
132 const char *unixpath;
134 if (qdict_haskey(options, "host")
135 || qdict_haskey(options, "port")
136 || qdict_haskey(options, "path"))
138 error_setg(errp, "host/port/path and a file name may not be specified "
139 "at the same time");
140 return;
143 if (strstr(filename, "://")) {
144 int ret = nbd_parse_uri(filename, options);
145 if (ret < 0) {
146 error_setg(errp, "No valid URL specified");
148 return;
151 file = g_strdup(filename);
153 export_name = strstr(file, EN_OPTSTR);
154 if (export_name) {
155 if (export_name[strlen(EN_OPTSTR)] == 0) {
156 goto out;
158 export_name[0] = 0; /* truncate 'file' */
159 export_name += strlen(EN_OPTSTR);
161 qdict_put(options, "export", qstring_from_str(export_name));
164 /* extract the host_spec - fail if it's not nbd:... */
165 if (!strstart(file, "nbd:", &host_spec)) {
166 error_setg(errp, "File name string for NBD must start with 'nbd:'");
167 goto out;
170 if (!*host_spec) {
171 goto out;
174 /* are we a UNIX or TCP socket? */
175 if (strstart(host_spec, "unix:", &unixpath)) {
176 qdict_put(options, "path", qstring_from_str(unixpath));
177 } else {
178 InetSocketAddress *addr = NULL;
180 addr = inet_parse(host_spec, errp);
181 if (!addr) {
182 goto out;
185 qdict_put(options, "host", qstring_from_str(addr->host));
186 qdict_put(options, "port", qstring_from_str(addr->port));
187 qapi_free_InetSocketAddress(addr);
190 out:
191 g_free(file);
194 static SocketAddress *nbd_config(BDRVNBDState *s, QemuOpts *opts, Error **errp)
196 SocketAddress *saddr;
198 s->path = g_strdup(qemu_opt_get(opts, "path"));
199 s->host = g_strdup(qemu_opt_get(opts, "host"));
201 if (!s->path == !s->host) {
202 if (s->path) {
203 error_setg(errp, "path and host may not be used at the same time.");
204 } else {
205 error_setg(errp, "one of path and host must be specified.");
207 return NULL;
210 saddr = g_new0(SocketAddress, 1);
212 if (s->path) {
213 UnixSocketAddress *q_unix;
214 saddr->type = SOCKET_ADDRESS_KIND_UNIX;
215 q_unix = saddr->u.q_unix.data = g_new0(UnixSocketAddress, 1);
216 q_unix->path = g_strdup(s->path);
217 } else {
218 InetSocketAddress *inet;
220 s->port = g_strdup(qemu_opt_get(opts, "port"));
222 saddr->type = SOCKET_ADDRESS_KIND_INET;
223 inet = saddr->u.inet.data = g_new0(InetSocketAddress, 1);
224 inet->host = g_strdup(s->host);
225 inet->port = g_strdup(s->port);
226 if (!inet->port) {
227 inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
231 s->client.is_unix = saddr->type == SOCKET_ADDRESS_KIND_UNIX;
233 s->export = g_strdup(qemu_opt_get(opts, "export"));
235 return saddr;
238 NbdClientSession *nbd_get_client_session(BlockDriverState *bs)
240 BDRVNBDState *s = bs->opaque;
241 return &s->client;
244 static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr,
245 Error **errp)
247 QIOChannelSocket *sioc;
248 Error *local_err = NULL;
250 sioc = qio_channel_socket_new();
251 qio_channel_set_name(QIO_CHANNEL(sioc), "nbd-client");
253 qio_channel_socket_connect_sync(sioc,
254 saddr,
255 &local_err);
256 if (local_err) {
257 error_propagate(errp, local_err);
258 return NULL;
261 qio_channel_set_delay(QIO_CHANNEL(sioc), false);
263 return sioc;
267 static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp)
269 Object *obj;
270 QCryptoTLSCreds *creds;
272 obj = object_resolve_path_component(
273 object_get_objects_root(), id);
274 if (!obj) {
275 error_setg(errp, "No TLS credentials with id '%s'",
276 id);
277 return NULL;
279 creds = (QCryptoTLSCreds *)
280 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
281 if (!creds) {
282 error_setg(errp, "Object with id '%s' is not TLS credentials",
283 id);
284 return NULL;
287 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
288 error_setg(errp,
289 "Expecting TLS credentials with a client endpoint");
290 return NULL;
292 object_ref(obj);
293 return creds;
297 static QemuOptsList nbd_runtime_opts = {
298 .name = "nbd",
299 .head = QTAILQ_HEAD_INITIALIZER(nbd_runtime_opts.head),
300 .desc = {
302 .name = "host",
303 .type = QEMU_OPT_STRING,
304 .help = "TCP host to connect to",
307 .name = "port",
308 .type = QEMU_OPT_STRING,
309 .help = "TCP port to connect to",
312 .name = "path",
313 .type = QEMU_OPT_STRING,
314 .help = "Unix socket path to connect to",
317 .name = "export",
318 .type = QEMU_OPT_STRING,
319 .help = "Name of the NBD export to open",
322 .name = "tls-creds",
323 .type = QEMU_OPT_STRING,
324 .help = "ID of the TLS credentials to use",
329 static int nbd_open(BlockDriverState *bs, QDict *options, int flags,
330 Error **errp)
332 BDRVNBDState *s = bs->opaque;
333 QemuOpts *opts = NULL;
334 Error *local_err = NULL;
335 QIOChannelSocket *sioc = NULL;
336 SocketAddress *saddr = NULL;
337 QCryptoTLSCreds *tlscreds = NULL;
338 const char *hostname = NULL;
339 int ret = -EINVAL;
341 opts = qemu_opts_create(&nbd_runtime_opts, NULL, 0, &error_abort);
342 qemu_opts_absorb_qdict(opts, options, &local_err);
343 if (local_err) {
344 error_propagate(errp, local_err);
345 goto error;
348 /* Pop the config into our state object. Exit if invalid. */
349 saddr = nbd_config(s, opts, errp);
350 if (!saddr) {
351 goto error;
354 s->tlscredsid = g_strdup(qemu_opt_get(opts, "tls-creds"));
355 if (s->tlscredsid) {
356 tlscreds = nbd_get_tls_creds(s->tlscredsid, errp);
357 if (!tlscreds) {
358 goto error;
361 if (saddr->type != SOCKET_ADDRESS_KIND_INET) {
362 error_setg(errp, "TLS only supported over IP sockets");
363 goto error;
365 hostname = saddr->u.inet.data->host;
368 /* establish TCP connection, return error if it fails
369 * TODO: Configurable retry-until-timeout behaviour.
371 sioc = nbd_establish_connection(saddr, errp);
372 if (!sioc) {
373 ret = -ECONNREFUSED;
374 goto error;
377 /* NBD handshake */
378 ret = nbd_client_init(bs, sioc, s->export,
379 tlscreds, hostname, errp);
380 error:
381 if (sioc) {
382 object_unref(OBJECT(sioc));
384 if (tlscreds) {
385 object_unref(OBJECT(tlscreds));
387 if (ret < 0) {
388 g_free(s->path);
389 g_free(s->host);
390 g_free(s->port);
391 g_free(s->export);
392 g_free(s->tlscredsid);
394 qapi_free_SocketAddress(saddr);
395 qemu_opts_del(opts);
396 return ret;
399 static int nbd_co_flush(BlockDriverState *bs)
401 return nbd_client_co_flush(bs);
404 static void nbd_refresh_limits(BlockDriverState *bs, Error **errp)
406 bs->bl.max_pdiscard = NBD_MAX_BUFFER_SIZE;
407 bs->bl.max_transfer = NBD_MAX_BUFFER_SIZE;
410 static void nbd_close(BlockDriverState *bs)
412 BDRVNBDState *s = bs->opaque;
414 nbd_client_close(bs);
416 g_free(s->path);
417 g_free(s->host);
418 g_free(s->port);
419 g_free(s->export);
420 g_free(s->tlscredsid);
423 static int64_t nbd_getlength(BlockDriverState *bs)
425 BDRVNBDState *s = bs->opaque;
427 return s->client.size;
430 static void nbd_detach_aio_context(BlockDriverState *bs)
432 nbd_client_detach_aio_context(bs);
435 static void nbd_attach_aio_context(BlockDriverState *bs,
436 AioContext *new_context)
438 nbd_client_attach_aio_context(bs, new_context);
441 static void nbd_refresh_filename(BlockDriverState *bs, QDict *options)
443 BDRVNBDState *s = bs->opaque;
444 QDict *opts = qdict_new();
446 qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("nbd")));
448 if (s->path && s->export) {
449 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
450 "nbd+unix:///%s?socket=%s", s->export, s->path);
451 } else if (s->path && !s->export) {
452 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
453 "nbd+unix://?socket=%s", s->path);
454 } else if (!s->path && s->export && s->port) {
455 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
456 "nbd://%s:%s/%s", s->host, s->port, s->export);
457 } else if (!s->path && s->export && !s->port) {
458 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
459 "nbd://%s/%s", s->host, s->export);
460 } else if (!s->path && !s->export && s->port) {
461 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
462 "nbd://%s:%s", s->host, s->port);
463 } else if (!s->path && !s->export && !s->port) {
464 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
465 "nbd://%s", s->host);
468 if (s->path) {
469 qdict_put_obj(opts, "path", QOBJECT(qstring_from_str(s->path)));
470 } else if (s->port) {
471 qdict_put_obj(opts, "host", QOBJECT(qstring_from_str(s->host)));
472 qdict_put_obj(opts, "port", QOBJECT(qstring_from_str(s->port)));
473 } else {
474 qdict_put_obj(opts, "host", QOBJECT(qstring_from_str(s->host)));
476 if (s->export) {
477 qdict_put_obj(opts, "export", QOBJECT(qstring_from_str(s->export)));
479 if (s->tlscredsid) {
480 qdict_put_obj(opts, "tls-creds",
481 QOBJECT(qstring_from_str(s->tlscredsid)));
484 bs->full_open_options = opts;
487 static BlockDriver bdrv_nbd = {
488 .format_name = "nbd",
489 .protocol_name = "nbd",
490 .instance_size = sizeof(BDRVNBDState),
491 .bdrv_parse_filename = nbd_parse_filename,
492 .bdrv_file_open = nbd_open,
493 .bdrv_co_preadv = nbd_client_co_preadv,
494 .bdrv_co_pwritev = nbd_client_co_pwritev,
495 .bdrv_close = nbd_close,
496 .bdrv_co_flush_to_os = nbd_co_flush,
497 .bdrv_co_pdiscard = nbd_client_co_pdiscard,
498 .bdrv_refresh_limits = nbd_refresh_limits,
499 .bdrv_getlength = nbd_getlength,
500 .bdrv_detach_aio_context = nbd_detach_aio_context,
501 .bdrv_attach_aio_context = nbd_attach_aio_context,
502 .bdrv_refresh_filename = nbd_refresh_filename,
505 static BlockDriver bdrv_nbd_tcp = {
506 .format_name = "nbd",
507 .protocol_name = "nbd+tcp",
508 .instance_size = sizeof(BDRVNBDState),
509 .bdrv_parse_filename = nbd_parse_filename,
510 .bdrv_file_open = nbd_open,
511 .bdrv_co_preadv = nbd_client_co_preadv,
512 .bdrv_co_pwritev = nbd_client_co_pwritev,
513 .bdrv_close = nbd_close,
514 .bdrv_co_flush_to_os = nbd_co_flush,
515 .bdrv_co_pdiscard = nbd_client_co_pdiscard,
516 .bdrv_refresh_limits = nbd_refresh_limits,
517 .bdrv_getlength = nbd_getlength,
518 .bdrv_detach_aio_context = nbd_detach_aio_context,
519 .bdrv_attach_aio_context = nbd_attach_aio_context,
520 .bdrv_refresh_filename = nbd_refresh_filename,
523 static BlockDriver bdrv_nbd_unix = {
524 .format_name = "nbd",
525 .protocol_name = "nbd+unix",
526 .instance_size = sizeof(BDRVNBDState),
527 .bdrv_parse_filename = nbd_parse_filename,
528 .bdrv_file_open = nbd_open,
529 .bdrv_co_preadv = nbd_client_co_preadv,
530 .bdrv_co_pwritev = nbd_client_co_pwritev,
531 .bdrv_close = nbd_close,
532 .bdrv_co_flush_to_os = nbd_co_flush,
533 .bdrv_co_pdiscard = nbd_client_co_pdiscard,
534 .bdrv_refresh_limits = nbd_refresh_limits,
535 .bdrv_getlength = nbd_getlength,
536 .bdrv_detach_aio_context = nbd_detach_aio_context,
537 .bdrv_attach_aio_context = nbd_attach_aio_context,
538 .bdrv_refresh_filename = nbd_refresh_filename,
541 static void bdrv_nbd_init(void)
543 bdrv_register(&bdrv_nbd);
544 bdrv_register(&bdrv_nbd_tcp);
545 bdrv_register(&bdrv_nbd_unix);
548 block_init(bdrv_nbd_init);