]>
git.proxmox.com Git - qemu.git/blob - nbd.c
ca18c10a1918a494cc292d4e9cb393e87d67e9d1
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"
39 #include "qemu-queue.h"
44 #define TRACE(msg, ...) do { \
45 LOG(msg, ## __VA_ARGS__); \
48 #define TRACE(msg, ...) \
52 #define LOG(msg, ...) do { \
53 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
54 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
57 /* This is all part of the "official" NBD API */
59 #define NBD_REPLY_SIZE (4 + 4 + 8)
60 #define NBD_REQUEST_MAGIC 0x25609513
61 #define NBD_REPLY_MAGIC 0x67446698
63 #define NBD_SET_SOCK _IO(0xab, 0)
64 #define NBD_SET_BLKSIZE _IO(0xab, 1)
65 #define NBD_SET_SIZE _IO(0xab, 2)
66 #define NBD_DO_IT _IO(0xab, 3)
67 #define NBD_CLEAR_SOCK _IO(0xab, 4)
68 #define NBD_CLEAR_QUE _IO(0xab, 5)
69 #define NBD_PRINT_DEBUG _IO(0xab, 6)
70 #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
71 #define NBD_DISCONNECT _IO(0xab, 8)
72 #define NBD_SET_TIMEOUT _IO(0xab, 9)
73 #define NBD_SET_FLAGS _IO(0xab, 10)
75 #define NBD_OPT_EXPORT_NAME (1 << 0)
77 /* That's all folks */
79 #define read_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, true)
80 #define write_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, false)
82 size_t nbd_wr_sync(int fd
, void *buffer
, size_t size
, bool do_read
)
86 if (qemu_in_coroutine()) {
88 return qemu_co_recv(fd
, buffer
, size
);
90 return qemu_co_send(fd
, buffer
, size
);
94 while (offset
< size
) {
98 len
= qemu_recv(fd
, buffer
+ offset
, size
- offset
, 0);
100 len
= send(fd
, buffer
+ offset
, size
- offset
, 0);
104 errno
= socket_error();
106 /* recoverable error */
107 if (len
== -1 && (errno
== EAGAIN
|| errno
== EINTR
)) {
116 /* unrecoverable error */
127 static void combine_addr(char *buf
, size_t len
, const char* address
,
130 /* If the address-part contains a colon, it's an IPv6 IP so needs [] */
131 if (strstr(address
, ":")) {
132 snprintf(buf
, len
, "[%s]:%u", address
, port
);
134 snprintf(buf
, len
, "%s:%u", address
, port
);
138 int tcp_socket_outgoing(const char *address
, uint16_t port
)
140 char address_and_port
[128];
141 combine_addr(address_and_port
, 128, address
, port
);
142 return tcp_socket_outgoing_spec(address_and_port
);
145 int tcp_socket_outgoing_spec(const char *address_and_port
)
147 return inet_connect(address_and_port
, SOCK_STREAM
);
150 int tcp_socket_incoming(const char *address
, uint16_t port
)
152 char address_and_port
[128];
153 combine_addr(address_and_port
, 128, address
, port
);
154 return tcp_socket_incoming_spec(address_and_port
);
157 int tcp_socket_incoming_spec(const char *address_and_port
)
161 return inet_listen(address_and_port
, ostr
, olen
, SOCK_STREAM
, 0);
164 int unix_socket_incoming(const char *path
)
169 return unix_listen(path
, ostr
, olen
);
172 int unix_socket_outgoing(const char *path
)
174 return unix_connect(path
);
191 static int nbd_send_negotiate(int csock
, off_t size
, uint32_t flags
)
193 char buf
[8 + 8 + 8 + 128];
196 [ 0 .. 7] passwd ("NBDMAGIC")
197 [ 8 .. 15] magic (0x00420281861253)
200 [28 .. 151] reserved (0)
203 TRACE("Beginning negotiation.");
204 memcpy(buf
, "NBDMAGIC", 8);
205 cpu_to_be64w((uint64_t*)(buf
+ 8), 0x00420281861253LL
);
206 cpu_to_be64w((uint64_t*)(buf
+ 16), size
);
207 cpu_to_be32w((uint32_t*)(buf
+ 24),
208 flags
| NBD_FLAG_HAS_FLAGS
| NBD_FLAG_SEND_TRIM
|
209 NBD_FLAG_SEND_FLUSH
| NBD_FLAG_SEND_FUA
);
210 memset(buf
+ 28, 0, 124);
212 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
218 TRACE("Negotiation succeeded.");
223 int nbd_receive_negotiate(int csock
, const char *name
, uint32_t *flags
,
224 off_t
*size
, size_t *blocksize
)
230 TRACE("Receiving negotiation.");
232 if (read_sync(csock
, buf
, 8) != 8) {
239 if (strlen(buf
) == 0) {
240 LOG("server connection closed");
245 TRACE("Magic is %c%c%c%c%c%c%c%c",
246 qemu_isprint(buf
[0]) ? buf
[0] : '.',
247 qemu_isprint(buf
[1]) ? buf
[1] : '.',
248 qemu_isprint(buf
[2]) ? buf
[2] : '.',
249 qemu_isprint(buf
[3]) ? buf
[3] : '.',
250 qemu_isprint(buf
[4]) ? buf
[4] : '.',
251 qemu_isprint(buf
[5]) ? buf
[5] : '.',
252 qemu_isprint(buf
[6]) ? buf
[6] : '.',
253 qemu_isprint(buf
[7]) ? buf
[7] : '.');
255 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
256 LOG("Invalid magic received");
261 if (read_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
266 magic
= be64_to_cpu(magic
);
267 TRACE("Magic is 0x%" PRIx64
, magic
);
270 uint32_t reserved
= 0;
274 TRACE("Checking magic (opts_magic)");
275 if (magic
!= 0x49484156454F5054LL
) {
276 LOG("Bad magic received");
280 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
281 LOG("flags read failed");
285 *flags
= be16_to_cpu(tmp
) << 16;
286 /* reserved for future use */
287 if (write_sync(csock
, &reserved
, sizeof(reserved
)) !=
289 LOG("write failed (reserved)");
293 /* write the export name */
294 magic
= cpu_to_be64(magic
);
295 if (write_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
296 LOG("write failed (magic)");
300 opt
= cpu_to_be32(NBD_OPT_EXPORT_NAME
);
301 if (write_sync(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
302 LOG("write failed (opt)");
306 namesize
= cpu_to_be32(strlen(name
));
307 if (write_sync(csock
, &namesize
, sizeof(namesize
)) !=
309 LOG("write failed (namesize)");
313 if (write_sync(csock
, (char*)name
, strlen(name
)) != strlen(name
)) {
314 LOG("write failed (name)");
319 TRACE("Checking magic (cli_magic)");
321 if (magic
!= 0x00420281861253LL
) {
322 LOG("Bad magic received");
328 if (read_sync(csock
, &s
, sizeof(s
)) != sizeof(s
)) {
333 *size
= be64_to_cpu(s
);
335 TRACE("Size is %" PRIu64
, *size
);
338 if (read_sync(csock
, flags
, sizeof(*flags
)) != sizeof(*flags
)) {
339 LOG("read failed (flags)");
343 *flags
= be32_to_cpup(flags
);
345 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
346 LOG("read failed (tmp)");
350 *flags
|= be32_to_cpu(tmp
);
352 if (read_sync(csock
, &buf
, 124) != 124) {
353 LOG("read failed (buf)");
361 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
, size_t blocksize
)
363 TRACE("Setting NBD socket");
365 if (ioctl(fd
, NBD_SET_SOCK
, csock
) == -1) {
367 LOG("Failed to set NBD socket");
372 TRACE("Setting block size to %lu", (unsigned long)blocksize
);
374 if (ioctl(fd
, NBD_SET_BLKSIZE
, blocksize
) == -1) {
376 LOG("Failed setting NBD block size");
381 TRACE("Setting size to %zd block(s)", (size_t)(size
/ blocksize
));
383 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, size
/ blocksize
) == -1) {
385 LOG("Failed setting size (in blocks)");
390 if (flags
& NBD_FLAG_READ_ONLY
) {
392 TRACE("Setting readonly attribute");
394 if (ioctl(fd
, BLKROSET
, (unsigned long) &read_only
) < 0) {
396 LOG("Failed setting read-only attribute");
402 if (ioctl(fd
, NBD_SET_FLAGS
, flags
) < 0
403 && errno
!= ENOTTY
) {
405 LOG("Failed setting flags");
410 TRACE("Negotiation ended");
415 int nbd_disconnect(int fd
)
417 ioctl(fd
, NBD_CLEAR_QUE
);
418 ioctl(fd
, NBD_DISCONNECT
);
419 ioctl(fd
, NBD_CLEAR_SOCK
);
423 int nbd_client(int fd
)
428 TRACE("Doing NBD loop");
430 ret
= ioctl(fd
, NBD_DO_IT
);
431 if (ret
== -1 && errno
== EPIPE
) {
432 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
433 * the socket via NBD_DISCONNECT. We do not want to return 1 in
440 TRACE("NBD loop returned %d: %s", ret
, strerror(serrno
));
442 TRACE("Clearing NBD queue");
443 ioctl(fd
, NBD_CLEAR_QUE
);
445 TRACE("Clearing NBD socket");
446 ioctl(fd
, NBD_CLEAR_SOCK
);
452 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
, size_t blocksize
)
458 int nbd_disconnect(int fd
)
464 int nbd_client(int fd
)
471 int nbd_send_request(int csock
, struct nbd_request
*request
)
473 uint8_t buf
[4 + 4 + 8 + 8 + 4];
475 cpu_to_be32w((uint32_t*)buf
, NBD_REQUEST_MAGIC
);
476 cpu_to_be32w((uint32_t*)(buf
+ 4), request
->type
);
477 cpu_to_be64w((uint64_t*)(buf
+ 8), request
->handle
);
478 cpu_to_be64w((uint64_t*)(buf
+ 16), request
->from
);
479 cpu_to_be32w((uint32_t*)(buf
+ 24), request
->len
);
481 TRACE("Sending request to client: "
482 "{ .from = %" PRIu64
", .len = %u, .handle = %" PRIu64
", .type=%i}",
483 request
->from
, request
->len
, request
->handle
, request
->type
);
485 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
486 LOG("writing to socket failed");
493 static int nbd_receive_request(int csock
, struct nbd_request
*request
)
495 uint8_t buf
[4 + 4 + 8 + 8 + 4];
498 if (read_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
505 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
506 [ 4 .. 7] type (0 == READ, 1 == WRITE)
512 magic
= be32_to_cpup((uint32_t*)buf
);
513 request
->type
= be32_to_cpup((uint32_t*)(buf
+ 4));
514 request
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
515 request
->from
= be64_to_cpup((uint64_t*)(buf
+ 16));
516 request
->len
= be32_to_cpup((uint32_t*)(buf
+ 24));
518 TRACE("Got request: "
519 "{ magic = 0x%x, .type = %d, from = %" PRIu64
" , len = %u }",
520 magic
, request
->type
, request
->from
, request
->len
);
522 if (magic
!= NBD_REQUEST_MAGIC
) {
523 LOG("invalid magic (got 0x%x)", magic
);
530 int nbd_receive_reply(int csock
, struct nbd_reply
*reply
)
532 uint8_t buf
[NBD_REPLY_SIZE
];
535 memset(buf
, 0xAA, sizeof(buf
));
537 if (read_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
544 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
545 [ 4 .. 7] error (0 == no error)
549 magic
= be32_to_cpup((uint32_t*)buf
);
550 reply
->error
= be32_to_cpup((uint32_t*)(buf
+ 4));
551 reply
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
554 "{ magic = 0x%x, .error = %d, handle = %" PRIu64
" }",
555 magic
, reply
->error
, reply
->handle
);
557 if (magic
!= NBD_REPLY_MAGIC
) {
558 LOG("invalid magic (got 0x%x)", magic
);
565 static int nbd_send_reply(int csock
, struct nbd_reply
*reply
)
567 uint8_t buf
[4 + 4 + 8];
570 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
571 [ 4 .. 7] error (0 == no error)
574 cpu_to_be32w((uint32_t*)buf
, NBD_REPLY_MAGIC
);
575 cpu_to_be32w((uint32_t*)(buf
+ 4), reply
->error
);
576 cpu_to_be64w((uint64_t*)(buf
+ 8), reply
->handle
);
578 TRACE("Sending response to client");
580 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
581 LOG("writing to socket failed");
588 typedef struct NBDRequest NBDRequest
;
591 QSIMPLEQ_ENTRY(NBDRequest
) entry
;
597 BlockDriverState
*bs
;
601 QSIMPLEQ_HEAD(, NBDRequest
) requests
;
606 void (*close
)(NBDClient
*client
);
612 static void nbd_client_get(NBDClient
*client
)
617 static void nbd_client_put(NBDClient
*client
)
619 if (--client
->refcount
== 0) {
624 static void nbd_client_close(NBDClient
*client
)
626 qemu_set_fd_handler2(client
->sock
, NULL
, NULL
, NULL
, NULL
);
630 client
->close(client
);
632 nbd_client_put(client
);
635 static NBDRequest
*nbd_request_get(NBDClient
*client
)
638 NBDExport
*exp
= client
->exp
;
640 if (QSIMPLEQ_EMPTY(&exp
->requests
)) {
641 req
= g_malloc0(sizeof(NBDRequest
));
642 req
->data
= qemu_blockalign(exp
->bs
, NBD_BUFFER_SIZE
);
644 req
= QSIMPLEQ_FIRST(&exp
->requests
);
645 QSIMPLEQ_REMOVE_HEAD(&exp
->requests
, entry
);
647 nbd_client_get(client
);
648 req
->client
= client
;
652 static void nbd_request_put(NBDRequest
*req
)
654 NBDClient
*client
= req
->client
;
655 QSIMPLEQ_INSERT_HEAD(&client
->exp
->requests
, req
, entry
);
656 nbd_client_put(client
);
659 NBDExport
*nbd_export_new(BlockDriverState
*bs
, off_t dev_offset
,
660 off_t size
, uint32_t nbdflags
)
662 NBDExport
*exp
= g_malloc0(sizeof(NBDExport
));
663 QSIMPLEQ_INIT(&exp
->requests
);
665 exp
->dev_offset
= dev_offset
;
666 exp
->nbdflags
= nbdflags
;
667 exp
->size
= size
== -1 ? exp
->bs
->total_sectors
* 512 : size
;
671 void nbd_export_close(NBDExport
*exp
)
673 while (!QSIMPLEQ_EMPTY(&exp
->requests
)) {
674 NBDRequest
*first
= QSIMPLEQ_FIRST(&exp
->requests
);
675 QSIMPLEQ_REMOVE_HEAD(&exp
->requests
, entry
);
676 qemu_vfree(first
->data
);
684 static int nbd_do_send_reply(NBDRequest
*req
, struct nbd_reply
*reply
,
687 NBDClient
*client
= req
->client
;
688 int csock
= client
->sock
;
692 rc
= nbd_send_reply(csock
, reply
);
697 socket_set_cork(csock
, 1);
698 rc
= nbd_send_reply(csock
, reply
);
700 ret
= write_sync(csock
, req
->data
, len
);
709 socket_set_cork(csock
, 0);
714 static int nbd_do_receive_request(NBDRequest
*req
, struct nbd_request
*request
)
716 NBDClient
*client
= req
->client
;
717 int csock
= client
->sock
;
720 if (nbd_receive_request(csock
, request
) == -1) {
725 if (request
->len
> NBD_BUFFER_SIZE
) {
726 LOG("len (%u) is larger than max len (%u)",
727 request
->len
, NBD_BUFFER_SIZE
);
732 if ((request
->from
+ request
->len
) < request
->from
) {
733 LOG("integer overflow detected! "
734 "you're probably being attacked");
739 TRACE("Decoding type");
741 if ((request
->type
& NBD_CMD_MASK_COMMAND
) == NBD_CMD_WRITE
) {
742 TRACE("Reading %u byte(s)", request
->len
);
744 if (read_sync(csock
, req
->data
, request
->len
) != request
->len
) {
745 LOG("reading from socket failed");
756 static int nbd_trip(NBDClient
*client
)
758 NBDRequest
*req
= nbd_request_get(client
);
759 NBDExport
*exp
= client
->exp
;
760 struct nbd_request request
;
761 struct nbd_reply reply
;
765 TRACE("Reading request.");
767 ret
= nbd_do_receive_request(req
, &request
);
772 reply
.handle
= request
.handle
;
780 if ((request
.from
+ request
.len
) > exp
->size
) {
781 LOG("From: %" PRIu64
", Len: %u, Size: %" PRIu64
782 ", Offset: %" PRIu64
"\n",
783 request
.from
, request
.len
,
784 (uint64_t)exp
->size
, exp
->dev_offset
);
785 LOG("requested operation past EOF--bad client?");
786 goto invalid_request
;
789 switch (request
.type
& NBD_CMD_MASK_COMMAND
) {
791 TRACE("Request type is READ");
793 ret
= bdrv_read(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
794 req
->data
, request
.len
/ 512);
796 LOG("reading from file failed");
801 TRACE("Read %u byte(s)", request
.len
);
802 if (nbd_do_send_reply(req
, &reply
, request
.len
) < 0)
806 TRACE("Request type is WRITE");
808 if (exp
->nbdflags
& NBD_FLAG_READ_ONLY
) {
809 TRACE("Server is read-only, return error");
814 TRACE("Writing to device");
816 ret
= bdrv_write(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
817 req
->data
, request
.len
/ 512);
819 LOG("writing to file failed");
824 if (request
.type
& NBD_CMD_FLAG_FUA
) {
825 ret
= bdrv_flush(exp
->bs
);
833 if (nbd_do_send_reply(req
, &reply
, 0) < 0)
837 TRACE("Request type is DISCONNECT");
841 TRACE("Request type is FLUSH");
843 ret
= bdrv_flush(exp
->bs
);
849 if (nbd_do_send_reply(req
, &reply
, 0) < 0)
853 TRACE("Request type is TRIM");
854 ret
= bdrv_discard(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
857 LOG("discard failed");
860 if (nbd_do_send_reply(req
, &reply
, 0) < 0)
864 LOG("invalid request type (%u) received", request
.type
);
866 reply
.error
= -EINVAL
;
868 if (nbd_do_send_reply(req
, &reply
, 0) == -1)
873 TRACE("Request/Reply complete");
877 nbd_request_put(req
);
881 static void nbd_read(void *opaque
)
883 NBDClient
*client
= opaque
;
885 if (nbd_trip(client
) != 0) {
886 nbd_client_close(client
);
890 NBDClient
*nbd_client_new(NBDExport
*exp
, int csock
,
891 void (*close
)(NBDClient
*))
894 if (nbd_send_negotiate(csock
, exp
->size
, exp
->nbdflags
) == -1) {
897 client
= g_malloc0(sizeof(NBDClient
));
898 client
->refcount
= 1;
900 client
->sock
= csock
;
901 client
->close
= close
;
902 qemu_set_fd_handler2(csock
, NULL
, nbd_read
, NULL
, client
);