]>
git.proxmox.com Git - qemu.git/blob - nbd.c
8d2a3bc1138e523545e8935dc7d2cc3f4112aa15
2 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "block_int.h"
26 #include <sys/ioctl.h>
28 #if defined(__sun__) || defined(__HAIKU__)
29 #include <sys/ioccom.h>
38 #include "qemu_socket.h"
43 #define TRACE(msg, ...) do { \
44 LOG(msg, ## __VA_ARGS__); \
47 #define TRACE(msg, ...) \
51 #define LOG(msg, ...) do { \
52 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
53 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
56 /* This is all part of the "official" NBD API */
58 #define NBD_REPLY_SIZE (4 + 4 + 8)
59 #define NBD_REQUEST_MAGIC 0x25609513
60 #define NBD_REPLY_MAGIC 0x67446698
62 #define NBD_SET_SOCK _IO(0xab, 0)
63 #define NBD_SET_BLKSIZE _IO(0xab, 1)
64 #define NBD_SET_SIZE _IO(0xab, 2)
65 #define NBD_DO_IT _IO(0xab, 3)
66 #define NBD_CLEAR_SOCK _IO(0xab, 4)
67 #define NBD_CLEAR_QUE _IO(0xab, 5)
68 #define NBD_PRINT_DEBUG _IO(0xab, 6)
69 #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
70 #define NBD_DISCONNECT _IO(0xab, 8)
71 #define NBD_SET_TIMEOUT _IO(0xab, 9)
72 #define NBD_SET_FLAGS _IO(0xab, 10)
74 #define NBD_OPT_EXPORT_NAME (1 << 0)
76 /* That's all folks */
78 #define read_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, true)
79 #define write_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, false)
81 size_t nbd_wr_sync(int fd
, void *buffer
, size_t size
, bool do_read
)
85 if (qemu_in_coroutine()) {
87 return qemu_co_recv(fd
, buffer
, size
);
89 return qemu_co_send(fd
, buffer
, size
);
93 while (offset
< size
) {
97 len
= qemu_recv(fd
, buffer
+ offset
, size
- offset
, 0);
99 len
= send(fd
, buffer
+ offset
, size
- offset
, 0);
103 errno
= socket_error();
105 /* recoverable error */
106 if (len
== -1 && (errno
== EAGAIN
|| errno
== EINTR
)) {
115 /* unrecoverable error */
126 static void combine_addr(char *buf
, size_t len
, const char* address
,
129 /* If the address-part contains a colon, it's an IPv6 IP so needs [] */
130 if (strstr(address
, ":")) {
131 snprintf(buf
, len
, "[%s]:%u", address
, port
);
133 snprintf(buf
, len
, "%s:%u", address
, port
);
137 int tcp_socket_outgoing(const char *address
, uint16_t port
)
139 char address_and_port
[128];
140 combine_addr(address_and_port
, 128, address
, port
);
141 return tcp_socket_outgoing_spec(address_and_port
);
144 int tcp_socket_outgoing_spec(const char *address_and_port
)
146 return inet_connect(address_and_port
, SOCK_STREAM
);
149 int tcp_socket_incoming(const char *address
, uint16_t port
)
151 char address_and_port
[128];
152 combine_addr(address_and_port
, 128, address
, port
);
153 return tcp_socket_incoming_spec(address_and_port
);
156 int tcp_socket_incoming_spec(const char *address_and_port
)
160 return inet_listen(address_and_port
, ostr
, olen
, SOCK_STREAM
, 0);
163 int unix_socket_incoming(const char *path
)
168 return unix_listen(path
, ostr
, olen
);
171 int unix_socket_outgoing(const char *path
)
173 return unix_connect(path
);
190 static int nbd_send_negotiate(int csock
, off_t size
, uint32_t flags
)
192 char buf
[8 + 8 + 8 + 128];
195 [ 0 .. 7] passwd ("NBDMAGIC")
196 [ 8 .. 15] magic (0x00420281861253)
199 [28 .. 151] reserved (0)
202 TRACE("Beginning negotiation.");
203 memcpy(buf
, "NBDMAGIC", 8);
204 cpu_to_be64w((uint64_t*)(buf
+ 8), 0x00420281861253LL
);
205 cpu_to_be64w((uint64_t*)(buf
+ 16), size
);
206 cpu_to_be32w((uint32_t*)(buf
+ 24),
207 flags
| NBD_FLAG_HAS_FLAGS
| NBD_FLAG_SEND_TRIM
|
208 NBD_FLAG_SEND_FLUSH
| NBD_FLAG_SEND_FUA
);
209 memset(buf
+ 28, 0, 124);
211 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
217 TRACE("Negotiation succeeded.");
222 int nbd_receive_negotiate(int csock
, const char *name
, uint32_t *flags
,
223 off_t
*size
, size_t *blocksize
)
229 TRACE("Receiving negotiation.");
231 if (read_sync(csock
, buf
, 8) != 8) {
238 if (strlen(buf
) == 0) {
239 LOG("server connection closed");
244 TRACE("Magic is %c%c%c%c%c%c%c%c",
245 qemu_isprint(buf
[0]) ? buf
[0] : '.',
246 qemu_isprint(buf
[1]) ? buf
[1] : '.',
247 qemu_isprint(buf
[2]) ? buf
[2] : '.',
248 qemu_isprint(buf
[3]) ? buf
[3] : '.',
249 qemu_isprint(buf
[4]) ? buf
[4] : '.',
250 qemu_isprint(buf
[5]) ? buf
[5] : '.',
251 qemu_isprint(buf
[6]) ? buf
[6] : '.',
252 qemu_isprint(buf
[7]) ? buf
[7] : '.');
254 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
255 LOG("Invalid magic received");
260 if (read_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
265 magic
= be64_to_cpu(magic
);
266 TRACE("Magic is 0x%" PRIx64
, magic
);
269 uint32_t reserved
= 0;
273 TRACE("Checking magic (opts_magic)");
274 if (magic
!= 0x49484156454F5054LL
) {
275 LOG("Bad magic received");
279 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
280 LOG("flags read failed");
284 *flags
= be16_to_cpu(tmp
) << 16;
285 /* reserved for future use */
286 if (write_sync(csock
, &reserved
, sizeof(reserved
)) !=
288 LOG("write failed (reserved)");
292 /* write the export name */
293 magic
= cpu_to_be64(magic
);
294 if (write_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
295 LOG("write failed (magic)");
299 opt
= cpu_to_be32(NBD_OPT_EXPORT_NAME
);
300 if (write_sync(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
301 LOG("write failed (opt)");
305 namesize
= cpu_to_be32(strlen(name
));
306 if (write_sync(csock
, &namesize
, sizeof(namesize
)) !=
308 LOG("write failed (namesize)");
312 if (write_sync(csock
, (char*)name
, strlen(name
)) != strlen(name
)) {
313 LOG("write failed (name)");
318 TRACE("Checking magic (cli_magic)");
320 if (magic
!= 0x00420281861253LL
) {
321 LOG("Bad magic received");
327 if (read_sync(csock
, &s
, sizeof(s
)) != sizeof(s
)) {
332 *size
= be64_to_cpu(s
);
334 TRACE("Size is %" PRIu64
, *size
);
337 if (read_sync(csock
, flags
, sizeof(*flags
)) != sizeof(*flags
)) {
338 LOG("read failed (flags)");
342 *flags
= be32_to_cpup(flags
);
344 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
345 LOG("read failed (tmp)");
349 *flags
|= be32_to_cpu(tmp
);
351 if (read_sync(csock
, &buf
, 124) != 124) {
352 LOG("read failed (buf)");
360 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
, size_t blocksize
)
362 TRACE("Setting NBD socket");
364 if (ioctl(fd
, NBD_SET_SOCK
, csock
) == -1) {
366 LOG("Failed to set NBD socket");
371 TRACE("Setting block size to %lu", (unsigned long)blocksize
);
373 if (ioctl(fd
, NBD_SET_BLKSIZE
, blocksize
) == -1) {
375 LOG("Failed setting NBD block size");
380 TRACE("Setting size to %zd block(s)", (size_t)(size
/ blocksize
));
382 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, size
/ blocksize
) == -1) {
384 LOG("Failed setting size (in blocks)");
389 if (flags
& NBD_FLAG_READ_ONLY
) {
391 TRACE("Setting readonly attribute");
393 if (ioctl(fd
, BLKROSET
, (unsigned long) &read_only
) < 0) {
395 LOG("Failed setting read-only attribute");
401 if (ioctl(fd
, NBD_SET_FLAGS
, flags
) < 0
402 && errno
!= ENOTTY
) {
404 LOG("Failed setting flags");
409 TRACE("Negotiation ended");
414 int nbd_disconnect(int fd
)
416 ioctl(fd
, NBD_CLEAR_QUE
);
417 ioctl(fd
, NBD_DISCONNECT
);
418 ioctl(fd
, NBD_CLEAR_SOCK
);
422 int nbd_client(int fd
)
427 TRACE("Doing NBD loop");
429 ret
= ioctl(fd
, NBD_DO_IT
);
430 if (ret
== -1 && errno
== EPIPE
) {
431 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
432 * the socket via NBD_DISCONNECT. We do not want to return 1 in
439 TRACE("NBD loop returned %d: %s", ret
, strerror(serrno
));
441 TRACE("Clearing NBD queue");
442 ioctl(fd
, NBD_CLEAR_QUE
);
444 TRACE("Clearing NBD socket");
445 ioctl(fd
, NBD_CLEAR_SOCK
);
451 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
, size_t blocksize
)
457 int nbd_disconnect(int fd
)
463 int nbd_client(int fd
)
470 int nbd_send_request(int csock
, struct nbd_request
*request
)
472 uint8_t buf
[4 + 4 + 8 + 8 + 4];
474 cpu_to_be32w((uint32_t*)buf
, NBD_REQUEST_MAGIC
);
475 cpu_to_be32w((uint32_t*)(buf
+ 4), request
->type
);
476 cpu_to_be64w((uint64_t*)(buf
+ 8), request
->handle
);
477 cpu_to_be64w((uint64_t*)(buf
+ 16), request
->from
);
478 cpu_to_be32w((uint32_t*)(buf
+ 24), request
->len
);
480 TRACE("Sending request to client: "
481 "{ .from = %" PRIu64
", .len = %u, .handle = %" PRIu64
", .type=%i}",
482 request
->from
, request
->len
, request
->handle
, request
->type
);
484 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
485 LOG("writing to socket failed");
492 static int nbd_receive_request(int csock
, struct nbd_request
*request
)
494 uint8_t buf
[4 + 4 + 8 + 8 + 4];
497 if (read_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
504 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
505 [ 4 .. 7] type (0 == READ, 1 == WRITE)
511 magic
= be32_to_cpup((uint32_t*)buf
);
512 request
->type
= be32_to_cpup((uint32_t*)(buf
+ 4));
513 request
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
514 request
->from
= be64_to_cpup((uint64_t*)(buf
+ 16));
515 request
->len
= be32_to_cpup((uint32_t*)(buf
+ 24));
517 TRACE("Got request: "
518 "{ magic = 0x%x, .type = %d, from = %" PRIu64
" , len = %u }",
519 magic
, request
->type
, request
->from
, request
->len
);
521 if (magic
!= NBD_REQUEST_MAGIC
) {
522 LOG("invalid magic (got 0x%x)", magic
);
529 int nbd_receive_reply(int csock
, struct nbd_reply
*reply
)
531 uint8_t buf
[NBD_REPLY_SIZE
];
534 memset(buf
, 0xAA, sizeof(buf
));
536 if (read_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
543 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
544 [ 4 .. 7] error (0 == no error)
548 magic
= be32_to_cpup((uint32_t*)buf
);
549 reply
->error
= be32_to_cpup((uint32_t*)(buf
+ 4));
550 reply
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
553 "{ magic = 0x%x, .error = %d, handle = %" PRIu64
" }",
554 magic
, reply
->error
, reply
->handle
);
556 if (magic
!= NBD_REPLY_MAGIC
) {
557 LOG("invalid magic (got 0x%x)", magic
);
564 static int nbd_send_reply(int csock
, struct nbd_reply
*reply
)
566 uint8_t buf
[4 + 4 + 8];
569 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
570 [ 4 .. 7] error (0 == no error)
573 cpu_to_be32w((uint32_t*)buf
, NBD_REPLY_MAGIC
);
574 cpu_to_be32w((uint32_t*)(buf
+ 4), reply
->error
);
575 cpu_to_be64w((uint64_t*)(buf
+ 8), reply
->handle
);
577 TRACE("Sending response to client");
579 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
580 LOG("writing to socket failed");
588 BlockDriverState
*bs
;
595 NBDExport
*nbd_export_new(BlockDriverState
*bs
, off_t dev_offset
,
596 off_t size
, uint32_t nbdflags
)
598 NBDExport
*exp
= g_malloc0(sizeof(NBDExport
));
600 exp
->dev_offset
= dev_offset
;
601 exp
->nbdflags
= nbdflags
;
602 exp
->size
= size
== -1 ? exp
->bs
->total_sectors
* 512 : size
;
603 exp
->data
= qemu_blockalign(exp
->bs
, NBD_BUFFER_SIZE
);
607 void nbd_export_close(NBDExport
*exp
)
609 qemu_vfree(exp
->data
);
614 static int nbd_do_send_reply(int csock
, struct nbd_reply
*reply
,
615 uint8_t *data
, int len
)
620 rc
= nbd_send_reply(csock
, reply
);
625 socket_set_cork(csock
, 1);
626 rc
= nbd_send_reply(csock
, reply
);
628 ret
= write_sync(csock
, data
, len
);
637 socket_set_cork(csock
, 0);
642 static int nbd_do_receive_request(int csock
, struct nbd_request
*request
,
647 if (nbd_receive_request(csock
, request
) == -1) {
652 if (request
->len
> NBD_BUFFER_SIZE
) {
653 LOG("len (%u) is larger than max len (%u)",
654 request
->len
, NBD_BUFFER_SIZE
);
659 if ((request
->from
+ request
->len
) < request
->from
) {
660 LOG("integer overflow detected! "
661 "you're probably being attacked");
666 TRACE("Decoding type");
668 if ((request
->type
& NBD_CMD_MASK_COMMAND
) == NBD_CMD_WRITE
) {
669 TRACE("Reading %u byte(s)", request
->len
);
671 if (read_sync(csock
, data
, request
->len
) != request
->len
) {
672 LOG("reading from socket failed");
683 int nbd_trip(NBDExport
*exp
, int csock
)
685 struct nbd_request request
;
686 struct nbd_reply reply
;
689 TRACE("Reading request.");
691 ret
= nbd_do_receive_request(csock
, &request
, exp
->data
);
696 reply
.handle
= request
.handle
;
704 if ((request
.from
+ request
.len
) > exp
->size
) {
705 LOG("From: %" PRIu64
", Len: %u, Size: %" PRIu64
706 ", Offset: %" PRIu64
"\n",
707 request
.from
, request
.len
,
708 (uint64_t)exp
->size
, exp
->dev_offset
);
709 LOG("requested operation past EOF--bad client?");
710 goto invalid_request
;
713 switch (request
.type
& NBD_CMD_MASK_COMMAND
) {
715 TRACE("Request type is READ");
717 ret
= bdrv_read(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
718 exp
->data
, request
.len
/ 512);
720 LOG("reading from file failed");
725 TRACE("Read %u byte(s)", request
.len
);
726 if (nbd_do_send_reply(csock
, &reply
, exp
->data
, request
.len
) < 0)
730 TRACE("Request type is WRITE");
732 if (exp
->nbdflags
& NBD_FLAG_READ_ONLY
) {
733 TRACE("Server is read-only, return error");
738 TRACE("Writing to device");
740 ret
= bdrv_write(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
741 exp
->data
, request
.len
/ 512);
743 LOG("writing to file failed");
748 if (request
.type
& NBD_CMD_FLAG_FUA
) {
749 ret
= bdrv_flush(exp
->bs
);
757 if (nbd_do_send_reply(csock
, &reply
, NULL
, 0) < 0)
761 TRACE("Request type is DISCONNECT");
765 TRACE("Request type is FLUSH");
767 ret
= bdrv_flush(exp
->bs
);
773 if (nbd_do_send_reply(csock
, &reply
, NULL
, 0) < 0)
777 TRACE("Request type is TRIM");
778 ret
= bdrv_discard(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
781 LOG("discard failed");
784 if (nbd_do_send_reply(csock
, &reply
, NULL
, 0) < 0)
788 LOG("invalid request type (%u) received", request
.type
);
790 reply
.error
= -EINVAL
;
792 if (nbd_do_send_reply(csock
, &reply
, NULL
, 0) == -1)
797 TRACE("Request/Reply complete");
802 int nbd_negotiate(NBDExport
*exp
, int csock
)
804 return nbd_send_negotiate(csock
, exp
->size
, exp
->nbdflags
);