blob: 55124239df5286b8398b0555f70fe26158e287bb [file] [log] [blame]
ths75818252008-07-03 13:41:03 +00001/*
2 * QEMU Block driver for NBD
3 *
4 * Copyright (C) 2008 Bull S.A.S.
malcbd5921b2008-07-08 18:57:05 +00005 * Author: Laurent Vivier <Laurent.Vivier@bull.net>
ths75818252008-07-03 13:41:03 +00006 *
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
Marc-André Lureau2302c1c2013-12-01 22:23:41 +010029#include "block/nbd-client.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010030#include "qemu/uri.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010031#include "block/block_int.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010032#include "qemu/module.h"
33#include "qemu/sockets.h"
Kevin Wolff53a1fe2013-03-07 16:15:11 +010034#include "qapi/qmp/qjson.h"
35#include "qapi/qmp/qint.h"
ths75818252008-07-03 13:41:03 +000036
37#include <sys/types.h>
38#include <unistd.h>
ths75818252008-07-03 13:41:03 +000039
Laurent Vivier1d45f8b2010-08-25 22:48:33 +020040#define EN_OPTSTR ":exportname="
41
ths75818252008-07-03 13:41:03 +000042typedef struct BDRVNBDState {
Marc-André Lureau2302c1c2013-12-01 22:23:41 +010043 NbdClientSession client;
Kevin Wolff53a1fe2013-03-07 16:15:11 +010044 QemuOpts *socket_opts;
ths75818252008-07-03 13:41:03 +000045} BDRVNBDState;
46
Kevin Wolff53a1fe2013-03-07 16:15:11 +010047static int nbd_parse_uri(const char *filename, QDict *options)
Paolo Bonzini1d7d2a92012-11-04 13:04:24 +010048{
49 URI *uri;
50 const char *p;
51 QueryParams *qp = NULL;
52 int ret = 0;
Kevin Wolff53a1fe2013-03-07 16:15:11 +010053 bool is_unix;
Paolo Bonzini1d7d2a92012-11-04 13:04:24 +010054
55 uri = uri_parse(filename);
56 if (!uri) {
57 return -EINVAL;
58 }
59
60 /* transport */
61 if (!strcmp(uri->scheme, "nbd")) {
Kevin Wolff53a1fe2013-03-07 16:15:11 +010062 is_unix = false;
Paolo Bonzini1d7d2a92012-11-04 13:04:24 +010063 } else if (!strcmp(uri->scheme, "nbd+tcp")) {
Kevin Wolff53a1fe2013-03-07 16:15:11 +010064 is_unix = false;
Paolo Bonzini1d7d2a92012-11-04 13:04:24 +010065 } else if (!strcmp(uri->scheme, "nbd+unix")) {
Kevin Wolff53a1fe2013-03-07 16:15:11 +010066 is_unix = true;
Paolo Bonzini1d7d2a92012-11-04 13:04:24 +010067 } else {
68 ret = -EINVAL;
69 goto out;
70 }
71
72 p = uri->path ? uri->path : "/";
73 p += strspn(p, "/");
74 if (p[0]) {
Kevin Wolff53a1fe2013-03-07 16:15:11 +010075 qdict_put(options, "export", qstring_from_str(p));
Paolo Bonzini1d7d2a92012-11-04 13:04:24 +010076 }
77
78 qp = query_params_parse(uri->query);
Kevin Wolff53a1fe2013-03-07 16:15:11 +010079 if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) {
Paolo Bonzini1d7d2a92012-11-04 13:04:24 +010080 ret = -EINVAL;
81 goto out;
82 }
83
Kevin Wolff53a1fe2013-03-07 16:15:11 +010084 if (is_unix) {
Paolo Bonzini1d7d2a92012-11-04 13:04:24 +010085 /* nbd+unix:///export?socket=path */
86 if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
87 ret = -EINVAL;
88 goto out;
89 }
Kevin Wolff53a1fe2013-03-07 16:15:11 +010090 qdict_put(options, "path", qstring_from_str(qp->p[0].value));
Paolo Bonzini1d7d2a92012-11-04 13:04:24 +010091 } else {
Ján Tomko23307902013-06-03 17:54:56 +020092 QString *host;
Kevin Wolfbebbf7f2013-03-18 16:56:05 +010093 /* nbd[+tcp]://host[:port]/export */
Paolo Bonzini1d7d2a92012-11-04 13:04:24 +010094 if (!uri->server) {
95 ret = -EINVAL;
96 goto out;
97 }
Kevin Wolff17c90b2013-03-15 11:55:29 +010098
Ján Tomko23307902013-06-03 17:54:56 +020099 /* strip braces from literal IPv6 address */
100 if (uri->server[0] == '[') {
101 host = qstring_from_substr(uri->server, 1,
102 strlen(uri->server) - 2);
103 } else {
104 host = qstring_from_str(uri->server);
105 }
106
107 qdict_put(options, "host", host);
Kevin Wolfbebbf7f2013-03-18 16:56:05 +0100108 if (uri->port) {
109 char* port_str = g_strdup_printf("%d", uri->port);
110 qdict_put(options, "port", qstring_from_str(port_str));
111 g_free(port_str);
112 }
Paolo Bonzini1d7d2a92012-11-04 13:04:24 +0100113 }
114
115out:
116 if (qp) {
117 query_params_free(qp);
118 }
119 uri_free(uri);
120 return ret;
121}
122
Kevin Wolf6963a302013-03-15 18:47:22 +0100123static void nbd_parse_filename(const char *filename, QDict *options,
124 Error **errp)
ths75818252008-07-03 13:41:03 +0000125{
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200126 char *file;
Nick Thomas33897dc2011-02-22 15:44:54 +0000127 char *export_name;
128 const char *host_spec;
ths75818252008-07-03 13:41:03 +0000129 const char *unixpath;
ths75818252008-07-03 13:41:03 +0000130
Kevin Wolf681e7ad2013-03-20 19:23:23 +0100131 if (qdict_haskey(options, "host")
132 || qdict_haskey(options, "port")
133 || qdict_haskey(options, "path"))
134 {
135 error_setg(errp, "host/port/path and a file name may not be specified "
136 "at the same time");
137 return;
138 }
139
Paolo Bonzini1d7d2a92012-11-04 13:04:24 +0100140 if (strstr(filename, "://")) {
Kevin Wolf6963a302013-03-15 18:47:22 +0100141 int ret = nbd_parse_uri(filename, options);
142 if (ret < 0) {
143 error_setg(errp, "No valid URL specified");
144 }
145 return;
Paolo Bonzini1d7d2a92012-11-04 13:04:24 +0100146 }
147
Anthony Liguori7267c092011-08-20 22:09:37 -0500148 file = g_strdup(filename);
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200149
Nick Thomas33897dc2011-02-22 15:44:54 +0000150 export_name = strstr(file, EN_OPTSTR);
151 if (export_name) {
152 if (export_name[strlen(EN_OPTSTR)] == 0) {
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200153 goto out;
154 }
Nick Thomas33897dc2011-02-22 15:44:54 +0000155 export_name[0] = 0; /* truncate 'file' */
156 export_name += strlen(EN_OPTSTR);
Kevin Wolff53a1fe2013-03-07 16:15:11 +0100157
158 qdict_put(options, "export", qstring_from_str(export_name));
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200159 }
160
Nick Thomas33897dc2011-02-22 15:44:54 +0000161 /* extract the host_spec - fail if it's not nbd:... */
162 if (!strstart(file, "nbd:", &host_spec)) {
Kevin Wolf6963a302013-03-15 18:47:22 +0100163 error_setg(errp, "File name string for NBD must start with 'nbd:'");
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200164 goto out;
165 }
ths75818252008-07-03 13:41:03 +0000166
Kevin Wolff53a1fe2013-03-07 16:15:11 +0100167 if (!*host_spec) {
Kevin Wolff53a1fe2013-03-07 16:15:11 +0100168 goto out;
169 }
170
Nick Thomas33897dc2011-02-22 15:44:54 +0000171 /* are we a UNIX or TCP socket? */
172 if (strstart(host_spec, "unix:", &unixpath)) {
Kevin Wolff53a1fe2013-03-07 16:15:11 +0100173 qdict_put(options, "path", qstring_from_str(unixpath));
ths75818252008-07-03 13:41:03 +0000174 } else {
Kevin Wolff53a1fe2013-03-07 16:15:11 +0100175 InetSocketAddress *addr = NULL;
176
Kevin Wolf6963a302013-03-15 18:47:22 +0100177 addr = inet_parse(host_spec, errp);
178 if (error_is_set(errp)) {
Kevin Wolff17c90b2013-03-15 11:55:29 +0100179 goto out;
180 }
Kevin Wolff53a1fe2013-03-07 16:15:11 +0100181
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);
ths75818252008-07-03 13:41:03 +0000185 }
186
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200187out:
Anthony Liguori7267c092011-08-20 22:09:37 -0500188 g_free(file);
ths75818252008-07-03 13:41:03 +0000189}
190
Paolo Bonzini77e8b9c2014-02-17 14:43:49 +0100191static void nbd_config(BDRVNBDState *s, QDict *options, char **export,
192 Error **errp)
Kevin Wolff53a1fe2013-03-07 16:15:11 +0100193{
194 Error *local_err = NULL;
195
Paolo Bonzinia69d9af2014-02-17 14:43:48 +0100196 if (qdict_haskey(options, "path") == qdict_haskey(options, "host")) {
197 if (qdict_haskey(options, "path")) {
Paolo Bonzini77e8b9c2014-02-17 14:43:49 +0100198 error_setg(errp, "path and host may not be used at the same time.");
Paolo Bonzinia69d9af2014-02-17 14:43:48 +0100199 } else {
Paolo Bonzini77e8b9c2014-02-17 14:43:49 +0100200 error_setg(errp, "one of path and host must be specified.");
Kevin Wolf681e7ad2013-03-20 19:23:23 +0100201 }
Paolo Bonzini77e8b9c2014-02-17 14:43:49 +0100202 return;
Kevin Wolff53a1fe2013-03-07 16:15:11 +0100203 }
204
Paolo Bonzinia69d9af2014-02-17 14:43:48 +0100205 s->client.is_unix = qdict_haskey(options, "path");
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800206 s->socket_opts = qemu_opts_create(&socket_optslist, NULL, 0,
207 &error_abort);
Kevin Wolff53a1fe2013-03-07 16:15:11 +0100208
209 qemu_opts_absorb_qdict(s->socket_opts, options, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100210 if (local_err) {
Paolo Bonzini77e8b9c2014-02-17 14:43:49 +0100211 error_propagate(errp, local_err);
212 return;
Kevin Wolff53a1fe2013-03-07 16:15:11 +0100213 }
214
Kevin Wolfbebbf7f2013-03-18 16:56:05 +0100215 if (!qemu_opt_get(s->socket_opts, "port")) {
216 qemu_opt_set_number(s->socket_opts, "port", NBD_DEFAULT_PORT);
217 }
218
Marc-André Lureaue2bc6252013-12-01 22:23:43 +0100219 *export = g_strdup(qdict_get_try_str(options, "export"));
220 if (*export) {
Kevin Wolff53a1fe2013-03-07 16:15:11 +0100221 qdict_del(options, "export");
222 }
Kevin Wolff53a1fe2013-03-07 16:15:11 +0100223}
224
Paolo Bonzini77e8b9c2014-02-17 14:43:49 +0100225static int nbd_establish_connection(BlockDriverState *bs, Error **errp)
Nick Thomas33897dc2011-02-22 15:44:54 +0000226{
227 BDRVNBDState *s = bs->opaque;
228 int sock;
Nick Thomas33897dc2011-02-22 15:44:54 +0000229
Marc-André Lureau2302c1c2013-12-01 22:23:41 +0100230 if (s->client.is_unix) {
Paolo Bonzini77e8b9c2014-02-17 14:43:49 +0100231 sock = unix_connect_opts(s->socket_opts, errp, NULL, NULL);
Nick Thomas33897dc2011-02-22 15:44:54 +0000232 } else {
Paolo Bonzini77e8b9c2014-02-17 14:43:49 +0100233 sock = inet_connect_opts(s->socket_opts, errp, NULL, NULL);
Stefan Hajnoczi97ebbab2013-04-15 16:14:48 +0200234 if (sock >= 0) {
235 socket_set_nodelay(sock);
236 }
Nick Thomas33897dc2011-02-22 15:44:54 +0000237 }
238
239 /* Failed to establish connection */
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100240 if (sock < 0) {
Nick Thomas33897dc2011-02-22 15:44:54 +0000241 logout("Failed to establish connection to NBD server\n");
242 return -errno;
243 }
244
Marc-André Lureau2302c1c2013-12-01 22:23:41 +0100245 return sock;
Nick Thomas33897dc2011-02-22 15:44:54 +0000246}
247
Max Reitz015a1032013-09-05 14:22:29 +0200248static int nbd_open(BlockDriverState *bs, QDict *options, int flags,
249 Error **errp)
Nick Thomas33897dc2011-02-22 15:44:54 +0000250{
251 BDRVNBDState *s = bs->opaque;
Marc-André Lureaue2bc6252013-12-01 22:23:43 +0100252 char *export = NULL;
Marc-André Lureau2302c1c2013-12-01 22:23:41 +0100253 int result, sock;
Paolo Bonzini77e8b9c2014-02-17 14:43:49 +0100254 Error *local_err = NULL;
Paolo Bonziniae255e52011-09-08 14:28:59 +0200255
Nick Thomas33897dc2011-02-22 15:44:54 +0000256 /* Pop the config into our state object. Exit if invalid. */
Paolo Bonzini77e8b9c2014-02-17 14:43:49 +0100257 nbd_config(s, options, &export, &local_err);
258 if (local_err) {
259 error_propagate(errp, local_err);
260 return -EINVAL;
Nick Thomas33897dc2011-02-22 15:44:54 +0000261 }
262
263 /* establish TCP connection, return error if it fails
264 * TODO: Configurable retry-until-timeout behaviour.
265 */
Paolo Bonzini77e8b9c2014-02-17 14:43:49 +0100266 sock = nbd_establish_connection(bs, errp);
Marc-André Lureau2302c1c2013-12-01 22:23:41 +0100267 if (sock < 0) {
268 return sock;
Paolo Bonzini2c7989a2011-10-21 13:16:28 +0200269 }
270
Marc-André Lureau2302c1c2013-12-01 22:23:41 +0100271 /* NBD handshake */
Marc-André Lureaue2bc6252013-12-01 22:23:43 +0100272 result = nbd_client_session_init(&s->client, bs, sock, export);
273 g_free(export);
274 return result;
Paolo Bonzinie183ef72011-10-20 13:16:23 +0200275}
276
Paolo Bonzinid9b09f12011-09-10 15:06:52 +0200277static int nbd_co_readv(BlockDriverState *bs, int64_t sector_num,
278 int nb_sectors, QEMUIOVector *qiov)
279{
Marc-André Lureau2302c1c2013-12-01 22:23:41 +0100280 BDRVNBDState *s = bs->opaque;
281
282 return nbd_client_session_co_readv(&s->client, sector_num,
283 nb_sectors, qiov);
Paolo Bonzinid9b09f12011-09-10 15:06:52 +0200284}
285
286static int nbd_co_writev(BlockDriverState *bs, int64_t sector_num,
287 int nb_sectors, QEMUIOVector *qiov)
288{
Marc-André Lureau2302c1c2013-12-01 22:23:41 +0100289 BDRVNBDState *s = bs->opaque;
290
291 return nbd_client_session_co_writev(&s->client, sector_num,
292 nb_sectors, qiov);
Paolo Bonzinid9b09f12011-09-10 15:06:52 +0200293}
294
Paolo Bonzini1486d042011-10-21 13:17:14 +0200295static int nbd_co_flush(BlockDriverState *bs)
296{
297 BDRVNBDState *s = bs->opaque;
Paolo Bonzini1486d042011-10-21 13:17:14 +0200298
Marc-André Lureau2302c1c2013-12-01 22:23:41 +0100299 return nbd_client_session_co_flush(&s->client);
Paolo Bonzini1486d042011-10-21 13:17:14 +0200300}
301
Paolo Bonzini7a706632011-10-21 13:17:14 +0200302static int nbd_co_discard(BlockDriverState *bs, int64_t sector_num,
303 int nb_sectors)
304{
305 BDRVNBDState *s = bs->opaque;
Paolo Bonzini7a706632011-10-21 13:17:14 +0200306
Marc-André Lureau2302c1c2013-12-01 22:23:41 +0100307 return nbd_client_session_co_discard(&s->client, sector_num,
308 nb_sectors);
Paolo Bonzini7a706632011-10-21 13:17:14 +0200309}
310
ths75818252008-07-03 13:41:03 +0000311static void nbd_close(BlockDriverState *bs)
312{
Nick Thomasd2d979c2011-04-28 16:20:01 +0100313 BDRVNBDState *s = bs->opaque;
Nick Thomasd2d979c2011-04-28 16:20:01 +0100314
Marc-André Lureau2302c1c2013-12-01 22:23:41 +0100315 qemu_opts_del(s->socket_opts);
316 nbd_client_session_close(&s->client);
ths75818252008-07-03 13:41:03 +0000317}
318
319static int64_t nbd_getlength(BlockDriverState *bs)
320{
321 BDRVNBDState *s = bs->opaque;
322
Marc-André Lureau2302c1c2013-12-01 22:23:41 +0100323 return s->client.size;
ths75818252008-07-03 13:41:03 +0000324}
325
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500326static BlockDriver bdrv_nbd = {
Paolo Bonzini1486d042011-10-21 13:17:14 +0200327 .format_name = "nbd",
Paolo Bonzini1d7d2a92012-11-04 13:04:24 +0100328 .protocol_name = "nbd",
Paolo Bonzini1486d042011-10-21 13:17:14 +0200329 .instance_size = sizeof(BDRVNBDState),
Kevin Wolf6963a302013-03-15 18:47:22 +0100330 .bdrv_parse_filename = nbd_parse_filename,
Paolo Bonzini1486d042011-10-21 13:17:14 +0200331 .bdrv_file_open = nbd_open,
332 .bdrv_co_readv = nbd_co_readv,
333 .bdrv_co_writev = nbd_co_writev,
334 .bdrv_close = nbd_close,
335 .bdrv_co_flush_to_os = nbd_co_flush,
Paolo Bonzini7a706632011-10-21 13:17:14 +0200336 .bdrv_co_discard = nbd_co_discard,
Paolo Bonzini1486d042011-10-21 13:17:14 +0200337 .bdrv_getlength = nbd_getlength,
Paolo Bonzini1d7d2a92012-11-04 13:04:24 +0100338};
339
340static BlockDriver bdrv_nbd_tcp = {
341 .format_name = "nbd",
342 .protocol_name = "nbd+tcp",
343 .instance_size = sizeof(BDRVNBDState),
Kevin Wolf6963a302013-03-15 18:47:22 +0100344 .bdrv_parse_filename = nbd_parse_filename,
Paolo Bonzini1d7d2a92012-11-04 13:04:24 +0100345 .bdrv_file_open = nbd_open,
346 .bdrv_co_readv = nbd_co_readv,
347 .bdrv_co_writev = nbd_co_writev,
348 .bdrv_close = nbd_close,
349 .bdrv_co_flush_to_os = nbd_co_flush,
350 .bdrv_co_discard = nbd_co_discard,
351 .bdrv_getlength = nbd_getlength,
352};
353
354static BlockDriver bdrv_nbd_unix = {
355 .format_name = "nbd",
356 .protocol_name = "nbd+unix",
357 .instance_size = sizeof(BDRVNBDState),
Kevin Wolf6963a302013-03-15 18:47:22 +0100358 .bdrv_parse_filename = nbd_parse_filename,
Paolo Bonzini1d7d2a92012-11-04 13:04:24 +0100359 .bdrv_file_open = nbd_open,
360 .bdrv_co_readv = nbd_co_readv,
361 .bdrv_co_writev = nbd_co_writev,
362 .bdrv_close = nbd_close,
363 .bdrv_co_flush_to_os = nbd_co_flush,
364 .bdrv_co_discard = nbd_co_discard,
365 .bdrv_getlength = nbd_getlength,
ths75818252008-07-03 13:41:03 +0000366};
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500367
368static void bdrv_nbd_init(void)
369{
370 bdrv_register(&bdrv_nbd);
Paolo Bonzini1d7d2a92012-11-04 13:04:24 +0100371 bdrv_register(&bdrv_nbd_tcp);
372 bdrv_register(&bdrv_nbd_unix);
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500373}
374
375block_init(bdrv_nbd_init);