]>
git.proxmox.com Git - qemu.git/blob - nbd.c
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"
23 #include "qemu-coroutine.h"
28 #include <sys/ioctl.h>
30 #if defined(__sun__) || defined(__HAIKU__)
31 #include <sys/ioccom.h>
40 #include "qemu_socket.h"
41 #include "qemu-queue.h"
46 #define TRACE(msg, ...) do { \
47 LOG(msg, ## __VA_ARGS__); \
50 #define TRACE(msg, ...) \
54 #define LOG(msg, ...) do { \
55 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
56 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
59 /* This is all part of the "official" NBD API */
61 #define NBD_REPLY_SIZE (4 + 4 + 8)
62 #define NBD_REQUEST_MAGIC 0x25609513
63 #define NBD_REPLY_MAGIC 0x67446698
65 #define NBD_SET_SOCK _IO(0xab, 0)
66 #define NBD_SET_BLKSIZE _IO(0xab, 1)
67 #define NBD_SET_SIZE _IO(0xab, 2)
68 #define NBD_DO_IT _IO(0xab, 3)
69 #define NBD_CLEAR_SOCK _IO(0xab, 4)
70 #define NBD_CLEAR_QUE _IO(0xab, 5)
71 #define NBD_PRINT_DEBUG _IO(0xab, 6)
72 #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
73 #define NBD_DISCONNECT _IO(0xab, 8)
74 #define NBD_SET_TIMEOUT _IO(0xab, 9)
75 #define NBD_SET_FLAGS _IO(0xab, 10)
77 #define NBD_OPT_EXPORT_NAME (1 << 0)
79 /* That's all folks */
81 #define read_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, true)
82 #define write_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, false)
84 ssize_t
nbd_wr_sync(int fd
, void *buffer
, size_t size
, bool do_read
)
89 if (qemu_in_coroutine()) {
91 return qemu_co_recv(fd
, buffer
, size
);
93 return qemu_co_send(fd
, buffer
, size
);
97 while (offset
< size
) {
101 len
= qemu_recv(fd
, buffer
+ offset
, size
- offset
, 0);
103 len
= send(fd
, buffer
+ offset
, size
- offset
, 0);
107 err
= socket_error();
109 /* recoverable error */
110 if (err
== EINTR
|| err
== EAGAIN
) {
114 /* unrecoverable error */
129 static void combine_addr(char *buf
, size_t len
, const char* address
,
132 /* If the address-part contains a colon, it's an IPv6 IP so needs [] */
133 if (strstr(address
, ":")) {
134 snprintf(buf
, len
, "[%s]:%u", address
, port
);
136 snprintf(buf
, len
, "%s:%u", address
, port
);
140 int tcp_socket_outgoing(const char *address
, uint16_t port
)
142 char address_and_port
[128];
143 combine_addr(address_and_port
, 128, address
, port
);
144 return tcp_socket_outgoing_spec(address_and_port
);
147 int tcp_socket_outgoing_spec(const char *address_and_port
)
149 return inet_connect(address_and_port
, SOCK_STREAM
);
152 int tcp_socket_incoming(const char *address
, uint16_t port
)
154 char address_and_port
[128];
155 combine_addr(address_and_port
, 128, address
, port
);
156 return tcp_socket_incoming_spec(address_and_port
);
159 int tcp_socket_incoming_spec(const char *address_and_port
)
163 return inet_listen(address_and_port
, ostr
, olen
, SOCK_STREAM
, 0);
166 int unix_socket_incoming(const char *path
)
171 return unix_listen(path
, ostr
, olen
);
174 int unix_socket_outgoing(const char *path
)
176 return unix_connect(path
);
193 static int nbd_send_negotiate(int csock
, off_t size
, uint32_t flags
)
195 char buf
[8 + 8 + 8 + 128];
199 [ 0 .. 7] passwd ("NBDMAGIC")
200 [ 8 .. 15] magic (0x00420281861253)
203 [28 .. 151] reserved (0)
208 TRACE("Beginning negotiation.");
209 memcpy(buf
, "NBDMAGIC", 8);
210 cpu_to_be64w((uint64_t*)(buf
+ 8), 0x00420281861253LL
);
211 cpu_to_be64w((uint64_t*)(buf
+ 16), size
);
212 cpu_to_be32w((uint32_t*)(buf
+ 24),
213 flags
| NBD_FLAG_HAS_FLAGS
| NBD_FLAG_SEND_TRIM
|
214 NBD_FLAG_SEND_FLUSH
| NBD_FLAG_SEND_FUA
);
215 memset(buf
+ 28, 0, 124);
217 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
222 TRACE("Negotiation succeeded.");
228 int nbd_receive_negotiate(int csock
, const char *name
, uint32_t *flags
,
229 off_t
*size
, size_t *blocksize
)
236 TRACE("Receiving negotiation.");
240 if (read_sync(csock
, buf
, 8) != 8) {
246 if (strlen(buf
) == 0) {
247 LOG("server connection closed");
251 TRACE("Magic is %c%c%c%c%c%c%c%c",
252 qemu_isprint(buf
[0]) ? buf
[0] : '.',
253 qemu_isprint(buf
[1]) ? buf
[1] : '.',
254 qemu_isprint(buf
[2]) ? buf
[2] : '.',
255 qemu_isprint(buf
[3]) ? buf
[3] : '.',
256 qemu_isprint(buf
[4]) ? buf
[4] : '.',
257 qemu_isprint(buf
[5]) ? buf
[5] : '.',
258 qemu_isprint(buf
[6]) ? buf
[6] : '.',
259 qemu_isprint(buf
[7]) ? buf
[7] : '.');
261 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
262 LOG("Invalid magic received");
266 if (read_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
270 magic
= be64_to_cpu(magic
);
271 TRACE("Magic is 0x%" PRIx64
, magic
);
274 uint32_t reserved
= 0;
278 TRACE("Checking magic (opts_magic)");
279 if (magic
!= 0x49484156454F5054LL
) {
280 LOG("Bad magic received");
283 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
284 LOG("flags read failed");
287 *flags
= be16_to_cpu(tmp
) << 16;
288 /* reserved for future use */
289 if (write_sync(csock
, &reserved
, sizeof(reserved
)) !=
291 LOG("write failed (reserved)");
294 /* write the export name */
295 magic
= cpu_to_be64(magic
);
296 if (write_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
297 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)");
305 namesize
= cpu_to_be32(strlen(name
));
306 if (write_sync(csock
, &namesize
, sizeof(namesize
)) !=
308 LOG("write failed (namesize)");
311 if (write_sync(csock
, (char*)name
, strlen(name
)) != strlen(name
)) {
312 LOG("write failed (name)");
316 TRACE("Checking magic (cli_magic)");
318 if (magic
!= 0x00420281861253LL
) {
319 LOG("Bad magic received");
324 if (read_sync(csock
, &s
, sizeof(s
)) != sizeof(s
)) {
328 *size
= be64_to_cpu(s
);
330 TRACE("Size is %" PRIu64
, *size
);
333 if (read_sync(csock
, flags
, sizeof(*flags
)) != sizeof(*flags
)) {
334 LOG("read failed (flags)");
337 *flags
= be32_to_cpup(flags
);
339 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
340 LOG("read failed (tmp)");
343 *flags
|= be32_to_cpu(tmp
);
345 if (read_sync(csock
, &buf
, 124) != 124) {
346 LOG("read failed (buf)");
356 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
, size_t blocksize
)
358 TRACE("Setting NBD socket");
360 if (ioctl(fd
, NBD_SET_SOCK
, csock
) < 0) {
362 LOG("Failed to set NBD socket");
366 TRACE("Setting block size to %lu", (unsigned long)blocksize
);
368 if (ioctl(fd
, NBD_SET_BLKSIZE
, blocksize
) < 0) {
370 LOG("Failed setting NBD block size");
374 TRACE("Setting size to %zd block(s)", (size_t)(size
/ blocksize
));
376 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, size
/ blocksize
) < 0) {
378 LOG("Failed setting size (in blocks)");
382 if (flags
& NBD_FLAG_READ_ONLY
) {
384 TRACE("Setting readonly attribute");
386 if (ioctl(fd
, BLKROSET
, (unsigned long) &read_only
) < 0) {
388 LOG("Failed setting read-only attribute");
393 if (ioctl(fd
, NBD_SET_FLAGS
, flags
) < 0
394 && errno
!= ENOTTY
) {
396 LOG("Failed setting flags");
400 TRACE("Negotiation ended");
405 int nbd_disconnect(int fd
)
407 ioctl(fd
, NBD_CLEAR_QUE
);
408 ioctl(fd
, NBD_DISCONNECT
);
409 ioctl(fd
, NBD_CLEAR_SOCK
);
413 int nbd_client(int fd
)
418 TRACE("Doing NBD loop");
420 ret
= ioctl(fd
, NBD_DO_IT
);
421 if (ret
< 0 && errno
== EPIPE
) {
422 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
423 * the socket via NBD_DISCONNECT. We do not want to return 1 in
430 TRACE("NBD loop returned %d: %s", ret
, strerror(serrno
));
432 TRACE("Clearing NBD queue");
433 ioctl(fd
, NBD_CLEAR_QUE
);
435 TRACE("Clearing NBD socket");
436 ioctl(fd
, NBD_CLEAR_SOCK
);
442 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
, size_t blocksize
)
447 int nbd_disconnect(int fd
)
452 int nbd_client(int fd
)
458 ssize_t
nbd_send_request(int csock
, struct nbd_request
*request
)
460 uint8_t buf
[4 + 4 + 8 + 8 + 4];
463 cpu_to_be32w((uint32_t*)buf
, NBD_REQUEST_MAGIC
);
464 cpu_to_be32w((uint32_t*)(buf
+ 4), request
->type
);
465 cpu_to_be64w((uint64_t*)(buf
+ 8), request
->handle
);
466 cpu_to_be64w((uint64_t*)(buf
+ 16), request
->from
);
467 cpu_to_be32w((uint32_t*)(buf
+ 24), request
->len
);
469 TRACE("Sending request to client: "
470 "{ .from = %" PRIu64
", .len = %u, .handle = %" PRIu64
", .type=%i}",
471 request
->from
, request
->len
, request
->handle
, request
->type
);
473 ret
= write_sync(csock
, buf
, sizeof(buf
));
478 if (ret
!= sizeof(buf
)) {
479 LOG("writing to socket failed");
485 static ssize_t
nbd_receive_request(int csock
, struct nbd_request
*request
)
487 uint8_t buf
[4 + 4 + 8 + 8 + 4];
491 ret
= read_sync(csock
, buf
, sizeof(buf
));
496 if (ret
!= sizeof(buf
)) {
502 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
503 [ 4 .. 7] type (0 == READ, 1 == WRITE)
509 magic
= be32_to_cpup((uint32_t*)buf
);
510 request
->type
= be32_to_cpup((uint32_t*)(buf
+ 4));
511 request
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
512 request
->from
= be64_to_cpup((uint64_t*)(buf
+ 16));
513 request
->len
= be32_to_cpup((uint32_t*)(buf
+ 24));
515 TRACE("Got request: "
516 "{ magic = 0x%x, .type = %d, from = %" PRIu64
" , len = %u }",
517 magic
, request
->type
, request
->from
, request
->len
);
519 if (magic
!= NBD_REQUEST_MAGIC
) {
520 LOG("invalid magic (got 0x%x)", magic
);
526 ssize_t
nbd_receive_reply(int csock
, struct nbd_reply
*reply
)
528 uint8_t buf
[NBD_REPLY_SIZE
];
532 ret
= read_sync(csock
, buf
, sizeof(buf
));
537 if (ret
!= 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
);
563 static ssize_t
nbd_send_reply(int csock
, struct nbd_reply
*reply
)
565 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 ret
= write_sync(csock
, buf
, sizeof(buf
));
584 if (ret
!= sizeof(buf
)) {
585 LOG("writing to socket failed");
591 #define MAX_NBD_REQUESTS 16
593 typedef struct NBDRequest NBDRequest
;
596 QSIMPLEQ_ENTRY(NBDRequest
) entry
;
602 BlockDriverState
*bs
;
606 QSIMPLEQ_HEAD(, NBDRequest
) requests
;
611 void (*close
)(NBDClient
*client
);
616 Coroutine
*recv_coroutine
;
619 Coroutine
*send_coroutine
;
624 static void nbd_client_get(NBDClient
*client
)
629 static void nbd_client_put(NBDClient
*client
)
631 if (--client
->refcount
== 0) {
636 static void nbd_client_close(NBDClient
*client
)
638 qemu_set_fd_handler2(client
->sock
, NULL
, NULL
, NULL
, NULL
);
642 client
->close(client
);
644 nbd_client_put(client
);
647 static NBDRequest
*nbd_request_get(NBDClient
*client
)
650 NBDExport
*exp
= client
->exp
;
652 assert(client
->nb_requests
<= MAX_NBD_REQUESTS
- 1);
653 client
->nb_requests
++;
655 if (QSIMPLEQ_EMPTY(&exp
->requests
)) {
656 req
= g_malloc0(sizeof(NBDRequest
));
657 req
->data
= qemu_blockalign(exp
->bs
, NBD_BUFFER_SIZE
);
659 req
= QSIMPLEQ_FIRST(&exp
->requests
);
660 QSIMPLEQ_REMOVE_HEAD(&exp
->requests
, entry
);
662 nbd_client_get(client
);
663 req
->client
= client
;
667 static void nbd_request_put(NBDRequest
*req
)
669 NBDClient
*client
= req
->client
;
670 QSIMPLEQ_INSERT_HEAD(&client
->exp
->requests
, req
, entry
);
671 if (client
->nb_requests
-- == MAX_NBD_REQUESTS
) {
674 nbd_client_put(client
);
677 NBDExport
*nbd_export_new(BlockDriverState
*bs
, off_t dev_offset
,
678 off_t size
, uint32_t nbdflags
)
680 NBDExport
*exp
= g_malloc0(sizeof(NBDExport
));
681 QSIMPLEQ_INIT(&exp
->requests
);
683 exp
->dev_offset
= dev_offset
;
684 exp
->nbdflags
= nbdflags
;
685 exp
->size
= size
== -1 ? exp
->bs
->total_sectors
* 512 : size
;
689 void nbd_export_close(NBDExport
*exp
)
691 while (!QSIMPLEQ_EMPTY(&exp
->requests
)) {
692 NBDRequest
*first
= QSIMPLEQ_FIRST(&exp
->requests
);
693 QSIMPLEQ_REMOVE_HEAD(&exp
->requests
, entry
);
694 qemu_vfree(first
->data
);
702 static int nbd_can_read(void *opaque
);
703 static void nbd_read(void *opaque
);
704 static void nbd_restart_write(void *opaque
);
706 static ssize_t
nbd_co_send_reply(NBDRequest
*req
, struct nbd_reply
*reply
,
709 NBDClient
*client
= req
->client
;
710 int csock
= client
->sock
;
713 qemu_co_mutex_lock(&client
->send_lock
);
714 qemu_set_fd_handler2(csock
, nbd_can_read
, nbd_read
,
715 nbd_restart_write
, client
);
716 client
->send_coroutine
= qemu_coroutine_self();
719 rc
= nbd_send_reply(csock
, reply
);
721 socket_set_cork(csock
, 1);
722 rc
= nbd_send_reply(csock
, reply
);
724 ret
= qemu_co_send(csock
, req
->data
, len
);
729 socket_set_cork(csock
, 0);
732 client
->send_coroutine
= NULL
;
733 qemu_set_fd_handler2(csock
, nbd_can_read
, nbd_read
, NULL
, client
);
734 qemu_co_mutex_unlock(&client
->send_lock
);
738 static ssize_t
nbd_co_receive_request(NBDRequest
*req
, struct nbd_request
*request
)
740 NBDClient
*client
= req
->client
;
741 int csock
= client
->sock
;
744 client
->recv_coroutine
= qemu_coroutine_self();
745 if (nbd_receive_request(csock
, request
) < 0) {
750 if (request
->len
> NBD_BUFFER_SIZE
) {
751 LOG("len (%u) is larger than max len (%u)",
752 request
->len
, NBD_BUFFER_SIZE
);
757 if ((request
->from
+ request
->len
) < request
->from
) {
758 LOG("integer overflow detected! "
759 "you're probably being attacked");
764 TRACE("Decoding type");
766 if ((request
->type
& NBD_CMD_MASK_COMMAND
) == NBD_CMD_WRITE
) {
767 TRACE("Reading %u byte(s)", request
->len
);
769 if (qemu_co_recv(csock
, req
->data
, request
->len
) != request
->len
) {
770 LOG("reading from socket failed");
778 client
->recv_coroutine
= NULL
;
782 static void nbd_trip(void *opaque
)
784 NBDClient
*client
= opaque
;
785 NBDRequest
*req
= nbd_request_get(client
);
786 NBDExport
*exp
= client
->exp
;
787 struct nbd_request request
;
788 struct nbd_reply reply
;
791 TRACE("Reading request.");
793 ret
= nbd_co_receive_request(req
, &request
);
798 reply
.handle
= request
.handle
;
806 if ((request
.from
+ request
.len
) > exp
->size
) {
807 LOG("From: %" PRIu64
", Len: %u, Size: %" PRIu64
808 ", Offset: %" PRIu64
"\n",
809 request
.from
, request
.len
,
810 (uint64_t)exp
->size
, (uint64_t)exp
->dev_offset
);
811 LOG("requested operation past EOF--bad client?");
812 goto invalid_request
;
815 switch (request
.type
& NBD_CMD_MASK_COMMAND
) {
817 TRACE("Request type is READ");
819 ret
= bdrv_read(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
820 req
->data
, request
.len
/ 512);
822 LOG("reading from file failed");
827 TRACE("Read %u byte(s)", request
.len
);
828 if (nbd_co_send_reply(req
, &reply
, request
.len
) < 0)
832 TRACE("Request type is WRITE");
834 if (exp
->nbdflags
& NBD_FLAG_READ_ONLY
) {
835 TRACE("Server is read-only, return error");
840 TRACE("Writing to device");
842 ret
= bdrv_write(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
843 req
->data
, request
.len
/ 512);
845 LOG("writing to file failed");
850 if (request
.type
& NBD_CMD_FLAG_FUA
) {
851 ret
= bdrv_co_flush(exp
->bs
);
859 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
864 TRACE("Request type is DISCONNECT");
868 TRACE("Request type is FLUSH");
870 ret
= bdrv_co_flush(exp
->bs
);
875 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
880 TRACE("Request type is TRIM");
881 ret
= bdrv_co_discard(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
884 LOG("discard failed");
887 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
892 LOG("invalid request type (%u) received", request
.type
);
894 reply
.error
= -EINVAL
;
896 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
902 TRACE("Request/Reply complete");
904 nbd_request_put(req
);
908 nbd_request_put(req
);
909 nbd_client_close(client
);
912 static int nbd_can_read(void *opaque
)
914 NBDClient
*client
= opaque
;
916 return client
->recv_coroutine
|| client
->nb_requests
< MAX_NBD_REQUESTS
;
919 static void nbd_read(void *opaque
)
921 NBDClient
*client
= opaque
;
923 if (client
->recv_coroutine
) {
924 qemu_coroutine_enter(client
->recv_coroutine
, NULL
);
926 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip
), client
);
930 static void nbd_restart_write(void *opaque
)
932 NBDClient
*client
= opaque
;
934 qemu_coroutine_enter(client
->send_coroutine
, NULL
);
937 NBDClient
*nbd_client_new(NBDExport
*exp
, int csock
,
938 void (*close
)(NBDClient
*))
941 if (nbd_send_negotiate(csock
, exp
->size
, exp
->nbdflags
) < 0) {
944 client
= g_malloc0(sizeof(NBDClient
));
945 client
->refcount
= 1;
947 client
->sock
= csock
;
948 client
->close
= close
;
949 qemu_co_mutex_init(&client
->send_lock
);
950 qemu_set_fd_handler2(csock
, nbd_can_read
, nbd_read
, NULL
, client
);