]> Git Repo - qemu.git/blame - block/nbd.c
Split nbd block client code
[qemu.git] / block / nbd.c
CommitLineData
75818250
TS
1/*
2 * QEMU Block driver for NBD
3 *
4 * Copyright (C) 2008 Bull S.A.S.
bd5921b4 5 * Author: Laurent Vivier <[email protected]>
75818250
TS
6 *
7 * Some parts:
8 * Copyright (C) 2007 Anthony Liguori <[email protected]>
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
2302c1ca 29#include "block/nbd-client.h"
1de7afc9 30#include "qemu/uri.h"
737e150e 31#include "block/block_int.h"
1de7afc9
PB
32#include "qemu/module.h"
33#include "qemu/sockets.h"
f53a1feb
KW
34#include "qapi/qmp/qjson.h"
35#include "qapi/qmp/qint.h"
75818250
TS
36
37#include <sys/types.h>
38#include <unistd.h>
75818250 39
1d45f8b5
LV
40#define EN_OPTSTR ":exportname="
41
75818250 42typedef struct BDRVNBDState {
2302c1ca 43 NbdClientSession client;
f53a1feb 44 QemuOpts *socket_opts;
75818250
TS
45} BDRVNBDState;
46
f53a1feb 47static int nbd_parse_uri(const char *filename, QDict *options)
1d7d2a9d
PB
48{
49 URI *uri;
50 const char *p;
51 QueryParams *qp = NULL;
52 int ret = 0;
f53a1feb 53 bool is_unix;
1d7d2a9d
PB
54
55 uri = uri_parse(filename);
56 if (!uri) {
57 return -EINVAL;
58 }
59
60 /* transport */
61 if (!strcmp(uri->scheme, "nbd")) {
f53a1feb 62 is_unix = false;
1d7d2a9d 63 } else if (!strcmp(uri->scheme, "nbd+tcp")) {
f53a1feb 64 is_unix = false;
1d7d2a9d 65 } else if (!strcmp(uri->scheme, "nbd+unix")) {
f53a1feb 66 is_unix = true;
1d7d2a9d
PB
67 } else {
68 ret = -EINVAL;
69 goto out;
70 }
71
72 p = uri->path ? uri->path : "/";
73 p += strspn(p, "/");
74 if (p[0]) {
f53a1feb 75 qdict_put(options, "export", qstring_from_str(p));
1d7d2a9d
PB
76 }
77
78 qp = query_params_parse(uri->query);
f53a1feb 79 if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) {
1d7d2a9d
PB
80 ret = -EINVAL;
81 goto out;
82 }
83
f53a1feb 84 if (is_unix) {
1d7d2a9d
PB
85 /* nbd+unix:///export?socket=path */
86 if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
87 ret = -EINVAL;
88 goto out;
89 }
f53a1feb 90 qdict_put(options, "path", qstring_from_str(qp->p[0].value));
1d7d2a9d 91 } else {
23307908 92 QString *host;
bebbf7fa 93 /* nbd[+tcp]://host[:port]/export */
1d7d2a9d
PB
94 if (!uri->server) {
95 ret = -EINVAL;
96 goto out;
97 }
f17c90be 98
23307908
JT
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);
103 } else {
104 host = qstring_from_str(uri->server);
105 }
106
107 qdict_put(options, "host", host);
bebbf7fa
KW
108 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 }
1d7d2a9d
PB
113 }
114
115out:
116 if (qp) {
117 query_params_free(qp);
118 }
119 uri_free(uri);
120 return ret;
121}
122
6963a30d
KW
123static void nbd_parse_filename(const char *filename, QDict *options,
124 Error **errp)
75818250 125{
1d45f8b5 126 char *file;
33897dc7
NT
127 char *export_name;
128 const char *host_spec;
75818250 129 const char *unixpath;
75818250 130
681e7ad0
KW
131 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
1d7d2a9d 140 if (strstr(filename, "://")) {
6963a30d
KW
141 int ret = nbd_parse_uri(filename, options);
142 if (ret < 0) {
143 error_setg(errp, "No valid URL specified");
144 }
145 return;
1d7d2a9d
PB
146 }
147
7267c094 148 file = g_strdup(filename);
1d45f8b5 149
33897dc7
NT
150 export_name = strstr(file, EN_OPTSTR);
151 if (export_name) {
152 if (export_name[strlen(EN_OPTSTR)] == 0) {
1d45f8b5
LV
153 goto out;
154 }
33897dc7
NT
155 export_name[0] = 0; /* truncate 'file' */
156 export_name += strlen(EN_OPTSTR);
f53a1feb
KW
157
158 qdict_put(options, "export", qstring_from_str(export_name));
1d45f8b5
LV
159 }
160
33897dc7
NT
161 /* extract the host_spec - fail if it's not nbd:... */
162 if (!strstart(file, "nbd:", &host_spec)) {
6963a30d 163 error_setg(errp, "File name string for NBD must start with 'nbd:'");
1d45f8b5
LV
164 goto out;
165 }
75818250 166
f53a1feb 167 if (!*host_spec) {
f53a1feb
KW
168 goto out;
169 }
170
33897dc7
NT
171 /* are we a UNIX or TCP socket? */
172 if (strstart(host_spec, "unix:", &unixpath)) {
f53a1feb 173 qdict_put(options, "path", qstring_from_str(unixpath));
75818250 174 } else {
f53a1feb
KW
175 InetSocketAddress *addr = NULL;
176
6963a30d
KW
177 addr = inet_parse(host_spec, errp);
178 if (error_is_set(errp)) {
f17c90be
KW
179 goto out;
180 }
75818250 181
f53a1feb
KW
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);
185 }
75818250 186
33897dc7 187out:
7267c094 188 g_free(file);
f53a1feb
KW
189}
190
191static int nbd_config(BDRVNBDState *s, QDict *options)
192{
193 Error *local_err = NULL;
194
195 if (qdict_haskey(options, "path")) {
681e7ad0
KW
196 if (qdict_haskey(options, "host")) {
197 qerror_report(ERROR_CLASS_GENERIC_ERROR, "path and host may not "
198 "be used at the same time.");
199 return -EINVAL;
200 }
2302c1ca 201 s->client.is_unix = true;
f53a1feb 202 } else if (qdict_haskey(options, "host")) {
2302c1ca 203 s->client.is_unix = false;
f53a1feb
KW
204 } else {
205 return -EINVAL;
33897dc7 206 }
f53a1feb
KW
207
208 s->socket_opts = qemu_opts_create_nofail(&socket_optslist);
209
210 qemu_opts_absorb_qdict(s->socket_opts, options, &local_err);
211 if (error_is_set(&local_err)) {
212 qerror_report_err(local_err);
213 error_free(local_err);
214 return -EINVAL;
215 }
216
bebbf7fa
KW
217 if (!qemu_opt_get(s->socket_opts, "port")) {
218 qemu_opt_set_number(s->socket_opts, "port", NBD_DEFAULT_PORT);
219 }
220
2302c1ca
MAL
221 s->client.export_name = g_strdup(qdict_get_try_str(options, "export"));
222 if (s->client.export_name) {
f53a1feb
KW
223 qdict_del(options, "export");
224 }
225
226 return 0;
33897dc7 227}
1d45f8b5 228
33897dc7
NT
229static int nbd_establish_connection(BlockDriverState *bs)
230{
231 BDRVNBDState *s = bs->opaque;
232 int sock;
75818250 233
2302c1ca 234 if (s->client.is_unix) {
f53a1feb 235 sock = unix_socket_outgoing(qemu_opt_get(s->socket_opts, "path"));
33897dc7 236 } else {
f53a1feb 237 sock = tcp_socket_outgoing_opts(s->socket_opts);
97ebbab0
SH
238 if (sock >= 0) {
239 socket_set_nodelay(sock);
240 }
75818250
TS
241 }
242
33897dc7 243 /* Failed to establish connection */
fc19f8a0 244 if (sock < 0) {
33897dc7
NT
245 logout("Failed to establish connection to NBD server\n");
246 return -errno;
1d45f8b5 247 }
75818250 248
2302c1ca 249 return sock;
33897dc7
NT
250}
251
015a1036
HR
252static int nbd_open(BlockDriverState *bs, QDict *options, int flags,
253 Error **errp)
33897dc7
NT
254{
255 BDRVNBDState *s = bs->opaque;
2302c1ca 256 int result, sock;
ae255e52 257
33897dc7 258 /* Pop the config into our state object. Exit if invalid. */
f53a1feb 259 result = nbd_config(s, options);
33897dc7
NT
260 if (result != 0) {
261 return result;
262 }
263
264 /* establish TCP connection, return error if it fails
265 * TODO: Configurable retry-until-timeout behaviour.
266 */
2302c1ca
MAL
267 sock = nbd_establish_connection(bs);
268 if (sock < 0) {
269 return sock;
2c7989a9
PB
270 }
271
2302c1ca
MAL
272 /* NBD handshake */
273 return nbd_client_session_init(&s->client, bs, sock);
e183ef75
PB
274}
275
d9b09f13
PB
276static int nbd_co_readv(BlockDriverState *bs, int64_t sector_num,
277 int nb_sectors, QEMUIOVector *qiov)
278{
2302c1ca
MAL
279 BDRVNBDState *s = bs->opaque;
280
281 return nbd_client_session_co_readv(&s->client, sector_num,
282 nb_sectors, qiov);
d9b09f13
PB
283}
284
285static int nbd_co_writev(BlockDriverState *bs, int64_t sector_num,
286 int nb_sectors, QEMUIOVector *qiov)
287{
2302c1ca
MAL
288 BDRVNBDState *s = bs->opaque;
289
290 return nbd_client_session_co_writev(&s->client, sector_num,
291 nb_sectors, qiov);
d9b09f13
PB
292}
293
1486d04a
PB
294static int nbd_co_flush(BlockDriverState *bs)
295{
296 BDRVNBDState *s = bs->opaque;
1486d04a 297
2302c1ca 298 return nbd_client_session_co_flush(&s->client);
1486d04a
PB
299}
300
7a706633
PB
301static int nbd_co_discard(BlockDriverState *bs, int64_t sector_num,
302 int nb_sectors)
303{
304 BDRVNBDState *s = bs->opaque;
7a706633 305
2302c1ca
MAL
306 return nbd_client_session_co_discard(&s->client, sector_num,
307 nb_sectors);
7a706633
PB
308}
309
75818250
TS
310static void nbd_close(BlockDriverState *bs)
311{
d2d979c6 312 BDRVNBDState *s = bs->opaque;
d2d979c6 313
2302c1ca
MAL
314 qemu_opts_del(s->socket_opts);
315 nbd_client_session_close(&s->client);
75818250
TS
316}
317
318static int64_t nbd_getlength(BlockDriverState *bs)
319{
320 BDRVNBDState *s = bs->opaque;
321
2302c1ca 322 return s->client.size;
75818250
TS
323}
324
5efa9d5a 325static BlockDriver bdrv_nbd = {
1486d04a 326 .format_name = "nbd",
1d7d2a9d
PB
327 .protocol_name = "nbd",
328 .instance_size = sizeof(BDRVNBDState),
6963a30d 329 .bdrv_parse_filename = nbd_parse_filename,
1d7d2a9d
PB
330 .bdrv_file_open = nbd_open,
331 .bdrv_co_readv = nbd_co_readv,
332 .bdrv_co_writev = nbd_co_writev,
333 .bdrv_close = nbd_close,
334 .bdrv_co_flush_to_os = nbd_co_flush,
335 .bdrv_co_discard = nbd_co_discard,
336 .bdrv_getlength = nbd_getlength,
337};
338
339static BlockDriver bdrv_nbd_tcp = {
340 .format_name = "nbd",
341 .protocol_name = "nbd+tcp",
342 .instance_size = sizeof(BDRVNBDState),
6963a30d 343 .bdrv_parse_filename = nbd_parse_filename,
1d7d2a9d
PB
344 .bdrv_file_open = nbd_open,
345 .bdrv_co_readv = nbd_co_readv,
346 .bdrv_co_writev = nbd_co_writev,
347 .bdrv_close = nbd_close,
348 .bdrv_co_flush_to_os = nbd_co_flush,
349 .bdrv_co_discard = nbd_co_discard,
350 .bdrv_getlength = nbd_getlength,
351};
352
353static BlockDriver bdrv_nbd_unix = {
354 .format_name = "nbd",
355 .protocol_name = "nbd+unix",
1486d04a 356 .instance_size = sizeof(BDRVNBDState),
6963a30d 357 .bdrv_parse_filename = nbd_parse_filename,
1486d04a
PB
358 .bdrv_file_open = nbd_open,
359 .bdrv_co_readv = nbd_co_readv,
360 .bdrv_co_writev = nbd_co_writev,
361 .bdrv_close = nbd_close,
362 .bdrv_co_flush_to_os = nbd_co_flush,
7a706633 363 .bdrv_co_discard = nbd_co_discard,
1486d04a 364 .bdrv_getlength = nbd_getlength,
75818250 365};
5efa9d5a
AL
366
367static void bdrv_nbd_init(void)
368{
369 bdrv_register(&bdrv_nbd);
1d7d2a9d
PB
370 bdrv_register(&bdrv_nbd_tcp);
371 bdrv_register(&bdrv_nbd_unix);
5efa9d5a
AL
372}
373
374block_init(bdrv_nbd_init);
This page took 0.431004 seconds and 4 git commands to generate.