Statistics
| Branch: | Revision:

root / block / nbd.c @ 7fee199c

History | View | Annotate | Download (6.9 kB)

1 75818250 ths
/*
2 75818250 ths
 * QEMU Block driver for  NBD
3 75818250 ths
 *
4 75818250 ths
 * Copyright (C) 2008 Bull S.A.S.
5 bd5921b4 malc
 *     Author: Laurent Vivier <Laurent.Vivier@bull.net>
6 75818250 ths
 *
7 75818250 ths
 * Some parts:
8 75818250 ths
 *    Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
9 75818250 ths
 *
10 75818250 ths
 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 75818250 ths
 * of this software and associated documentation files (the "Software"), to deal
12 75818250 ths
 * in the Software without restriction, including without limitation the rights
13 75818250 ths
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 75818250 ths
 * copies of the Software, and to permit persons to whom the Software is
15 75818250 ths
 * furnished to do so, subject to the following conditions:
16 75818250 ths
 *
17 75818250 ths
 * The above copyright notice and this permission notice shall be included in
18 75818250 ths
 * all copies or substantial portions of the Software.
19 75818250 ths
 *
20 75818250 ths
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 75818250 ths
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 75818250 ths
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 75818250 ths
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 75818250 ths
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 75818250 ths
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 75818250 ths
 * THE SOFTWARE.
27 75818250 ths
 */
28 75818250 ths
29 75818250 ths
#include "qemu-common.h"
30 75818250 ths
#include "nbd.h"
31 5efa9d5a Anthony Liguori
#include "module.h"
32 33897dc7 Nick Thomas
#include "qemu_socket.h"
33 75818250 ths
34 75818250 ths
#include <sys/types.h>
35 75818250 ths
#include <unistd.h>
36 75818250 ths
37 1d45f8b5 Laurent Vivier
#define EN_OPTSTR ":exportname="
38 1d45f8b5 Laurent Vivier
39 33897dc7 Nick Thomas
/* #define DEBUG_NBD */
40 33897dc7 Nick Thomas
41 33897dc7 Nick Thomas
#if defined(DEBUG_NBD)
42 33897dc7 Nick Thomas
#define logout(fmt, ...) \
43 33897dc7 Nick Thomas
                fprintf(stderr, "nbd\t%-24s" fmt, __func__, ##__VA_ARGS__)
44 33897dc7 Nick Thomas
#else
45 33897dc7 Nick Thomas
#define logout(fmt, ...) ((void)0)
46 33897dc7 Nick Thomas
#endif
47 33897dc7 Nick Thomas
48 75818250 ths
typedef struct BDRVNBDState {
49 75818250 ths
    int sock;
50 75818250 ths
    off_t size;
51 75818250 ths
    size_t blocksize;
52 33897dc7 Nick Thomas
    char *export_name; /* An NBD server may export several devices */
53 33897dc7 Nick Thomas
54 33897dc7 Nick Thomas
    /* If it begins with  '/', this is a UNIX domain socket. Otherwise,
55 33897dc7 Nick Thomas
     * it's a string of the form <hostname|ip4|\[ip6\]>:port
56 33897dc7 Nick Thomas
     */
57 33897dc7 Nick Thomas
    char *host_spec;
58 75818250 ths
} BDRVNBDState;
59 75818250 ths
60 33897dc7 Nick Thomas
static int nbd_config(BDRVNBDState *s, const char *filename, int flags)
61 75818250 ths
{
62 1d45f8b5 Laurent Vivier
    char *file;
63 33897dc7 Nick Thomas
    char *export_name;
64 33897dc7 Nick Thomas
    const char *host_spec;
65 75818250 ths
    const char *unixpath;
66 1d45f8b5 Laurent Vivier
    int err = -EINVAL;
67 75818250 ths
68 1d45f8b5 Laurent Vivier
    file = qemu_strdup(filename);
69 1d45f8b5 Laurent Vivier
70 33897dc7 Nick Thomas
    export_name = strstr(file, EN_OPTSTR);
71 33897dc7 Nick Thomas
    if (export_name) {
72 33897dc7 Nick Thomas
        if (export_name[strlen(EN_OPTSTR)] == 0) {
73 1d45f8b5 Laurent Vivier
            goto out;
74 1d45f8b5 Laurent Vivier
        }
75 33897dc7 Nick Thomas
        export_name[0] = 0; /* truncate 'file' */
76 33897dc7 Nick Thomas
        export_name += strlen(EN_OPTSTR);
77 33897dc7 Nick Thomas
        s->export_name = qemu_strdup(export_name);
78 1d45f8b5 Laurent Vivier
    }
79 1d45f8b5 Laurent Vivier
80 33897dc7 Nick Thomas
    /* extract the host_spec - fail if it's not nbd:... */
81 33897dc7 Nick Thomas
    if (!strstart(file, "nbd:", &host_spec)) {
82 1d45f8b5 Laurent Vivier
        goto out;
83 1d45f8b5 Laurent Vivier
    }
84 75818250 ths
85 33897dc7 Nick Thomas
    /* are we a UNIX or TCP socket? */
86 33897dc7 Nick Thomas
    if (strstart(host_spec, "unix:", &unixpath)) {
87 33897dc7 Nick Thomas
        if (unixpath[0] != '/') { /* We demand  an absolute path*/
88 1d45f8b5 Laurent Vivier
            goto out;
89 1d45f8b5 Laurent Vivier
        }
90 33897dc7 Nick Thomas
        s->host_spec = qemu_strdup(unixpath);
91 75818250 ths
    } else {
92 33897dc7 Nick Thomas
        s->host_spec = qemu_strdup(host_spec);
93 33897dc7 Nick Thomas
    }
94 75818250 ths
95 33897dc7 Nick Thomas
    err = 0;
96 75818250 ths
97 33897dc7 Nick Thomas
out:
98 33897dc7 Nick Thomas
    qemu_free(file);
99 33897dc7 Nick Thomas
    if (err != 0) {
100 33897dc7 Nick Thomas
        qemu_free(s->export_name);
101 33897dc7 Nick Thomas
        qemu_free(s->host_spec);
102 33897dc7 Nick Thomas
    }
103 33897dc7 Nick Thomas
    return err;
104 33897dc7 Nick Thomas
}
105 1d45f8b5 Laurent Vivier
106 33897dc7 Nick Thomas
static int nbd_establish_connection(BlockDriverState *bs)
107 33897dc7 Nick Thomas
{
108 33897dc7 Nick Thomas
    BDRVNBDState *s = bs->opaque;
109 33897dc7 Nick Thomas
    int sock;
110 33897dc7 Nick Thomas
    int ret;
111 33897dc7 Nick Thomas
    off_t size;
112 33897dc7 Nick Thomas
    size_t blocksize;
113 33897dc7 Nick Thomas
    uint32_t nbdflags;
114 75818250 ths
115 33897dc7 Nick Thomas
    if (s->host_spec[0] == '/') {
116 33897dc7 Nick Thomas
        sock = unix_socket_outgoing(s->host_spec);
117 33897dc7 Nick Thomas
    } else {
118 33897dc7 Nick Thomas
        sock = tcp_socket_outgoing_spec(s->host_spec);
119 75818250 ths
    }
120 75818250 ths
121 33897dc7 Nick Thomas
    /* Failed to establish connection */
122 1d45f8b5 Laurent Vivier
    if (sock == -1) {
123 33897dc7 Nick Thomas
        logout("Failed to establish connection to NBD server\n");
124 33897dc7 Nick Thomas
        return -errno;
125 1d45f8b5 Laurent Vivier
    }
126 75818250 ths
127 33897dc7 Nick Thomas
    /* NBD handshake */
128 33897dc7 Nick Thomas
    ret = nbd_receive_negotiate(sock, s->export_name, &nbdflags, &size,
129 33897dc7 Nick Thomas
                                &blocksize);
130 1d45f8b5 Laurent Vivier
    if (ret == -1) {
131 33897dc7 Nick Thomas
        logout("Failed to negotiate with the NBD server\n");
132 33897dc7 Nick Thomas
        closesocket(sock);
133 33897dc7 Nick Thomas
        return -errno;
134 1d45f8b5 Laurent Vivier
    }
135 75818250 ths
136 33897dc7 Nick Thomas
    /* Now that we're connected, set the socket to be non-blocking */
137 33897dc7 Nick Thomas
    socket_set_nonblock(sock);
138 33897dc7 Nick Thomas
139 75818250 ths
    s->sock = sock;
140 75818250 ths
    s->size = size;
141 75818250 ths
    s->blocksize = blocksize;
142 75818250 ths
143 33897dc7 Nick Thomas
    logout("Established connection with NBD server\n");
144 33897dc7 Nick Thomas
    return 0;
145 33897dc7 Nick Thomas
}
146 33897dc7 Nick Thomas
147 33897dc7 Nick Thomas
static void nbd_teardown_connection(BlockDriverState *bs)
148 33897dc7 Nick Thomas
{
149 33897dc7 Nick Thomas
    BDRVNBDState *s = bs->opaque;
150 33897dc7 Nick Thomas
    struct nbd_request request;
151 33897dc7 Nick Thomas
152 33897dc7 Nick Thomas
    request.type = NBD_CMD_DISC;
153 33897dc7 Nick Thomas
    request.handle = (uint64_t)(intptr_t)bs;
154 33897dc7 Nick Thomas
    request.from = 0;
155 33897dc7 Nick Thomas
    request.len = 0;
156 33897dc7 Nick Thomas
    nbd_send_request(s->sock, &request);
157 33897dc7 Nick Thomas
158 33897dc7 Nick Thomas
    closesocket(s->sock);
159 33897dc7 Nick Thomas
}
160 33897dc7 Nick Thomas
161 33897dc7 Nick Thomas
static int nbd_open(BlockDriverState *bs, const char* filename, int flags)
162 33897dc7 Nick Thomas
{
163 33897dc7 Nick Thomas
    BDRVNBDState *s = bs->opaque;
164 33897dc7 Nick Thomas
    int result;
165 33897dc7 Nick Thomas
166 33897dc7 Nick Thomas
    /* Pop the config into our state object. Exit if invalid. */
167 33897dc7 Nick Thomas
    result = nbd_config(s, filename, flags);
168 33897dc7 Nick Thomas
    if (result != 0) {
169 33897dc7 Nick Thomas
        return result;
170 33897dc7 Nick Thomas
    }
171 33897dc7 Nick Thomas
172 33897dc7 Nick Thomas
    /* establish TCP connection, return error if it fails
173 33897dc7 Nick Thomas
     * TODO: Configurable retry-until-timeout behaviour.
174 33897dc7 Nick Thomas
     */
175 33897dc7 Nick Thomas
    result = nbd_establish_connection(bs);
176 33897dc7 Nick Thomas
177 33897dc7 Nick Thomas
    return result;
178 75818250 ths
}
179 75818250 ths
180 75818250 ths
static int nbd_read(BlockDriverState *bs, int64_t sector_num,
181 75818250 ths
                    uint8_t *buf, int nb_sectors)
182 75818250 ths
{
183 75818250 ths
    BDRVNBDState *s = bs->opaque;
184 75818250 ths
    struct nbd_request request;
185 75818250 ths
    struct nbd_reply reply;
186 75818250 ths
187 75818250 ths
    request.type = NBD_CMD_READ;
188 75818250 ths
    request.handle = (uint64_t)(intptr_t)bs;
189 75818250 ths
    request.from = sector_num * 512;;
190 75818250 ths
    request.len = nb_sectors * 512;
191 75818250 ths
192 75818250 ths
    if (nbd_send_request(s->sock, &request) == -1)
193 75818250 ths
        return -errno;
194 75818250 ths
195 75818250 ths
    if (nbd_receive_reply(s->sock, &reply) == -1)
196 75818250 ths
        return -errno;
197 75818250 ths
198 75818250 ths
    if (reply.error !=0)
199 75818250 ths
        return -reply.error;
200 75818250 ths
201 75818250 ths
    if (reply.handle != request.handle)
202 75818250 ths
        return -EIO;
203 75818250 ths
204 75818250 ths
    if (nbd_wr_sync(s->sock, buf, request.len, 1) != request.len)
205 75818250 ths
        return -EIO;
206 75818250 ths
207 75818250 ths
    return 0;
208 75818250 ths
}
209 75818250 ths
210 75818250 ths
static int nbd_write(BlockDriverState *bs, int64_t sector_num,
211 75818250 ths
                     const uint8_t *buf, int nb_sectors)
212 75818250 ths
{
213 75818250 ths
    BDRVNBDState *s = bs->opaque;
214 75818250 ths
    struct nbd_request request;
215 75818250 ths
    struct nbd_reply reply;
216 75818250 ths
217 75818250 ths
    request.type = NBD_CMD_WRITE;
218 75818250 ths
    request.handle = (uint64_t)(intptr_t)bs;
219 75818250 ths
    request.from = sector_num * 512;;
220 75818250 ths
    request.len = nb_sectors * 512;
221 75818250 ths
222 75818250 ths
    if (nbd_send_request(s->sock, &request) == -1)
223 75818250 ths
        return -errno;
224 75818250 ths
225 75818250 ths
    if (nbd_wr_sync(s->sock, (uint8_t*)buf, request.len, 0) != request.len)
226 75818250 ths
        return -EIO;
227 75818250 ths
228 75818250 ths
    if (nbd_receive_reply(s->sock, &reply) == -1)
229 75818250 ths
        return -errno;
230 75818250 ths
231 75818250 ths
    if (reply.error !=0)
232 75818250 ths
        return -reply.error;
233 75818250 ths
234 75818250 ths
    if (reply.handle != request.handle)
235 75818250 ths
        return -EIO;
236 75818250 ths
237 75818250 ths
    return 0;
238 75818250 ths
}
239 75818250 ths
240 75818250 ths
static void nbd_close(BlockDriverState *bs)
241 75818250 ths
{
242 d2d979c6 Nick Thomas
    BDRVNBDState *s = bs->opaque;
243 d2d979c6 Nick Thomas
    qemu_free(s->export_name);
244 d2d979c6 Nick Thomas
    qemu_free(s->host_spec);
245 d2d979c6 Nick Thomas
246 33897dc7 Nick Thomas
    nbd_teardown_connection(bs);
247 75818250 ths
}
248 75818250 ths
249 75818250 ths
static int64_t nbd_getlength(BlockDriverState *bs)
250 75818250 ths
{
251 75818250 ths
    BDRVNBDState *s = bs->opaque;
252 75818250 ths
253 75818250 ths
    return s->size;
254 75818250 ths
}
255 75818250 ths
256 5efa9d5a Anthony Liguori
static BlockDriver bdrv_nbd = {
257 e60f469c aurel32
    .format_name        = "nbd",
258 e60f469c aurel32
    .instance_size        = sizeof(BDRVNBDState),
259 66f82cee Kevin Wolf
    .bdrv_file_open        = nbd_open,
260 e60f469c aurel32
    .bdrv_read                = nbd_read,
261 e60f469c aurel32
    .bdrv_write                = nbd_write,
262 e60f469c aurel32
    .bdrv_close                = nbd_close,
263 e60f469c aurel32
    .bdrv_getlength        = nbd_getlength,
264 e60f469c aurel32
    .protocol_name        = "nbd",
265 75818250 ths
};
266 5efa9d5a Anthony Liguori
267 5efa9d5a Anthony Liguori
static void bdrv_nbd_init(void)
268 5efa9d5a Anthony Liguori
{
269 5efa9d5a Anthony Liguori
    bdrv_register(&bdrv_nbd);
270 5efa9d5a Anthony Liguori
}
271 5efa9d5a Anthony Liguori
272 5efa9d5a Anthony Liguori
block_init(bdrv_nbd_init);