blob: ff701d3dc8a5afb70c9e0133498410f806fadef2 [file] [log] [blame]
ths75818252008-07-03 13:41:03 +00001/*
bellard7a5ca862008-05-27 21:13:40 +00002 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
3 *
4 * Network Block Device
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; under version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Blue Swirl8167ee82009-07-16 20:47:01 +000016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
ths75818252008-07-03 13:41:03 +000017 */
bellard7a5ca862008-05-27 21:13:40 +000018
19#include "nbd.h"
Markus Armbrusterab359cd2011-09-06 18:58:58 +020020#include "block.h"
bellard7a5ca862008-05-27 21:13:40 +000021
22#include <errno.h>
23#include <string.h>
aliguori03ff3ca2008-09-15 15:51:35 +000024#ifndef _WIN32
bellard7a5ca862008-05-27 21:13:40 +000025#include <sys/ioctl.h>
aliguori03ff3ca2008-09-15 15:51:35 +000026#endif
Andreas Färber5dc2eec2010-09-20 00:50:46 +020027#if defined(__sun__) || defined(__HAIKU__)
aliguori7e00eb92008-08-02 01:57:02 +000028#include <sys/ioccom.h>
29#endif
bellard7a5ca862008-05-27 21:13:40 +000030#include <ctype.h>
31#include <inttypes.h>
bellard7a5ca862008-05-27 21:13:40 +000032
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +020033#ifdef __linux__
34#include <linux/fs.h>
35#endif
36
aliguori03ff3ca2008-09-15 15:51:35 +000037#include "qemu_socket.h"
ths75818252008-07-03 13:41:03 +000038
aliguori03ff3ca2008-09-15 15:51:35 +000039//#define DEBUG_NBD
40
41#ifdef DEBUG_NBD
ths75818252008-07-03 13:41:03 +000042#define TRACE(msg, ...) do { \
aliguori03ff3ca2008-09-15 15:51:35 +000043 LOG(msg, ## __VA_ARGS__); \
ths75818252008-07-03 13:41:03 +000044} while(0)
aliguori03ff3ca2008-09-15 15:51:35 +000045#else
46#define TRACE(msg, ...) \
47 do { } while (0)
48#endif
bellard7a5ca862008-05-27 21:13:40 +000049
50#define LOG(msg, ...) do { \
51 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
52 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
53} while(0)
54
bellard7a5ca862008-05-27 21:13:40 +000055/* This is all part of the "official" NBD API */
56
Nick Thomasb2e3d872011-02-22 15:44:51 +000057#define NBD_REPLY_SIZE (4 + 4 + 8)
bellard7a5ca862008-05-27 21:13:40 +000058#define NBD_REQUEST_MAGIC 0x25609513
59#define NBD_REPLY_MAGIC 0x67446698
60
61#define NBD_SET_SOCK _IO(0xab, 0)
62#define NBD_SET_BLKSIZE _IO(0xab, 1)
63#define NBD_SET_SIZE _IO(0xab, 2)
64#define NBD_DO_IT _IO(0xab, 3)
65#define NBD_CLEAR_SOCK _IO(0xab, 4)
66#define NBD_CLEAR_QUE _IO(0xab, 5)
Nick Thomasb2e3d872011-02-22 15:44:51 +000067#define NBD_PRINT_DEBUG _IO(0xab, 6)
68#define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
bellard7a5ca862008-05-27 21:13:40 +000069#define NBD_DISCONNECT _IO(0xab, 8)
Paolo Bonzinibbb74ed2011-09-08 17:24:55 +020070#define NBD_SET_TIMEOUT _IO(0xab, 9)
71#define NBD_SET_FLAGS _IO(0xab, 10)
bellard7a5ca862008-05-27 21:13:40 +000072
Nick Thomasb2e3d872011-02-22 15:44:51 +000073#define NBD_OPT_EXPORT_NAME (1 << 0)
Laurent Vivier1d45f8b2010-08-25 22:48:33 +020074
bellard7a5ca862008-05-27 21:13:40 +000075/* That's all folks */
76
ths75818252008-07-03 13:41:03 +000077#define read_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, true)
78#define write_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, false)
bellard7a5ca862008-05-27 21:13:40 +000079
ths75818252008-07-03 13:41:03 +000080size_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read)
bellard7a5ca862008-05-27 21:13:40 +000081{
82 size_t offset = 0;
83
Paolo Bonziniae255e52011-09-08 14:28:59 +020084 if (qemu_in_coroutine()) {
85 if (do_read) {
86 return qemu_co_recv(fd, buffer, size);
87 } else {
88 return qemu_co_send(fd, buffer, size);
89 }
90 }
91
bellard7a5ca862008-05-27 21:13:40 +000092 while (offset < size) {
93 ssize_t len;
94
95 if (do_read) {
Blue Swirl00aa0042011-07-23 20:04:29 +000096 len = qemu_recv(fd, buffer + offset, size - offset, 0);
bellard7a5ca862008-05-27 21:13:40 +000097 } else {
aliguori03ff3ca2008-09-15 15:51:35 +000098 len = send(fd, buffer + offset, size - offset, 0);
bellard7a5ca862008-05-27 21:13:40 +000099 }
100
aliguori03ff3ca2008-09-15 15:51:35 +0000101 if (len == -1)
102 errno = socket_error();
103
bellard7a5ca862008-05-27 21:13:40 +0000104 /* recoverable error */
ths75818252008-07-03 13:41:03 +0000105 if (len == -1 && (errno == EAGAIN || errno == EINTR)) {
bellard7a5ca862008-05-27 21:13:40 +0000106 continue;
107 }
108
109 /* eof */
110 if (len == 0) {
111 break;
112 }
113
114 /* unrecoverable error */
115 if (len == -1) {
116 return 0;
117 }
118
119 offset += len;
120 }
121
122 return offset;
123}
124
Nick Thomasc12504c2011-02-22 15:44:53 +0000125static void combine_addr(char *buf, size_t len, const char* address,
126 uint16_t port)
127{
128 /* If the address-part contains a colon, it's an IPv6 IP so needs [] */
129 if (strstr(address, ":")) {
130 snprintf(buf, len, "[%s]:%u", address, port);
131 } else {
132 snprintf(buf, len, "%s:%u", address, port);
133 }
134}
135
ths75818252008-07-03 13:41:03 +0000136int tcp_socket_outgoing(const char *address, uint16_t port)
bellard7a5ca862008-05-27 21:13:40 +0000137{
Nick Thomasc12504c2011-02-22 15:44:53 +0000138 char address_and_port[128];
139 combine_addr(address_and_port, 128, address, port);
140 return tcp_socket_outgoing_spec(address_and_port);
141}
bellard7a5ca862008-05-27 21:13:40 +0000142
Nick Thomasc12504c2011-02-22 15:44:53 +0000143int tcp_socket_outgoing_spec(const char *address_and_port)
144{
145 return inet_connect(address_and_port, SOCK_STREAM);
bellard7a5ca862008-05-27 21:13:40 +0000146}
147
148int tcp_socket_incoming(const char *address, uint16_t port)
149{
Nick Thomasc12504c2011-02-22 15:44:53 +0000150 char address_and_port[128];
151 combine_addr(address_and_port, 128, address, port);
152 return tcp_socket_incoming_spec(address_and_port);
bellard7a5ca862008-05-27 21:13:40 +0000153}
154
Nick Thomasc12504c2011-02-22 15:44:53 +0000155int tcp_socket_incoming_spec(const char *address_and_port)
156{
157 char *ostr = NULL;
158 int olen = 0;
159 return inet_listen(address_and_port, ostr, olen, SOCK_STREAM, 0);
160}
161
thscd831bd2008-07-03 10:23:51 +0000162int unix_socket_incoming(const char *path)
163{
Nick Thomasc12504c2011-02-22 15:44:53 +0000164 char *ostr = NULL;
165 int olen = 0;
thscd831bd2008-07-03 10:23:51 +0000166
Nick Thomasc12504c2011-02-22 15:44:53 +0000167 return unix_listen(path, ostr, olen);
thscd831bd2008-07-03 10:23:51 +0000168}
169
170int unix_socket_outgoing(const char *path)
171{
Nick Thomasc12504c2011-02-22 15:44:53 +0000172 return unix_connect(path);
thscd831bd2008-07-03 10:23:51 +0000173}
thscd831bd2008-07-03 10:23:51 +0000174
bellard7a5ca862008-05-27 21:13:40 +0000175/* Basic flow
176
177 Server Client
178
179 Negotiate
180 Request
181 Response
182 Request
183 Response
184 ...
185 ...
186 Request (type == 2)
187*/
188
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200189int nbd_negotiate(int csock, off_t size, uint32_t flags)
bellard7a5ca862008-05-27 21:13:40 +0000190{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000191 char buf[8 + 8 + 8 + 128];
bellard7a5ca862008-05-27 21:13:40 +0000192
Nick Thomasb2e3d872011-02-22 15:44:51 +0000193 /* Negotiate
194 [ 0 .. 7] passwd ("NBDMAGIC")
195 [ 8 .. 15] magic (0x00420281861253)
196 [16 .. 23] size
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200197 [24 .. 27] flags
198 [28 .. 151] reserved (0)
Nick Thomasb2e3d872011-02-22 15:44:51 +0000199 */
bellard7a5ca862008-05-27 21:13:40 +0000200
Nick Thomasb2e3d872011-02-22 15:44:51 +0000201 TRACE("Beginning negotiation.");
202 memcpy(buf, "NBDMAGIC", 8);
203 cpu_to_be64w((uint64_t*)(buf + 8), 0x00420281861253LL);
204 cpu_to_be64w((uint64_t*)(buf + 16), size);
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200205 cpu_to_be32w((uint32_t*)(buf + 24), flags | NBD_FLAG_HAS_FLAGS);
206 memset(buf + 28, 0, 124);
bellard7a5ca862008-05-27 21:13:40 +0000207
Nick Thomasb2e3d872011-02-22 15:44:51 +0000208 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
209 LOG("write failed");
210 errno = EINVAL;
211 return -1;
212 }
bellard7a5ca862008-05-27 21:13:40 +0000213
Dong Xu Wang07f35072011-11-22 18:06:26 +0800214 TRACE("Negotiation succeeded.");
bellard7a5ca862008-05-27 21:13:40 +0000215
Nick Thomasb2e3d872011-02-22 15:44:51 +0000216 return 0;
bellard7a5ca862008-05-27 21:13:40 +0000217}
218
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200219int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
220 off_t *size, size_t *blocksize)
bellard7a5ca862008-05-27 21:13:40 +0000221{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000222 char buf[256];
223 uint64_t magic, s;
224 uint16_t tmp;
bellard7a5ca862008-05-27 21:13:40 +0000225
Dong Xu Wang07f35072011-11-22 18:06:26 +0800226 TRACE("Receiving negotiation.");
bellard7a5ca862008-05-27 21:13:40 +0000227
Nick Thomasb2e3d872011-02-22 15:44:51 +0000228 if (read_sync(csock, buf, 8) != 8) {
229 LOG("read failed");
230 errno = EINVAL;
231 return -1;
232 }
bellard7a5ca862008-05-27 21:13:40 +0000233
Nick Thomasb2e3d872011-02-22 15:44:51 +0000234 buf[8] = '\0';
235 if (strlen(buf) == 0) {
236 LOG("server connection closed");
237 errno = EINVAL;
238 return -1;
239 }
bellard7a5ca862008-05-27 21:13:40 +0000240
Nick Thomasb2e3d872011-02-22 15:44:51 +0000241 TRACE("Magic is %c%c%c%c%c%c%c%c",
242 qemu_isprint(buf[0]) ? buf[0] : '.',
243 qemu_isprint(buf[1]) ? buf[1] : '.',
244 qemu_isprint(buf[2]) ? buf[2] : '.',
245 qemu_isprint(buf[3]) ? buf[3] : '.',
246 qemu_isprint(buf[4]) ? buf[4] : '.',
247 qemu_isprint(buf[5]) ? buf[5] : '.',
248 qemu_isprint(buf[6]) ? buf[6] : '.',
249 qemu_isprint(buf[7]) ? buf[7] : '.');
bellard7a5ca862008-05-27 21:13:40 +0000250
Nick Thomasb2e3d872011-02-22 15:44:51 +0000251 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
252 LOG("Invalid magic received");
253 errno = EINVAL;
254 return -1;
255 }
bellard7a5ca862008-05-27 21:13:40 +0000256
Nick Thomasb2e3d872011-02-22 15:44:51 +0000257 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
258 LOG("read failed");
259 errno = EINVAL;
260 return -1;
261 }
262 magic = be64_to_cpu(magic);
263 TRACE("Magic is 0x%" PRIx64, magic);
bellard7a5ca862008-05-27 21:13:40 +0000264
Nick Thomasb2e3d872011-02-22 15:44:51 +0000265 if (name) {
266 uint32_t reserved = 0;
267 uint32_t opt;
268 uint32_t namesize;
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200269
Nick Thomasb2e3d872011-02-22 15:44:51 +0000270 TRACE("Checking magic (opts_magic)");
271 if (magic != 0x49484156454F5054LL) {
272 LOG("Bad magic received");
273 errno = EINVAL;
274 return -1;
275 }
276 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
277 LOG("flags read failed");
278 errno = EINVAL;
279 return -1;
280 }
281 *flags = be16_to_cpu(tmp) << 16;
282 /* reserved for future use */
283 if (write_sync(csock, &reserved, sizeof(reserved)) !=
284 sizeof(reserved)) {
285 LOG("write failed (reserved)");
286 errno = EINVAL;
287 return -1;
288 }
289 /* write the export name */
290 magic = cpu_to_be64(magic);
291 if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
292 LOG("write failed (magic)");
293 errno = EINVAL;
294 return -1;
295 }
296 opt = cpu_to_be32(NBD_OPT_EXPORT_NAME);
297 if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
298 LOG("write failed (opt)");
299 errno = EINVAL;
300 return -1;
301 }
302 namesize = cpu_to_be32(strlen(name));
303 if (write_sync(csock, &namesize, sizeof(namesize)) !=
304 sizeof(namesize)) {
305 LOG("write failed (namesize)");
306 errno = EINVAL;
307 return -1;
308 }
309 if (write_sync(csock, (char*)name, strlen(name)) != strlen(name)) {
310 LOG("write failed (name)");
311 errno = EINVAL;
312 return -1;
313 }
314 } else {
315 TRACE("Checking magic (cli_magic)");
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200316
Nick Thomasb2e3d872011-02-22 15:44:51 +0000317 if (magic != 0x00420281861253LL) {
318 LOG("Bad magic received");
319 errno = EINVAL;
320 return -1;
321 }
322 }
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200323
Nick Thomasb2e3d872011-02-22 15:44:51 +0000324 if (read_sync(csock, &s, sizeof(s)) != sizeof(s)) {
325 LOG("read failed");
326 errno = EINVAL;
327 return -1;
328 }
329 *size = be64_to_cpu(s);
330 *blocksize = 1024;
331 TRACE("Size is %" PRIu64, *size);
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200332
Nick Thomasb2e3d872011-02-22 15:44:51 +0000333 if (!name) {
334 if (read_sync(csock, flags, sizeof(*flags)) != sizeof(*flags)) {
335 LOG("read failed (flags)");
336 errno = EINVAL;
337 return -1;
338 }
339 *flags = be32_to_cpup(flags);
340 } else {
341 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
342 LOG("read failed (tmp)");
343 errno = EINVAL;
344 return -1;
345 }
346 *flags |= be32_to_cpu(tmp);
347 }
348 if (read_sync(csock, &buf, 124) != 124) {
349 LOG("read failed (buf)");
350 errno = EINVAL;
351 return -1;
352 }
thscd831bd2008-07-03 10:23:51 +0000353 return 0;
354}
bellard7a5ca862008-05-27 21:13:40 +0000355
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200356#ifdef __linux__
357int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
thscd831bd2008-07-03 10:23:51 +0000358{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000359 TRACE("Setting block size to %lu", (unsigned long)blocksize);
bellard7a5ca862008-05-27 21:13:40 +0000360
Nick Thomasb2e3d872011-02-22 15:44:51 +0000361 if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) == -1) {
362 int serrno = errno;
363 LOG("Failed setting NBD block size");
364 errno = serrno;
365 return -1;
366 }
bellard7a5ca862008-05-27 21:13:40 +0000367
Blue Swirl0bfcd592010-05-22 08:02:12 +0000368 TRACE("Setting size to %zd block(s)", (size_t)(size / blocksize));
bellard7a5ca862008-05-27 21:13:40 +0000369
Nick Thomasb2e3d872011-02-22 15:44:51 +0000370 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) == -1) {
371 int serrno = errno;
372 LOG("Failed setting size (in blocks)");
373 errno = serrno;
374 return -1;
375 }
bellard7a5ca862008-05-27 21:13:40 +0000376
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200377 if (flags & NBD_FLAG_READ_ONLY) {
378 int read_only = 1;
379 TRACE("Setting readonly attribute");
380
381 if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
382 int serrno = errno;
383 LOG("Failed setting read-only attribute");
384 errno = serrno;
385 return -1;
386 }
387 }
388
Paolo Bonzini973b3d02011-09-08 17:24:56 +0200389 if (ioctl(fd, NBD_SET_FLAGS, flags) < 0
390 && errno != ENOTTY) {
391 int serrno = errno;
392 LOG("Failed setting flags");
393 errno = serrno;
394 return -1;
395 }
396
Nick Thomasb2e3d872011-02-22 15:44:51 +0000397 TRACE("Clearing NBD socket");
bellard7a5ca862008-05-27 21:13:40 +0000398
Nick Thomasb2e3d872011-02-22 15:44:51 +0000399 if (ioctl(fd, NBD_CLEAR_SOCK) == -1) {
400 int serrno = errno;
401 LOG("Failed clearing NBD socket");
402 errno = serrno;
403 return -1;
404 }
bellard7a5ca862008-05-27 21:13:40 +0000405
Nick Thomasb2e3d872011-02-22 15:44:51 +0000406 TRACE("Setting NBD socket");
bellard7a5ca862008-05-27 21:13:40 +0000407
Nick Thomasb2e3d872011-02-22 15:44:51 +0000408 if (ioctl(fd, NBD_SET_SOCK, csock) == -1) {
409 int serrno = errno;
410 LOG("Failed to set NBD socket");
411 errno = serrno;
412 return -1;
413 }
bellard7a5ca862008-05-27 21:13:40 +0000414
Nick Thomasb2e3d872011-02-22 15:44:51 +0000415 TRACE("Negotiation ended");
bellard7a5ca862008-05-27 21:13:40 +0000416
Nick Thomasb2e3d872011-02-22 15:44:51 +0000417 return 0;
bellard7a5ca862008-05-27 21:13:40 +0000418}
419
420int nbd_disconnect(int fd)
421{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000422 ioctl(fd, NBD_CLEAR_QUE);
423 ioctl(fd, NBD_DISCONNECT);
424 ioctl(fd, NBD_CLEAR_SOCK);
425 return 0;
bellard7a5ca862008-05-27 21:13:40 +0000426}
427
Jes Sorensen0a4eb862010-08-31 09:30:33 +0200428int nbd_client(int fd)
bellard7a5ca862008-05-27 21:13:40 +0000429{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000430 int ret;
431 int serrno;
bellard7a5ca862008-05-27 21:13:40 +0000432
Nick Thomasb2e3d872011-02-22 15:44:51 +0000433 TRACE("Doing NBD loop");
bellard7a5ca862008-05-27 21:13:40 +0000434
Nick Thomasb2e3d872011-02-22 15:44:51 +0000435 ret = ioctl(fd, NBD_DO_IT);
Paolo Bonzini74624682011-11-04 15:51:18 +0100436 if (ret == -1 && errno == EPIPE) {
437 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
438 * the socket via NBD_DISCONNECT. We do not want to return 1 in
439 * that case.
440 */
441 ret = 0;
442 }
Nick Thomasb2e3d872011-02-22 15:44:51 +0000443 serrno = errno;
bellard7a5ca862008-05-27 21:13:40 +0000444
Nick Thomasb2e3d872011-02-22 15:44:51 +0000445 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
bellard7a5ca862008-05-27 21:13:40 +0000446
Nick Thomasb2e3d872011-02-22 15:44:51 +0000447 TRACE("Clearing NBD queue");
448 ioctl(fd, NBD_CLEAR_QUE);
bellard7a5ca862008-05-27 21:13:40 +0000449
Nick Thomasb2e3d872011-02-22 15:44:51 +0000450 TRACE("Clearing NBD socket");
451 ioctl(fd, NBD_CLEAR_SOCK);
bellard7a5ca862008-05-27 21:13:40 +0000452
Nick Thomasb2e3d872011-02-22 15:44:51 +0000453 errno = serrno;
454 return ret;
bellard7a5ca862008-05-27 21:13:40 +0000455}
aliguori03ff3ca2008-09-15 15:51:35 +0000456#else
Paolo Bonzini8e725062011-09-21 09:34:12 +0200457int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
aliguori03ff3ca2008-09-15 15:51:35 +0000458{
459 errno = ENOTSUP;
460 return -1;
461}
462
463int nbd_disconnect(int fd)
464{
465 errno = ENOTSUP;
466 return -1;
467}
468
Jes Sorensen0a4eb862010-08-31 09:30:33 +0200469int nbd_client(int fd)
aliguori03ff3ca2008-09-15 15:51:35 +0000470{
471 errno = ENOTSUP;
472 return -1;
473}
474#endif
bellard7a5ca862008-05-27 21:13:40 +0000475
ths75818252008-07-03 13:41:03 +0000476int nbd_send_request(int csock, struct nbd_request *request)
477{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000478 uint8_t buf[4 + 4 + 8 + 8 + 4];
ths75818252008-07-03 13:41:03 +0000479
Nick Thomasb2e3d872011-02-22 15:44:51 +0000480 cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC);
481 cpu_to_be32w((uint32_t*)(buf + 4), request->type);
482 cpu_to_be64w((uint64_t*)(buf + 8), request->handle);
483 cpu_to_be64w((uint64_t*)(buf + 16), request->from);
484 cpu_to_be32w((uint32_t*)(buf + 24), request->len);
ths75818252008-07-03 13:41:03 +0000485
Nick Thomasb2e3d872011-02-22 15:44:51 +0000486 TRACE("Sending request to client: "
487 "{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}",
488 request->from, request->len, request->handle, request->type);
ths75818252008-07-03 13:41:03 +0000489
Nick Thomasb2e3d872011-02-22 15:44:51 +0000490 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
491 LOG("writing to socket failed");
492 errno = EINVAL;
493 return -1;
494 }
495 return 0;
ths75818252008-07-03 13:41:03 +0000496}
497
ths75818252008-07-03 13:41:03 +0000498static int nbd_receive_request(int csock, struct nbd_request *request)
bellard7a5ca862008-05-27 21:13:40 +0000499{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000500 uint8_t buf[4 + 4 + 8 + 8 + 4];
501 uint32_t magic;
bellard7a5ca862008-05-27 21:13:40 +0000502
Nick Thomasb2e3d872011-02-22 15:44:51 +0000503 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
504 LOG("read failed");
505 errno = EINVAL;
506 return -1;
507 }
bellard7a5ca862008-05-27 21:13:40 +0000508
Nick Thomasb2e3d872011-02-22 15:44:51 +0000509 /* Request
510 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
511 [ 4 .. 7] type (0 == READ, 1 == WRITE)
512 [ 8 .. 15] handle
513 [16 .. 23] from
514 [24 .. 27] len
515 */
bellard7a5ca862008-05-27 21:13:40 +0000516
Nick Thomasb2e3d872011-02-22 15:44:51 +0000517 magic = be32_to_cpup((uint32_t*)buf);
518 request->type = be32_to_cpup((uint32_t*)(buf + 4));
519 request->handle = be64_to_cpup((uint64_t*)(buf + 8));
520 request->from = be64_to_cpup((uint64_t*)(buf + 16));
521 request->len = be32_to_cpup((uint32_t*)(buf + 24));
bellard7a5ca862008-05-27 21:13:40 +0000522
Nick Thomasb2e3d872011-02-22 15:44:51 +0000523 TRACE("Got request: "
524 "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
525 magic, request->type, request->from, request->len);
bellard7a5ca862008-05-27 21:13:40 +0000526
Nick Thomasb2e3d872011-02-22 15:44:51 +0000527 if (magic != NBD_REQUEST_MAGIC) {
528 LOG("invalid magic (got 0x%x)", magic);
529 errno = EINVAL;
530 return -1;
531 }
532 return 0;
ths75818252008-07-03 13:41:03 +0000533}
bellard7a5ca862008-05-27 21:13:40 +0000534
ths75818252008-07-03 13:41:03 +0000535int nbd_receive_reply(int csock, struct nbd_reply *reply)
536{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000537 uint8_t buf[NBD_REPLY_SIZE];
538 uint32_t magic;
ths75818252008-07-03 13:41:03 +0000539
Nick Thomasb2e3d872011-02-22 15:44:51 +0000540 memset(buf, 0xAA, sizeof(buf));
ths75818252008-07-03 13:41:03 +0000541
Nick Thomasb2e3d872011-02-22 15:44:51 +0000542 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
543 LOG("read failed");
544 errno = EINVAL;
545 return -1;
546 }
bellard7a5ca862008-05-27 21:13:40 +0000547
Nick Thomasb2e3d872011-02-22 15:44:51 +0000548 /* Reply
549 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
550 [ 4 .. 7] error (0 == no error)
551 [ 7 .. 15] handle
552 */
ths75818252008-07-03 13:41:03 +0000553
Nick Thomasb2e3d872011-02-22 15:44:51 +0000554 magic = be32_to_cpup((uint32_t*)buf);
555 reply->error = be32_to_cpup((uint32_t*)(buf + 4));
556 reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
ths75818252008-07-03 13:41:03 +0000557
Nick Thomasb2e3d872011-02-22 15:44:51 +0000558 TRACE("Got reply: "
559 "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
560 magic, reply->error, reply->handle);
ths75818252008-07-03 13:41:03 +0000561
Nick Thomasb2e3d872011-02-22 15:44:51 +0000562 if (magic != NBD_REPLY_MAGIC) {
563 LOG("invalid magic (got 0x%x)", magic);
564 errno = EINVAL;
565 return -1;
566 }
567 return 0;
ths75818252008-07-03 13:41:03 +0000568}
569
570static int nbd_send_reply(int csock, struct nbd_reply *reply)
571{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000572 uint8_t buf[4 + 4 + 8];
ths75818252008-07-03 13:41:03 +0000573
Nick Thomasb2e3d872011-02-22 15:44:51 +0000574 /* Reply
575 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
576 [ 4 .. 7] error (0 == no error)
577 [ 7 .. 15] handle
578 */
579 cpu_to_be32w((uint32_t*)buf, NBD_REPLY_MAGIC);
580 cpu_to_be32w((uint32_t*)(buf + 4), reply->error);
581 cpu_to_be64w((uint64_t*)(buf + 8), reply->handle);
ths75818252008-07-03 13:41:03 +0000582
Nick Thomasb2e3d872011-02-22 15:44:51 +0000583 TRACE("Sending response to client");
ths75818252008-07-03 13:41:03 +0000584
Nick Thomasb2e3d872011-02-22 15:44:51 +0000585 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
586 LOG("writing to socket failed");
587 errno = EINVAL;
588 return -1;
589 }
590 return 0;
ths75818252008-07-03 13:41:03 +0000591}
592
593int nbd_trip(BlockDriverState *bs, int csock, off_t size, uint64_t dev_offset,
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200594 off_t *offset, uint32_t nbdflags, uint8_t *data, int data_size)
ths75818252008-07-03 13:41:03 +0000595{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000596 struct nbd_request request;
597 struct nbd_reply reply;
ths75818252008-07-03 13:41:03 +0000598
Nick Thomasb2e3d872011-02-22 15:44:51 +0000599 TRACE("Reading request.");
ths75818252008-07-03 13:41:03 +0000600
Nick Thomasb2e3d872011-02-22 15:44:51 +0000601 if (nbd_receive_request(csock, &request) == -1)
602 return -1;
ths75818252008-07-03 13:41:03 +0000603
Nick Thomasb2e3d872011-02-22 15:44:51 +0000604 if (request.len + NBD_REPLY_SIZE > data_size) {
605 LOG("len (%u) is larger than max len (%u)",
606 request.len + NBD_REPLY_SIZE, data_size);
607 errno = EINVAL;
608 return -1;
609 }
ths75818252008-07-03 13:41:03 +0000610
Nick Thomasb2e3d872011-02-22 15:44:51 +0000611 if ((request.from + request.len) < request.from) {
612 LOG("integer overflow detected! "
613 "you're probably being attacked");
614 errno = EINVAL;
615 return -1;
616 }
bellard7a5ca862008-05-27 21:13:40 +0000617
Nick Thomasb2e3d872011-02-22 15:44:51 +0000618 if ((request.from + request.len) > size) {
619 LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64
620 ", Offset: %" PRIu64 "\n",
blueswir1b9e82a52009-04-05 18:03:31 +0000621 request.from, request.len, (uint64_t)size, dev_offset);
Nick Thomasb2e3d872011-02-22 15:44:51 +0000622 LOG("requested operation past EOF--bad client?");
623 errno = EINVAL;
624 return -1;
625 }
bellard7a5ca862008-05-27 21:13:40 +0000626
Nick Thomasb2e3d872011-02-22 15:44:51 +0000627 TRACE("Decoding type");
bellard7a5ca862008-05-27 21:13:40 +0000628
Nick Thomasb2e3d872011-02-22 15:44:51 +0000629 reply.handle = request.handle;
630 reply.error = 0;
ths75818252008-07-03 13:41:03 +0000631
Nick Thomasb2e3d872011-02-22 15:44:51 +0000632 switch (request.type) {
633 case NBD_CMD_READ:
634 TRACE("Request type is READ");
bellard7a5ca862008-05-27 21:13:40 +0000635
Nick Thomasb2e3d872011-02-22 15:44:51 +0000636 if (bdrv_read(bs, (request.from + dev_offset) / 512,
637 data + NBD_REPLY_SIZE,
638 request.len / 512) == -1) {
639 LOG("reading from file failed");
640 errno = EINVAL;
641 return -1;
642 }
643 *offset += request.len;
bellard7a5ca862008-05-27 21:13:40 +0000644
Nick Thomasb2e3d872011-02-22 15:44:51 +0000645 TRACE("Read %u byte(s)", request.len);
bellard7a5ca862008-05-27 21:13:40 +0000646
Nick Thomasb2e3d872011-02-22 15:44:51 +0000647 /* Reply
648 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
649 [ 4 .. 7] error (0 == no error)
650 [ 7 .. 15] handle
651 */
Laurent Vivier5fe16882010-09-17 18:13:57 +0200652
Nick Thomasb2e3d872011-02-22 15:44:51 +0000653 cpu_to_be32w((uint32_t*)data, NBD_REPLY_MAGIC);
654 cpu_to_be32w((uint32_t*)(data + 4), reply.error);
655 cpu_to_be64w((uint64_t*)(data + 8), reply.handle);
bellard7a5ca862008-05-27 21:13:40 +0000656
Nick Thomasb2e3d872011-02-22 15:44:51 +0000657 TRACE("Sending data to client");
bellard7a5ca862008-05-27 21:13:40 +0000658
Nick Thomasb2e3d872011-02-22 15:44:51 +0000659 if (write_sync(csock, data,
660 request.len + NBD_REPLY_SIZE) !=
661 request.len + NBD_REPLY_SIZE) {
662 LOG("writing to socket failed");
663 errno = EINVAL;
664 return -1;
665 }
666 break;
667 case NBD_CMD_WRITE:
668 TRACE("Request type is WRITE");
bellard7a5ca862008-05-27 21:13:40 +0000669
Nick Thomasb2e3d872011-02-22 15:44:51 +0000670 TRACE("Reading %u byte(s)", request.len);
bellard7a5ca862008-05-27 21:13:40 +0000671
Nick Thomasb2e3d872011-02-22 15:44:51 +0000672 if (read_sync(csock, data, request.len) != request.len) {
673 LOG("reading from socket failed");
674 errno = EINVAL;
675 return -1;
676 }
bellard7a5ca862008-05-27 21:13:40 +0000677
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200678 if (nbdflags & NBD_FLAG_READ_ONLY) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000679 TRACE("Server is read-only, return error");
680 reply.error = 1;
681 } else {
682 TRACE("Writing to device");
bellard7a5ca862008-05-27 21:13:40 +0000683
Nick Thomasb2e3d872011-02-22 15:44:51 +0000684 if (bdrv_write(bs, (request.from + dev_offset) / 512,
685 data, request.len / 512) == -1) {
686 LOG("writing to file failed");
687 errno = EINVAL;
688 return -1;
689 }
bellard7a5ca862008-05-27 21:13:40 +0000690
Nick Thomasb2e3d872011-02-22 15:44:51 +0000691 *offset += request.len;
692 }
bellard7a5ca862008-05-27 21:13:40 +0000693
Nick Thomasb2e3d872011-02-22 15:44:51 +0000694 if (nbd_send_reply(csock, &reply) == -1)
695 return -1;
696 break;
697 case NBD_CMD_DISC:
698 TRACE("Request type is DISCONNECT");
699 errno = 0;
700 return 1;
701 default:
702 LOG("invalid request type (%u) received", request.type);
703 errno = EINVAL;
704 return -1;
705 }
bellard7a5ca862008-05-27 21:13:40 +0000706
Nick Thomasb2e3d872011-02-22 15:44:51 +0000707 TRACE("Request/Reply complete");
bellard7a5ca862008-05-27 21:13:40 +0000708
Nick Thomasb2e3d872011-02-22 15:44:51 +0000709 return 0;
bellard7a5ca862008-05-27 21:13:40 +0000710}