xref: /openbmc/qemu/block/nbd.c (revision 438c78da)
1 /*
2  * QEMU Block driver for  NBD
3  *
4  * Copyright (C) 2008 Bull S.A.S.
5  *     Author: Laurent Vivier <Laurent.Vivier@bull.net>
6  *
7  * Some parts:
8  *    Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
9  *
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:
16  *
17  * The above copyright notice and this permission notice shall be included in
18  * all copies or substantial portions of the Software.
19  *
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.
27  */
28 
29 #include "qemu/osdep.h"
30 #include "nbd-client.h"
31 #include "block/qdict.h"
32 #include "qapi/error.h"
33 #include "qemu/uri.h"
34 #include "block/block_int.h"
35 #include "qemu/module.h"
36 #include "qemu/option.h"
37 #include "qapi/qapi-visit-sockets.h"
38 #include "qapi/qobject-input-visitor.h"
39 #include "qapi/qobject-output-visitor.h"
40 #include "qapi/qmp/qdict.h"
41 #include "qapi/qmp/qstring.h"
42 #include "qemu/cutils.h"
43 
44 #define EN_OPTSTR ":exportname="
45 
46 typedef struct BDRVNBDState {
47     NBDClientSession client;
48 
49     /* For nbd_refresh_filename() */
50     SocketAddress *saddr;
51     char *export, *tlscredsid;
52 } BDRVNBDState;
53 
54 static int nbd_parse_uri(const char *filename, QDict *options)
55 {
56     URI *uri;
57     const char *p;
58     QueryParams *qp = NULL;
59     int ret = 0;
60     bool is_unix;
61 
62     uri = uri_parse(filename);
63     if (!uri) {
64         return -EINVAL;
65     }
66 
67     /* transport */
68     if (!g_strcmp0(uri->scheme, "nbd")) {
69         is_unix = false;
70     } else if (!g_strcmp0(uri->scheme, "nbd+tcp")) {
71         is_unix = false;
72     } else if (!g_strcmp0(uri->scheme, "nbd+unix")) {
73         is_unix = true;
74     } else {
75         ret = -EINVAL;
76         goto out;
77     }
78 
79     p = uri->path ? uri->path : "/";
80     p += strspn(p, "/");
81     if (p[0]) {
82         qdict_put_str(options, "export", p);
83     }
84 
85     qp = query_params_parse(uri->query);
86     if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) {
87         ret = -EINVAL;
88         goto out;
89     }
90 
91     if (is_unix) {
92         /* nbd+unix:///export?socket=path */
93         if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
94             ret = -EINVAL;
95             goto out;
96         }
97         qdict_put_str(options, "server.type", "unix");
98         qdict_put_str(options, "server.path", qp->p[0].value);
99     } else {
100         QString *host;
101         char *port_str;
102 
103         /* nbd[+tcp]://host[:port]/export */
104         if (!uri->server) {
105             ret = -EINVAL;
106             goto out;
107         }
108 
109         /* strip braces from literal IPv6 address */
110         if (uri->server[0] == '[') {
111             host = qstring_from_substr(uri->server, 1,
112                                        strlen(uri->server) - 1);
113         } else {
114             host = qstring_from_str(uri->server);
115         }
116 
117         qdict_put_str(options, "server.type", "inet");
118         qdict_put(options, "server.host", host);
119 
120         port_str = g_strdup_printf("%d", uri->port ?: NBD_DEFAULT_PORT);
121         qdict_put_str(options, "server.port", port_str);
122         g_free(port_str);
123     }
124 
125 out:
126     if (qp) {
127         query_params_free(qp);
128     }
129     uri_free(uri);
130     return ret;
131 }
132 
133 static bool nbd_has_filename_options_conflict(QDict *options, Error **errp)
134 {
135     const QDictEntry *e;
136 
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))
143         {
144             error_setg(errp, "Option '%s' cannot be used with a file name",
145                        e->key);
146             return true;
147         }
148     }
149 
150     return false;
151 }
152 
153 static void nbd_parse_filename(const char *filename, QDict *options,
154                                Error **errp)
155 {
156     char *file;
157     char *export_name;
158     const char *host_spec;
159     const char *unixpath;
160 
161     if (nbd_has_filename_options_conflict(options, errp)) {
162         return;
163     }
164 
165     if (strstr(filename, "://")) {
166         int ret = nbd_parse_uri(filename, options);
167         if (ret < 0) {
168             error_setg(errp, "No valid URL specified");
169         }
170         return;
171     }
172 
173     file = g_strdup(filename);
174 
175     export_name = strstr(file, EN_OPTSTR);
176     if (export_name) {
177         if (export_name[strlen(EN_OPTSTR)] == 0) {
178             goto out;
179         }
180         export_name[0] = 0; /* truncate 'file' */
181         export_name += strlen(EN_OPTSTR);
182 
183         qdict_put_str(options, "export", export_name);
184     }
185 
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:'");
189         goto out;
190     }
191 
192     if (!*host_spec) {
193         goto out;
194     }
195 
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);
200     } else {
201         InetSocketAddress *addr = g_new(InetSocketAddress, 1);
202 
203         if (inet_parse(addr, host_spec, errp)) {
204             goto out_inet;
205         }
206 
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);
210     out_inet:
211         qapi_free_InetSocketAddress(addr);
212     }
213 
214 out:
215     g_free(file);
216 }
217 
218 static bool nbd_process_legacy_socket_options(QDict *output_options,
219                                               QemuOpts *legacy_opts,
220                                               Error **errp)
221 {
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");
225     const QDictEntry *e;
226 
227     if (!path && !host && !port) {
228         return true;
229     }
230 
231     for (e = qdict_first(output_options); e; e = qdict_next(output_options, e))
232     {
233         if (strstart(e->key, "server.", NULL)) {
234             error_setg(errp, "Cannot use 'server' and path/host/port at the "
235                        "same time");
236             return false;
237         }
238     }
239 
240     if (path && host) {
241         error_setg(errp, "path and host may not be used at the same time");
242         return false;
243     } else if (path) {
244         if (port) {
245             error_setg(errp, "port may not be used without host");
246             return false;
247         }
248 
249         qdict_put_str(output_options, "server.type", "unix");
250         qdict_put_str(output_options, "server.path", path);
251     } else if (host) {
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));
256     }
257 
258     return true;
259 }
260 
261 static SocketAddress *nbd_config(BDRVNBDState *s, QDict *options,
262                                  Error **errp)
263 {
264     SocketAddress *saddr = NULL;
265     QDict *addr = NULL;
266     Visitor *iv = NULL;
267     Error *local_err = NULL;
268 
269     qdict_extract_subqdict(options, &addr, "server.");
270     if (!qdict_size(addr)) {
271         error_setg(errp, "NBD server address missing");
272         goto done;
273     }
274 
275     iv = qobject_input_visitor_new_flat_confused(addr, errp);
276     if (!iv) {
277         goto done;
278     }
279 
280     visit_type_SocketAddress(iv, NULL, &saddr, &local_err);
281     if (local_err) {
282         error_propagate(errp, local_err);
283         goto done;
284     }
285 
286 done:
287     qobject_unref(addr);
288     visit_free(iv);
289     return saddr;
290 }
291 
292 NBDClientSession *nbd_get_client_session(BlockDriverState *bs)
293 {
294     BDRVNBDState *s = bs->opaque;
295     return &s->client;
296 }
297 
298 static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr,
299                                                   Error **errp)
300 {
301     QIOChannelSocket *sioc;
302     Error *local_err = NULL;
303 
304     sioc = qio_channel_socket_new();
305     qio_channel_set_name(QIO_CHANNEL(sioc), "nbd-client");
306 
307     qio_channel_socket_connect_sync(sioc,
308                                     saddr,
309                                     &local_err);
310     if (local_err) {
311         object_unref(OBJECT(sioc));
312         error_propagate(errp, local_err);
313         return NULL;
314     }
315 
316     qio_channel_set_delay(QIO_CHANNEL(sioc), false);
317 
318     return sioc;
319 }
320 
321 
322 static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp)
323 {
324     Object *obj;
325     QCryptoTLSCreds *creds;
326 
327     obj = object_resolve_path_component(
328         object_get_objects_root(), id);
329     if (!obj) {
330         error_setg(errp, "No TLS credentials with id '%s'",
331                    id);
332         return NULL;
333     }
334     creds = (QCryptoTLSCreds *)
335         object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
336     if (!creds) {
337         error_setg(errp, "Object with id '%s' is not TLS credentials",
338                    id);
339         return NULL;
340     }
341 
342     if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
343         error_setg(errp,
344                    "Expecting TLS credentials with a client endpoint");
345         return NULL;
346     }
347     object_ref(obj);
348     return creds;
349 }
350 
351 
352 static QemuOptsList nbd_runtime_opts = {
353     .name = "nbd",
354     .head = QTAILQ_HEAD_INITIALIZER(nbd_runtime_opts.head),
355     .desc = {
356         {
357             .name = "host",
358             .type = QEMU_OPT_STRING,
359             .help = "TCP host to connect to",
360         },
361         {
362             .name = "port",
363             .type = QEMU_OPT_STRING,
364             .help = "TCP port to connect to",
365         },
366         {
367             .name = "path",
368             .type = QEMU_OPT_STRING,
369             .help = "Unix socket path to connect to",
370         },
371         {
372             .name = "export",
373             .type = QEMU_OPT_STRING,
374             .help = "Name of the NBD export to open",
375         },
376         {
377             .name = "tls-creds",
378             .type = QEMU_OPT_STRING,
379             .help = "ID of the TLS credentials to use",
380         },
381         {
382             .name = "x-dirty-bitmap",
383             .type = QEMU_OPT_STRING,
384             .help = "experimental: expose named dirty bitmap in place of "
385                     "block status",
386         },
387         { /* end of list */ }
388     },
389 };
390 
391 static int nbd_open(BlockDriverState *bs, QDict *options, int flags,
392                     Error **errp)
393 {
394     BDRVNBDState *s = bs->opaque;
395     QemuOpts *opts = NULL;
396     Error *local_err = NULL;
397     QIOChannelSocket *sioc = NULL;
398     QCryptoTLSCreds *tlscreds = NULL;
399     const char *hostname = NULL;
400     int ret = -EINVAL;
401 
402     opts = qemu_opts_create(&nbd_runtime_opts, NULL, 0, &error_abort);
403     qemu_opts_absorb_qdict(opts, options, &local_err);
404     if (local_err) {
405         error_propagate(errp, local_err);
406         goto error;
407     }
408 
409     /* Translate @host, @port, and @path to a SocketAddress */
410     if (!nbd_process_legacy_socket_options(options, opts, errp)) {
411         goto error;
412     }
413 
414     /* Pop the config into our state object. Exit if invalid. */
415     s->saddr = nbd_config(s, options, errp);
416     if (!s->saddr) {
417         goto error;
418     }
419 
420     s->export = g_strdup(qemu_opt_get(opts, "export"));
421 
422     s->tlscredsid = g_strdup(qemu_opt_get(opts, "tls-creds"));
423     if (s->tlscredsid) {
424         tlscreds = nbd_get_tls_creds(s->tlscredsid, errp);
425         if (!tlscreds) {
426             goto error;
427         }
428 
429         /* TODO SOCKET_ADDRESS_KIND_FD where fd has AF_INET or AF_INET6 */
430         if (s->saddr->type != SOCKET_ADDRESS_TYPE_INET) {
431             error_setg(errp, "TLS only supported over IP sockets");
432             goto error;
433         }
434         hostname = s->saddr->u.inet.host;
435     }
436 
437     /* establish TCP connection, return error if it fails
438      * TODO: Configurable retry-until-timeout behaviour.
439      */
440     sioc = nbd_establish_connection(s->saddr, errp);
441     if (!sioc) {
442         ret = -ECONNREFUSED;
443         goto error;
444     }
445 
446     /* NBD handshake */
447     ret = nbd_client_init(bs, sioc, s->export, tlscreds, hostname,
448                           qemu_opt_get(opts, "x-dirty-bitmap"), errp);
449  error:
450     if (sioc) {
451         object_unref(OBJECT(sioc));
452     }
453     if (tlscreds) {
454         object_unref(OBJECT(tlscreds));
455     }
456     if (ret < 0) {
457         qapi_free_SocketAddress(s->saddr);
458         g_free(s->export);
459         g_free(s->tlscredsid);
460     }
461     qemu_opts_del(opts);
462     return ret;
463 }
464 
465 static int nbd_co_flush(BlockDriverState *bs)
466 {
467     return nbd_client_co_flush(bs);
468 }
469 
470 static void nbd_refresh_limits(BlockDriverState *bs, Error **errp)
471 {
472     NBDClientSession *s = nbd_get_client_session(bs);
473     uint32_t min = s->info.min_block;
474     uint32_t max = MIN_NON_ZERO(NBD_MAX_BUFFER_SIZE, s->info.max_block);
475 
476     bs->bl.request_alignment = min ? min : BDRV_SECTOR_SIZE;
477     bs->bl.max_pdiscard = max;
478     bs->bl.max_pwrite_zeroes = max;
479     bs->bl.max_transfer = max;
480 
481     if (s->info.opt_block &&
482         s->info.opt_block > bs->bl.opt_transfer) {
483         bs->bl.opt_transfer = s->info.opt_block;
484     }
485 }
486 
487 static void nbd_close(BlockDriverState *bs)
488 {
489     BDRVNBDState *s = bs->opaque;
490 
491     nbd_client_close(bs);
492 
493     qapi_free_SocketAddress(s->saddr);
494     g_free(s->export);
495     g_free(s->tlscredsid);
496 }
497 
498 static int64_t nbd_getlength(BlockDriverState *bs)
499 {
500     BDRVNBDState *s = bs->opaque;
501 
502     return s->client.info.size;
503 }
504 
505 static void nbd_detach_aio_context(BlockDriverState *bs)
506 {
507     nbd_client_detach_aio_context(bs);
508 }
509 
510 static void nbd_attach_aio_context(BlockDriverState *bs,
511                                    AioContext *new_context)
512 {
513     nbd_client_attach_aio_context(bs, new_context);
514 }
515 
516 static void nbd_refresh_filename(BlockDriverState *bs, QDict *options)
517 {
518     BDRVNBDState *s = bs->opaque;
519     QDict *opts = qdict_new();
520     QObject *saddr_qdict;
521     Visitor *ov;
522     const char *host = NULL, *port = NULL, *path = NULL;
523 
524     if (s->saddr->type == SOCKET_ADDRESS_TYPE_INET) {
525         const InetSocketAddress *inet = &s->saddr->u.inet;
526         if (!inet->has_ipv4 && !inet->has_ipv6 && !inet->has_to) {
527             host = inet->host;
528             port = inet->port;
529         }
530     } else if (s->saddr->type == SOCKET_ADDRESS_TYPE_UNIX) {
531         path = s->saddr->u.q_unix.path;
532     } /* else can't represent as pseudo-filename */
533 
534     qdict_put_str(opts, "driver", "nbd");
535 
536     if (path && s->export) {
537         snprintf(bs->exact_filename, sizeof(bs->exact_filename),
538                  "nbd+unix:///%s?socket=%s", s->export, path);
539     } else if (path && !s->export) {
540         snprintf(bs->exact_filename, sizeof(bs->exact_filename),
541                  "nbd+unix://?socket=%s", path);
542     } else if (host && s->export) {
543         snprintf(bs->exact_filename, sizeof(bs->exact_filename),
544                  "nbd://%s:%s/%s", host, port, s->export);
545     } else if (host && !s->export) {
546         snprintf(bs->exact_filename, sizeof(bs->exact_filename),
547                  "nbd://%s:%s", host, port);
548     }
549 
550     ov = qobject_output_visitor_new(&saddr_qdict);
551     visit_type_SocketAddress(ov, NULL, &s->saddr, &error_abort);
552     visit_complete(ov, &saddr_qdict);
553     visit_free(ov);
554     qdict_put_obj(opts, "server", saddr_qdict);
555 
556     if (s->export) {
557         qdict_put_str(opts, "export", s->export);
558     }
559     if (s->tlscredsid) {
560         qdict_put_str(opts, "tls-creds", s->tlscredsid);
561     }
562 
563     qdict_flatten(opts);
564     bs->full_open_options = opts;
565 }
566 
567 static BlockDriver bdrv_nbd = {
568     .format_name                = "nbd",
569     .protocol_name              = "nbd",
570     .instance_size              = sizeof(BDRVNBDState),
571     .bdrv_parse_filename        = nbd_parse_filename,
572     .bdrv_file_open             = nbd_open,
573     .bdrv_co_preadv             = nbd_client_co_preadv,
574     .bdrv_co_pwritev            = nbd_client_co_pwritev,
575     .bdrv_co_pwrite_zeroes      = nbd_client_co_pwrite_zeroes,
576     .bdrv_close                 = nbd_close,
577     .bdrv_co_flush_to_os        = nbd_co_flush,
578     .bdrv_co_pdiscard           = nbd_client_co_pdiscard,
579     .bdrv_refresh_limits        = nbd_refresh_limits,
580     .bdrv_getlength             = nbd_getlength,
581     .bdrv_detach_aio_context    = nbd_detach_aio_context,
582     .bdrv_attach_aio_context    = nbd_attach_aio_context,
583     .bdrv_refresh_filename      = nbd_refresh_filename,
584     .bdrv_co_block_status       = nbd_client_co_block_status,
585 };
586 
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,
604     .bdrv_co_block_status       = nbd_client_co_block_status,
605 };
606 
607 static BlockDriver bdrv_nbd_unix = {
608     .format_name                = "nbd",
609     .protocol_name              = "nbd+unix",
610     .instance_size              = sizeof(BDRVNBDState),
611     .bdrv_parse_filename        = nbd_parse_filename,
612     .bdrv_file_open             = nbd_open,
613     .bdrv_co_preadv             = nbd_client_co_preadv,
614     .bdrv_co_pwritev            = nbd_client_co_pwritev,
615     .bdrv_co_pwrite_zeroes      = nbd_client_co_pwrite_zeroes,
616     .bdrv_close                 = nbd_close,
617     .bdrv_co_flush_to_os        = nbd_co_flush,
618     .bdrv_co_pdiscard           = nbd_client_co_pdiscard,
619     .bdrv_refresh_limits        = nbd_refresh_limits,
620     .bdrv_getlength             = nbd_getlength,
621     .bdrv_detach_aio_context    = nbd_detach_aio_context,
622     .bdrv_attach_aio_context    = nbd_attach_aio_context,
623     .bdrv_refresh_filename      = nbd_refresh_filename,
624     .bdrv_co_block_status       = nbd_client_co_block_status,
625 };
626 
627 static void bdrv_nbd_init(void)
628 {
629     bdrv_register(&bdrv_nbd);
630     bdrv_register(&bdrv_nbd_tcp);
631     bdrv_register(&bdrv_nbd_unix);
632 }
633 
634 block_init(bdrv_nbd_init);
635