blob: c597d4733f3130640cadd1fc3fe31fffb63f4a12 [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 Bonzini2c7989a2011-10-21 13:16:28 +0200205 cpu_to_be32w((uint32_t*)(buf + 24),
206 flags | NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_FUA);
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200207 memset(buf + 28, 0, 124);
bellard7a5ca862008-05-27 21:13:40 +0000208
Nick Thomasb2e3d872011-02-22 15:44:51 +0000209 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
210 LOG("write failed");
211 errno = EINVAL;
212 return -1;
213 }
bellard7a5ca862008-05-27 21:13:40 +0000214
Dong Xu Wang07f35072011-11-22 18:06:26 +0800215 TRACE("Negotiation succeeded.");
bellard7a5ca862008-05-27 21:13:40 +0000216
Nick Thomasb2e3d872011-02-22 15:44:51 +0000217 return 0;
bellard7a5ca862008-05-27 21:13:40 +0000218}
219
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200220int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
221 off_t *size, size_t *blocksize)
bellard7a5ca862008-05-27 21:13:40 +0000222{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000223 char buf[256];
224 uint64_t magic, s;
225 uint16_t tmp;
bellard7a5ca862008-05-27 21:13:40 +0000226
Dong Xu Wang07f35072011-11-22 18:06:26 +0800227 TRACE("Receiving negotiation.");
bellard7a5ca862008-05-27 21:13:40 +0000228
Nick Thomasb2e3d872011-02-22 15:44:51 +0000229 if (read_sync(csock, buf, 8) != 8) {
230 LOG("read failed");
231 errno = EINVAL;
232 return -1;
233 }
bellard7a5ca862008-05-27 21:13:40 +0000234
Nick Thomasb2e3d872011-02-22 15:44:51 +0000235 buf[8] = '\0';
236 if (strlen(buf) == 0) {
237 LOG("server connection closed");
238 errno = EINVAL;
239 return -1;
240 }
bellard7a5ca862008-05-27 21:13:40 +0000241
Nick Thomasb2e3d872011-02-22 15:44:51 +0000242 TRACE("Magic is %c%c%c%c%c%c%c%c",
243 qemu_isprint(buf[0]) ? buf[0] : '.',
244 qemu_isprint(buf[1]) ? buf[1] : '.',
245 qemu_isprint(buf[2]) ? buf[2] : '.',
246 qemu_isprint(buf[3]) ? buf[3] : '.',
247 qemu_isprint(buf[4]) ? buf[4] : '.',
248 qemu_isprint(buf[5]) ? buf[5] : '.',
249 qemu_isprint(buf[6]) ? buf[6] : '.',
250 qemu_isprint(buf[7]) ? buf[7] : '.');
bellard7a5ca862008-05-27 21:13:40 +0000251
Nick Thomasb2e3d872011-02-22 15:44:51 +0000252 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
253 LOG("Invalid magic received");
254 errno = EINVAL;
255 return -1;
256 }
bellard7a5ca862008-05-27 21:13:40 +0000257
Nick Thomasb2e3d872011-02-22 15:44:51 +0000258 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
259 LOG("read failed");
260 errno = EINVAL;
261 return -1;
262 }
263 magic = be64_to_cpu(magic);
264 TRACE("Magic is 0x%" PRIx64, magic);
bellard7a5ca862008-05-27 21:13:40 +0000265
Nick Thomasb2e3d872011-02-22 15:44:51 +0000266 if (name) {
267 uint32_t reserved = 0;
268 uint32_t opt;
269 uint32_t namesize;
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200270
Nick Thomasb2e3d872011-02-22 15:44:51 +0000271 TRACE("Checking magic (opts_magic)");
272 if (magic != 0x49484156454F5054LL) {
273 LOG("Bad magic received");
274 errno = EINVAL;
275 return -1;
276 }
277 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
278 LOG("flags read failed");
279 errno = EINVAL;
280 return -1;
281 }
282 *flags = be16_to_cpu(tmp) << 16;
283 /* reserved for future use */
284 if (write_sync(csock, &reserved, sizeof(reserved)) !=
285 sizeof(reserved)) {
286 LOG("write failed (reserved)");
287 errno = EINVAL;
288 return -1;
289 }
290 /* write the export name */
291 magic = cpu_to_be64(magic);
292 if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
293 LOG("write failed (magic)");
294 errno = EINVAL;
295 return -1;
296 }
297 opt = cpu_to_be32(NBD_OPT_EXPORT_NAME);
298 if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
299 LOG("write failed (opt)");
300 errno = EINVAL;
301 return -1;
302 }
303 namesize = cpu_to_be32(strlen(name));
304 if (write_sync(csock, &namesize, sizeof(namesize)) !=
305 sizeof(namesize)) {
306 LOG("write failed (namesize)");
307 errno = EINVAL;
308 return -1;
309 }
310 if (write_sync(csock, (char*)name, strlen(name)) != strlen(name)) {
311 LOG("write failed (name)");
312 errno = EINVAL;
313 return -1;
314 }
315 } else {
316 TRACE("Checking magic (cli_magic)");
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200317
Nick Thomasb2e3d872011-02-22 15:44:51 +0000318 if (magic != 0x00420281861253LL) {
319 LOG("Bad magic received");
320 errno = EINVAL;
321 return -1;
322 }
323 }
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200324
Nick Thomasb2e3d872011-02-22 15:44:51 +0000325 if (read_sync(csock, &s, sizeof(s)) != sizeof(s)) {
326 LOG("read failed");
327 errno = EINVAL;
328 return -1;
329 }
330 *size = be64_to_cpu(s);
331 *blocksize = 1024;
332 TRACE("Size is %" PRIu64, *size);
Laurent Vivier1d45f8b2010-08-25 22:48:33 +0200333
Nick Thomasb2e3d872011-02-22 15:44:51 +0000334 if (!name) {
335 if (read_sync(csock, flags, sizeof(*flags)) != sizeof(*flags)) {
336 LOG("read failed (flags)");
337 errno = EINVAL;
338 return -1;
339 }
340 *flags = be32_to_cpup(flags);
341 } else {
342 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
343 LOG("read failed (tmp)");
344 errno = EINVAL;
345 return -1;
346 }
347 *flags |= be32_to_cpu(tmp);
348 }
349 if (read_sync(csock, &buf, 124) != 124) {
350 LOG("read failed (buf)");
351 errno = EINVAL;
352 return -1;
353 }
thscd831bd2008-07-03 10:23:51 +0000354 return 0;
355}
bellard7a5ca862008-05-27 21:13:40 +0000356
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200357#ifdef __linux__
358int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
thscd831bd2008-07-03 10:23:51 +0000359{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000360 TRACE("Setting block size to %lu", (unsigned long)blocksize);
bellard7a5ca862008-05-27 21:13:40 +0000361
Nick Thomasb2e3d872011-02-22 15:44:51 +0000362 if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) == -1) {
363 int serrno = errno;
364 LOG("Failed setting NBD block size");
365 errno = serrno;
366 return -1;
367 }
bellard7a5ca862008-05-27 21:13:40 +0000368
Blue Swirl0bfcd592010-05-22 08:02:12 +0000369 TRACE("Setting size to %zd block(s)", (size_t)(size / blocksize));
bellard7a5ca862008-05-27 21:13:40 +0000370
Nick Thomasb2e3d872011-02-22 15:44:51 +0000371 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) == -1) {
372 int serrno = errno;
373 LOG("Failed setting size (in blocks)");
374 errno = serrno;
375 return -1;
376 }
bellard7a5ca862008-05-27 21:13:40 +0000377
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200378 if (flags & NBD_FLAG_READ_ONLY) {
379 int read_only = 1;
380 TRACE("Setting readonly attribute");
381
382 if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
383 int serrno = errno;
384 LOG("Failed setting read-only attribute");
385 errno = serrno;
386 return -1;
387 }
388 }
389
Paolo Bonzini973b3d02011-09-08 17:24:56 +0200390 if (ioctl(fd, NBD_SET_FLAGS, flags) < 0
391 && errno != ENOTTY) {
392 int serrno = errno;
393 LOG("Failed setting flags");
394 errno = serrno;
395 return -1;
396 }
397
Nick Thomasb2e3d872011-02-22 15:44:51 +0000398 TRACE("Clearing NBD socket");
bellard7a5ca862008-05-27 21:13:40 +0000399
Nick Thomasb2e3d872011-02-22 15:44:51 +0000400 if (ioctl(fd, NBD_CLEAR_SOCK) == -1) {
401 int serrno = errno;
402 LOG("Failed clearing NBD socket");
403 errno = serrno;
404 return -1;
405 }
bellard7a5ca862008-05-27 21:13:40 +0000406
Nick Thomasb2e3d872011-02-22 15:44:51 +0000407 TRACE("Setting NBD socket");
bellard7a5ca862008-05-27 21:13:40 +0000408
Nick Thomasb2e3d872011-02-22 15:44:51 +0000409 if (ioctl(fd, NBD_SET_SOCK, csock) == -1) {
410 int serrno = errno;
411 LOG("Failed to set NBD socket");
412 errno = serrno;
413 return -1;
414 }
bellard7a5ca862008-05-27 21:13:40 +0000415
Nick Thomasb2e3d872011-02-22 15:44:51 +0000416 TRACE("Negotiation ended");
bellard7a5ca862008-05-27 21:13:40 +0000417
Nick Thomasb2e3d872011-02-22 15:44:51 +0000418 return 0;
bellard7a5ca862008-05-27 21:13:40 +0000419}
420
421int nbd_disconnect(int fd)
422{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000423 ioctl(fd, NBD_CLEAR_QUE);
424 ioctl(fd, NBD_DISCONNECT);
425 ioctl(fd, NBD_CLEAR_SOCK);
426 return 0;
bellard7a5ca862008-05-27 21:13:40 +0000427}
428
Jes Sorensen0a4eb862010-08-31 09:30:33 +0200429int nbd_client(int fd)
bellard7a5ca862008-05-27 21:13:40 +0000430{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000431 int ret;
432 int serrno;
bellard7a5ca862008-05-27 21:13:40 +0000433
Nick Thomasb2e3d872011-02-22 15:44:51 +0000434 TRACE("Doing NBD loop");
bellard7a5ca862008-05-27 21:13:40 +0000435
Nick Thomasb2e3d872011-02-22 15:44:51 +0000436 ret = ioctl(fd, NBD_DO_IT);
Paolo Bonzini74624682011-11-04 15:51:18 +0100437 if (ret == -1 && errno == EPIPE) {
438 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
439 * the socket via NBD_DISCONNECT. We do not want to return 1 in
440 * that case.
441 */
442 ret = 0;
443 }
Nick Thomasb2e3d872011-02-22 15:44:51 +0000444 serrno = errno;
bellard7a5ca862008-05-27 21:13:40 +0000445
Nick Thomasb2e3d872011-02-22 15:44:51 +0000446 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
bellard7a5ca862008-05-27 21:13:40 +0000447
Nick Thomasb2e3d872011-02-22 15:44:51 +0000448 TRACE("Clearing NBD queue");
449 ioctl(fd, NBD_CLEAR_QUE);
bellard7a5ca862008-05-27 21:13:40 +0000450
Nick Thomasb2e3d872011-02-22 15:44:51 +0000451 TRACE("Clearing NBD socket");
452 ioctl(fd, NBD_CLEAR_SOCK);
bellard7a5ca862008-05-27 21:13:40 +0000453
Nick Thomasb2e3d872011-02-22 15:44:51 +0000454 errno = serrno;
455 return ret;
bellard7a5ca862008-05-27 21:13:40 +0000456}
aliguori03ff3ca2008-09-15 15:51:35 +0000457#else
Paolo Bonzini8e725062011-09-21 09:34:12 +0200458int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
aliguori03ff3ca2008-09-15 15:51:35 +0000459{
460 errno = ENOTSUP;
461 return -1;
462}
463
464int nbd_disconnect(int fd)
465{
466 errno = ENOTSUP;
467 return -1;
468}
469
Jes Sorensen0a4eb862010-08-31 09:30:33 +0200470int nbd_client(int fd)
aliguori03ff3ca2008-09-15 15:51:35 +0000471{
472 errno = ENOTSUP;
473 return -1;
474}
475#endif
bellard7a5ca862008-05-27 21:13:40 +0000476
ths75818252008-07-03 13:41:03 +0000477int nbd_send_request(int csock, struct nbd_request *request)
478{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000479 uint8_t buf[4 + 4 + 8 + 8 + 4];
ths75818252008-07-03 13:41:03 +0000480
Nick Thomasb2e3d872011-02-22 15:44:51 +0000481 cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC);
482 cpu_to_be32w((uint32_t*)(buf + 4), request->type);
483 cpu_to_be64w((uint64_t*)(buf + 8), request->handle);
484 cpu_to_be64w((uint64_t*)(buf + 16), request->from);
485 cpu_to_be32w((uint32_t*)(buf + 24), request->len);
ths75818252008-07-03 13:41:03 +0000486
Nick Thomasb2e3d872011-02-22 15:44:51 +0000487 TRACE("Sending request to client: "
488 "{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}",
489 request->from, request->len, request->handle, request->type);
ths75818252008-07-03 13:41:03 +0000490
Nick Thomasb2e3d872011-02-22 15:44:51 +0000491 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
492 LOG("writing to socket failed");
493 errno = EINVAL;
494 return -1;
495 }
496 return 0;
ths75818252008-07-03 13:41:03 +0000497}
498
ths75818252008-07-03 13:41:03 +0000499static int nbd_receive_request(int csock, struct nbd_request *request)
bellard7a5ca862008-05-27 21:13:40 +0000500{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000501 uint8_t buf[4 + 4 + 8 + 8 + 4];
502 uint32_t magic;
bellard7a5ca862008-05-27 21:13:40 +0000503
Nick Thomasb2e3d872011-02-22 15:44:51 +0000504 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
505 LOG("read failed");
506 errno = EINVAL;
507 return -1;
508 }
bellard7a5ca862008-05-27 21:13:40 +0000509
Nick Thomasb2e3d872011-02-22 15:44:51 +0000510 /* Request
511 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
512 [ 4 .. 7] type (0 == READ, 1 == WRITE)
513 [ 8 .. 15] handle
514 [16 .. 23] from
515 [24 .. 27] len
516 */
bellard7a5ca862008-05-27 21:13:40 +0000517
Nick Thomasb2e3d872011-02-22 15:44:51 +0000518 magic = be32_to_cpup((uint32_t*)buf);
519 request->type = be32_to_cpup((uint32_t*)(buf + 4));
520 request->handle = be64_to_cpup((uint64_t*)(buf + 8));
521 request->from = be64_to_cpup((uint64_t*)(buf + 16));
522 request->len = be32_to_cpup((uint32_t*)(buf + 24));
bellard7a5ca862008-05-27 21:13:40 +0000523
Nick Thomasb2e3d872011-02-22 15:44:51 +0000524 TRACE("Got request: "
525 "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
526 magic, request->type, request->from, request->len);
bellard7a5ca862008-05-27 21:13:40 +0000527
Nick Thomasb2e3d872011-02-22 15:44:51 +0000528 if (magic != NBD_REQUEST_MAGIC) {
529 LOG("invalid magic (got 0x%x)", magic);
530 errno = EINVAL;
531 return -1;
532 }
533 return 0;
ths75818252008-07-03 13:41:03 +0000534}
bellard7a5ca862008-05-27 21:13:40 +0000535
ths75818252008-07-03 13:41:03 +0000536int nbd_receive_reply(int csock, struct nbd_reply *reply)
537{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000538 uint8_t buf[NBD_REPLY_SIZE];
539 uint32_t magic;
ths75818252008-07-03 13:41:03 +0000540
Nick Thomasb2e3d872011-02-22 15:44:51 +0000541 memset(buf, 0xAA, sizeof(buf));
ths75818252008-07-03 13:41:03 +0000542
Nick Thomasb2e3d872011-02-22 15:44:51 +0000543 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
544 LOG("read failed");
545 errno = EINVAL;
546 return -1;
547 }
bellard7a5ca862008-05-27 21:13:40 +0000548
Nick Thomasb2e3d872011-02-22 15:44:51 +0000549 /* Reply
550 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
551 [ 4 .. 7] error (0 == no error)
552 [ 7 .. 15] handle
553 */
ths75818252008-07-03 13:41:03 +0000554
Nick Thomasb2e3d872011-02-22 15:44:51 +0000555 magic = be32_to_cpup((uint32_t*)buf);
556 reply->error = be32_to_cpup((uint32_t*)(buf + 4));
557 reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
ths75818252008-07-03 13:41:03 +0000558
Nick Thomasb2e3d872011-02-22 15:44:51 +0000559 TRACE("Got reply: "
560 "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
561 magic, reply->error, reply->handle);
ths75818252008-07-03 13:41:03 +0000562
Nick Thomasb2e3d872011-02-22 15:44:51 +0000563 if (magic != NBD_REPLY_MAGIC) {
564 LOG("invalid magic (got 0x%x)", magic);
565 errno = EINVAL;
566 return -1;
567 }
568 return 0;
ths75818252008-07-03 13:41:03 +0000569}
570
571static int nbd_send_reply(int csock, struct nbd_reply *reply)
572{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000573 uint8_t buf[4 + 4 + 8];
ths75818252008-07-03 13:41:03 +0000574
Nick Thomasb2e3d872011-02-22 15:44:51 +0000575 /* Reply
576 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
577 [ 4 .. 7] error (0 == no error)
578 [ 7 .. 15] handle
579 */
580 cpu_to_be32w((uint32_t*)buf, NBD_REPLY_MAGIC);
581 cpu_to_be32w((uint32_t*)(buf + 4), reply->error);
582 cpu_to_be64w((uint64_t*)(buf + 8), reply->handle);
ths75818252008-07-03 13:41:03 +0000583
Nick Thomasb2e3d872011-02-22 15:44:51 +0000584 TRACE("Sending response to client");
ths75818252008-07-03 13:41:03 +0000585
Nick Thomasb2e3d872011-02-22 15:44:51 +0000586 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
587 LOG("writing to socket failed");
588 errno = EINVAL;
589 return -1;
590 }
591 return 0;
ths75818252008-07-03 13:41:03 +0000592}
593
594int nbd_trip(BlockDriverState *bs, int csock, off_t size, uint64_t dev_offset,
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200595 off_t *offset, uint32_t nbdflags, uint8_t *data, int data_size)
ths75818252008-07-03 13:41:03 +0000596{
Nick Thomasb2e3d872011-02-22 15:44:51 +0000597 struct nbd_request request;
598 struct nbd_reply reply;
Paolo Bonziniadcf6302011-09-13 17:27:45 +0200599 int ret;
ths75818252008-07-03 13:41:03 +0000600
Nick Thomasb2e3d872011-02-22 15:44:51 +0000601 TRACE("Reading request.");
ths75818252008-07-03 13:41:03 +0000602
Nick Thomasb2e3d872011-02-22 15:44:51 +0000603 if (nbd_receive_request(csock, &request) == -1)
604 return -1;
ths75818252008-07-03 13:41:03 +0000605
Nick Thomasb2e3d872011-02-22 15:44:51 +0000606 if (request.len + NBD_REPLY_SIZE > data_size) {
607 LOG("len (%u) is larger than max len (%u)",
608 request.len + NBD_REPLY_SIZE, data_size);
609 errno = EINVAL;
610 return -1;
611 }
ths75818252008-07-03 13:41:03 +0000612
Nick Thomasb2e3d872011-02-22 15:44:51 +0000613 if ((request.from + request.len) < request.from) {
614 LOG("integer overflow detected! "
615 "you're probably being attacked");
616 errno = EINVAL;
617 return -1;
618 }
bellard7a5ca862008-05-27 21:13:40 +0000619
Nick Thomasb2e3d872011-02-22 15:44:51 +0000620 if ((request.from + request.len) > size) {
621 LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64
622 ", Offset: %" PRIu64 "\n",
blueswir1b9e82a52009-04-05 18:03:31 +0000623 request.from, request.len, (uint64_t)size, dev_offset);
Nick Thomasb2e3d872011-02-22 15:44:51 +0000624 LOG("requested operation past EOF--bad client?");
625 errno = EINVAL;
626 return -1;
627 }
bellard7a5ca862008-05-27 21:13:40 +0000628
Nick Thomasb2e3d872011-02-22 15:44:51 +0000629 TRACE("Decoding type");
bellard7a5ca862008-05-27 21:13:40 +0000630
Nick Thomasb2e3d872011-02-22 15:44:51 +0000631 reply.handle = request.handle;
632 reply.error = 0;
ths75818252008-07-03 13:41:03 +0000633
Paolo Bonzini2c7989a2011-10-21 13:16:28 +0200634 switch (request.type & NBD_CMD_MASK_COMMAND) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000635 case NBD_CMD_READ:
636 TRACE("Request type is READ");
bellard7a5ca862008-05-27 21:13:40 +0000637
Paolo Bonziniadcf6302011-09-13 17:27:45 +0200638 ret = bdrv_read(bs, (request.from + dev_offset) / 512,
639 data + NBD_REPLY_SIZE,
640 request.len / 512);
641 if (ret < 0) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000642 LOG("reading from file failed");
Paolo Bonziniadcf6302011-09-13 17:27:45 +0200643 reply.error = -ret;
644 request.len = 0;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000645 }
646 *offset += request.len;
bellard7a5ca862008-05-27 21:13:40 +0000647
Nick Thomasb2e3d872011-02-22 15:44:51 +0000648 TRACE("Read %u byte(s)", request.len);
bellard7a5ca862008-05-27 21:13:40 +0000649
Nick Thomasb2e3d872011-02-22 15:44:51 +0000650 /* Reply
651 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
652 [ 4 .. 7] error (0 == no error)
653 [ 7 .. 15] handle
654 */
Laurent Vivier5fe16882010-09-17 18:13:57 +0200655
Nick Thomasb2e3d872011-02-22 15:44:51 +0000656 cpu_to_be32w((uint32_t*)data, NBD_REPLY_MAGIC);
657 cpu_to_be32w((uint32_t*)(data + 4), reply.error);
658 cpu_to_be64w((uint64_t*)(data + 8), reply.handle);
bellard7a5ca862008-05-27 21:13:40 +0000659
Nick Thomasb2e3d872011-02-22 15:44:51 +0000660 TRACE("Sending data to client");
bellard7a5ca862008-05-27 21:13:40 +0000661
Nick Thomasb2e3d872011-02-22 15:44:51 +0000662 if (write_sync(csock, data,
663 request.len + NBD_REPLY_SIZE) !=
664 request.len + NBD_REPLY_SIZE) {
665 LOG("writing to socket failed");
666 errno = EINVAL;
667 return -1;
668 }
669 break;
670 case NBD_CMD_WRITE:
671 TRACE("Request type is WRITE");
bellard7a5ca862008-05-27 21:13:40 +0000672
Nick Thomasb2e3d872011-02-22 15:44:51 +0000673 TRACE("Reading %u byte(s)", request.len);
bellard7a5ca862008-05-27 21:13:40 +0000674
Nick Thomasb2e3d872011-02-22 15:44:51 +0000675 if (read_sync(csock, data, request.len) != request.len) {
676 LOG("reading from socket failed");
677 errno = EINVAL;
678 return -1;
679 }
bellard7a5ca862008-05-27 21:13:40 +0000680
Paolo Bonzinib90fb4b2011-09-08 17:24:54 +0200681 if (nbdflags & NBD_FLAG_READ_ONLY) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000682 TRACE("Server is read-only, return error");
683 reply.error = 1;
684 } else {
685 TRACE("Writing to device");
bellard7a5ca862008-05-27 21:13:40 +0000686
Paolo Bonziniadcf6302011-09-13 17:27:45 +0200687 ret = bdrv_write(bs, (request.from + dev_offset) / 512,
688 data, request.len / 512);
689 if (ret < 0) {
Nick Thomasb2e3d872011-02-22 15:44:51 +0000690 LOG("writing to file failed");
Paolo Bonziniadcf6302011-09-13 17:27:45 +0200691 reply.error = -ret;
692 request.len = 0;
Nick Thomasb2e3d872011-02-22 15:44:51 +0000693 }
bellard7a5ca862008-05-27 21:13:40 +0000694
Nick Thomasb2e3d872011-02-22 15:44:51 +0000695 *offset += request.len;
Paolo Bonzini2c7989a2011-10-21 13:16:28 +0200696
697 if (request.type & NBD_CMD_FLAG_FUA) {
698 ret = bdrv_flush(bs);
699 if (ret < 0) {
700 LOG("flush failed");
701 reply.error = -ret;
702 }
703 }
Nick Thomasb2e3d872011-02-22 15:44:51 +0000704 }
bellard7a5ca862008-05-27 21:13:40 +0000705
Nick Thomasb2e3d872011-02-22 15:44:51 +0000706 if (nbd_send_reply(csock, &reply) == -1)
707 return -1;
708 break;
709 case NBD_CMD_DISC:
710 TRACE("Request type is DISCONNECT");
711 errno = 0;
712 return 1;
713 default:
714 LOG("invalid request type (%u) received", request.type);
715 errno = EINVAL;
716 return -1;
717 }
bellard7a5ca862008-05-27 21:13:40 +0000718
Nick Thomasb2e3d872011-02-22 15:44:51 +0000719 TRACE("Request/Reply complete");
bellard7a5ca862008-05-27 21:13:40 +0000720
Nick Thomasb2e3d872011-02-22 15:44:51 +0000721 return 0;
bellard7a5ca862008-05-27 21:13:40 +0000722}