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/>.
25 #include <sys/ioctl.h>
27 #if defined(__sun__) || defined(__HAIKU__)
28 #include <sys/ioccom.h>
33 #include "qemu_socket.h"
38 #define TRACE(msg, ...) do { \
39 LOG(msg, ## __VA_ARGS__); \
42 #define TRACE(msg, ...) \
46 #define LOG(msg, ...) do { \
47 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
48 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
51 /* This is all part of the "official" NBD API */
53 #define NBD_REPLY_SIZE (4 + 4 + 8)
54 #define NBD_REQUEST_MAGIC 0x25609513
55 #define NBD_REPLY_MAGIC 0x67446698
57 #define NBD_SET_SOCK _IO(0xab, 0)
58 #define NBD_SET_BLKSIZE _IO(0xab, 1)
59 #define NBD_SET_SIZE _IO(0xab, 2)
60 #define NBD_DO_IT _IO(0xab, 3)
61 #define NBD_CLEAR_SOCK _IO(0xab, 4)
62 #define NBD_CLEAR_QUE _IO(0xab, 5)
63 #define NBD_PRINT_DEBUG _IO(0xab, 6)
64 #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
65 #define NBD_DISCONNECT _IO(0xab, 8)
67 #define NBD_OPT_EXPORT_NAME (1 << 0)
69 /* That's all folks */
71 #define read_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, true)
72 #define write_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, false)
74 size_t nbd_wr_sync(int fd
, void *buffer
, size_t size
, bool do_read
)
78 while (offset
< size
) {
82 len
= qemu_recv(fd
, buffer
+ offset
, size
- offset
, 0);
84 len
= send(fd
, buffer
+ offset
, size
- offset
, 0);
88 errno
= socket_error();
90 /* recoverable error */
91 if (len
== -1 && (errno
== EAGAIN
|| errno
== EINTR
)) {
100 /* unrecoverable error */
111 static void combine_addr(char *buf
, size_t len
, const char* address
,
114 /* If the address-part contains a colon, it's an IPv6 IP so needs [] */
115 if (strstr(address
, ":")) {
116 snprintf(buf
, len
, "[%s]:%u", address
, port
);
118 snprintf(buf
, len
, "%s:%u", address
, port
);
122 int tcp_socket_outgoing(const char *address
, uint16_t port
)
124 char address_and_port
[128];
125 combine_addr(address_and_port
, 128, address
, port
);
126 return tcp_socket_outgoing_spec(address_and_port
);
129 int tcp_socket_outgoing_spec(const char *address_and_port
)
131 return inet_connect(address_and_port
, SOCK_STREAM
);
134 int tcp_socket_incoming(const char *address
, uint16_t port
)
136 char address_and_port
[128];
137 combine_addr(address_and_port
, 128, address
, port
);
138 return tcp_socket_incoming_spec(address_and_port
);
141 int tcp_socket_incoming_spec(const char *address_and_port
)
145 return inet_listen(address_and_port
, ostr
, olen
, SOCK_STREAM
, 0);
148 int unix_socket_incoming(const char *path
)
153 return unix_listen(path
, ostr
, olen
);
156 int unix_socket_outgoing(const char *path
)
158 return unix_connect(path
);
175 int nbd_negotiate(int csock
, off_t size
)
177 char buf
[8 + 8 + 8 + 128];
180 [ 0 .. 7] passwd ("NBDMAGIC")
181 [ 8 .. 15] magic (0x00420281861253)
183 [24 .. 151] reserved (0)
186 TRACE("Beginning negotiation.");
187 memcpy(buf
, "NBDMAGIC", 8);
188 cpu_to_be64w((uint64_t*)(buf
+ 8), 0x00420281861253LL
);
189 cpu_to_be64w((uint64_t*)(buf
+ 16), size
);
190 memset(buf
+ 24, 0, 128);
192 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
198 TRACE("Negotation succeeded.");
203 int nbd_receive_negotiate(int csock
, const char *name
, uint32_t *flags
,
204 off_t
*size
, size_t *blocksize
)
210 TRACE("Receiving negotation.");
212 if (read_sync(csock
, buf
, 8) != 8) {
219 if (strlen(buf
) == 0) {
220 LOG("server connection closed");
225 TRACE("Magic is %c%c%c%c%c%c%c%c",
226 qemu_isprint(buf
[0]) ? buf
[0] : '.',
227 qemu_isprint(buf
[1]) ? buf
[1] : '.',
228 qemu_isprint(buf
[2]) ? buf
[2] : '.',
229 qemu_isprint(buf
[3]) ? buf
[3] : '.',
230 qemu_isprint(buf
[4]) ? buf
[4] : '.',
231 qemu_isprint(buf
[5]) ? buf
[5] : '.',
232 qemu_isprint(buf
[6]) ? buf
[6] : '.',
233 qemu_isprint(buf
[7]) ? buf
[7] : '.');
235 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
236 LOG("Invalid magic received");
241 if (read_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
246 magic
= be64_to_cpu(magic
);
247 TRACE("Magic is 0x%" PRIx64
, magic
);
250 uint32_t reserved
= 0;
254 TRACE("Checking magic (opts_magic)");
255 if (magic
!= 0x49484156454F5054LL
) {
256 LOG("Bad magic received");
260 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
261 LOG("flags read failed");
265 *flags
= be16_to_cpu(tmp
) << 16;
266 /* reserved for future use */
267 if (write_sync(csock
, &reserved
, sizeof(reserved
)) !=
269 LOG("write failed (reserved)");
273 /* write the export name */
274 magic
= cpu_to_be64(magic
);
275 if (write_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
276 LOG("write failed (magic)");
280 opt
= cpu_to_be32(NBD_OPT_EXPORT_NAME
);
281 if (write_sync(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
282 LOG("write failed (opt)");
286 namesize
= cpu_to_be32(strlen(name
));
287 if (write_sync(csock
, &namesize
, sizeof(namesize
)) !=
289 LOG("write failed (namesize)");
293 if (write_sync(csock
, (char*)name
, strlen(name
)) != strlen(name
)) {
294 LOG("write failed (name)");
299 TRACE("Checking magic (cli_magic)");
301 if (magic
!= 0x00420281861253LL
) {
302 LOG("Bad magic received");
308 if (read_sync(csock
, &s
, sizeof(s
)) != sizeof(s
)) {
313 *size
= be64_to_cpu(s
);
315 TRACE("Size is %" PRIu64
, *size
);
318 if (read_sync(csock
, flags
, sizeof(*flags
)) != sizeof(*flags
)) {
319 LOG("read failed (flags)");
323 *flags
= be32_to_cpup(flags
);
325 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
326 LOG("read failed (tmp)");
330 *flags
|= be32_to_cpu(tmp
);
332 if (read_sync(csock
, &buf
, 124) != 124) {
333 LOG("read failed (buf)");
341 int nbd_init(int fd
, int csock
, off_t size
, size_t blocksize
)
343 TRACE("Setting block size to %lu", (unsigned long)blocksize
);
345 if (ioctl(fd
, NBD_SET_BLKSIZE
, blocksize
) == -1) {
347 LOG("Failed setting NBD block size");
352 TRACE("Setting size to %zd block(s)", (size_t)(size
/ blocksize
));
354 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, size
/ blocksize
) == -1) {
356 LOG("Failed setting size (in blocks)");
361 TRACE("Clearing NBD socket");
363 if (ioctl(fd
, NBD_CLEAR_SOCK
) == -1) {
365 LOG("Failed clearing NBD socket");
370 TRACE("Setting NBD socket");
372 if (ioctl(fd
, NBD_SET_SOCK
, csock
) == -1) {
374 LOG("Failed to set NBD socket");
379 TRACE("Negotiation ended");
384 int nbd_disconnect(int fd
)
386 ioctl(fd
, NBD_CLEAR_QUE
);
387 ioctl(fd
, NBD_DISCONNECT
);
388 ioctl(fd
, NBD_CLEAR_SOCK
);
392 int nbd_client(int fd
)
397 TRACE("Doing NBD loop");
399 ret
= ioctl(fd
, NBD_DO_IT
);
402 TRACE("NBD loop returned %d: %s", ret
, strerror(serrno
));
404 TRACE("Clearing NBD queue");
405 ioctl(fd
, NBD_CLEAR_QUE
);
407 TRACE("Clearing NBD socket");
408 ioctl(fd
, NBD_CLEAR_SOCK
);
414 int nbd_init(int fd
, int csock
, off_t size
, size_t blocksize
)
420 int nbd_disconnect(int fd
)
426 int nbd_client(int fd
)
433 int nbd_send_request(int csock
, struct nbd_request
*request
)
435 uint8_t buf
[4 + 4 + 8 + 8 + 4];
437 cpu_to_be32w((uint32_t*)buf
, NBD_REQUEST_MAGIC
);
438 cpu_to_be32w((uint32_t*)(buf
+ 4), request
->type
);
439 cpu_to_be64w((uint64_t*)(buf
+ 8), request
->handle
);
440 cpu_to_be64w((uint64_t*)(buf
+ 16), request
->from
);
441 cpu_to_be32w((uint32_t*)(buf
+ 24), request
->len
);
443 TRACE("Sending request to client: "
444 "{ .from = %" PRIu64
", .len = %u, .handle = %" PRIu64
", .type=%i}",
445 request
->from
, request
->len
, request
->handle
, request
->type
);
447 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
448 LOG("writing to socket failed");
455 static int nbd_receive_request(int csock
, struct nbd_request
*request
)
457 uint8_t buf
[4 + 4 + 8 + 8 + 4];
460 if (read_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
467 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
468 [ 4 .. 7] type (0 == READ, 1 == WRITE)
474 magic
= be32_to_cpup((uint32_t*)buf
);
475 request
->type
= be32_to_cpup((uint32_t*)(buf
+ 4));
476 request
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
477 request
->from
= be64_to_cpup((uint64_t*)(buf
+ 16));
478 request
->len
= be32_to_cpup((uint32_t*)(buf
+ 24));
480 TRACE("Got request: "
481 "{ magic = 0x%x, .type = %d, from = %" PRIu64
" , len = %u }",
482 magic
, request
->type
, request
->from
, request
->len
);
484 if (magic
!= NBD_REQUEST_MAGIC
) {
485 LOG("invalid magic (got 0x%x)", magic
);
492 int nbd_receive_reply(int csock
, struct nbd_reply
*reply
)
494 uint8_t buf
[NBD_REPLY_SIZE
];
497 memset(buf
, 0xAA, sizeof(buf
));
499 if (read_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
506 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
507 [ 4 .. 7] error (0 == no error)
511 magic
= be32_to_cpup((uint32_t*)buf
);
512 reply
->error
= be32_to_cpup((uint32_t*)(buf
+ 4));
513 reply
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
516 "{ magic = 0x%x, .error = %d, handle = %" PRIu64
" }",
517 magic
, reply
->error
, reply
->handle
);
519 if (magic
!= NBD_REPLY_MAGIC
) {
520 LOG("invalid magic (got 0x%x)", magic
);
527 static int nbd_send_reply(int csock
, struct nbd_reply
*reply
)
529 uint8_t buf
[4 + 4 + 8];
532 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
533 [ 4 .. 7] error (0 == no error)
536 cpu_to_be32w((uint32_t*)buf
, NBD_REPLY_MAGIC
);
537 cpu_to_be32w((uint32_t*)(buf
+ 4), reply
->error
);
538 cpu_to_be64w((uint64_t*)(buf
+ 8), reply
->handle
);
540 TRACE("Sending response to client");
542 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
543 LOG("writing to socket failed");
550 int nbd_trip(BlockDriverState
*bs
, int csock
, off_t size
, uint64_t dev_offset
,
551 off_t
*offset
, bool readonly
, uint8_t *data
, int data_size
)
553 struct nbd_request request
;
554 struct nbd_reply reply
;
556 TRACE("Reading request.");
558 if (nbd_receive_request(csock
, &request
) == -1)
561 if (request
.len
+ NBD_REPLY_SIZE
> data_size
) {
562 LOG("len (%u) is larger than max len (%u)",
563 request
.len
+ NBD_REPLY_SIZE
, data_size
);
568 if ((request
.from
+ request
.len
) < request
.from
) {
569 LOG("integer overflow detected! "
570 "you're probably being attacked");
575 if ((request
.from
+ request
.len
) > size
) {
576 LOG("From: %" PRIu64
", Len: %u, Size: %" PRIu64
577 ", Offset: %" PRIu64
"\n",
578 request
.from
, request
.len
, (uint64_t)size
, dev_offset
);
579 LOG("requested operation past EOF--bad client?");
584 TRACE("Decoding type");
586 reply
.handle
= request
.handle
;
589 switch (request
.type
) {
591 TRACE("Request type is READ");
593 if (bdrv_read(bs
, (request
.from
+ dev_offset
) / 512,
594 data
+ NBD_REPLY_SIZE
,
595 request
.len
/ 512) == -1) {
596 LOG("reading from file failed");
600 *offset
+= request
.len
;
602 TRACE("Read %u byte(s)", request
.len
);
605 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
606 [ 4 .. 7] error (0 == no error)
610 cpu_to_be32w((uint32_t*)data
, NBD_REPLY_MAGIC
);
611 cpu_to_be32w((uint32_t*)(data
+ 4), reply
.error
);
612 cpu_to_be64w((uint64_t*)(data
+ 8), reply
.handle
);
614 TRACE("Sending data to client");
616 if (write_sync(csock
, data
,
617 request
.len
+ NBD_REPLY_SIZE
) !=
618 request
.len
+ NBD_REPLY_SIZE
) {
619 LOG("writing to socket failed");
625 TRACE("Request type is WRITE");
627 TRACE("Reading %u byte(s)", request
.len
);
629 if (read_sync(csock
, data
, request
.len
) != request
.len
) {
630 LOG("reading from socket failed");
636 TRACE("Server is read-only, return error");
639 TRACE("Writing to device");
641 if (bdrv_write(bs
, (request
.from
+ dev_offset
) / 512,
642 data
, request
.len
/ 512) == -1) {
643 LOG("writing to file failed");
648 *offset
+= request
.len
;
651 if (nbd_send_reply(csock
, &reply
) == -1)
655 TRACE("Request type is DISCONNECT");
659 LOG("invalid request type (%u) received", request
.type
);
664 TRACE("Request/Reply complete");